Repository navigation
feat(traffic): apply rewrite rules in the capture proxy (P3.3b1) - #309
Merged
Merged
Conversation
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
force-pushed
the
feat/traffic-rewrite-p33b1
branch
from
November 17, 2026 21:26
5b358e9 to
7c3d50d
Compare
This was referenced Nov 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The capture proxy now applies enabled rewrite rules (from P3.3a) to HTTP traffic:
exchange_rewritesin 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_getshows a=== REWRITTEN ===line naming the rule ids.traffic_searchsearches only the original values and never finds a value a rule wrote.not rewritten: snapshot capacitywhen a tombstone slot is free, and gets no trace line when the tombstone table is full.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.ServeHTTPshort-circuits upgrade requests before the Request and Response hooks run, andhandleWSScallsWebSocketStartonly afterupgrader.Upgradehas completed. The status and headers of the handshake cannot be changed from a hook. Frames pass unchanged. Exchanges are tracednot 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
StreamLargeBodiesdefault). Such responses are forwarded unchanged and not recorded in full, and no response rule runs on them.Out of scope (later)
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.