Skip to content

Commit bdfbdda

Browse files
panvaaduh95
authored andcommitted
crypto: read algorithm name property only once in normalizeAlgorithm
PR-URL: #62170 Refs: https://redirect.github.com/web-platform-tests/wpt/pull/57614#pullrequestreview-3808145365 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 3d0f97e commit bdfbdda

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/internal/crypto/util.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,13 @@ function normalizeAlgorithm(algorithm, op) {
577577
return { name: algName };
578578

579579
// 6.
580-
const normalizedAlgorithm = webidl.converters[desiredType](algorithm, {
581-
prefix: 'Failed to normalize algorithm',
582-
context: 'passed algorithm',
583-
});
580+
const normalizedAlgorithm = webidl.converters[desiredType](
581+
{ __proto__: algorithm, name: algName },
582+
{
583+
prefix: 'Failed to normalize algorithm',
584+
context: 'passed algorithm',
585+
},
586+
);
584587
// 7.
585588
normalizedAlgorithm.name = algName;
586589

test/parallel/test-webcrypto-util.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,35 @@ const {
1717
assert.strictEqual(normalizeAlgorithm(algorithm, 'sign') !== algorithm, true);
1818
assert.deepStrictEqual(algorithm, { name: 'ECDSA', hash: 'SHA-256' });
1919
}
20+
21+
// The algorithm name getter should only be invoked once during
22+
// normalizeAlgorithm, including for algorithms with a non-null desiredType
23+
// where step 6 runs the specialized dictionary converter.
24+
// Refs: https://github.com/web-platform-tests/wpt/pull/57614#pullrequestreview-3808145365
25+
{
26+
let nameReadCount = 0;
27+
const algorithm = {
28+
get name() {
29+
nameReadCount++;
30+
return 'AES-GCM';
31+
},
32+
iv: new Uint8Array(12),
33+
};
34+
const normalized = normalizeAlgorithm(algorithm, 'encrypt');
35+
assert.strictEqual(normalized.name, 'AES-GCM');
36+
assert.strictEqual(nameReadCount, 1);
37+
}
38+
39+
{
40+
let nameReadCount = 0;
41+
const algorithm = {
42+
get name() {
43+
nameReadCount++;
44+
return 'ECDSA';
45+
},
46+
hash: 'SHA-256',
47+
};
48+
const normalized = normalizeAlgorithm(algorithm, 'sign');
49+
assert.strictEqual(normalized.name, 'ECDSA');
50+
assert.strictEqual(nameReadCount, 1);
51+
}

0 commit comments

Comments
 (0)