From 015e78928a374afb016a12db137d9ca7f1a37a03 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 7 Oct 2020 20:05:14 +0200 Subject: [PATCH] node: relax websocket connection header check (#21646) This makes it accept the "upgrade,keep-alive" header value, which apparently is a thing. --- node/rpcstack.go | 2 +- node/rpcstack_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/node/rpcstack.go b/node/rpcstack.go index caf7e5b7a..731e807ac 100644 --- a/node/rpcstack.go +++ b/node/rpcstack.go @@ -313,7 +313,7 @@ func (h *httpServer) wsAllowed() bool { // isWebsocket checks the header of an http request for a websocket upgrade request. func isWebsocket(r *http.Request) bool { return strings.ToLower(r.Header.Get("Upgrade")) == "websocket" && - strings.ToLower(r.Header.Get("Connection")) == "upgrade" + strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade") } // NewHTTPHandlerStack returns wrapped http-related handlers diff --git a/node/rpcstack_test.go b/node/rpcstack_test.go index efab5b37d..0ee120efd 100644 --- a/node/rpcstack_test.go +++ b/node/rpcstack_test.go @@ -73,6 +73,21 @@ func TestWebsocketOrigins(t *testing.T) { assert.Error(t, err) } +// TestIsWebsocket tests if an incoming websocket upgrade request is handled properly. +func TestIsWebsocket(t *testing.T) { + r, _ := http.NewRequest("GET", "/", nil) + + assert.False(t, isWebsocket(r)) + r.Header.Set("upgrade", "websocket") + assert.False(t, isWebsocket(r)) + r.Header.Set("connection", "upgrade") + assert.True(t, isWebsocket(r)) + r.Header.Set("connection", "upgrade,keep-alive") + assert.True(t, isWebsocket(r)) + r.Header.Set("connection", " UPGRADE,keep-alive") + assert.True(t, isWebsocket(r)) +} + func createAndStartServer(t *testing.T, conf httpConfig, ws bool, wsConf wsConfig) *httpServer { t.Helper()