From 4e44b88b9d27e9a86f0f1ed08a4569567be3742b Mon Sep 17 00:00:00 2001 From: Matt Krick Date: Wed, 11 Nov 2026 12:25:03 -0800 Subject: [PATCH] Only upgrade a GET that carries Upgrade: websocket --- src/App.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/App.h b/src/App.h index 58c3f2b45..89c4d0205 100644 --- a/src/App.h +++ b/src/App.h @@ -46,6 +46,17 @@ namespace uWS { return true; } + + /* A WebSocket handshake carries "Upgrade: websocket", compared case-insensitively (RFC 6455 4.2.1). + * A proxy that strips this hop-by-hop header still forwards Sec-WebSocket-Key, and answering + * that plain GET with 101 Switching Protocols breaks the proxy */ + inline bool isWebSocketUpgrade(std::string_view upgrade) { + if (upgrade.length() != 9) return false; + for (size_t i = 0; i < 9; i++) { + if ((upgrade[i] | 32) != "websocket"[i]) return false; + } + return true; + } } /* An app is a convenience wrapper of some of the most used fuctionalities and allows a @@ -440,9 +451,9 @@ struct TemplatedApp : public HttpCache> { httpContext->onHttp("GET", pattern, [webSocketContext, behavior = std::move(behavior)](auto *res, auto *req) mutable { - /* If we have this header set, it's a websocket */ + /* If we have these headers set, it's a websocket */ std::string_view secWebSocketKey = req->getHeader("sec-websocket-key"); - if (secWebSocketKey.length() == 24) { + if (secWebSocketKey.length() == 24 && isWebSocketUpgrade(req->getHeader("upgrade"))) { /* Emit upgrade handler */ if (behavior.upgrade) {