Skip to content

Commit

Permalink
fix(ext/http): allow multiple values in upgrade header for websocket (d…
Browse files Browse the repository at this point in the history
…enoland#12551)

Co-authored-by: Aaron O'Mullan <[email protected]>
  • Loading branch information
crowlKats and AaronO committed Oct 26, 2021
1 parent 9161e74 commit 6268703
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
16 changes: 8 additions & 8 deletions cli/tests/unit/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,10 @@ unitTest(function httpUpgradeWebSocket() {
);
});

unitTest(function httpUpgradeWebSocketLowercaseUpgradeHeader() {
unitTest(function httpUpgradeWebSocketMultipleConnectionOptions() {
const request = new Request("https://deno.land/", {
headers: {
connection: "upgrade",
connection: "keep-alive, upgrade",
upgrade: "websocket",
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
},
Expand All @@ -738,11 +738,11 @@ unitTest(function httpUpgradeWebSocketLowercaseUpgradeHeader() {
assertEquals(response.status, 101);
});

unitTest(function httpUpgradeWebSocketMultipleConnectionOptions() {
unitTest(function httpUpgradeWebSocketMultipleUpgradeOptions() {
const request = new Request("https://deno.land/", {
headers: {
connection: "keep-alive, upgrade",
upgrade: "websocket",
connection: "upgrade",
upgrade: "websocket, foo",
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
},
});
Expand All @@ -754,7 +754,7 @@ unitTest(function httpUpgradeWebSocketCaseInsensitiveUpgradeHeader() {
const request = new Request("https://deno.land/", {
headers: {
connection: "upgrade",
upgrade: "websocket",
upgrade: "Websocket",
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
},
});
Expand All @@ -775,7 +775,7 @@ unitTest(function httpUpgradeWebSocketInvalidUpgradeHeader() {
Deno.upgradeWebSocket(request);
},
TypeError,
"Invalid Header: 'upgrade' header must be 'websocket'",
"Invalid Header: 'upgrade' header must contain 'websocket'",
);
});

Expand All @@ -791,7 +791,7 @@ unitTest(function httpUpgradeWebSocketWithoutUpgradeHeader() {
Deno.upgradeWebSocket(request);
},
TypeError,
"Invalid Header: 'upgrade' header must be 'websocket'",
"Invalid Header: 'upgrade' header must contain 'websocket'",
);
});

Expand Down
11 changes: 8 additions & 3 deletions ext/http/01_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,14 @@

function upgradeWebSocket(request, options = {}) {
const upgrade = request.headers.get("upgrade");
if (!upgrade || StringPrototypeToLowerCase(upgrade) !== "websocket") {
const upgradeHasWebSocketOption = upgrade !== null &&
ArrayPrototypeSome(
StringPrototypeSplit(upgrade, /\s*,\s*/),
(option) => StringPrototypeToLowerCase(option) === "websocket",
);
if (!upgradeHasWebSocketOption) {
throw new TypeError(
"Invalid Header: 'upgrade' header must be 'websocket'",
"Invalid Header: 'upgrade' header must contain 'websocket'",
);
}

Expand All @@ -363,7 +368,7 @@
);
if (!connectionHasUpgradeOption) {
throw new TypeError(
"Invalid Header: 'connection' header must be 'Upgrade'",
"Invalid Header: 'connection' header must contain 'Upgrade'",
);
}

Expand Down
4 changes: 3 additions & 1 deletion ext/http/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ fn is_websocket_request(req: &hyper::Request<hyper::Body>) -> bool {
&& req.method() == hyper::Method::GET
&& req.headers().contains_key(&SEC_WEBSOCKET_KEY)
&& header(req.headers(), &SEC_WEBSOCKET_VERSION) == b"13"
&& header(req.headers(), &UPGRADE).eq_ignore_ascii_case(b"websocket")
&& header(req.headers(), &UPGRADE)
.split(|c| *c == b' ' || *c == b',')
.any(|token| token.eq_ignore_ascii_case(b"websocket"))
&& header(req.headers(), &CONNECTION)
.split(|c| *c == b' ' || *c == b',')
.any(|token| token.eq_ignore_ascii_case(b"upgrade"))
Expand Down

0 comments on commit 6268703

Please sign in to comment.