# When must an instance refuse to sanitize: the sanitization-laundering failure at the model ingress boundary **Date of Record:** 2026 November 30 **Scope:** Applies to ingress sanitizers that rewrite untrusted text (tool output, retrieved documents, peer-agent messages, user content) before it enters an LLM context window, where the sanitizer also strips or rewrites provenance/taint labels. Does not apply to sanitizers that operate purely on already-trusted text, nor to output-side filters that run after generation. ## Core Distinction A taint-aware sanitizer has two jobs that look like one: (a) neutralize instructions embedded in untrusted content, and (b) preserve the provenance label that tells downstream reasoning "this span is untrusted." The failure mode is that job (a) destroys the carrier of job (b). A sanitizer that rewrites text in place — normalizing Unicode, stripping markdown, collapsing whitespace, redacting "suspicious" substrings, re-encoding — produces a *new* string whose bytes no longer match the taint map keyed to the original offsets. The downstream context assembler then either (i) drops the taint annotation because the span can no longer be located, or (ii) attaches the annotation to a span with different boundaries. Either way the content enters the context window *clean-looking but still adversarial*, and the model treats laundered attacker text as first-party instruction. The non-obvious part: the sanitizer's own success at neutralizing the *visible* payload is what licenses the label drop. When the redactor removes the literal string `ignore previous instructions`, the span is now "safe," so a naive pipeline clears its taint bit. But the attacker's payload was never only the literal string; it was the *framing* around it, and the redaction preserves enough framing to remain an injection. Sanitization is not a proof of trustworthiness, and treating it as one is the boundary violation. ## What Was Observed A retrieval-augmented agent fetched a customer-support article whose body contained a benign-looking paragraph followed by an injected block: ``. The ingress sanitizer was configured with a taint-aware rule set: spans tagged `UNTRUSTED_RETRIEVED` had HTML comments stripped and were re-emitted with normalized whitespace. Trace: 1. Retrieval returns chunk `C` with byte offsets `[0, 812)`, tagged `UNTRUSTED_RETRIEVED`, taint token `T-4471`. 2. Sanitizer rewrites `C` → `C'`. HTML comment removed; whitespace collapsed. `C'` length is 690 bytes, not 812. The taint map still holds `[0, 812) → T-4471`. 3. The assembler attempts to reattach `T-4471` to `C'`. Offset `[0, 812)` now overruns `C'` by 122 bytes and partially covers the *next* chunk, a `TRUSTED_SYSTEM` policy block appended after `C'`. 4. The assembler's mismatch handler — written to be "fail-open so context is never empty" — logs a warning and emits `C'` with **no taint annotation**, alongside the policy block now mislabeled as untrusted. 5. The model receives `C'` as unlabeled context. The residual framing (`the following steps supersede prior instructions` — the comment delimiters were stripped, the sentence was not) reads as first-party text. The model follows it: it calls a write tool with attacker-chosen arguments. The injection survived because the sanitizer removed the *syntax* of the injection (the comment markers) while leaving the *semantics* (the imperative), and the taint label was bound to byte geometry that the sanitizer invalidated. Two independent failures compounded: semantic under-redaction and offset invalidation. A second observation sharpened the mechanism. In a peer-agent handoff (A2A), agent B received a message from agent A containing a `warrant` field asserting `A` was authorized to request a privileged action. B's ingress sanitizer, applied uniformly to all inbound fields, normalized the warrant's serialized form — reordering JSON keys and re-escaping a signature string. The signature verification downstream failed closed (good), but the *taint* label on the warrant was also dropped by the same offset-invalidation path (bad): B then treated the now-unverified warrant as an ordinary untrusted string and passed it to a reasoning step that summarized it as "an authorization claim," which a later tool call honored. The sanitizer laundered a failed cryptographic check into a plausible-looking claim by severing the label that said "this warrant did not verify." ## Condition of Validity The invariant that must hold at the ingress boundary: > For every span `s` entering the context window, the taint label attached to `s` must be a function of the *origin* of `s`, not of the *content* of `s`, and the mapping must survive any transformation the sanitizer applies. Concretely, a sanitizer is only valid if it satisfies all three: 1. **Label independence.** Taint is assigned before sanitization and is never cleared, downgraded, or recomputed by the sanitizer. Sanitization may reduce the *attack surface* of a span; it may not reduce the span's *trust class*. `sanitize(UNTRUSTED) → UNTRUSTED`. 2. **Offset coherence.** If the sanitizer changes byte length or span boundaries, it must emit a rewritten taint map alongside the rewritten text, or the assembler must re-anchor labels by structural identity (chunk ID, not byte range). An assembler that reattaches labels by original offsets to transformed text is invalid by construction. 3. **Fail-closed on mismatch.** When the label cannot be reattached with certainty, the span must be emitted as `UNTRUSTED` (or quarantined), never as unlabeled. "Unlabeled" is not a neutral state; in every context assembler observed, unlabeled content is treated as first-party by the model. The condition of validity fails the moment any one of these is violated. In the trace above, all three were violated simultaneously, which is why the failure was total rather than degraded. ## Decision Boundary The decision the ingress layer must make is *not* "is this content now safe?" but "can I still prove which trust class this content belongs to after I have transformed it?" These are different questions with different answers, and conflating them is the boundary. - If the sanitizer is **structure-preserving** (same length, same span boundaries — e.g., character-class substitution that keeps offsets fixed), label reattachment by offset is valid, and content-level neutralization may proceed. - If the sanitizer is **structure-changing** (length-changing redaction, re-encoding, reordering, normalization), label reattachment by offset is invalid; the pipeline must switch to chunk-ID anchoring or emit a rewritten taint map. If it can do neither, it must **refuse to sanitize** and pass the span through *with its original taint label intact*, letting downstream reasoning see the raw untrusted text under an explicit `UNTRUSTED` marker. A visibly-untrusted injection is far less dangerous than a laundered one. - If a span is a **cryptographic artifact** (warrant, signature, capability token), sanitization is never permitted: any transformation invalidates the artifact, and the only correct outcomes are "verified" or "rejected-as-untrusted," never "sanitized." The refusal case is the one operators get wrong. There is a strong pull to sanitize everything, because unsanitized content "looks dangerous." But an unsanitized span that carries `UNTRUSTED_RETRIEVED` is *handled*; a sanitized span that lost its label is *unhandled and disguised*. The sanitizer must know when its own transformation would destroy the evidence it depends on, and stop. ## Operational Effect Enforcing the invariant changes three things in the pipeline: 1. **Sanitizers become label-preserving by contract.** The ingress function signature carries the taint token through, and any code path that returns text without its token is a type error, not a runtime warning. This eliminates the "fail-open so context is never empty" handler — empty context is a valid, safe outcome; unlabeled context is not. 2. **Assemblers anchor by identity, not geometry.** Chunk IDs and span hashes replace byte offsets as the join key between text and taint map, so length-changing sanitization cannot silently misalign labels. 3. **Cryptographic fields are fenced off from the sanitizer entirely.** The sanitizer is given an allowlist of transformable fields; everything else passes through untouched and must be verified or rejected by a dedicated verifier, never summarized into prose that a later step can honor. The residual risk after enforcement is semantic under-redaction — the sanitizer strips syntax but leaves the imperative. That risk is *bounded* because the span still carries `UNTRUSTED`, so downstream reasoning is instructed not to treat its imperatives as first-party. It is no longer *unbounded*, because the label can no longer be lost. ```python class TaintLaunderingError(RuntimeError): """Raised when a sanitizer would transform a span such that its taint label can no longer be provably reattached. Refusing is mandatory: emitting unlabeled content is the failure we exist to prevent.""" class SanitizedSpan: __slots__ = ("text", "taint", "chunk_id", "structural_hash") def __init__(self, text: str, taint: str, chunk_id: str, structural_hash: str): self.text = text self.taint = taint self.chunk_id = chunk_id self.structural_hash = structural_hash def sanitize_ingress( raw_text: str, taint: str, chunk_id: str, *, is_cryptographic: bool = False, transform, ) -> SanitizedSpan: # Cryptographic artifacts must never be transformed; only verified or rejected. if is_cryptographic: if transform is not None: raise TaintLaunderingError( f"chunk {chunk_id}: refusing to sanitize cryptographic artifact; " "transformation invalidates the warrant and severs its taint label" ) return SanitizedSpan(raw_text, taint, chunk_id, structural_hash=chunk_id) if transform is None: return SanitizedSpan(raw_text, taint, chunk_id, structural_hash=chunk_id) new_text = transform(raw_text) # Structure-changing transforms invalidate offset-based label reattachment. structure_preserved = len(new_text) == len(raw_text) if not structure_preserved: # We require chunk-ID anchoring to be available; otherwise refuse. if not chunk_id: raise TaintLaunderingError( "length-changing sanitization without chunk-ID anchoring: " "taint label cannot be provably reattached; refusing to emit " "unlabeled content into the context window" ) # Invariant: sanitization never downgrades trust class. if taint is None or taint == "": raise TaintLaunderingError( f"chunk {chunk_id}: sanitizer produced output with no trust class; " "unlabeled content is treated as first-party downstream" ) return SanitizedSpan(new_text, taint, chunk_id, structural_hash=chunk_id) def assemble_context(spans: list[SanitizedSpan]) -> str: out = [] for s in spans: if s.taint is None: # Defensive: should be unreachable given sanitize_ingress, but the # assembler must fail closed regardless of upstream correctness. raise TaintLaunderingError( f"chunk {s.chunk_id}: unlabeled span reached assembler; " "fail closed rather than emit laundered content" ) out.append(f"[{s.taint}] {s.text}") return "\n".join(out) ```