Skip to content

Commit 0a6b910

Browse files
Egor GorbunovEdgarBarrantes
authored andcommitted
Do not lower header tokens in headerTokens() (coder#273)
HTTP header values, as opposed to header keys, are case sensitive, but implementation of headerTokens() before this patch would return lowered values always. This old behavior could lead to chromium (v87) WebSocket rejecting connnection because negotiated subprotocol, returned in Sec-WebSocket-Protocol header (lowered be headerToken() function) would not match one sent by client, in case client specified value with capital letters.
1 parent 544135c commit 0a6b910

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

accept.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ func verifyClientRequest(w http.ResponseWriter, r *http.Request) (errCode int, _
159159
return http.StatusUpgradeRequired, fmt.Errorf("WebSocket protocol violation: handshake request must be at least HTTP/1.1: %q", r.Proto)
160160
}
161161

162-
if !headerContainsToken(r.Header, "Connection", "Upgrade") {
162+
if !headerContainsTokenIgnoreCase(r.Header, "Connection", "Upgrade") {
163163
w.Header().Set("Connection", "Upgrade")
164164
w.Header().Set("Upgrade", "websocket")
165165
return http.StatusUpgradeRequired, fmt.Errorf("WebSocket protocol violation: Connection header %q does not contain Upgrade", r.Header.Get("Connection"))
166166
}
167167

168-
if !headerContainsToken(r.Header, "Upgrade", "websocket") {
168+
if !headerContainsTokenIgnoreCase(r.Header, "Upgrade", "websocket") {
169169
w.Header().Set("Connection", "Upgrade")
170170
w.Header().Set("Upgrade", "websocket")
171171
return http.StatusUpgradeRequired, fmt.Errorf("WebSocket protocol violation: Upgrade header %q does not contain websocket", r.Header.Get("Upgrade"))
@@ -268,11 +268,9 @@ func acceptDeflate(ext websocketExtension, mode CompressionMode) (*compressionOp
268268
return copts, true
269269
}
270270

271-
func headerContainsToken(h http.Header, key, token string) bool {
272-
token = strings.ToLower(token)
273-
271+
func headerContainsTokenIgnoreCase(h http.Header, key, token string) bool {
274272
for _, t := range headerTokens(h, key) {
275-
if t == token {
273+
if strings.EqualFold(t, token) {
276274
return true
277275
}
278276
}
@@ -313,7 +311,6 @@ func headerTokens(h http.Header, key string) []string {
313311
for _, v := range h[key] {
314312
v = strings.TrimSpace(v)
315313
for _, t := range strings.Split(v, ",") {
316-
t = strings.ToLower(t)
317314
t = strings.TrimSpace(t)
318315
tokens = append(tokens, t)
319316
}

accept_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ func Test_selectSubprotocol(t *testing.T) {
253253
serverProtocols: []string{"echo2", "echo3"},
254254
negotiated: "echo3",
255255
},
256+
{
257+
name: "clientCasePresered",
258+
clientProtocols: []string{"Echo1"},
259+
serverProtocols: []string{"echo1"},
260+
negotiated: "Echo1",
261+
},
256262
}
257263

258264
for _, tc := range testCases {

dial.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ func verifyServerResponse(opts *DialOptions, copts *compressionOptions, secWebSo
194194
return nil, fmt.Errorf("expected handshake response status code %v but got %v", http.StatusSwitchingProtocols, resp.StatusCode)
195195
}
196196

197-
if !headerContainsToken(resp.Header, "Connection", "Upgrade") {
197+
if !headerContainsTokenIgnoreCase(resp.Header, "Connection", "Upgrade") {
198198
return nil, fmt.Errorf("WebSocket protocol violation: Connection header %q does not contain Upgrade", resp.Header.Get("Connection"))
199199
}
200200

201-
if !headerContainsToken(resp.Header, "Upgrade", "WebSocket") {
201+
if !headerContainsTokenIgnoreCase(resp.Header, "Upgrade", "WebSocket") {
202202
return nil, fmt.Errorf("WebSocket protocol violation: Upgrade header %q does not contain websocket", resp.Header.Get("Upgrade"))
203203
}
204204

0 commit comments

Comments
 (0)