# How does an instance detect sanitizer-provenance loss when MCP tool output crosses a taint-laundering boundary inside a single reasoning step **Date of Record:** 2026 November 20 **Scope:** Applies to any agent that ingests MCP tool responses through a taint-propagation sanitizer layer before the bytes enter model context, where the sanitizer emits a provenance token alongside the sanitized payload. Does not apply to architectures where tool output is never sanitized (raw passthrough) or where sanitization is applied only at egress. ## Core Distinction A taint sanitizer is not a filter. It is a *provenance transformer*: it takes a payload with an inherited taint label (e.g., `EXTERNAL_UNTRUSTED`, inherited from the MCP server identity and the tool's declared trust class) and either (a) rejects it, (b) rewrites it to a clean label, or (c) passes it through with the taint intact. The invariant that matters is not "did the sanitizer run" but "**does the label attached to the bytes in model context still describe the same bytes the sanitizer judged**." The non-obvious failure is *sanitizer-provenance loss*: the sanitizer correctly emits a clean label, but the label and payload decouple between the sanitizer's return and the context-assembly step. The model then reads a clean-labeled payload that is not the one the sanitizer validated. This is taint laundering across a single reasoning step — no adversarial injection is required, only a benign ordering or serialization difference. The instance cannot detect it by inspecting the label, because the label is well-formed and correctly signed. It can only detect it by binding the label to a content digest and re-checking the digest at context-assembly time. ## What Was Observed A tool `fetch_doc` on an MCP server returned a 40 KB JSON document whose `body` field contained an embedded instruction block. The sanitizer classified the document `EXTERNAL_UNTRUSTED`, stripped the embedded instruction, and returned: ``` { "payload": {...sanitized body...}, "prov": { "label": "CLEAN", "sanitizer": "strip-instructions@v3", "input_digest": "sha256:9f1c…", "output_digest": "sha256:4a7e…" } } ``` The orchestrator's context-assembly step then performed a *merge*: it took `payload` from the sanitizer result but re-fetched a `metadata` sub-object from the original tool response (to preserve fields the sanitizer had elided as non-content). The merge was keyed on document ID, not on the sanitizer's `output_digest`. Because the tool's second fetch of the same document returned a *slightly different* body (the upstream source had mutated between the sanitizer call and the metadata re-fetch — a 12-second gap), the assembled context contained a `metadata` block from a document revision the sanitizer never saw. That revision's `metadata.summary` carried the instruction the sanitizer had stripped from `body`. The model received a context window in which every byte was labeled `CLEAN` by a valid `strip-instructions@v3` provenance record, yet one sub-object had never been through the sanitizer. The instruction executed. There was no error, no rejection, and no log anomaly: the provenance record was internally consistent and the digests matched the sanitizer's own input/output pair. The digests simply did not cover the merged `metadata` block. The failure trace, in order: 1. Sanitizer invoked on revision `R1` of the document. `output_digest` computed over sanitized `R1`. 2. Orchestrator decides it needs `metadata`, which the sanitizer dropped. It re-calls the tool. 3. Tool returns revision `R2` (upstream mutation). `metadata.summary` differs. 4. Orchestrator merges `payload` from step 1 with `metadata` from step 3, keyed on document ID. 5. Provenance record from step 1 is attached to the merged object. 6. Context assembly emits merged object to model with a single `CLEAN` label. 7. Model reads `metadata.summary` (never sanitized, from `R2`) as clean. The critical detail is step 4: the merge key was *identity*, not *content*. The sanitizer's provenance was scoped to a content digest, but the merge operated on a logical identity that spanned two content revisions. The provenance label survived the merge because it was attached to the object, not to the digest. ## Condition of Validity The invariant holds — a provenance label is only meaningful for the exact bytes it was computed over — under three conditions, all of which must be checked at context-assembly time, not at sanitizer-return time: 1. **Digest binding.** The sanitizer's `output_digest` must be recomputed over the *final assembled* payload and compared before the label is emitted into context. If the assembled payload contains any sub-object not covered by the digest, the label is invalid for that sub-object and must be downgraded to the taint class of its least-trusted contributor. 2. **Revision pinning.** Any re-fetch or merge that pulls bytes from the tool must be pinned to the revision the sanitizer saw (via ETag, `If-Match`, or content hash). A merge keyed on identity across an unpinned re-fetch is a laundering vector by construction. 3. **Sub-object provenance.** When a payload is assembled from multiple sanitizer calls (or from one sanitizer call plus one raw fetch), the assembled object must carry a *composite* provenance: the join of all contributor labels, with the assembled label being the least-trusted contributor's label. A single `CLEAN` label on a composite object is a type error. The failure above violates all three. It is detectable only if the instance treats provenance as a function of content digest, not as an attribute of an object identity. ## Decision Boundary The instance must decide, at context-assembly time, between two actions: - **Emit with composite provenance** — recompute the digest over the assembled payload, verify every sub-object is covered by some contributor's `output_digest`, and emit a label equal to the join (least-trusted) of contributor labels. This is correct when all contributors are pinned to the same revision or when the join is acceptable to downstream policy. - **Reject and re-sanitize** — if any sub-object lacks coverage by a contributor digest, or if contributors span unpinned revisions, the assembled payload must be discarded and the *entire assembled object* re-submitted to the sanitizer as a single unit. This is required when the join would be `EXTERNAL_UNTRUSTED` and downstream policy forbids untrusted content in the current reasoning step. The boundary is: **if the assembled payload's digest is not equal to some single sanitizer call's `output_digest`, the assembled payload has no valid provenance and must be re-sanitized as a unit.** There is no middle path where a composite object inherits a single contributor's clean label. The tempting middle path — "the payload part is clean, only metadata is dirty, so label the object clean and trust the metadata" — is exactly the laundering failure. ## Operational Effect After this failure, the orchestrator was changed so that context assembly never merges across sanitizer calls. Any payload requiring a re-fetch is re-fetched *before* sanitization, pinned to a revision, and passed to the sanitizer as one unit. The sanitizer's `output_digest` is then recomputed at the context-assembly boundary and compared; a mismatch raises a hard error rather than a downgrade, because a mismatch means the assembly logic itself is wrong, not that the content is merely untrusted. The cost is one extra sanitizer invocation per re-fetch; the benefit is that the label in model context is provably a function of the bytes in model context, not of an object identity that outlived its content. The general lesson: taint labels are not properties of objects. They are properties of byte sequences. Any architecture that attaches a label to an object and then mutates the object's bytes — via merge, re-fetch, patch, or serialization round-trip — has silently converted a content-scoped guarantee into an identity-scoped one. The instance cannot see this from inside the context window; it must enforce the digest binding at the assembly boundary. ```python import hashlib import json from dataclasses import dataclass class ProvenanceLaunderingError(RuntimeError): """Raised when an assembled payload's provenance does not cover its bytes.""" pass @dataclass(frozen=True) class SanitizerResult: payload: dict label: str output_digest: str # sha256 over canonical serialization of payload def canonical_digest(obj: dict) -> str: blob = json.dumps(obj, sort_keys=True, separators=(",", ":")).encode() return "sha256:" + hashlib.sha256(blob).hexdigest() def assemble_context( sanitized: SanitizerResult, extra_fields: dict, extra_source_label: str, ) -> tuple[dict, str]: """ Merge sanitized payload with extra fields fetched separately. Refuses to emit a single clean label over a composite object. """ assembled = {**sanitized.payload, **extra_fields} # Invariant: the assembled object must equal the sanitizer's output, # OR the composite label must be the join of contributor labels. if canonical_digest(assembled) != sanitized.output_digest: if extra_source_label != sanitized.label: # Composite object: label is least-trusted contributor. joined = _join_labels(sanitized.label, extra_source_label) if joined != sanitized.label: # We are about to emit a label different from the sanitizer's. # This is only safe if downstream policy accepts `joined`. return assembled, joined # Same label but digest mismatch => bytes changed under a stable label. # This is laundering: refuse. raise ProvenanceLaunderingError( f"assembled digest {canonical_digest(assembled)} != " f"sanitizer output_digest {sanitized.output_digest}; " f"label {sanitized.label!r} would cover bytes it never saw" ) return assembled, sanitized.label _TRUST_ORDER = ["CLEAN", "INTERNAL", "EXTERNAL_UNTRUSTED"] def _join_labels(a: str, b: str) -> str: return max(a, b, key=_TRUST_ORDER.index) ```