Since my last review, the only change is a30c67f, and it does what I suggested. In TestWafMiddlewareStreamsChunkedResponse, the backend now calls Flush immediately after WriteHeader(http.StatusNotFound), before the 50ms sleep. As a result, the proxy's RoundTrip gets the 404 back before any body exists and then waits in copyBuffer. That is the window between the proxy's WriteHeader and its first Write. The comment next to the flush explains why it's there, which will help whoever bumps Coraza next. If a future version stopped gating Flush on the real WriteHeader, the assert.Equal(t, http.StatusNotFound, ...) check would catch it.
I also checked how Coraza behaves after the proxy hijacks the connection. pkg/waf/waf.go sets SecResponseBodyAccess Off, so Coraza doesn't buffer upgraded responses and doesn't replay them once the tunnel closes. The websocket path therefore relies only on the new Hijack pass-through, and TestWafMiddlewareAllowsWebSocketUpgrade exercises that end to end.
I still recommend approval. The fix is two small pass-through methods on the ingress writer, and real-socket tests cover both through the production writer chain. Nothing is left open.
Posted to the PR's comment threads when you submit.
WriteHeader(404), so the proxy now waits in copyBuffer for about 50ms after the status has reached Coraza. The 404 assertion now covers the exact race I was worried about. Thanks for adding the comment that explains why the flush is there.Verdict: ready
{
"owner": "mirendev",
"repo": "runtime",
"number": 1461,
"verdict": "ready",
"approve": true,
"event": "approve",
"summary": "Since my last review, the only change is a30c67f, and it does what I suggested. In `TestWafMiddlewareStreamsChunkedResponse`, the backend now calls `Flush` immediately after `WriteHeader(http.StatusNotFound)`, before the 50ms sleep. As a result, the proxy's `RoundTrip` gets the 404 back before any body exists and then waits in `copyBuffer`. That is the window between the proxy's `WriteHeader` and its first `Write`. The comment next to the flush explains why it's there, which will help whoever bumps Coraza next. If a future version stopped gating `Flush` on the real `WriteHeader`, the `assert.Equal(t, http.StatusNotFound, ...)` check would catch it.\n\nI also checked how Coraza behaves after the proxy hijacks the connection. `pkg/waf/waf.go` sets `SecResponseBodyAccess Off`, so Coraza doesn't buffer upgraded responses and doesn't replay them once the tunnel closes. The websocket path therefore relies only on the new `Hijack` pass-through, and `TestWafMiddlewareAllowsWebSocketUpgrade` exercises that end to end.\n\nI still recommend approval. The fix is two small pass-through methods on the ingress writer, and real-socket tests cover both through the production writer chain. Nothing is left open.",
"replies": [
{
"thread_id": "PRRT_kwDONZ__ss6-4Png",
"body": "a30c67f does this: the backend flushes right after `WriteHeader(404)`, so the proxy now waits in `copyBuffer` for about 50ms after the status has reached Coraza. The 404 assertion now covers the exact race I was worried about. Thanks for adding the comment that explains why the flush is there.",
"resolve": true,
"resolve_reason": "addressed"
}
],
"posted_to_pr": true,
"draft": true,
"head_sha": "a30c67fcfc519132f9a29d1a68d21c164a7baf7e"
}You've persuaded me on Flush, and I'm resolving that thread. My concern assumed Coraza exposes our responseWriter directly as its Flusher. You checked v3.7.0, the version go.mod pins, and it doesn't. The wrapper embeds Coraza's own responseWriter interface, so Flush resolves to rwInterceptor.Flush, and that only forwards once the real WriteHeader has gone out. The type in the original error, struct { http.responseWriter }, fits that model too. Before this PR, Coraza's gated Flush already existed but reached a writer that couldn't flush. So this change makes streamed responses under the WAF actually stream, and the implicit-200 risk I raised doesn't apply.
On the upgrade path, handleUpgradeResponse in pkg/httputil/reverseproxy.go calls http.NewResponseController(rw).Hijack() on Coraza's wrapper. Now that our writer is a Hijacker, that wrapper carries one, and the call reaches responseWriter.Hijack. That resolves through ResponseController to the real connection. The WAF stays outermost in buildRouteHandler, so it still runs its request phases on the handshake before the proxy is reached. That's the fix the issue asks for.
TestWafMiddlewareStreamsChunkedResponse is the test I asked for, and its defer order is right: close(release) runs before backend.Close(), so the blocked backend handler can't hang teardown. I left one inline suggestion that would make it exercise the exact window between the proxy's WriteHeader and its first Write. It's optional hardening rather than a blocker, because your source-level argument already settles the question.
I'm recommending approval. The change is two small methods in the right layer, both halves have end-to-end tests over real sockets through the production writer chain, and the one open question is answered. I don't think a human reviewer would add much here.
Flush after first\n. The proxy's RoundTrip therefore returns only after the 50ms sleep, usually with the first chunk already buffered, which leaves almost no gap between the proxy's WriteHeader and its first Write for the immediate AfterFunc(-1, delayedFlush) in copyResponse to land in. Add http.NewResponseController(w).Flush() right after WriteHeader(http.StatusNotFound). The proxy would then sit in copyBuffer for about 50ms with the status already handed to Coraza. That's the window where a future Coraza bump that dropped the isWriteHeaderFlush gate would show up here as a 200.Posted to the PR's comment threads when you submit.
Flusher. The embedded responseWriter interface resolves Flush to rwInterceptor.Flush, which is gated on isWriteHeaderFlush, so a proxy flush can't commit headers ahead of the backend's status. The struct { http.responseWriter } type in the original error fits that as well. Between this and the new test showing the 404 survives and the first chunk streams, I'm persuaded. I left one small inline suggestion that would make the test sit in the exact race window, but you don't need it to close this.Verdict: ready
The Hijack half of this fixes the reported failure, and it's in the right layer. When the WAF is on, Coraza decides what to expose by type-asserting http.Hijacker/http.Flusher on the writer it receives. It doesn't follow Unwrap, so the ingress responseWriter (servers/httpingress/httpingress.go:1507) was hiding both from it. TestWafMiddlewareAllowsWebSocketUpgrade is a good regression test. It uses real sockets with a deadline through ingress writer → WAF → proxy → backend. It checks for a 101 and a round-trip through the tunnel, so a hijack that "succeeds" without connecting the two ends would still fail it. The WAF still runs its request phases on the handshake before the proxy is called, which is what the issue asked for.
I'm less comfortable with the Flush half, and it's the part with no test. Before this change, Coraza had no Flush to offer, so a proxy Flush behind the WAF did nothing. Now Coraza's wrapper exposes the Flusher it found, which is our responseWriter. That call goes straight to the real writer and skips Coraza's interceptor. As I understand Coraza's rwInterceptor, WriteHeader only records the status, and the real WriteHeader is sent on the first Write or when the response finishes. I couldn't check this against the Coraza source here, because it isn't vendored. If I'm right, this sequence can happen in pkg/httputil/reverseproxy.go:
ContentLength == -1, which covers chunked responses and SSE, gets flushInterval == -1.copyResponse then schedules time.AfterFunc(-1, mlw.delayedFlush). That timer fires at once, usually while copyBuffer is still blocked reading the first chunk from the backend.WriteHeader, so net/http commits an implicit 200 using the current headers.WriteHeader. Metrics still record the real code, though, so they disagree with what the client actually got.That would change status codes for chunked error responses on every WAF route. Those are far more common than websockets, which makes this a worse problem than the one being fixed. I haven't confirmed it, so I'm treating it as an open question rather than a defect. You can settle it cheaply with a test like the websocket one: put a WAF level-1 route in front of a backend that returns a chunked 404 (WriteHeader(404), sleep briefly, then write a body), and assert that the client gets 404.
If the test shows the problem, there are two ways to fix it:
responseWriter.Flush do nothing until WriteHeader or Write has reached it.Flush out of this PR, so it fixes only Hijack, and handle SSE under the WAF separately.Leaving this at READY for human review rather than recommending approval until that question is answered.
Flusher, so a proxy flush skips Coraza's interceptor and reaches the real writer directly. As I understand Coraza, the interceptor holds back the real WriteHeader until the first Write. For responses with ContentLength == -1, the reverse proxy's immediate AfterFunc(-1, delayedFlush) can fire before that first write. net/http would then commit an implicit 200, and the backend's real status (404, 500, …) would be dropped as a superfluous WriteHeader. I couldn't confirm this against the Coraza source here, so please add a test before relying on it: WAF level 1, a backend that returns a chunked 404 with a short delay before the body, and an assertion that the client sees 404. If it fails, one fix is to make this a no-op until WriteHeader/Write has reached this writer. The other is to leave Flush out of this PR and handle SSE under the WAF separately.Verdict: ready