fix: generate proper Sec-WebSocket-Key for WebSocket handshake#1228
Open
Tonkpils wants to merge 1 commit intogo-rod:mainfrom
Open
fix: generate proper Sec-WebSocket-Key for WebSocket handshake#1228Tonkpils wants to merge 1 commit intogo-rod:mainfrom
Tonkpils wants to merge 1 commit intogo-rod:mainfrom
Conversation
The handshake method set Sec-WebSocket-Key to the literal string "nil", which causes 400 Bad Request on servers that validate the key per RFC 6455 (e.g. Browserless.io). - Generate a random 16-byte base64-encoded key instead of "nil" - Fix header override comparison to use case-insensitive match (Go canonicalizes to Sec-Websocket-Key, breaking exact match) - Add test verifying the key is valid base64, not "nil"
da85f59 to
950cf4d
Compare
| defaultSecKey := "nil" | ||
| keyBytes := make([]byte, 16) | ||
| _, _ = rand.Read(keyBytes) | ||
| defaultSecKey := base64.StdEncoding.EncodeToString(keyBytes) |
There was a problem hiding this comment.
You could set directly secKey here instead of using 2 different vars.
krichprollsch
approved these changes
Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
handshakemethod setsSec-WebSocket-Keyto the literal string"nil", which causes400 Bad Requeston WebSocket servers that validate the key per RFC 6455 §4.1 — notably Browserless.io. #1092 (comment)What changed
"nil"k == "Sec-WebSocket-Key"check uses exact case, but Go'shttp.Headercanonicalizes keys (lowercasewinWebsocket). Switched tostrings.EqualFoldso user-provided header overrides actually work.TestSecWebSocketKeyIsValidverifies the generated key is valid base64 (16 bytes decoded) and not"nil", and that consecutive keys are unique.Context
Ran into this connecting to Browserless.io from a Go service using Rod. The literal
"nil"key doesn't pass their handshake validation. Generating a real key per the RFC spec fixes it.