deno/tests/unit_node/crypto/crypto_timing_safe_equal_test.ts
Daniel Osvaldo Rahmanto 328d5ef2a0
Some checks are pending
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
fix(ext/node): accept ArrayBuffer on crypto.timingSafeEqual (#30773)
Fixes #30759

The buffer validation is based on Node.js implementation:
591ba692bf/src/crypto/crypto_util.h (L467-L472)
2025-09-18 16:51:56 +02:00

39 lines
1.3 KiB
TypeScript

// Copyright 2018-2025 the Deno authors. MIT license.
import * as crypto from "node:crypto";
import { assertEquals, assertThrows } from "@std/assert";
import { Buffer } from "node:buffer";
Deno.test("timingSafeEqual ArrayBuffer and TypedArray", async () => {
const data = new TextEncoder().encode("foo"); // Uint8Array
const hash = await crypto.subtle.digest("SHA-256", data); // ArrayBuffer
const ui8a = new Uint8Array(hash); // Uint8Array
// @ts-ignore crypto.timingSafeEqual accepts ArrayBuffer
const eq = crypto.timingSafeEqual(hash, ui8a);
assertEquals(eq, true);
});
Deno.test("timingSafeEqual rejects non ArrayBuffer/TypedArray", () => {
const str = "foo";
const buf = Buffer.from(str);
const i = 123;
assertThrows(
() => {
// @ts-expect-error testing invalid input
crypto.timingSafeEqual(str, buf);
},
'TypeError: The "buf1" argument must be an instance of Buffer, ArrayBuffer, TypedArray, or DataView.' +
"Received type string ('foo')",
);
assertThrows(
() => {
// @ts-expect-error testing invalid input
crypto.timingSafeEqual(buf, i);
},
'TypeError: The "buf2" argument must be an instance of Buffer, ArrayBuffer, TypedArray, or DataView.' +
"Received type number (123)",
);
});