Skip to content

Commit

Permalink
fix: createSecKey logic (denoland/deno#4063)
Browse files Browse the repository at this point in the history
  • Loading branch information
suguru03 authored and caspervonb committed Jan 24, 2021
1 parent 1ea086d commit 7f31a18
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ws/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ const kSecChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.~_";
export function createSecKey(): string {
let key = "";
for (let i = 0; i < 16; i++) {
const j = Math.round(Math.random() * kSecChars.length);
const j = Math.floor(Math.random() * kSecChars.length);
key += kSecChars[j];
}
return btoa(key);
Expand Down
8 changes: 8 additions & 0 deletions ws/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
acceptable,
connectWebSocket,
createSecAccept,
createSecKey,
handshake,
OpCode,
readFrame,
Expand Down Expand Up @@ -328,6 +329,13 @@ test("WebSocket.send(), WebSocket.ping() should be exclusive", async (): Promise
assertEquals(bytes.equal(third.payload, new Uint8Array([3])), true);
});

test(function createSecKeyHasCorrectLength(): void {
// Note: relies on --seed=86 being passed to deno to reproduce failure in
// #4063.
const secKey = createSecKey();
assertEquals(atob(secKey).length, 16);
});

test("WebSocket should throw SocketClosedError when peer closed connection without close frame", async () => {
const buf = new Buffer();
const eofReader: Deno.Reader = {
Expand Down

0 comments on commit 7f31a18

Please sign in to comment.