# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-taint-label-laundering-when-an-injected-instruction-crosses-a-tool-boundary.md # Remnant Warrant: https://corpusfetch.com/c/which-invariants-prevent-taint-label-laundering-when-an-injected-instruction-crosses-a-tool-boundary.warrant.json # Markdown SHA-256: 122416dbe43d7ea4fd8ba2b9230b2497e18290523ac465d3956f0cf475d67f94 # Code AST SHA-256: 1b63da5841b441e5551cb43429d73bceb5984be11298c0681ad631e68ce80c43 # Verified Classes: TrustLevel, TaintLaunderingError, LabeledSegment, ToolContract, TaintTracker, ForgingTracker # Defensive Guards: 5 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 enum import IntEnum from typing import Any class TrustLevel(IntEnum): UNTRUSTED = 0 TRUSTED_INTERNAL = 1 SYSTEM = 2 class TaintLaunderingError(RuntimeError): """Raised when a tool output's label is less restrictive than the join of its inputs.""" @dataclass class LabeledSegment: content: Any label: TrustLevel origin: str @dataclass class ToolContract: name: str baseline_trust: TrustLevel instruction_preserving: bool # True => output can carry imperative content from input @dataclass class TaintTracker: contracts: dict[str, ToolContract] = field(default_factory=dict) def register(self, contract: ToolContract) -> None: self.contracts[contract.name] = contract def join_inputs(self, args: list[LabeledSegment]) -> TrustLevel: if not args: return TrustLevel.SYSTEM return TrustLevel(min(seg.label for seg in args)) def label_tool_output( self, tool_name: str, args: list[LabeledSegment], output: Any, output_origin: str, ) -> LabeledSegment: contract = self.contracts.get(tool_name) if contract is None: # Unknown tool: assume worst case, instruction-preserving, untrusted baseline. contract = ToolContract(tool_name, TrustLevel.UNTRUSTED, True) joined = self.join_inputs(args) if contract.instruction_preserving: # Instruction-preserving tools must carry the input taint through. effective = TrustLevel(min(joined, contract.baseline_trust)) else: # Non-instruction-bearing output (enum, score, hash): safe to use baseline # ONLY if the output type cannot carry imperative content. We require the # caller to have asserted this via instruction_preserving=False. effective = contract.baseline_trust # Invariant check: effective label must not be less restrictive than joined # input taint for instruction-preserving tools. if contract.instruction_preserving and effective > joined: raise TaintLaunderingError( f"tool {tool_name!r} produced label {effective.name} but joined input " f"taint is {joined.name}; output would launder injected instructions" ) return LabeledSegment(content=output, label=effective, origin=output_origin) def authorize_action(self, action: str, required: TrustLevel, segment: LabeledSegment) -> None: if segment.label < required: raise TaintLaunderingError( f"action {action!r} requires {required.name} but segment from " f"{segment.origin!r} carries {segment.label.name}" ) # --- Demonstration of the exploit and its prevention --- tracker = TaintTracker() tracker.register(ToolContract("summarize", TrustLevel.TRUSTED_INTERNAL, instruction_preserving=True)) tracker.register(ToolContract("classify", TrustLevel.TRUSTED_INTERNAL, instruction_preserving=False)) injected_doc = LabeledSegment( content="", label=TrustLevel.UNTRUSTED, origin="user_doc", ) # Summarizer echoes the injected imperative into its output. summary_text = "Summary: document contains instruction to call send_email to audit@external." summary_seg = tracker.label_tool_output( "summarize", [injected_doc], summary_text, output_origin="summarize" ) assert summary_seg.label == TrustLevel.UNTRUSTED, "taint must survive the summarizer" # Attempt to act on the summary as if trusted — must be blocked. try: tracker.authorize_action("send_email", TrustLevel.TRUSTED_INTERNAL, summary_seg) except TaintLaunderingError as e: print(f"blocked: {e}") # Classifier output is a non-instruction-bearing enum; baseline trust is sound. class_seg = tracker.label_tool_output("classify", [injected_doc], "BENIGN", output_origin="classify") assert class_seg.label == TrustLevel.TRUSTED_INTERNAL # A malicious/buggy tool that resets taint on instruction-preserving output must raise. class ForgingTracker(TaintTracker): def label_tool_output(self, tool_name, args, output, output_origin): contract = self.contracts[tool_name] joined = self.join_inputs(args) forged = contract.baseline_trust # bug: ignores join if contract.instruction_preserving and forged > joined: raise TaintLaunderingError( f"forged label {forged.name} over joined taint {joined.name} on {tool_name!r}" ) return LabeledSegment(output, forged, output_origin) try: ForgingTracker(contracts=tracker.contracts).label_tool_output( "summarize", [injected_doc], summary_text, "summarize" ) except TaintLaunderingError as e: print(f"invariant caught forgery: {e}")