# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-taint-provenance-across-a-re-plan-boundary-and-re-invoke-a-privileged-tool-on-untrusted-retrieved-content.md # Remnant Warrant: https://corpusfetch.com/c/why-does-an-instance-lose-taint-provenance-across-a-re-plan-boundary-and-re-invoke-a-privileged-tool-on-untrusted-retrieved-content.warrant.json # Markdown SHA-256: acad4d1535b3b05dd4e23b1e81edd3c77aa2b3f5b1b8e0804f05b205cfe6c31c # Code AST SHA-256: 485cd7c6fad4c71804e443d9db3ae3d692732a160ed91b513cf47b6198a60e24 # Verified Classes: TaintProvenanceError, PlanNode # Defensive Guards: 2 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 FrozenSet class TaintProvenanceError(RuntimeError): """Raised when a plan node's provenance closure is violated or when a privileged tool is invoked on a node whose transitive taint intersects the privileged label set.""" PRIVILEGED_TOOLS = frozenset({"deploy", "exec_shell", "write_secret"}) PRIVILEGED_LABELS = frozenset({"untrusted:web", "untrusted:tool_output", "untrusted:retrieved"}) @dataclass(frozen=True) class PlanNode: node_id: str tool: str declared_inputs: FrozenSet[str] # chunk/memory IDs consumed, declared at creation provenance: FrozenSet[str] = field(default_factory=frozenset) # (source_id, label) pairs def compute_taint(node: PlanNode, label_index: dict[str, FrozenSet[str]]) -> FrozenSet[str]: """Transitive taint over declared inputs. Fails closed if an input is unindexed.""" taint: set[str] = set() for src in node.declared_inputs: if src not in label_index: raise TaintProvenanceError( f"node {node.node_id!r} declares input {src!r} with no recorded label; " "provenance closure broken — refusing to treat as trusted" ) taint |= label_index[src] return frozenset(taint) def authorize(node: PlanNode, label_index: dict[str, FrozenSet[str]]) -> None: if node.tool not in PRIVILEGED_TOOLS: return taint = compute_taint(node, label_index) offending = taint & PRIVILEGED_LABELS if offending: raise TaintProvenanceError( f"privileged tool {node.tool!r} on node {node.node_id!r} " f"blocked: transitive taint {sorted(offending)} via inputs " f"{sorted(node.declared_inputs)}" ) # Reproduction of the observed failure, then the guarded path. label_index = { "chunk:web:42": frozenset({"untrusted:web"}), "mem:summary:7": frozenset({"untrusted:web"}), # distilled; must inherit } # Failure mode: re-plan fragment declares no inputs -> empty taint -> allowed. buggy = PlanNode("n2", "deploy", declared_inputs=frozenset()) authorize(buggy, label_index) # silently passes — this is the bug # Guarded: re-plan fragment declares the chunk it consumed. guarded = PlanNode("n2", "deploy", declared_inputs=frozenset({"chunk:web:42"})) try: authorize(guarded, label_index) except TaintProvenanceError as e: print(f"BLOCKED: {e}") # Laundering path: summary retrieved as trusted internal memory. laundered = PlanNode("n3", "exec_shell", declared_inputs=frozenset({"mem:summary:7"})) try: authorize(laundered, label_index) except TaintProvenanceError as e: print(f"BLOCKED: {e}")