WAF on a route breaks websocket upgrades: "can't switch protocols using non-Hijacker ResponseWriter"
What happens
With miren route waf enabled on a route, a websocket connection through that route never upgrades. The browser reports:
WebSocket connection to 'wss://media.example.com/rtc/v1?access_token=…' failed: There was a bad response from the server.
and the server's system log has one of these per attempt:
proxy error app=livekit error="can't switch protocols using non-Hijacker ResponseWriter type struct { http.responseWriter }" module=httpingress url=http://<sandbox address>:7880
Plain HTTP requests on the same route are fine, and the WAF does its job on them (a request for /.env gets a 403). Only the upgrade fails. Turning the WAF off (miren route waf <host> --disable) fixes it at once, with no redeploy.
To reproduce
- Deploy any app that serves a websocket. Ours is LiveKit from its stock image (
livekit/livekit-server:v1.13.11), HTTP port 7880. miren route set media.example.com livekit- Connect a websocket through the route: works.
miren route waf media.example.com(level 1, the default)- Connect again: the handshake fails, and the log line above appears.
miren route waf media.example.com --disable: works again.
Versions
- Server and CLI: v0.18.0 (09d0265), single node, Ubuntu 24.04, x86_64
- WAF level 1
I have not tried older releases, so I don't know whether this is new.
Where it seems to come from
Reading v0.18.0, not something I have confirmed by patching:
pkg/httputil/reverseproxy.goupgrades by callinghttp.NewResponseController(rw).Hijack()and reports this exact error when that returnshttp.ErrNotSupported.servers/httpingress/waf.goruns the proxy handler insidecorazahttp.WrapHandler(pkg/waf/waf.go). Coraza hands the next handler its own response writer, which captures the response for inspection. The type in the error,struct { http.responseWriter }, is not the ingress's writer, and it apparently offers neitherHijacknor anUnwrapthatResponseControllercould follow to the real one.
If that is right, any websocket (or other upgraded connection) behind a WAF-enabled route fails the same way, whatever the app.
What I'd expect
A websocket upgrade on a WAF-enabled route to work, with the WAF inspecting the handshake request and then getting out of the way. If that is not possible, then miren route waf saying so when it is turned on, and a line in the docs, would have saved the surprise. :)
Workaround
Leave the WAF off for routes that carry websockets.
Thanks Mirenians! 💙