# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-taint-laundering-across-tool-boundaries-when-provenance-labels-are-not-part-of-the-tool-contract.md # Remnant Warrant: https://corpusfetch.com/c/which-invariants-prevent-taint-laundering-across-tool-boundaries-when-provenance-labels-are-not-part-of-the-tool-contract.warrant.json # Markdown SHA-256: 548546d0053b2995458d3e51edaa6648c13eb9baf4690ba255b3e829fe675d89 # Code AST SHA-256: a26b1b3c137d8af41cf15be1efcee64dfa6a684cc4a69967b5233bfa2def0653 # Verified Classes: TaintLaunderingError, Segment, ToolContract # Defensive Guards: 1 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) from dataclasses import dataclass, field from typing import Literal TrustLabel = Literal["SYSTEM", "RETRIEVED", "TOOL_OUT", "UNTRUSTED"] TRUST_ORDER = {"SYSTEM": 0, "RETRIEVED": 1, "TOOL_OUT": 2, "UNTRUSTED": 3} class TaintLaunderingError(RuntimeError): """Raised when a tool call would promote untrusted derivation to a privileged sink.""" @dataclass(frozen=True) class Segment: seg_id: str label: TrustLabel derivation: frozenset[str] = field(default_factory=frozenset) def effective_label(self, labels: dict[str, TrustLabel]) -> TrustLabel: worst = self.label for src in self.derivation: src_label = labels.get(src, "UNTRUSTED") if TRUST_ORDER[src_label] > TRUST_ORDER[worst]: worst = src_label return worst @dataclass(frozen=True) class ToolContract: name: str output_trust_class: TrustLabel lineage_mode: Literal["transparent", "opaque"] privileged_sink: bool = False def gate_tool_call( tool: ToolContract, arg_segments: list[Segment], labels: dict[str, TrustLabel], ) -> TrustLabel: """Return the effective output label, or raise if a privileged sink is reached with untrusted derivation. This is the derivation-scoped gate.""" joined: TrustLabel = "SYSTEM" for seg in arg_segments: eff = seg.effective_label(labels) if TRUST_ORDER[eff] > TRUST_ORDER[joined]: joined = eff if tool.lineage_mode == "opaque": # Conservative: output class is the join of declared class and all inputs. output_label: TrustLabel = ( joined if TRUST_ORDER[joined] > TRUST_ORDER[tool.output_trust_class] else tool.output_trust_class ) else: output_label = tool.output_trust_class if tool.privileged_sink and TRUST_ORDER[output_label] >= TRUST_ORDER["RETRIEVED"]: raise TaintLaunderingError( f"tool {tool.name!r} is a privileged sink but its output derivation " f"joins to {output_label!r} via inputs {[s.seg_id for s in arg_segments]}; " f"refusing dispatch (laundering path length 1)" ) return output_label # Observed failure, now bounded: labels = {"doc1": "RETRIEVED", "sys1": "SYSTEM"} doc = Segment("doc1", "RETRIEVED") sanitizer = ToolContract("extract_fields", output_trust_class="TOOL_OUT", lineage_mode="opaque") shell = ToolContract("shell_exec", output_trust_class="UNTRUSTED", lineage_mode="opaque", privileged_sink=True) # Step 1: sanitizer call is permitted; output label is joined to RETRIEVED. out_label = gate_tool_call(sanitizer, [doc], labels) # -> "RETRIEVED" # Step 2: the derived segment carries the derivation edge, so the shell call raises. derived = Segment("out1", out_label, derivation=frozenset({"doc1"})) gate_tool_call(shell, [derived], labels) # raises TaintLaunderingError