From 64a9dd7b83981710a8e0b0ffa18a526f774279a2 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Tue, 14 Jun 2022 21:28:59 +0200 Subject: [PATCH] crypto: fix webcrypto digest() invalid algorithm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/43431 Reviewed-By: Tobias Nießen Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- lib/internal/crypto/hash.js | 25 ++++++++++++++++++++----- test/parallel/test-webcrypto-digest.js | 6 ++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js index 6c502f52b66503..956053af0a8c2c 100644 --- a/lib/internal/crypto/hash.js +++ b/lib/internal/crypto/hash.js @@ -28,6 +28,10 @@ const { prepareSecretKey, } = require('internal/crypto/keys'); +const { + lazyDOMException, +} = require('internal/util'); + const { Buffer, } = require('buffer'); @@ -171,11 +175,22 @@ async function asyncDigest(algorithm, data) { if (algorithm.length !== undefined) validateUint32(algorithm.length, 'algorithm.length'); - return jobPromise(new HashJob( - kCryptoJobAsync, - normalizeHashName(algorithm.name), - data, - algorithm.length)); + switch (algorithm.name) { + case 'SHA-1': + // Fall through + case 'SHA-256': + // Fall through + case 'SHA-384': + // Fall through + case 'SHA-512': + return jobPromise(new HashJob( + kCryptoJobAsync, + normalizeHashName(algorithm.name), + data, + algorithm.length)); + } + + throw lazyDOMException('Unrecognized name.', 'NotSupportedError'); } module.exports = { diff --git a/test/parallel/test-webcrypto-digest.js b/test/parallel/test-webcrypto-digest.js index 78b12ea6a5574c..b8680564d1a1a3 100644 --- a/test/parallel/test-webcrypto-digest.js +++ b/test/parallel/test-webcrypto-digest.js @@ -168,3 +168,9 @@ async function testDigest(size, name) { await Promise.all(variations); })().then(common.mustCall()); + +(async () => { + await assert.rejects(subtle.digest('RSA-OAEP', Buffer.alloc(1)), { + name: 'NotSupportedError', + }); +})().then(common.mustCall());