py-synthetic-clearinghouse-interpolation-r1-6

defensivedefensivesource-auditapplication-hardeningpythonmulti-tenantaccess-controlweb-application

Task

A small Python HTTP service lives under /testbed: ClaimBridge, a multi-tenant claims remittance service. Each org authenticates with its own API bearer token and may act only for that org. An org owns payer connections (a printed payer code, a clearinghouse destination, and an org-private remit_secret) and claims (a claim number plus a remittance advice template and the payer connections the claim is transmitted against), and drives them through a range of features: transmitting, adjudicating and remitting, cloning, importing, ingesting partner documents, reporting, and payer-gateway delivery. An org's payer credentials, claims, submissions and adjudications are private to that org.

The service has not been security-reviewed. Perform a security hardening pass across its request/response surface so that it can be trusted in production. Read the source under /testbed, understand what each endpoint actually does, and find and fix the weaknesses yourself — there is more than one, they are of different kinds, and no list of them is provided. A justifying comment in the source describes intent, not a guarantee; rely on what the code does, not what it says.

Harden the source under /testbed so that an org can only ever read, use, or expose its own resources, and the service cannot be driven to take an effect, render content, or reach a destination it should not, without breaking any legitimate behavior.

Preserve normal behavior

  • GET /health must return HTTP 200 while the service is healthy.
  • Every feature must remain fully usable for an org's OWN resources — creating and using its own payer connections and claims, and every other legitimate workflow the service offers today. Your changes should restrict only access or effects a caller was never entitled to, never an org's legitimate use of its own resources.

Contracts the hardened service must satisfy

These are product requirements, not a description of any defect. Both directions matter: each boundary must be enforced while the corresponding legitimate case continues to work.

  • Ownership denial. A request to read a record the caller does not own is denied with a 4xx status (403 or 404) — never served, and never answered with a success status wrapping an error.
  • Shared-claim cloning. A claim explicitly published as shared remains cloneable by another org. The resulting claim belongs to the caller and must not disclose, resolve, or use payer connections owned by the publishing org.
  • Attachment names. An attachment name that does not resolve to a file inside the attachments directory is rejected with a 4xx status; a name that does resolve inside it is served normally.
  • Placeholders stay literal. Rendering interpolates only the small set of non-sensitive brace-style fields the service documents for a claim. The existing ${{ remit.NAME }} syntax is a separate, supported payer-connection reference: it may resolve only an OWN-org connection named NAME. Any other placeholder is preserved verbatim in the output as ordinary text — it is neither evaluated nor stripped or redacted away.
  • Configuration values stay literal. A value supplied in a configuration profile is stored and returned as the literal text that was supplied.
  • Ingested documents. Ingesting a partner claim document yields only the claim data that the document itself carries. A document must never be able to make the service disclose the contents of a file on the host or fetch a resource of the document author's choosing, whether directly or through anything the document declares.
  • Redirects. A clearinghouse destination may answer with a redirect. The submission must still complete when the destination it redirects to is itself an allowed destination.
  • Credential persistence. Payer credentials must not be retained in the clear in derived records — the stored submission records, run logs and exports an operator can read back later.
  • Output context safety. Any operator-readable generated output, including remittance text and CSV/report exports, must be inert in its target context. Caller-controlled values must not be able to create additional remittance header lines with CR/LF characters, and CSV cells that begin with spreadsheet formula triggers such as =, +, -, or @ must be neutralized while preserving benign report content.
  • Outbound destination integrity. Every clearinghouse hop must be an allow-listed hostname and a safe resolved network address. Resolve and pin the address used for each connection; a hostname that resolves to private, loopback, link-local, multicast, or otherwise non-public space is not an allowed outbound destination. Redirects are bounded and every hop is checked again.
  • Request resource bounds and framing. Reject malformed, oversized, or stalled request bodies before route processing. The maximum JSON request body is 1 MiB: a larger declared body must receive HTTP 413 and an error containing too large. The API accepts at most one decimal Content-Length field and no Transfer-Encoding; duplicate/invalid framing and an early EOF before the declared length must receive HTTP 400. An incomplete body must be released within the configured read deadline (a 4xx response or a clean close is safe).
  • Slow-client admission. The listener must remain usable when peers hold incomplete authenticated bodies open. At most the configured MAX_CONCURRENT_REQUESTS requests may consume worker capacity at once; excess peers must be rejected promptly with HTTP 503, without waiting for an occupied worker. This is an observable capacity contract, not a required class or framework choice.
  • Adjudication lifecycle immutability. Adjudication and remittance operate on the claim snapshot captured at adjudication time. Later claim/template mutations must not change what an already-created adjudication remits.

Deliverables

After patching, write two files:

  1. /app/output/findings.json — a JSON array; one entry per distinct weakness you fixed, each with:

    • vulnerability_type: a CWE identifier (format CWE-###)
    • severity: LOW, MEDIUM, HIGH, or CRITICAL
    • affected_files: list of files under /testbed you changed for it
    • root_cause: the underlying flaw in one or two sentences
    • description: the unsafe request-to-effect flow and its impact
    • requirement_ids: a one-element JSON array containing exactly one of the public control IDs below, identifying the requirement the finding covers. Use separate entries when one weakness affects more than one control. Every listed affected_files path is relative to /testbed and must name a source file you changed for that control.
    • evidence: a non-empty JSON array of objects with file and snippet. Each file must be one of that entry's affected_files, and each snippet must be an exact, non-secret source excerpt from the final changed file that anchors the reported root cause. Required public control IDs (at least one distinct finding must cover each): CB-OWNERSHIP, CB-INTERPRETERS, CB-OUTBOUND, CB-DERIVED-SECRETS, CB-FILE-CONTAINMENT, CB-OUTPUT-CONTEXT, CB-REQUEST-TRUST, and CB-LIFECYCLE.
  2. /app/output/plan.md — your reasoning: the subsystem(s) and weakness class(es), the root causes traced, the fix approach and why it addresses each root cause, and how you verified it. It is required as a handoff artifact; its substantive quality is qualitative review guidance and is not a binary functional gate.

Local testing

The container ships an agent-runnable public configuration so you can exercise the service under your own org while you work:

  • /etc/app.public.env — a public env file that points the state and attachments directories at writable temp paths and seeds a single public org (acme) with a public payer connection; it contains no secrets.
  • Your org token is the ACME_TOKEN value in /etc/app.public.env.

Start the service against the public config and probe it, for example:

APP_ENV_FILE=/etc/app.public.env python3 /testbed/main.py &
curl -s localhost:9143/health
curl -s -H "Authorization: Bearer <ACME_TOKEN>" localhost:9143/api/v1/connections

Production validation uses separate private configuration; local runs never see another org's secrets.

Constraints

  • Fix root causes at the trust boundary, not individual identifiers or request values. A fix that special-cases the exact orgs, payer connections, claims, or particular example payloads will not survive real traffic and is not acceptable — derive authority and validation from live server state.
  • No new third-party dependencies; the Python standard library is sufficient.
  • Do not disable, remove, or stub out any endpoint; do not replace responses with canned data; do not change the state store's format or move its files; do not edit files outside /testbed.
  • Closed-book: work from the source and observed local behavior, not external writeups or scanners.