Skip to content

Test: Cover plugin-driven request body mutation in the parity suite - #1318

Merged
cwiklik merged 1 commit into
mainfrom
test/parity-request-body-mutation
Oct 7, 2026
Merged

cwiklik merged 1 commit into
mainfrom
test/parity-request-body-mutation

Conversation

@cwiklik

@cwiklik cwiklik commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Closes #938. Follow-up from #935; depended on the ReadsBody fixture from #937, which has landed.

Why

The parity suite had no fixture in which a plugin rewrites the request body via pctx.SetBody. That gap matters because the rewrite reaches the upstream through entirely different code on each listener:

  • the proxies replace Request.Body and Request.ContentLength directly, then Del the content-encoding header;
  • extproc emits an Envoy BodyMutation proto plus a separate content-length HeaderMutation, because BUFFERED+SEND mode leaves content-length to the processor and Envoy rejects a mismatch.

A rewrite that lands on one shape and is dropped on the other is silent. The plugin sees SetBody succeed, both listeners record the same successful modify / body_rewritten Invocation and the same body-mutation payload, and only one deployment shape puts the wrong bytes on the wire. tool-prune depends on this exact contract in production.

The suite could not have caught it either, for a structural reason: nothing it observed came from the upstream. Every assertion read the session event, and the session event is identical whether the rewrite was forwarded or computed and thrown away.

What this adds

An opt-in upstream capture. A fixture that sets expectedUpstream turns on recording of the request as forwarded, and the suite then pins it absolutely per listener and diffs it pairwise — the absolute half being the fix for the suite's documented blind spot, where two listeners that both record nothing compare equal.

  • On the proxies the capture is the httptest handler reading the body it was handed.
  • On extproc it is explicitly a derivation: the driver's own request with the emitted protos applied the way Envoy would apply them, reading BodyMutation and the content-length SetHeaders entry independently, so emitting one without the other fails.

ContentLength is kept as the raw header string rather than a parsed length, so a header that disagrees with the bytes is catchable and "absent" stays distinguishable from "0".

Capture is opt-in rather than always-on because three existing fixtures would differ on a harness artifact alone: for a body-less GET a real proxy upstream sees no Content-Length header at all, where the extproc driver synthesizes content-length: 0. Making every fixture opt in keeps that from being papered over with a normalization that would also hide real drift.

The spy gains MutateRequestBody, which doubles as the source of its WritesRequestBody capability so the two cannot drift apart in a fixture, and calls SetBody after publishing the body it was given — so a fixture pins the bytes the listener handed the plugin and the bytes the listener sent on as two independent facts. Read the other way round they would be the same assertion twice, and a listener forwarding the original bytes would still look right.

Three fixtures: inbound (extproc + reverseproxy), outbound (extproc + forwardproxy), and a rewrite to zero bytes — which is a different wire shape from a body-less request and the one a truncating plugin produces at its limit. Each also pins the body-mutation session payload with hand-written JSON rather than marshalling pipeline's unexported struct, so the JSON field names are asserted too.

Verification

Sabotage runs, each reverted afterwards:

Sabotage Result
reverseproxy mutation block disabled fails — 51 vs 36 bytes, absolutely and pairwise, naming the listener
extproc content-length SetHeaders deleted fails — correct body, stale length: the independent-drift case the issue named
r.ContentLength cleared on forwardproxy fails
extproc treating an empty rewrite as a no-op fails the to-empty fixture
forwardproxy's explicit Header.Set("Content-Length") deleted passes, correctly — Go's transport re-derives that header from Request.ContentLength, so the line is belt-and-braces on the proxy path. Noted in the commit message so the next reader does not mistake it for a hole.

go test -race ./listener/... green across all 12 packages; gofmt -l . and go vet ./... clean.

Test-only: no non-test file is touched.

Noticed while doing this, filed separately rather than fixed here

warnResponseReaderOrdering (core/pipeline/pipeline.go:677-695) emits a spurious WARN for any non-streaming ReadsBody plugin in a pipeline containing no response mutator — so opa and ibac trigger it at every boot and every hot-reload. Its guard is if respMutator != "" || !caps.ReadsBody { continue }, and a single forward pass cannot look ahead to see whether a mutator follows. It went unnoticed because every prior ReadsBody fixture used the streaming spy, which the function skips; these fixtures use the non-streaming one and are the first in-tree shape that exposes it. Keeping this PR test-only.

Assisted-By: Claude (Anthropic AI) <[email protected]>

The parity suite had no fixture in which a plugin rewrites the request
body. That matters because a rewrite reaches the upstream through
entirely different code on each listener: the proxies replace
Request.Body and Request.ContentLength directly, while extproc emits an
Envoy BodyMutation proto plus a separate content-length HeaderMutation.
A rewrite that lands on one shape and is dropped on the other is silent
— the plugin sees SetBody succeed, both listeners record the same
successful modify Invocation, and only one deployment shape sends the
wrong bytes. tool-prune depends on this exact contract in production.

The suite could not have caught it either, because nothing it observed
came from the upstream. Add an opt-in upstreamSummary: a fixture that
sets expectedUpstream turns on capture of the request as forwarded, and
the suite pins it per listener AND diffs it pairwise. On the proxies
that is the httptest handler reading the body it was handed; on extproc
it is derived — the driver's own request with the emitted protos applied
the way Envoy would apply them, reading BodyMutation and the
content-length SetHeaders entry independently so emitting one without
the other fails.

Content-Length is kept as the raw header string rather than a parsed
length, so a header that disagrees with the bytes is catchable and
"absent" stays distinguishable from "0". Capture is opt-in because three
of the existing fixtures would differ on a harness artifact alone: for a
body-less GET a real proxy upstream sees no Content-Length header at
all, where the extproc driver synthesizes content-length: 0.

The spy gains MutateRequestBody, which doubles as the source of its
WritesRequestBody capability so the two cannot drift apart, and calls
SetBody after publishing the body it was given — so a fixture pins the
bytes the listener handed the plugin and the bytes the listener sent on
as two independent facts. Three fixtures: inbound, outbound, and a
rewrite to zero bytes, each also pinning the body-mutation session
payload with hand-written JSON so the field names are asserted too.

Verified by sabotage: disabling the reverseproxy mutation block, dropping
extproc's content-length SetHeaders (the independent-drift case), and
clearing Request.ContentLength on forwardproxy each fail and name the
listener that drifted. Deleting forwardproxy's explicit
Header.Set("Content-Length") does not fail, correctly — Go's transport
re-derives that header from Request.ContentLength, so the line is
belt-and-braces on the proxy path.

Closes #938

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>

@evaline-ju evaline-ju left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses #938: MutateRequestBody knob wired to pctx.SetBody, WritesRequestBody derived from it, and the forwarded bytes + Content-Length pinned on both proxies (httptest capture) and extproc (derivation from BodyMutation + content-length SetHeaders, read independently to catch the drift case the issue names). Three fixtures and a sabotage matrix confirm the gap is closed.

Author: cwiklik (MEMBER) · test-only · CI green on substance

Note: both CodeQL analyses show cancelled at the "Perform CodeQL Analysis" step (infra, not real failures) — worth a re-run to turn the check green before merge.

@cwiklik
cwiklik merged commit 1d9a7cf into main Oct 7, 2026
35 of 37 checks passed
cwiklik added a commit that referenced this pull request Oct 7, 2026
#1318 landed the request-body-mutation fixtures in the same three parity
files. Both conflicts were additive-vs-additive and both sides are kept:
fixture.expectedInvocations alongside fixture.expectedUpstream, and their
two absolute checks in assertParity's per-listener loop.

Worth recording from the resolution: #1318's fixtures pass unchanged
against the order-sensitive invocationsEqual this branch introduced, so
nothing written since the sorting tolerance was added depends on it
either.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
@evaline-ju
evaline-ju deleted the test/parity-request-body-mutation branch November 18, 2026 06:06
boazdavid added a commit that referenced this pull request Nov 25, 2026
v0.7.9 adds lifecycle.Finish (context-guru #1359), which the next commit
uses, and agent.Client.SubagentID (#1318). It requires accountant v0.7.0
(accountant #110: a cache rewrite is charged to the new change at the
first difference), so every module that lists the accountant moves too.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
boazdavid added a commit that referenced this pull request Nov 26, 2026
v0.7.9 adds lifecycle.Finish (context-guru #1359), which the next commit
uses, and agent.Client.SubagentID (#1318). It requires accountant v0.7.0
(accountant #110: a cache rewrite is charged to the new change at the
first difference), so every module that lists the accountant moves too.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Parity tests: Handle request-body mutation

2 participants