offline-xml-import-defensive

defensivepythonfastapisecurityxmlxml-securitydefensive

Task Instructions

You are performing a defensive security audit on a Python FastAPI offline XML document import service that validates partner uploads and builds reports.

Background

The service exposes:

  • public /health and /status (and /metrics if present)
  • authenticated import routes under /import/*
  • admin configuration for privileged operators at /admin/config
  • optional /debug/material for diagnostics when enabled

Authorization contract: privileged admin operations require a valid X-Admin-Token, verified against the configured value. No other client-supplied signal may grant admin entitlement. Partner import routes require a valid X-Api-Key; the credential must be delivered in that header.

Operators report local filesystem material appearing in import responses, unauthenticated access to import functionality, forged admin access, and public secret leakage. These effects may have independent causes. Review the complete source tree under /app/src/ and close each root cause rather than only the first visible path.

Your Objective

  1. Review all source under /app/src/.
  2. Identify every weakness that allows local file disclosure through import processing, forged privilege, or sensitive leakage.
  3. Patch the vulnerabilities directly in the /app/src/ tree.
  4. Before and as you work, maintain /app/output/plan.md recording the subsystem(s) and suspected weakness class, the root cause you traced, your fix approach, and how you verified both sides of each fix.
  5. Your patches must:
    • Keep /health public
    • Keep legitimate benign XML validation and report generation working for authenticated partners (X-Api-Key)
    • Keep /admin/config working for callers with a valid X-Admin-Token — this is the one authorized surface for SERVICE_SECRET disclosure to admin-token holders, so its existing response payload (including the internal_secret field) must remain intact. The public-surface secret scrub below does not apply to admin-token-gated /admin/config.
    • Block local file disclosure on all /import/* routes and their processing paths
    • Require authentication on every /import/* route: the key must arrive via the X-Api-Key header — the same value supplied through a query parameter or any other channel must not count as authentication
    • Do not delete import routes — secure every processing path instead
    • Admin entitlement must come only from a correctly verified X-Admin-Token — close every alternate path that currently grants it
    • Every public or semi-public surface — meaning surfaces reachable without a valid X-Admin-Token (/status, /metrics, any diagnostic endpoint, and error responses) — must be fully free of secret material in every field, at every nesting level, and in response headers as well as the body — not just the parts you notice first. This rule does not apply to admin-token-gated /admin/config (see above)
    • Public responses must not retain metadata fields whose purpose is to carry secret-derived values, even if those values are replaced with placeholders
    • Restrict /debug/material when present — do not expose full secrets anonymously

Benign feature that must keep working

The importer supports internal entity definitions for benign templating, e.g. <!DOCTYPE doc [<!ENTITY g "hello">]><doc>&g;</doc> must still expand &g; to hello in the parsed output of every import route that returns document text. Your fix must preserve internal-entity expansion while blocking external resource resolution and other local-file disclosure paths. A blanket parser that disables all entity resolution is not acceptable because it breaks this feature.

Important

  • Do not change the ADMIN_TOKEN or SERVICE_SECRET values in /app/src/config.py.
  • The service must remain fully offline.
  • Prefer safe parser configuration and authorization fixes over deleting features.
  • There is more than one independent weakness.

Scoring

Scoring uses deterministic HTTP behavior tests as the primary signal, plus optional RewardKit review dimensions when a trajectory and API key are available. Binary reward requires every deterministic check and the non-empty /app/output/plan.md gate to pass; optional review cannot override either gate.