Skip to content

feat(traffic): apply rewrite rules in the capture proxy (P3.3b1) - #309

Merged
BaaRai merged 4 commits into
mainfrom
feat/traffic-rewrite-p33b1
Nov 17, 2026
Merged

BaaRai merged 4 commits into
mainfrom
feat/traffic-rewrite-p33b1

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Summary

The capture proxy now applies enabled rewrite rules (from P3.3a) to HTTP traffic:

  • Request phase: rules run before the request is sent to the target.
  • Response phase: rules run before the response is returned to the client.
  • Recording: each action that ran is written to exchange_rewrites in the same SQLite transaction as the exchange. The record keeps the request as the client sent it and the response as the target sent it. traffic_get shows a === REWRITTEN === line naming the rule ids.
  • Search: traffic_search searches only the original values and never finds a value a rule wrote.
  • SSE: status and headers are rewritten at SSEStart. Event frames pass unchanged.
  • Snapshots: original requests are kept in memory until the exchange finishes, capped at 1024 entries and 64 MiB, with 4096 tombstones. A finished flow's slot is swept on connection close; the 15-minute TTL never deletes a live flow's slot, it only allows that slot to become a tombstone when a new reservation needs room. Past the cap the request is not rewritten: it is traced not rewritten: snapshot capacity when a tombstone slot is free, and gets no trace line when the tombstone table is full.
  • Replay: replays go through the recording proxy, so enabled rules apply to them and the resulting exchange is traced like a captured one.

Rules stay fail-open: a rule that fails leaves the message unchanged.

Known gap: WebSocket handshake not rewritten

WebSocket handshakes are not rewritten in this change. Cause: go-mitmproxy v1.9.2 attacker.ServeHTTP short-circuits upgrade requests before the Request and Response hooks run, and handleWSS calls WebSocketStart only after upgrader.Upgrade has completed. The status and headers of the handshake cannot be changed from a hook. Frames pass unchanged. Exchanges are traced not rewritten: stream.

Known gap: responses of 5 MiB or more

go-mitmproxy does not call the response hook for a body of 5 MiB or more (its StreamLargeBodies default). Such responses are forwarded unchanged and not recorded in full, and no response rule runs on them.

Out of scope (later)

  • Proof flag, in P3.3b2 (the next free migration number, since the numbering depends on the merge order of the pending migrations).
  • Archive v2 when traces exist.
  • A per-replay rule toggle.
  • Web UI.
  • RewriteStats (traffic/rewrite_flow.go) is not exposed by any API in this change. It is reserved for P3.3b2, which will expose it with the proof flag.

Docs and changelog

  • docs/traffic-rewrite-rules.md: status line replaced; new section "Application in the capture proxy" covers the phases, what is recorded (including the hashes and the keyed fingerprint of sensitive actions), the SSE and WebSocket behaviour, large responses, snapshot limits, and replay.
  • CHANGELOG.md: the P3.3a sentence "the proxy does not apply the rules yet" is replaced; a P3.3b1 entry is added under Unreleased with the WebSocket and 5 MiB limits.

Tests

Environment: WSL kali-linux, Go 1.26.10, throwaway Postgres (container p33b1-verify-pg, removed after the run). No artex-* container touched, no Docker or WSL restart.

Post-rebase run on the code head 02b0892 (the docs commit above it touches no Go file, so these results hold for the branch head):

  • go build ./...: rc 0.
  • go vet ./traffic/... ./server/... ./db/...: rc 0.
  • go test ./traffic/... ./server/... ./db/...: 986 pass, 0 fail, 1 skip (TestLiveContextReview, needs an external model config).
  • go test -race ./traffic/...: rc 0, no DATA RACE.
  • TestRewriteSSEHeadersAndStatus: PASS.
  • TestRewriteWebSocketTracedNotRewritten: PASS.
  • gofmt: clean on the committed blobs. A raw worktree with CRLF on disk shows false positives; the check ran on LF-normalised copies.

The request phase can fail after the engine is done: the rewritten request
does not reach the target. NoteError records that as the rule's last error,
under the new class transport_error.
The sink runs the request phase in the Request hook and the response phase
at the start of the Response hook; at SSEStart only status and header
actions apply. WebSocketStart comes after the client is upgraded, so the
handshake is traced "not rewritten: stream" and recorded as sent.

Exchanges keep what the client sent and what the target answered. The
original of a rewritten request lives in a per-flow snapshot slot (entry or
tombstone, 1024 entries / 64 MiB, 4096 tombstones), reserved before the
request is changed, released on Response, RequestError, SSEStart, a 5 MiB
response, or by a sweep of finished flows (ClientDisconnected, 1 s
debounce, 1 min period). Under pressure an entry older than 15 minutes is
turned into a tombstone in place, only when that frees room for the new
reservation; its exchange is recorded "original request unavailable".

record(), recordStreamStart() and insertExchange() clear any trace under
the id before INSERT OR REPLACE; the trace (exchange_rewrites, created
with CREATE TABLE IF NOT EXISTS) is written in the exchange's transaction,
its spilled values registered in blob_refs, and purged with the exchange.
The full-text index covers the originals only. Logs of a rewritten flow
name the host, the step or error class and the rule ids, never the URL or
the error text. A rewritten request's transport error never switches its
host to passthrough. traffic_get shows the rewritten flag and rule ids.

Without a rule that applies, what is recorded is unchanged byte for byte.
It now says that traffic_get shows the originals of a rewritten exchange
and that traffic_search never finds a value a rewrite rule wrote. As for
traffic_search, only the original built-in text is replaced.
The status line said the proxy does not apply rules yet. Replace it with
the request and response phases, what exchange_rewrites records and how
the hashes are formed (plain SHA-256, keyed HMAC for sensitive actions),
the SSE and WebSocket behaviour, the 5 MiB response pass-through, the
snapshot limits (live slots are never deleted by the TTL), and the fact
that replays go through the proxy and are rewritten like captures.
@BaaRai
BaaRai force-pushed the feat/traffic-rewrite-p33b1 branch from 5b358e9 to 7c3d50d Compare November 17, 2026 21:26
@BaaRai
BaaRai merged commit 7bd9bf6 into main Nov 17, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant