From 79c6e052ed29b9b78c31f2e01da3b91f76b6a017 Mon Sep 17 00:00:00 2001 From: Suguru Motegi Date: Mon, 24 Feb 2020 13:37:15 -0800 Subject: [PATCH] fix: createSecKey logic (#4063) --- cli/tests/std_tests.rs | 1 + std/ws/mod.ts | 2 +- std/ws/test.ts | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cli/tests/std_tests.rs b/cli/tests/std_tests.rs index 7b7928f70b..6ed5d25185 100644 --- a/cli/tests/std_tests.rs +++ b/cli/tests/std_tests.rs @@ -20,6 +20,7 @@ mod tests { cwd.push("std"); let mut deno = deno_cmd .current_dir(cwd) // note: std tests expect to run from "std" dir + .arg("--seed=86") // Some tests rely on specific random numbers. .arg("-A") // .arg("-Ldebug") .arg("./testing/runner.ts") diff --git a/std/ws/mod.ts b/std/ws/mod.ts index 5a8f0bd2e5..3128a88b56 100644 --- a/std/ws/mod.ts +++ b/std/ws/mod.ts @@ -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); diff --git a/std/ws/test.ts b/std/ws/test.ts index 5a0ff99298..3f5475c80c 100644 --- a/std/ws/test.ts +++ b/std/ws/test.ts @@ -8,6 +8,7 @@ import { acceptable, connectWebSocket, createSecAccept, + createSecKey, handshake, OpCode, readFrame, @@ -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 = {