Skip to content

Commit

Permalink
node: fix websocket connection header check (ethereum#21646)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Nov 8, 2024
1 parent 63a53f9 commit 42ffeef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion node/rpcstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,5 @@ func NewWebsocketUpgradeHandler(h http.Handler, ws http.Handler) http.Handler {
// 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")
}
15 changes: 15 additions & 0 deletions node/rpcstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,18 @@ func TestNewWebsocketUpgradeHandler_websocket(t *testing.T) {
response := <-responses
assert.Equal(t, "websocket", response.Header.Get("Upgrade"))
}

// 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))
}

0 comments on commit 42ffeef

Please sign in to comment.