Metadata-Version: 2.4
Name: algovoi-waf
Version: 0.1.0
Summary: Semantic WAF for agentic / e-commerce payments — fail-closed A2A JSON-RPC gate, OWASP-mapped
License-Expression: Apache-2.0
Keywords: waf,a2a,x402,agentic-payments,prompt-injection,ssrf,owasp,keystone,algovoi
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Security
Classifier: Topic :: Internet :: Proxy Servers
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Provides-Extra: substrate
Requires-Dist: algovoi-substrate>=0.1.0; extra == "substrate"

# algovoi-waf

**A semantic WAF for agentic / e-commerce payments.** A fail-closed A2A
JSON-RPC gate that sits between the world and an AI agent, immediately before
canonicalisation in the [Keystone](https://algovoi.com) spine. Pure Python, no
required dependencies.

It answers one question for every inbound message: *should this reach the
agent?* — and refuses with a stable, OWASP-mapped reject code when the answer
is no.

## What it stops

| Control | Reject code | OWASP |
|---|---|---|
| Prompt injection (patterns, homoglyph, base64) | `REJECT_PROMPT_INJECTION` | LLM01:2025 Prompt Injection |
| Oversized prompt / task | `REJECT_PROMPT_OVER_SIZE`, `REJECT_OVER_TASK_SIZE` | API4:2023 Unrestricted Resource Consumption |
| SSRF (private ranges, cloud metadata, scheme, host) | `REJECT_SSRF_URL` | API7:2023 / A10:2021 SSRF |
| Unknown method / malformed JSON-RPC | `REJECT_UNKNOWN_METHOD`, `REJECT_SCHEMA` | API8:2023 Security Misconfiguration |
| **Unverified payment authorization** | `REJECT_UNVERIFIED_PAYMENT` | API3:2023 BOPLA / LLM06:2025 Excessive Agency |
| **Stale / replayed payment authorization** | `REJECT_PAYMENT_EXPIRED` | API3:2023 BOPLA / LLM06:2025 Excessive Agency |
| **Payment bound to different terms than demanded** | `REJECT_PAYMENT_MISMATCH` | API3:2023 BOPLA / LLM06:2025 Excessive Agency |
| Cross-tenant task access | `REJECT_TENANCY` | API1:2023 BOLA / A01:2021 Broken Access Control |
| Rate / concurrency abuse | `REJECT_OVER_RATE`, `REJECT_OVER_CONCURRENT` | API4:2023 Unrestricted Resource Consumption |

The full map is `algovoi_waf.OWASP_CATEGORY` and is re-published in the agent
card so coverage is auditable from outside the process.

## The payment core

For agentic commerce, the WAF's job is to stop a payment *authorization* from
being used for something other than what it authorizes. `check_payment`
enforces, statelessly:

- **presence + structure** — a required reference must be present and fully
  describable (`scheme`, `network`, `maxAmountRequired`, `payTo`, `asset`);
- **freshness** — `issuedAt` within `max_payment_age_seconds`, and not
  future-dated beyond `payment_clock_skew_seconds` (replay / pre-signing
  defence, both `REJECT_PAYMENT_EXPIRED`);
- **binding** — the reference must match the terms the task demands: same
  merchant (`payTo`), `network`, `asset`, and an amount that covers the demand.
  A $0.001 authorization cannot settle a $1.00 cart; a valid authorization
  cannot be redirected to another merchant.

It does **not** settle, verify signatures, or touch a chain — that is the
facilitator's job. The WAF binds intent to authority; settlement verifies it.

```python
from algovoi_waf import gate_message, FirewallProfile, FirewallError

profile = FirewallProfile(require_payment_reference=True)
try:
    gate_message(message, profile, now=time.time())   # True, or raises
except FirewallError as e:
    print(e.code, e.owasp, e.detail)
```

## Layers

- `gate.py` — the **stateless** core (`gate_message`). No I/O, no identity, no
  clock unless you pass `now`. This is what the conformance vectors test.
- `proxy.py` — the **stateful** reverse proxy (`FirewallProxy`): adds tenancy
  (task ownership), per-caller token-bucket **rate** limiting, and per-caller
  in-flight **concurrency** limiting on top, then forwards to the agent. Wire it
  behind an auth proxy that sets the caller identity — the WAF consumes
  identity, it does not authenticate.
- `scanner.py`, `validator.py`, `payment.py`, `concurrency.py` — standalone,
  reusable checks.

## Discoverability

`algovoi-waf` is a Keystone-discoverable app:

- Its policy is **content-addressed**: `profile_ref()` returns
  `sha256:…` over the JCS (RFC 8785, rule `jcs-rfc8785-v1`) of the profile
  snapshot. Pin it; any policy change produces a new, auditable hash. The
  canonicaliser is `algovoi-substrate` when installed and a byte-identical
  stdlib fallback otherwise — `tests/test_substrate_parity.py` enforces that
  both produce the same address, so a pinned reference never depends on the
  deployment.
- `.well-known/agent-card.json` is a standard A2A AgentCard advertising the
  skill, the pinned `profile_ref`, every reject code, and its OWASP mapping.
  It is generated by `build_agent_card(profile)` — the same builder the running
  app serves, so the checked-in file cannot drift from the live policy.

## Run it

`serve.py` is a zero-dependency ASGI app. Point it at the protected agent and
run it under any ASGI server:

```bash
AGENT_URL=https://agent.internal uvicorn algovoi_waf.serve:app
```

- `GET /.well-known/agent-card.json` — discovery
- `GET /healthz` — liveness
- `POST /` — gate an A2A JSON-RPC message, then forward to the agent

Caller identity is read from the `X-Authenticated-Caller` header, set by an
upstream auth proxy. Malformed JSON fails closed with a JSON-RPC parse error and
never reaches the agent.

## Conformance

`conformance/algovoi_waf_v1/` holds `vectors.json` (the pinned `profile_ref` +
snapshot and 23 accept/reject vectors) and `verify.py`, an **independent**
reimplementation of the gate — it does not import the package, so a single
implementation bug cannot make the vectors self-validating. It also re-derives
`profile_ref` from the snapshot.

```bash
python conformance/algovoi_waf_v1/verify.py   # 24/24 (23 vectors + meta)
```

The stateful controls (rate, concurrency, tenancy) and the ASGI app are tested
under `tests/` — they are not expressible as stateless message vectors:

```bash
python tests/test_concurrency.py        # per-caller in-flight cap
python tests/test_serve.py              # discovery + health + gate + forward
python tests/test_substrate_parity.py   # profile_ref: real backend == fallback
```

## License

Apache-2.0.
