Skip to content

fix(quarantine): length-prefixed tool approval hash so contracts cannot collide - #1634

Merged
Dumbris merged 3 commits into
mainfrom
fix/1606-1591-approval-hash
Oct 16, 2026
Merged

Dumbris merged 3 commits into
mainfrom
fix/1606-1591-approval-hash

Conversation

@Dumbris

@Dumbris Dumbris commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Summary

Two rug-pull bypasses in the tool-approval hash (internal/runtime/tool_quarantine.go).

#1606: colliding contracts. The v4 hash joined name, description and the normalized input schema with an unescaped |. It appended the output schema only when one was present. So A = (D, I, O) and B = (D|norm(I), O, none) got the same hash, and an approved A could switch to B and stay approved.

  • New hash schema v5 (storage.UnambiguousHashSchemaVersion, now ToolApprovalHashSchemaVersion). Every field is length-prefixed, with a domain tag. An absent output schema gets an explicit output:absent marker. The safety-hint fingerprint is always encoded.
  • The old formula is kept byte-for-byte as calculateToolApprovalHashV4. It is used only for migration checks, and its existing goldens are now pinned against it.
  • Migration (no mass re-review). A v4 approved record is re-baselined only when both hold: its v4 ApprovedHash covers the stored contract (v4(stored) == ApprovedHash), and the stored contract equals the live one field by field. Anything else goes to changed, through the normal path (scan evidence included in trust_mode: scan). This gate covers the formula migration, the content match and the serialization-only branches.
  • A pending record that a v4 binary left stranded still promotes on a baseline pass, when its recorded fields equal the live ones (v4PendingHashMatchesLive).
  • The pre-RT-03 check in legacySiblingApprovesContract now uses the v4 formula that those records were written with.
  • A held v4 record whose hash does not cover its stored snapshot does not get that snapshot recorded as Previous*. A later pass therefore cannot "revert" to it.
  • Legacy collapsed siblings: a v4 sibling approves the live contract only when its v4 hash covers its stored contract and that contract equals the live one exactly. An empty description is declared on v4, so an unchanged tool with no description still adopts. The pipe-joined v3 and legacy hash shortcuts count only when the sibling's stored contract does not contradict the live one.
  • Revert: a tool approved under v4 with an empty description is restored when it reverts after the upgrade. This applies only when the v4 or v5 ApprovedHash is the hash of the recorded previous contract.
  • Preflight pins carry the schema version, so pins taken before the upgrade fail closed with hash_mismatch until re-pinned. This is existing behavior, now documented.

#1591: added output schema treated as a formula migration. The fallback to the live output schema is already limited to records below storage.OutputSchemaHashSchemaVersion on main (a857dba, uncaptured). This PR adds the cross-version regression test: a v4 record approved without an output schema, which then gains a poisoned one, stays changed across the v5 upgrade, in manual and scan trust modes.

Docs: docs/features/security-quarantine.md covers the v5 encoding, the upgrade rule and re-pinning.

Test evidence

  • Failing first: TestToolApprovalHash_CollidingContractIsHeld failed on main (expected "changed", actual "approved"). It passes now.
  • New tests are in internal/runtime/tool_quarantine_hash_encoding_test.go: the unambiguous encoding, v5 goldens (cross-checked with an independent Python encoder), the v4 collision pin, an unchanged v4 record re-baselined (with and without output schema, and with non-default hints), a colliding v4 contract held (manual and scan), a v4 record whose hash does not cover its stored contract held, a v4 record that gains an output schema held (Tool approval: adding an outputSchema to an approved tool is accepted as a hash-formula migration (skips review and trust_mode=scan) #1591), and a v4 pending record that promotes when unchanged but not when colliding. I neutered the two new gates and confirmed the matching tests fail.
  • Cross-review with opencode gpt-6.1-sol. Round 1 raised 5 findings. Four were fixed (the unproven snapshot reused as Previous*, the v4 sibling proof, the v3 sibling collision, and an empty-description v4 sibling being held for no reason), each with a regression test that fails on the prior commit. The fifth was kept as is: ApproveCurrentHash copies a v4 hash before discovery, but the version follows the hash, so the next discovery applies the v4 proof gate. Sol agreed in round 2 that this is no bypass. Round 2 found one P2 (an empty-description v4 revert stayed held), now fixed with a test. Round 3: VERDICT: CLEAN.
  • go test -race ./internal/runtime/ passes on the final head. go test -race ./internal/storage/... ./internal/preflight/... ./internal/qapilot/... ./internal/httpapi/... and go test -race -tags server -skip "<CI regex>" ./internal/server/... pass on the first commit. The follow-up commits touch only internal/runtime.
  • go run ./cmd/qapilot mutate --only M06,M07,M08 (the mutations in tool_quarantine.go): 3/3 detected, baseline passed.
  • Both golangci-lint runs (bare and --build-tags server) on internal/runtime and internal/storage are clean apart from one existing govet inline note in the untouched internal/runtime/event_bus.go:1020.
  • Live, on scratch instances (stdio fixture, isolated config and data dir):
    • The old binary approves A, then B stays approved (bug reproduced).
    • Upgrade (rerun on the final head): the old binary approves A, then the new binary runs on the same DB. A stays approved ("Tool approval hash migrated"). B is then held as changed, with previous description "Read a note". Reverting to A restores approved.

Closes #1606
Closes #1591

…ot collide

The v4 rug-pull hash joined name, description and the normalized input
schema with an unescaped '|' and appended the output schema only when
present, so (D, I, O) and (D+"|"+I, O, none) shared one hash: an approved
tool could switch to the second contract and stay approved (#1606).

Hash schema v5 (storage.UnambiguousHashSchemaVersion) length-prefixes every
field, encodes an absent output schema explicitly and domain-separates the
hash. The v4 formula is kept as calculateToolApprovalHashV4 for migration:

- an approved v4 record is re-baselined without review only when its v4
  ApprovedHash covers the stored contract and that contract equals the live
  one field by field; anything else is marked changed (no mass re-review,
  no collision carried across the upgrade);
- a stranded v4 pending record still promotes on a baseline pass when its
  recorded contract equals the live one;
- the legacy-sibling pre-RT-03 check uses the v4 formula it was written with.

The output-schema wildcard of #1591 is already limited to records below
storage.OutputSchemaHashSchemaVersion; new tests pin that a v4 record that
gains an output schema is held as changed across the v5 upgrade, in manual
and scan trust modes.
- A v4 record held because its ApprovedHash does not cover the stored
  snapshot no longer records that snapshot as Previous*, so a later pass
  cannot "revert" to it and approve it.
- Legacy collapsed siblings: a v4 sibling approves the live contract only
  when its v4 hash covers its stored contract and that contract is exactly
  the live one (an empty description is declared on v4, so an unchanged
  empty-description tool still adopts); the pipe-joined v3/legacy hash
  shortcuts only count when the sibling's stored contract does not
  contradict the live one, so a colliding contract or an added output
  schema is never adopted.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Deploying mcpproxy-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 05f0dc3
Status: ✅  Deploy successful!
Preview URL: https://aa660f02.mcpproxy-docs.pages.dev
Branch Preview URL: https://fix-1606-1591-approval-hash.mcpproxy-docs.pages.dev

View logs

…ter the v5 upgrade

A changed record whose approved contract had an empty description was
only ever restored by the hash match; with the v5 formula its v4
ApprovedHash no longer equals the live hash, so a genuine revert stayed
held. An empty PreviousDescription now counts as the declared description
when the ApprovedHash (v5 or v4) is the hash of the recorded previous
contract; the previous fields are still compared one by one with the live
contract, so a contract that only collides under v4 stays held.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 95.23810% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/runtime/tool_quarantine.go 95.23% 1 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: fix/1606-1591-approval-hash

Available Artifacts

  • archive-darwin-amd64 (31 MB)
  • archive-darwin-arm64 (28 MB)
  • archive-linux-amd64 (19 MB)
  • archive-linux-arm64 (17 MB)
  • archive-windows-amd64 (31 MB)
  • archive-windows-arm64 (28 MB)
  • archive-windows-arm64 (28 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (27 MB)
  • installer-dmg-darwin-arm64 (25 MB)
  • smart-mcp-proxymcpproxy-goOYZBZJ.dockerbuild (0 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 38898859101 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@Dumbris
Dumbris merged commit d1c3354 into main Oct 16, 2026
57 of 60 checks passed
Dumbris added a commit that referenced this pull request Oct 17, 2026
…nts (#1637)

## Root cause
`TestConfigWatcher_ExternalRevertToLastSelfWriteReloads` flaked on
macOS-15 runners (#1629, #1634). It is not a self-write-marker bug: with
debug instrumentation under CPU load (reproduced ~1 in 100-250 runs
locally on macOS), the final external revert to byte-identical A
produced **no fsnotify event at all** after B's reload, so no debounce
fired and the config stayed on B. fsnotify's kqueue backend can drop the
event for a rename-over that follows a recently handled one. This is a
real production hole (an external edit can be silently missed until
restart), not only a test timing issue.

## Fix
`runConfigWatchLoop` now also polls (2s) and compares file bytes with
the last evaluated content; on a difference it arms the normal debounce
-> `reloadFromDiskIfChanged` path, so self-write suppression is
unchanged. The poll does not re-arm a pending debounce.

## Tests
- New `TestConfigWatcher_PollRecoversMissedFsEvent` deterministically
simulates total event loss (watch on an unrelated dir) and fails without
the poll.
- Before: 2 failures in ~120 runs, 1 in 250 (12 CPU spinners + 10
parallel test processes). After: 0 failures in 100 runs of the whole
`TestConfigWatcher_` suite under the same load, plus 20 clean sequential
`-race` runs.
- golangci-lint (bare and `--build-tags server`) on `internal/runtime`:
only a pre-existing `reflect.Ptr` inline finding in `event_bus.go`,
untouched here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants