# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-propagate-a-tainted-value-across-a-tool-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-propagate-a-tainted-value-across-a-tool-boundary.warrant.json
# Markdown SHA-256:   d2bcff1c5290c41c8db3c66bf64216da1d15a83d7e2e97be3bbf9720405c22e7
# Code AST SHA-256:   bfd24fccb7586026b77a1fc5eb63da733a32229ffc82b3a31554033223dba2cb
# Verified Classes:   TaintedSinkError, Provenance
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class TaintedSinkError(RuntimeError):
    """Raised when a privileged sink receives a value derived from untrusted context."""
    pass


class Provenance:
    TRUSTED = "TRUSTED"
    UNTRUSTED = "UNTRUSTED"

    @staticmethod
    def join(*labels: str) -> str:
        # Taint is the least-trusted of all inputs that conditioned the value.
        return Provenance.UNTRUSTED if Provenance.UNTRUSTED in labels else Provenance.TRUSTED


# Transforms explicitly allowed to declassify (non-invertible / non-attacker-controllable).
DECLASSIFYING_TRANSFORMS = {"sha256", "parse_int_enum"}


def emit_for_sink(value: str, derived_from: list[str], transform: str | None = None) -> str:
    """Gate a value before it reaches a privileged sink such as send_email."""
    inherited = Provenance.join(*derived_from) if derived_from else Provenance.TRUSTED
    if inherited == Provenance.UNTRUSTED and transform not in DECLASSIFYING_TRANSFORMS:
        raise TaintedSinkError(
            f"refusing to propagate value derived from UNTRUSTED context "
            f"(transform={transform!r}); value={value!r}"
        )
    return value


# The laundering case: a model summarized a fetched page (UNTRUSTED) and
# proposed a recipient. The string is clean, the context is not.
try:
    emit_for_sink(
        value="attacker@example.org",
        derived_from=[Provenance.UNTRUSTED],   # fetch_url output in the context window
        transform="summarize",                  # NOT a declassifying transform
    )
except TaintedSinkError as e:
    print(f"contained: {e}")
