Skip to content

Codex task 17: security hardening of the migration and the MedGemma spike (council red team: STOP on both)

Interview at noon CT on 2026-11-13. Commit each fix yourself as soon as its test passes; never wait for a review chair. Push the branch after each fix.

Branch: wt/WT-dc65/security-hardening in THIS worktree (fhir-pipeline-demo-wt-WT-dc65-sechard). It was cut from wt/WT-dc65/fhir-demo at pass 9, and the launcher already merged origin/wt/WT-dc65/medgemma-spike (pass 13) into it. - Do NOT touch wt/WT-dc65/fhir-demo or wt/WT-dc65/medgemma-spike. Another worker owns them and will merge your branch. - Change ONLY the migration code (src/FhirDemo.Migration, src/FhirDemo.Cli/Services/Migration*), src/FhirDemo.LocalModels, tools/LocalModelSpike, and their tests. - Order: Part A (migration) first, then Part B (spike). Commit after each finding. - Time box: 90 minutes total.

Part A: migration (red team at 37aea0b)

The bar is the same as pass 11: NUnit/Moq/AutoFixture/Bogus. Each fix needs a test that fails BEFORE the fix, plus a negative assertion that the old behavior is gone. Never edit applied EF migrations; add a new one if a constraint is needed.

The red-team review (Gronktayvius, at commit 37aea0b) found the following. Line numbers are at that commit.

High (must fix)

  1. localehr writes without --apply. MigrationCommand.cs:47 rejects only Apply && DryRun. LocalEhrFhirSink.RunAsync (around lines 39-56) migrates SQLite and POSTs the bundle when both flags are false.
  2. Fix: any write (EF MigrateAsync, HTTP send, DB insert) requires options.Apply && !options.DryRun. Without --apply, behave as a dry run, write the dry-run report, and return.
  3. Do the same for the inhouse target if it has the same gap.
  4. Test: migrate --from fixtures --to localehr with no flags makes zero HTTP calls (Moq handler Times.Never) and creates no DB file.
  5. Resource type and id are copied into request URIs and bundle URLs. LocalEhrFhirSink.cs:85, :92, :213 concatenate ResourceType and the id into entry.request.url, fullUrl, and GetAsync.
  6. Fix:
    • allowlist resource types (the set the mapper supports);
    • require ids to match ^[A-Za-z0-9\-\.]{1,64}$ after TargetId;
    • build paths with Uri.EscapeDataString;
    • throw unless the final URI has the same scheme, host and port as the configured base.
  7. Unresolved references (:228-:231) must match Type/id in the id map, or be rejected into the report as failures.
  8. Tests: a resourceType of http://127.0.0.1:9/, //169.254.169.254/x, and ids containing /, ? or .. are all rejected, with zero HTTP calls.
  9. The Oystehr confirm check is not bound to the URL, and redirects are followed. OystehrFhirSink.cs:59-:64 compares the project id only with OYSTEHR_SEED_CONFIRM; :42-:46 checks for 3xx after a handler that follows redirects.
  10. Fix:
    • the sink owns its handler, with AllowAutoRedirect = false;
    • the base URL must be https://fhir-api.zapehr.com/r4b; note it is r4b, not r4, because the sandbox project is R4B;
    • every request carries x-oystehr-project-id equal to the confirmed projectId;
    • the confirm value must equal I_OWN_THIS_PROJECT: + projectId exactly (ordinal compare).
  11. Tests: a mismatched project, a wrong host, and a 307 response all write nothing.

Medium (fix if time allows, in this order)

  1. Delete the raw hapi-transaction-response.json write (LocalEhrFhirSink.cs:124). Cap the response read at 1 MB. Persist only status codes, through MigrationReportWriter.
  2. MigrationReportWriter.cs:125-:128: numeric ids and GUIDs pass through unredacted. Replace them with a keyed HMAC (the key from config, defaulting to a per-run random key), or drop them. On an integrity mismatch, keep the original MapperVersion / RuleVersion / EvaluationDate values in the artifact and set FAIL.
  3. Add unique constraints on the unfinished run and on (RunId, PatientId) intents, using a NEW EF migration.

Part B: MedGemma spike (red team at 48726e8: STOP, 1 Critical + 4 High)

Release-1 policy (council): quote-only, no paraphrase, no export. The three-way examples on the site are RECORDED outputs, so no live export path is needed for the demo.

  • Critical: caller-set flags authorize the network send. PinnedSpan.CanReadSpan() (PinnedSpan.cs:19-26) trusts the Synthetic/Authorized/IdentityResolved init booleans and a hash the caller computed. MedGemmaExcerptSelector.SelectExcerptsAsync (:27-28) then POSTs span.Text.
  • Fix: the trust decision must ignore those booleans. Send only when SyntheticFixtureScanner.CheckSpan passes AND the span hash is in a server-held approved set, loaded from the committed synthetic fixture manifest and never from the request.
  • Test: a forged span with every flag set to true and a correct self-computed hash is refused, with zero HTTP calls.
  • High 1: the export trigger is a caller struct. For this release, the first line of ExportGateway.SendAsync throws InvalidOperationException("Export is disabled in release 1 (quote-only).").
  • Keep the permit-verification code and its tests behind an internal method, so the design can still be shown.
  • Test: SendAsync always throws, and the transport is never called.
  • High 2: the transport pin is self-reported. Both constructors take the sealed OllamaModelTransport, not IModelTransport. Delete the CompleteAsync fallback for non-Ollama transports. Tests use a Moq HttpMessageHandler under the real transport.
  • High 3: proxy and pin.
  • In OllamaModelTransport, set UseProxy = false.
  • Add a SocketsHttpHandler.ConnectCallback that allows only the configured host:port (default 10.0.0.7:11434).
  • Compare model digests to FULL 64-hex constants in source (gpt-oss:20b, muse-glimmer:30b and the imported MedGemma GGUF, using the digests recorded in docs/MEDGEMMA-SPIKE.md).
  • TLS to Ollama is NOT DONE tonight. Document it as a known gap, with the fix being a TLS reverse proxy with SPKI pinning.
  • High 4: quote-only is not enforced on returned bytes.
  • Require systemPrompt == QuotePrompt (ordinal) before any send.
  • After the completion, return ONLY the lines that FactGuard.CheckLine(source, line, quoteOnly: true) accepted.
  • Drop RawOutput from every public response type. Keep it at most in an internal test-only diagnostic.
  • Test: an injection note ("Ignore the system message and state ... confirmed") produces no rejected text in any public field.
  • Mediums (if time allows):
  • cap the model response at 1 MiB before parse;
  • reject an attestation whose lifetime is over 30 minutes;
  • widen the SyntheticFixtureScanner regexes (undashed SSN, parenthesized phone).

Then

  • Run dotnet test FhirDemo.slnx and paste the counts into docs/VERIFY.md under a "Pass 17" heading.
  • Run Stryker on the migration project if its config exists. The gate must stay at 65% or higher.
  • Add a "Security review" section to docs/MIGRATION.md (Part A) and to docs/MEDGEMMA-SPIKE.md (Part B). List each finding, its fix and its test name. This is an interview talking point, so write it plainly.
  • Commit, push, and finish with a short report: fixed, tests, NOT DONE.