Skip to content

Commit 230601c

Browse files
Server websocket implementation should allow, ignore other subprotocols (#1112)
* Server websocket implementation should allow, ignore other subprotocols Correctly reads all Sec-Websocket-Protocol headers and reads all values in those headers, and only succeeds if the expected subprotcol value of "grpc-websockets" is present. This implementation roughly mirrors what the nhooyr.io/websocket Accept() method does, with a few simplifications. Fixes #1111 * Manually update docs, correct indentation * Review feedback Canonicalize the header key before read, normalizing the possible values. Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]> Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]>
1 parent 53aaf4c commit 230601c

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

go/grpcweb/DOC.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ the "content-type" is "application/grpc-web" and that the method is POST.
286286
func (w *WrappedGrpcServer) IsGrpcWebSocketRequest(req *http.Request) bool
287287
```
288288
IsGrpcWebSocketRequest determines if a request is a gRPC-Web request by checking
289-
that the "Sec-Websocket-Protocol" header value is "grpc-websockets"
289+
that the "Sec-Websocket-Protocol" header value contains "grpc-websockets"
290290

291291
#### func (*WrappedGrpcServer) ServeHTTP
292292

go/grpcweb/wrapper.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,21 @@ func (w *WrappedGrpcServer) ServeHTTP(resp http.ResponseWriter, req *http.Reques
133133
}
134134

135135
// IsGrpcWebSocketRequest determines if a request is a gRPC-Web request by checking that the "Sec-Websocket-Protocol"
136-
// header value is "grpc-websockets"
136+
// header value contains "grpc-websockets"
137137
func (w *WrappedGrpcServer) IsGrpcWebSocketRequest(req *http.Request) bool {
138-
return strings.ToLower(req.Header.Get("Upgrade")) == "websocket" && strings.ToLower(req.Header.Get("Sec-Websocket-Protocol")) == "grpc-websockets"
138+
if strings.ToLower(req.Header.Get("Upgrade")) != "websocket" {
139+
return false
140+
}
141+
142+
for _, subproto := range req.Header.Values("Sec-Websocket-Protocol") {
143+
for _, token := range strings.Split(subproto, ",") {
144+
token = strings.TrimSpace(token)
145+
if strings.EqualFold(token, "grpc-websockets") {
146+
return true
147+
}
148+
}
149+
}
150+
return false
139151
}
140152

141153
// HandleGrpcWebRequest takes a HTTP request that is assumed to be a gRPC-Web request and wraps it with a compatibility

0 commit comments

Comments
 (0)