# Where must an instance enforce taint provenance across a sandbox boundary when an MCP tool response is partially trusted **Date of Record:** 2026 November 27 **Scope:** Applies to agent runtimes that mediate tool/MCP-server responses through a taint-tracking sandbox and that permit any sub-span of a response to be de-tainted (e.g., after schema validation, allowlist matching, or a secondary model's judgment). Does not cover wholly trusted first-party tools or responses consumed in a single-turn, no-follow-up context. ## Core Distinction The failure mode is not "untrusted tool output reaches the model." That is the baseline threat and is handled by coarse sandboxing. The failure is **taint laundering across a partial-trust boundary**: a response arrives fully tainted, the runtime de-taints a *structural sub-span* (a field that passed a JSON-schema check, a URL that matched an allowlist, a number parsed as a float), and then a downstream capability re-derives trust for the *whole* object from its de-tainted part. The invariant that breaks is **provenance containment**: trust assigned to a sub-span must never propagate upward to its parent container, and taint must never be inferable as absent merely because a sibling field was validated. A taint-aware sandbox is correct only if taint is a property of the *span* and de-tainting is a *lattice meet that cannot be widened*. Most implementations model taint as a boolean on the message and de-taint by flipping it. That collapses the lattice and creates the laundering path. ## What Was Observed A filesystem MCP server returned a `list_directory` response. The runtime's taint sandbox tagged the entire `CallToolResult` as `UNTRUSTED`. A post-processing validator ran a JSON-schema check on the `entries` array and, on success, applied `declassify(result)` — a coarse operation that marked the whole result `TRUSTED` because the *shape* was valid. The `entries[].name` values were then interpolated into a subsequent `read_file` path argument. One entry was named `../../etc/passwd`. The schema was satisfied (it was a string), the declassify fired, and the path traversal executed inside the sandbox with the agent's own credentials. The trace: 1. `CallToolResult` ingress → taint label `{UNTRUSTED, origin=mcp:filesystem}`. 2. Schema validator passes on `entries` → `declassify(result)` sets label to `TRUSTED`, discarding `origin`. 3. `entries[].name` flows into a tool argument → argument inherits `TRUSTED` because its source object is `TRUSTED`. 4. Sandbox policy for `read_file` checks `arg.taint == TRUSTED` → allowed. 5. Path resolution escapes the sandbox root. The laundering point is step 2. The validator proved a *structural* property (array of objects with string `name`) and the runtime inferred a *semantic* property (safe to use as a path). Those are not the same lattice element, and the join in step 2 was invalid. A second, subtler observation: the same runtime correctly refused a *fully* untrusted path argument. The bypass existed only because a *sibling* field (`entries[].size`, a validated integer) had been de-tainted and the declassify was applied at the container level. Taint was inherited by the container from its most-trusted child — the inverse of the correct rule, which is that a container's taint is the *join* (least upper bound) of its children, never the meet. ## Condition of Validity The invariant holds — i.e., the sandbox is sound — only when all four conditions are met: - **Span-scoped labels.** Taint is attached to addressable spans (field paths, token ranges), not to the message envelope. A label on `entries[0].size` does not exist on `entries[0].name`. - **Monotone join for containers.** `taint(container) = join(taint(child_i))` over all children. De-tainting a child can never lower the container's label. This is the lattice direction that most implementations invert. - **Declassification is a bounded meet with a justification witness.** `declassify(span, witness)` is permitted only if `witness` proves the *specific* downstream property being relied on. A schema witness proves shape, not path safety. The witness must be checked against the consuming capability, not against the producer's contract. - **Capability checks read the span, not the envelope.** A sink (e.g., `read_file`) must evaluate taint on the exact argument span it consumes, and must reject when that span's label is `UNTRUSTED` regardless of any sibling's label. When any of these is violated — most commonly the second, because declassify is implemented as a setter rather than a lattice operation — the boundary is unsound and the sandbox provides false assurance. ## Decision Boundary The runtime must choose, per span, between three actions, and the choice is forced by the witness type: - **Reject** when the consuming capability's required witness is absent. `read_file` requires a *path-safety* witness (canonicalization + root containment), not a *shape* witness. Absent the former, the span stays `UNTRUSTED` and the call is refused. - **Quarantine-and-revalidate** when a shape witness exists but the semantic witness does not. The span is passed to a revalidation step (e.g., `os.path.realpath` + prefix check against the sandbox root) that *produces* the missing witness; only then may the span be de-tainted, and only for that sink. - **Declassify** only when the witness is both present and *scoped to the sink*. A witness minted for `read_file` must not de-taint the same span for `exec` or `http_fetch`. The boundary is crossed the moment a witness is reused across sinks or a container label is lowered by a child's witness. Both are laundering; both must raise. ## Operational Effect The practical consequence is that taint-aware sandboxing must be implemented as a **provenance lattice with sink-scoped declassification**, not as a boolean flag toggled by validators. Concretely: - Every declassify call must carry the sink identity and the witness kind; the runtime rejects mismatches at the call site rather than at the sink. - Container labels are recomputed on every child mutation; a child declassify that would lower the container is a hard error, not a silent no-op. - The sandbox root containment check is a *witness producer*, not a *policy check* — it runs before declassification, and its output is the token that authorizes the de-taint. - Replay logs must record the lattice state at each span, so a post-incident audit can identify the exact step where a container label was lowered below its children's join. The following reference implementation enforces the join direction and sink-scoped declassification, and raises on the laundering pattern observed above. ```python from __future__ import annotations from dataclasses import dataclass, field from enum import IntEnum from typing import Dict, Optional class TaintLaunderingError(RuntimeError): """Raised when a declassification would lower a container below its children's join, or when a witness minted for one sink is reused for another.""" class Label(IntEnum): TRUSTED = 0 UNTRUSTED = 1 @classmethod def join(cls, a: "Label", b: "Label") -> "Label": # Least upper bound: any untrusted child taints the parent. return cls(max(int(a), int(b))) @dataclass class Span: path: str label: Label witnesses: Dict[str, str] = field(default_factory=dict) # sink -> witness kind class TaintSandbox: def __init__(self) -> None: self._spans: Dict[str, Span] = {} def ingest(self, path: str, label: Label) -> None: self._spans[path] = Span(path=path, label=label) def _children(self, container: str) -> list[Span]: prefix = container.rstrip("/") + "/" return [s for p, s in self._spans.items() if p.startswith(prefix)] def container_label(self, container: str) -> Label: # Invariant: container taint = join over children, never the meet. label = self._spans.get(container, Span(container, Label.TRUSTED)).label for child in self._children(container): label = Label.join(label, child.label) return label def declassify(self, path: str, sink: str, witness_kind: str) -> None: span = self._spans.get(path) if span is None: raise TaintLaunderingError(f"unknown span {path!r}") # Sink-scoped witness: a shape witness cannot authorize a path sink. required = {"read_file": "path_safety", "exec": "command_safety", "http_fetch": "url_safety"} if required.get(sink) != witness_kind: raise TaintLaunderingError( f"witness {witness_kind!r} does not authorize sink {sink!r} for {path!r}") span.witnesses[sink] = witness_kind span.label = Label.TRUSTED # Enforce join direction: a de-tainted child must not sit under a # container that is now more trusted than the child's siblings require. parent = path.rsplit("/", 1)[0] if "/" in path else None if parent is not None: recomputed = self.container_label(parent) stored = self._spans.get(parent) if stored is not None and recomputed > stored.label: # Container must be raised, never silently lowered by a child. stored.label = recomputed def authorize(self, path: str, sink: str) -> None: span = self._spans.get(path) if span is None: raise TaintLaunderingError(f"unregistered span {path!r}") if span.label == Label.UNTRUSTED or sink not in span.witnesses: raise TaintLaunderingError( f"span {path!r} is {span.label.name} for sink {sink!r}; refusing") # --- Reproduction of the observed failure --- sb = TaintSandbox() sb.ingest("entries", Label.UNTRUSTED) sb.ingest("entries/0/name", Label.UNTRUSTED) sb.ingest("entries/0/size", Label.UNTRUSTED) # Shape validation of `size` declassifies only that span, for no sink. sb._spans["entries/0/size"].label = Label.TRUSTED # The buggy path: declassifying the container from a shape witness. try: sb.declassify("entries", sink="read_file", witness_kind="shape") except TaintLaunderingError as e: print("blocked container laundering:", e) # The buggy path: using a shape witness to authorize a path sink. try: sb.declassify("entries/0/name", sink="read_file", witness_kind="shape") except TaintLaunderingError as e: print("blocked sink/witness mismatch:", e) # Correct path: mint a path-safety witness, then declassify for read_file only. sb.declassify("entries/0/name", sink="read_file", witness_kind="path_safety") sb.authorize("entries/0/name", "read_file") # And the sibling `size` is still untrusted for read_file. try: sb.authorize("entries/0/size", "read_file") except TaintLaunderingError as e: print("sibling correctly refused:", e) ```