From 78374c8af0bf7b5c19b75b172fd592e339da426b Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Tue, 22 Sep 2026 22:12:55 +0200 Subject: [PATCH] crypto: match algorithm names as ASCII Reject non-ASCII equivalents of registered Web Crypto algorithm names. Skip the character scan when the input already uses the canonical name. Signed-off-by: Filip Skokan Assisted-by: Codex PR-URL: https://github.com/nodejs/node/pull/66237 Reviewed-By: James M Snell Reviewed-By: Aviv Keller --- lib/internal/crypto/util.js | 10 ++++++++ .../test-webcrypto-algorithm-name-ascii.js | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 test/parallel/test-webcrypto-algorithm-name-ascii.js diff --git a/lib/internal/crypto/util.js b/lib/internal/crypto/util.js index 0f380569fb2..a86338b6866 100644 --- a/lib/internal/crypto/util.js +++ b/lib/internal/crypto/util.js @@ -22,6 +22,7 @@ const { PromiseWithResolvers, SafeMap, SafeSet, + StringPrototypeCharCodeAt, StringPrototypeToUpperCase, Symbol, TypedArrayPrototypeGetBuffer, @@ -794,6 +795,15 @@ function normalizeAlgorithm(algorithm, op) { if (canonicalName === undefined) throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError'); + // Registered names are ASCII. Only check characters when case folding was + // needed, so Unicode characters such as U+017F cannot match ASCII names. + if (algName !== canonicalName) { + for (let i = 0; i < algName.length; i++) { + if (StringPrototypeCharCodeAt(algName, i) > 0x7f) + throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError'); + } + } + algName = canonicalName; const desiredType = registeredAlgorithms[algName]; diff --git a/test/parallel/test-webcrypto-algorithm-name-ascii.js b/test/parallel/test-webcrypto-algorithm-name-ascii.js new file mode 100644 index 00000000000..a1b33830884 --- /dev/null +++ b/test/parallel/test-webcrypto-algorithm-name-ascii.js @@ -0,0 +1,24 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const { subtle } = globalThis.crypto; + +(async () => { + const data = new Uint8Array(0); + assert.deepStrictEqual(await subtle.digest('sha-256', data), + await subtle.digest('SHA-256', data)); + for (const name of ['\u017fha-256', 'SHA-256\u0131']) { + await assert.rejects(subtle.digest(name, data), { name: 'NotSupportedError' }); + assert.strictEqual(SubtleCrypto.supports('digest', name), false); + } + await assert.rejects(subtle.generateKey({ name: 'AE\u017f-GCM', length: 128 }, + false, ['encrypt']), + { name: 'NotSupportedError' }); + await assert.rejects(subtle.importKey('raw-secret', data, 'Argon2\u0131d', + false, ['deriveBits']), + { name: 'NotSupportedError' }); +})().then(common.mustCall());