crypto: validate normalized hash names · nodejs/node@2d84b7e · GitHub
Skip to content

Commit 2d84b7e

Browse files
panvaaduh95
authored andcommitted
crypto: validate normalized hash names
Check the normalized SHA algorithm instead of reading the caller hash name before conversion. This prevents changing getters from bypassing the supported hash check. Signed-off-by: Filip Skokan <panva.ip@gmail.com> Assisted-by: Codex PR-URL: #66237 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 6bfd392 commit 2d84b7e

3 files changed

Lines changed: 54 additions & 20 deletions

File tree

‎lib/internal/crypto/util.js‎

Lines changed: 7 additions & 1 deletion

‎lib/internal/crypto/webidl.js‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
ObjectPrototypeHasOwnProperty,
88
StringPrototypeCharCodeAt,
99
StringPrototypeSplit,
10-
StringPrototypeStartsWith,
1110
StringPrototypeToLowerCase,
1211
} = primordials;
1312

@@ -68,18 +67,6 @@ function namedCurveValidator(V, dict) {
6867

6968
const converters = { __proto__: null, ...webidl };
7069

71-
/**
72-
* @param {string | object} V - The hash algorithm identifier (string or object).
73-
* @param {string} label - The dictionary name for the error message.
74-
*/
75-
function ensureSHA(V, label) {
76-
const name = typeof V === 'string' ? V : V.name;
77-
if (typeof name !== 'string' ||
78-
!StringPrototypeStartsWith(StringPrototypeToLowerCase(name), 'sha'))
79-
throw lazyDOMException(
80-
`Only SHA hashes are supported in ${label}`, 'NotSupportedError');
81-
}
82-
8370
converters.AlgorithmIdentifier = (V, opts) => {
8471
// Union for (object or DOMString)
8572
if (type(V) === 'Object') {
@@ -224,7 +211,6 @@ converters.RsaHashedKeyGenParams = createDictionaryConverter(
224211
{
225212
key: 'hash',
226213
converter: converters.HashAlgorithmIdentifier,
227-
validator: (V, dict) => ensureSHA(V, 'RsaHashedKeyGenParams'),
228214
required: true,
229215
},
230216
],
@@ -237,7 +223,6 @@ converters.RsaHashedImportParams = createDictionaryConverter(
237223
{
238224
key: 'hash',
239225
converter: converters.HashAlgorithmIdentifier,
240-
validator: (V, dict) => ensureSHA(V, 'RsaHashedImportParams'),
241226
required: true,
242227
},
243228
],
@@ -366,7 +351,6 @@ converters.EcdsaParams = createDictionaryConverter(
366351
{
367352
key: 'hash',
368353
converter: converters.HashAlgorithmIdentifier,
369-
validator: (V, dict) => ensureSHA(V, 'EcdsaParams'),
370354
required: true,
371355
},
372356
],
@@ -392,7 +376,6 @@ for (let i = 0; i < kHmacDictionaries.length; i++) {
392376
{
393377
key: 'hash',
394378
converter: converters.HashAlgorithmIdentifier,
395-
validator: (V, dict) => ensureSHA(V, name),
396379
required: true,
397380
},
398381
{
@@ -456,7 +439,6 @@ converters.HkdfParams = createDictionaryConverter(
456439
{
457440
key: 'hash',
458441
converter: converters.HashAlgorithmIdentifier,
459-
validator: (V, dict) => ensureSHA(V, 'HkdfParams'),
460442
required: true,
461443
},
462444
{
@@ -524,7 +506,6 @@ converters.Pbkdf2Params = createDictionaryConverter(
524506
{
525507
key: 'hash',
526508
converter: converters.HashAlgorithmIdentifier,
527-
validator: (V, dict) => ensureSHA(V, 'Pbkdf2Params'),
528509
required: true,
529510
},
530511
],
Lines changed: 47 additions & 0 deletions

0 commit comments

Comments
 (0)