Skip to content

Commit

Permalink
ws: make acceptable() more robust (denoland/deno_std#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 14, 2019
1 parent 2cf289d commit 9f877cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ws/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,12 @@ class WebSocketImpl implements WebSocket {

/** Return whether given headers is acceptable for websocket */
export function acceptable(req: { headers: Headers }): boolean {
const upgrade = req.headers.get("upgrade");
if (!upgrade || upgrade.toLowerCase() !== "websocket") {
return false;
}
const secKey = req.headers.get("sec-websocket-key");
return (
req.headers.get("upgrade") === "websocket" &&
req.headers.has("sec-websocket-key") &&
typeof secKey === "string" &&
secKey.length > 0
Expand Down
16 changes: 16 additions & 0 deletions ws/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ test(function wsAcceptable(): void {
})
});
assertEquals(ret, true);

assert(
acceptable({
headers: new Headers([
["connection", "Upgrade"],
["host", "127.0.0.1:9229"],
[
"sec-websocket-extensions",
"permessage-deflate; client_max_window_bits"
],
["sec-websocket-key", "dGhlIHNhbXBsZSBub25jZQ=="],
["sec-websocket-version", "13"],
["upgrade", "WebSocket"]
])
})
);
});

const invalidHeaders = [
Expand Down

0 comments on commit 9f877cd

Please sign in to comment.