Skip to content

Let websocket upgrades through WAF-enabled routes - #1461

Merged
evanphx merged 3 commits into
mainfrom
evan/mir-2272-waf-on-a-route-breaks-websocket-upgrades-cant
Dec 3, 2026
Merged

evanphx merged 3 commits into
mainfrom
evan/mir-2272-waf-on-a-route-breaks-websocket-upgrades-cant

Conversation

@evanphx

@evanphx evanphx commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1456 (MIR-2272).

With miren route waf on, websocket upgrades through the route failed with can't switch protocols using non-Hijacker ResponseWriter type struct { http.responseWriter }.

The ingress wraps every response in its own responseWriter to record status and size. That wrapper offered Hijack and Flush only through Unwrap. Without the WAF that's enough, because the reverse proxy uses http.ResponseController, which follows Unwrap. Coraza doesn't: it type-asserts http.Hijacker and http.Flusher on the writer it's handed, finds neither, and passes the proxy a writer that can't hijack.

The fix gives responseWriter its own Hijack and Flush, 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/waf covers any other middleware that checks for these interfaces the same way.

Verification

  • New TestWafMiddlewareAllowsWebSocketUpgrade sends an upgrade through the same writer chain as production (ingress responseWriter → 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-lint reports 0 issues.
  • New TestWafMiddlewareStreamsChunkedResponse sends a chunked 404 through the same chain. The client gets the 404 and receives the first chunk while the backend is still writing. With Flush removed, the client never receives response headers.

Amp thread: https://ampcode.com/threads/T-4c35255d-c925-417e-a808-8c2ff0b741de

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
miren-code-agent[bot]

This comment was marked as outdated.

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.
miren-code-agent[bot]

This comment was marked as outdated.

@miren-code-agent miren-code-agent Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🍪 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

@evanphx
evanphx marked this pull request as ready for review December 2, 2026 23:13
@evanphx
evanphx requested a review from a team as a code owner December 2, 2026 23:13
@phinze phinze self-assigned this Dec 2, 2026

@phinze phinze left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 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.

@evanphx
evanphx merged commit 364071e into main Dec 3, 2026
41 of 43 checks passed
@evanphx
evanphx deleted the evan/mir-2272-waf-on-a-route-breaks-websocket-upgrades-cant branch December 3, 2026 20:22
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.

WAF on a route breaks websocket upgrades: "can't switch protocols using non-Hijacker ResponseWriter"

2 participants