Repository navigation
Let websocket upgrades through WAF-enabled routes - #1461
Conversation
The ingress wraps every response in a responseWriter that records status and size. It exposed Hijack and Flush only through Unwrap, which http.ResponseController follows but Coraza does not: Coraza type-asserts http.Hijacker and http.Flusher on the writer it receives. Behind a WAF-enabled route the reverse proxy therefore could not hijack the connection, and upgrades failed with "can't switch protocols using non-Hijacker ResponseWriter". Streamed responses also lost their flushes. Implement Hijack and Flush on responseWriter directly. Fixes #1456 Fixes MIR-2272
Covers the Flush half of the websocket fix: a chunked 404 behind a level-1 WAF route reaches the client as a 404, and its first chunk arrives while the backend is still writing. Without responseWriter.Flush, the client never receives the response headers.
There was a problem hiding this comment.
🍪 biscuit: ✅ ready to merge — auto-review, non-blocking
Approval recommended, not submitted: this repository is not enabled for autonomous approvals. This review is a comment, not a GitHub approval.
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.
🍪 full review note · reviewed at a30c67f
phinze
left a comment
There was a problem hiding this comment.
🤖 Traced this against Coraza v3.7.0's wrap()/rwInterceptor and our proxy's upgrade path, and checked that reverting Hijack or Flush alone fails the matching new test. Request-phase inspection is untouched and the hijack tracker keeps Coraza's response phase out of the way. Nice fix, and it finally closes the loop on MIR-379, which was marked done on the theory that Unwrap was enough.
While tracing I noticed upgrades record as a 200 with 0 bytes and skip the access log (same with or without WAF, so not this PR's problem). Filed as MIR-2282.
Fixes #1456 (MIR-2272).
With
miren route wafon, websocket upgrades through the route failed withcan't switch protocols using non-Hijacker ResponseWriter type struct { http.responseWriter }.The ingress wraps every response in its own
responseWriterto record status and size. That wrapper offeredHijackandFlushonly throughUnwrap. Without the WAF that's enough, because the reverse proxy useshttp.ResponseController, which followsUnwrap. Coraza doesn't: it type-assertshttp.Hijackerandhttp.Flusheron the writer it's handed, finds neither, and passes the proxy a writer that can't hijack.The fix gives
responseWriterits ownHijackandFlush, which pass the call to the real writer. That repairs upgrades, and also flushes of streamed responses (SSE) under WAF, which Coraza was dropping for the same reason. The WAF still inspects the handshake request.Fixing it in our wrapper rather than in
pkg/wafcovers any other middleware that checks for these interfaces the same way.Verification
TestWafMiddlewareAllowsWebSocketUpgradesends an upgrade through the same writer chain as production (ingressresponseWriter→ WAF level 1 → reverse proxy → backend), then sends a line each way over the tunnel. On the old code it fails with the exact error from the issue and a 502; with the fix it gets 101 and the echo comes back.go test ./servers/httpingress/passes;golangci-lintreports 0 issues.TestWafMiddlewareStreamsChunkedResponsesends a chunked 404 through the same chain. The client gets the 404 and receives the first chunk while the backend is still writing. WithFlushremoved, the client never receives response headers.Amp thread: https://ampcode.com/threads/T-4c35255d-c925-417e-a808-8c2ff0b741de