mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-30 07:14:39 +08:00
createHash, createHmac, crypto.hash, pbkdf2, pbkdf2Sync, hkdf and hkdfSync accept every digest crypto.getHashes() lists in Node 24: SHA-512/224, SHA-512/256, SHA-256/192, SHA3-224 to SHA3-512, Keccak-224 to Keccak-512, SHAKE128, SHAKE256, KECCAK-KMAC-128, KECCAK-KMAC-256, RIPEMD-160, BLAKE2b-512, BLAKE2s-256, SM3 and MD5-SHA1. The new runtime unit scr_hash.c implements Keccak, RIPEMD-160, BLAKE2b, BLAKE2s and SM3, and is registered in both runtime pack matrices and the C-compile path. Names resolve as Node 24.22 resolves them: case-insensitively, with OpenSSL's provider aliases and object identifiers, ending at the first NUL, and with trailing spaces allowed except on the RSA signature aliases. "dss1" and "DSS1" are SHA-1, and createHash and crypto.hash accept "dss1" in any case. The compiler no longer refuses literal digest names outside SHA-1/SHA-2 and MD5; unknown names throw Node's errors at run time. SHAKE and KMAC digests use their default output lengths. createHmac rejects them with Node's OpenSSL error, pbkdf2 with "xof digests not allowed", and hkdf with "Deriving bits failed". pbkdf2 with a key length of 0 now fails with "Deriving bits failed" as in Node 24.22, and the callback forms pass these failures to the callback. PBKDF2 computes the keyed HMAC states once instead of on every iteration, and digest contexts are cleared with memset and a compiler barrier instead of a byte loop. The Keccak permutation, the RIPEMD-160 and BLAKE2 compression functions and the SM3 rounds are unrolled with constant indices.
75 lines
3.0 KiB
TypeScript
75 lines
3.0 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { mkdir } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { promisify } from "node:util";
|
|
import { beforeAll, expect, test } from "vitest";
|
|
|
|
const execFileAsync = promisify(execFile);
|
|
const testDir = import.meta.dirname;
|
|
const bin = join(testDir, "build", "test_url_legacy");
|
|
const vendorDir = join(testDir, "../vendor/quickjs-ng");
|
|
|
|
// Compiled once with ASan + the RC audit: the oracle run also proves the
|
|
// legacy url port neither leaks nor double-frees across the case file's
|
|
// calls, thrown errors included. Hostnames lowercase and normalize through
|
|
// the vendored libunicode, as in programs.
|
|
beforeAll(async () => {
|
|
await mkdir(join(testDir, "build"), { recursive: true });
|
|
await execFileAsync("clang", [
|
|
// no -Wall/-Wextra: the vendored libunicode.c is not warning-clean
|
|
"-std=c11",
|
|
"-O1",
|
|
"-fsanitize=address",
|
|
"-DSCR_RC_AUDIT",
|
|
"-I",
|
|
vendorDir,
|
|
"-o",
|
|
bin,
|
|
join(testDir, "test_url_legacy.c"),
|
|
join(testDir, "../src/scr_url_legacy.c"),
|
|
join(testDir, "../src/scr_idna.c"),
|
|
join(testDir, "../src/scr_qs.c"),
|
|
join(testDir, "../src/scr_regex.c"),
|
|
// scr_regex.c hosts assert.match and calls the assert throw/inspect
|
|
// helpers — regex-linking binaries always carry scr_assert.c.
|
|
join(testDir, "../src/scr_assert.c"),
|
|
join(vendorDir, "libregexp.c"),
|
|
join(vendorDir, "libunicode.c"),
|
|
join(testDir, "../src/scr_string.c"),
|
|
join(testDir, "../src/scr_map.c"),
|
|
join(testDir, "../src/scr_array.c"),
|
|
join(testDir, "../src/scr_json.c"),
|
|
join(testDir, "../src/scr_bigint.c"),
|
|
join(testDir, "../src/scr_exception.c"),
|
|
join(testDir, "../src/scr_error.c"),
|
|
join(testDir, "../src/scr_number.c"),
|
|
join(testDir, "../src/scr_console.c"),
|
|
join(testDir, "../src/scr_closure.c"),
|
|
join(testDir, "../src/scr_object.c"),
|
|
join(testDir, "../src/scr_union.c"),
|
|
join(testDir, "../src/scr_cycle.c"),
|
|
join(testDir, "../src/scr_alloc.c"),
|
|
join(testDir, "../src/scr_lib.c"),
|
|
join(testDir, "../src/scr_hash.c"),
|
|
join(testDir, "../src/scr_url.c"),
|
|
join(testDir, "../src/scr_url_params.c"),
|
|
join(testDir, "../src/scr_path.c"),
|
|
join(testDir, "../src/scr_bytes.c"),
|
|
join(testDir, "../src/scr_shared.c"),
|
|
...(process.platform === "linux" ? ["-D_GNU_SOURCE", "-lm"] : []),
|
|
]);
|
|
}, 120_000);
|
|
|
|
// Runs against the committed case file, generated from Node v24 itself
|
|
// (gen-url-legacy-cases.mjs): curated domains (IDNA mapping, Punycode,
|
|
// ContextJ and bidi rules, IPv4 and IPv6 hosts), legacy URLs (hosts,
|
|
// auth, ports, backslashes, whitespace, autoEscape, IPv6, IDNA hostnames,
|
|
// parsed queries), url.resolve over a base/relative grid, url.format over
|
|
// objects, and a seeded fuzz corpus of each.
|
|
test("node:url legacy API and domain conversion match Node on committed oracle cases", async () => {
|
|
const { stderr } = await execFileAsync(bin, [join(testDir, "url-legacy-cases.txt")], {
|
|
maxBuffer: 16 * 1024 * 1024,
|
|
});
|
|
expect(stderr.trim()).toMatch(/^(\d+)\/\1 cases passed$/);
|
|
}, 120_000);
|