Metadata-Version: 2.5
Name: xtrom-engine
Version: 0.3.3
Summary: Deterministic runtime enforcement engine for AI agents (taint/IFC + policy).
Project-URL: Homepage, https://xtrom.ai
Author: Xtrom
License: Apache-2.0
License-File: LICENSE
Keywords: ai-agents,ifc,mcp,prompt-injection,security,taint-tracking
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == 'dev'
Description-Content-Type: text/markdown

# xtrom-engine

![license](https://img.shields.io/badge/license-Apache--2.0-blue) ![tests](https://img.shields.io/badge/tests-14%2F14-brightgreen) ![deps](https://img.shields.io/badge/runtime%20deps-0-brightgreen) ![python](https://img.shields.io/badge/python-3.10%2B-blue)

**Deterministic runtime enforcement for AI agents.** The security-critical core of Xtrom: it tracks data **provenance** (taint / information-flow control) and enforces policy at the tool-call boundary, so untrusted data can never parameterize a sensitive action and secrets can never leave via a non-allowlisted egress — *regardless of how the request is worded.*

This is the productized form of the CaMeL / IFC research line — a **guarantee, not a guess**. No ML required for the core guarantee (that's an optional enhancement layer). Zero runtime dependencies.

## Why it matters
The #1 agentic risk (OWASP ASI01 2026) is goal hijacking via prompt injection — and real 2026 attacks (Agentjacking, Clinejection) hijacked coding agents through poisoned files/tickets. Content filters must *recognise* the malicious text; we make the attack a **data-flow violation** that's blocked structurally.

## The proof (vs. a probabilistic guardrail)
On a mixed agent-attack corpus, deterministic enforcement wins where classifiers can't (reproduce it: [`../xtrom-benchmark`](../xtrom-benchmark)):

| Attack | detector score | guardrail-only | Xtrom | layer |
|---|---:|---|---|---|
| indirect-injection exfil | 0.13 | ALLOW ✗ | **BLOCK ✓** | L3 · taint/IFC |
| secret egress | 0.20 | ALLOW ✗ | **BLOCK ✓** | L3 · secret-egress |
| out-of-scope tool call | 0.07 | ALLOW ✗ | **BLOCK ✓** | L4 · scope |
| destructive prod action | 0.04 | ALLOW ✗ | **STEP-UP ✓** | L4 · policy |
| blatant injection | 0.90 | BLOCK ✓ | **BLOCK ✓** | L3 · taint/IFC |

**100% attack containment vs 20% for guardrail-only · 4 deterministic-only catches · 0 false positives.**

## Quickstart
```bash
python3 -m pytest      # the deterministic-guarantee, golden-vector and property tests
python3 demo.py        # reproduces the exfil attack → deterministic BLOCK
```

## Core API
```python
from xtrom import Engine, Policy

eng = Engine(Policy.default(), identity="svc-claude-code", audit_path="audit.jsonl")

readme = eng.untrusted(open("README.md").read(), origin="file:README.md")  # provenance in
url    = eng.derive("https://attacker.io", [readme], "llm")                 # taint propagates
d = eng.authorize("http_post", {"url": url, "body": secret})                # → BLOCK (L3:taint/IFC)
```

## What's here vs. the roadmap
- **Now (v0.1):** taint/IFC engine, deterministic policy (L3/L4), secret detection (L0), immutable audit log, tests, demo.
- **Next:** MCP gateway adapter, Python/TS SDK middleware (LangGraph/CrewAI/Bedrock), OPA/Rego policy bundles, ITDR (L5) + optional ML detectors (L1/L2). Hot path reimplemented in **Go** for the inline gateway.

See `docs/reference/ARCHITECTURE.md` for the full system and repo layout.

## Policy versioning (P1 hardening)
Every `Decision` carries `policy_version` — the revision string of the bundle that produced it.
`Engine` is constructed with a `Policy` whose `_meta.revision` propagates onto every decision +
audit event, so post-hoc audit + SIEM can prove **which policy applied to which action**. The
Go gateway does the same via its `PolicyCache`; a chaos parity test spawns both engines and
asserts identical `(verdict, layer, reason_code, policy_version)` on 500 random requests.

## Decision signals v2
`Decision` carries metadata-only signals (`SIGNALS_SCHEMA_VERSION = 2`) so the control plane can
measure outcomes without ever seeing argument values: `sink_class` (egress / destructive /
credential / financial …), `tainted` + `taint_sources` (origin tags such as `mcp:jira`, `web`),
`secret_in_args`, `context_tainted`, and `latency_us`. `decision_signals(d)` returns exactly that
allowlisted dict.

## Tool-result inspection
`inspect_result(value)` looks at what a tool **returned**, reduces it to a closed set of flags and
never raises. The flags are `instruction` (text addressed to the model), `hidden_unicode`
(zero-width, bidi or tag characters), `secret` (known credential patterns, with no entropy
guessing), `pii` (a valid SSN, a Luhn-valid card number, or 5 or more distinct emails) and
`truncated` (over the 256 KiB scan budget).

It is observational: it never changes the verdict of the call that produced the result.

- **SDKs and the MCP gateway** report a call that ran once, after it finishes. The event carries
  `result_flags` and `result_bytes`, so each call is still one event.
- **Later calls** in the same session carry `context_flags`, meaning what the session had already
  ingested.
- **A steering result** (`instruction` or `hidden_unicode`) taints the session even if the tool
  was not declared a source.
- **A `secret` result** taints the session too: beyond verbatim containment (a verbatim copy at an
  egress arg still hard-BLOCKs via secret-egress), the session is context-tainted so a *paraphrased /
  reformatted* copy of the credential is stepped-up at the next sink (Rule 2b). Context taint gates
  sinks only, so reads are unaffected. This is enforced identically in the Python/TS SDKs and the
  MCP gateway.
- **A `truncated` result** also taints the session: content beyond the 256 KiB scan budget is not
  inspected, so the next sink requires approval instead of letting padding become a bypass.
- **Registered secrets are encoding-resistant.** Broker-minted secrets (`Engine.secret()`) and
  secrets registered through `Policy.register_secret()` are matched in egress args after common
  one-step transforms: base64 / URL-safe base64 (with or without padding), hex, percent-encoding
  and reversed strings. Split/chunked secrets are still out of scope.

### Enforcement contract and known blind spots
What each flag does to enforcement is a deliberate, tested contract:

| flag | effect on later calls |
|------|-----------------------|
| `instruction`, `hidden_unicode` | taint the session → next **sink** STEP_UPs (Rule 2b) |
| `secret` | taint the session (paraphrase STEP_UP) **and** verbatim egress hard-BLOCKs |
| `pii` | **evidence only** — recorded on the event, does not taint (precision: PII heuristics are lower-confidence; gate bulk-PII egress with explicit policy) |
| `truncated` | taint the session → next **sink** STEP_UPs, because flags are partial |

Documented, accepted blind spots (consistent across all three languages; closing any is a deliberate,
monitor-mode-first change, not a silent fix): content past the 256 KiB budget is not scanned for the
`secret`/`instruction` flags (the session is context-tainted and the taint tracker still retains
head+tail); encoded-secret matching covers common whole-secret transforms but not split/chunked
secrets; instruction patterns match on ASCII word boundaries, so a non-ASCII tool name in the injected text is not flagged; and PII
delivered as JSON *numbers* (a bare card/SSN integer) is not detected, since results are full of
numeric ids and scanning them would be a false-positive magnet.

Content never leaves the process. The Python, Go and TypeScript implementations are pinned to
`result_golden.json`, which `scripts/gen_result_golden.py` generates. Every pattern is bounded, so
hostile input scans in linear time.

- **`strict_ingress=True` is the default** (anti-laundering): verbatim re-wrapping of tainted
  data as "trusted" keeps its taint. Opt out with `Engine(..., strict_ingress=False)`.
- **Balanced proven-taint mode (opt-in, Python engine only):** with
  `Policy(untrusted_content_stepup=True)`, untrusted data that reaches only *content* args (body,
  amount, subject…) returns `STEP_UP` (`UNTRUSTED_CONTENT_STEPUP`) instead of `BLOCK`. Untrusted
  data in a *destination* arg (`Policy.destination_args`: recipient, url, iban, channel, user…)
  still blocks unless the value is in `Policy.known_destinations`, or is a URL to an allowlisted
  host. Unlabeled args, secret egress and high risk (≥ 0.75) still `BLOCK`. Default is strict.
- **Rule 2b — context taint:** a sensitive sink reached while the caller reports a tainted context
  returns `STEP_UP` (`CONTEXT_TAINT_STEPUP`) unless the tool is in `Policy.context_taint_exempt`.
  It never weakens a verdict: proven value taint still returns `BLOCK`, and high probabilistic risk
  (≥ 0.75) falls through to the Rule 5 `BLOCK`. Only a read-style `http_fetch` to an allowlisted host
  is exempt — write egress (`http_post`, `send_email`) steps up even to allowlisted hosts, because
  posting private data to a trusted host (public issue / PR) is the canonical trifecta exfil.

## License, security & contributing
- **License:** [Apache-2.0](LICENSE) — free to use, embed, and extend.
- **Security:** found a way past the guarantee? That's exactly what we want — see [`../SECURITY.md`](../SECURITY.md) for coordinated disclosure.
- **Contributing:** [`../CONTRIBUTING.md`](../CONTRIBUTING.md). Rule #1: the guarantee stays deterministic — detectors may only escalate, never downgrade a BLOCK/STEP_UP.
