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)¶
localehrwrites without--apply.MigrationCommand.cs:47rejects onlyApply && DryRun.LocalEhrFhirSink.RunAsync(around lines 39-56) migrates SQLite and POSTs the bundle when both flags are false.- Fix: any write (EF
MigrateAsync, HTTP send, DB insert) requiresoptions.Apply && !options.DryRun. Without--apply, behave as a dry run, write the dry-run report, and return. - Do the same for the
inhousetarget if it has the same gap. - Test:
migrate --from fixtures --to localehrwith no flags makes zero HTTP calls (Moq handlerTimes.Never) and creates no DB file. - Resource type and id are copied into request URIs and bundle URLs.
LocalEhrFhirSink.cs:85,:92,:213concatenateResourceTypeand the id intoentry.request.url,fullUrl, andGetAsync. - Fix:
- allowlist resource types (the set the mapper supports);
- require ids to match
^[A-Za-z0-9\-\.]{1,64}$afterTargetId; - build paths with
Uri.EscapeDataString; - throw unless the final URI has the same scheme, host and port as the configured base.
- Unresolved references (
:228-:231) must matchType/idin the id map, or be rejected into the report as failures. - Tests: a
resourceTypeofhttp://127.0.0.1:9/,//169.254.169.254/x, and ids containing/,?or..are all rejected, with zero HTTP calls. - The Oystehr confirm check is not bound to the URL, and redirects are followed.
OystehrFhirSink.cs:59-:64compares the project id only withOYSTEHR_SEED_CONFIRM;:42-:46checks for 3xx after a handler that follows redirects. - 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-idequal to the confirmedprojectId; - the confirm value must equal
I_OWN_THIS_PROJECT:+ projectId exactly (ordinal compare).
- the sink owns its handler, with
- Tests: a mismatched project, a wrong host, and a 307 response all write nothing.
Medium (fix if time allows, in this order)¶
- Delete the raw
hapi-transaction-response.jsonwrite (LocalEhrFhirSink.cs:124). Cap the response read at 1 MB. Persist only status codes, throughMigrationReportWriter. 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 originalMapperVersion/RuleVersion/EvaluationDatevalues in the artifact and setFAIL.- 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 theSynthetic/Authorized/IdentityResolvedinit booleans and a hash the caller computed.MedGemmaExcerptSelector.SelectExcerptsAsync(:27-28) then POSTsspan.Text. - Fix: the trust decision must ignore those booleans. Send only when
SyntheticFixtureScanner.CheckSpanpasses 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.SendAsyncthrowsInvalidOperationException("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:
SendAsyncalways throws, and the transport is never called. - High 2: the transport pin is self-reported. Both constructors take the sealed
OllamaModelTransport, notIModelTransport. Delete theCompleteAsyncfallback for non-Ollama transports. Tests use a MoqHttpMessageHandlerunder the real transport. - High 3: proxy and pin.
- In
OllamaModelTransport, setUseProxy = false. - Add a
SocketsHttpHandler.ConnectCallbackthat allows only the configuredhost:port(default10.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
RawOutputfrom 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
SyntheticFixtureScannerregexes (undashed SSN, parenthesized phone).
Then¶
- Run
dotnet test FhirDemo.slnxand paste the counts intodocs/VERIFY.mdunder 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 todocs/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.