Skip to content

Commit 3233cb5

Browse files
Emyrknhooyr
authored andcommitted
Remove all leading and trailing whitespace
1 parent f46da9a commit 3233cb5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

accept.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !js
12
// +build !js
23

34
package websocket
@@ -185,6 +186,8 @@ func verifyClientRequest(w http.ResponseWriter, r *http.Request) (errCode int, _
185186
}
186187

187188
websocketSecKey := r.Header.Get("Sec-WebSocket-Key")
189+
// The RFC states to remove any leading or trailing whitespace.
190+
websocketSecKey = strings.TrimSpace(websocketSecKey)
188191
if websocketSecKey == "" {
189192
return http.StatusBadRequest, errors.New("WebSocket protocol violation: missing Sec-WebSocket-Key")
190193
}

accept_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !js
12
// +build !js
23

34
package websocket
@@ -229,6 +230,16 @@ func Test_verifyClientHandshake(t *testing.T) {
229230
},
230231
success: true,
231232
},
233+
{
234+
name: "successSecKeyExtraSpace",
235+
h: map[string]string{
236+
"Connection": "keep-alive, Upgrade",
237+
"Upgrade": "websocket",
238+
"Sec-WebSocket-Version": "13",
239+
"Sec-WebSocket-Key": " " + xrand.Base64(16) + " ",
240+
},
241+
success: true,
242+
},
232243
}
233244

234245
for _, tc := range testCases {

0 commit comments

Comments
 (0)