Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: use WebIDL converters in WebCryptoAPI #46067

Merged
merged 27 commits into from
Jan 17, 2023
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bc436eb
crypto: use WebIDL converters in WebCryptoAPI
panva Jan 2, 2023
03c0459
add docs change entry
panva Jan 3, 2023
28a3c4f
make webidl require a noop after first use
panva Jan 3, 2023
c11a280
add note to the normalizeAlgorithm
panva Jan 3, 2023
c1f2da3
match normalizeAlgorithm case-insensitive definition
panva Jan 3, 2023
c5bab06
update unrecognized name message and assertions
panva Jan 3, 2023
2900c12
remove unused converter
panva Jan 3, 2023
976cd80
refactor makeException to have default code
panva Jan 3, 2023
b40e895
update toNumber to use makeException
panva Jan 3, 2023
7208755
carry context to toNumber exceptions
panva Jan 3, 2023
cc91875
normalize webidl error message sentences
panva Jan 3, 2023
30353de
add webidl.requiredArguments tests
panva Jan 3, 2023
cdfbb9d
add webidl.converters.boolean tests
panva Jan 3, 2023
1216371
add more converter tests
panva Jan 3, 2023
ffcd2fc
add more converter tests
panva Jan 3, 2023
4bed6b0
lint and doc change change
panva Jan 3, 2023
609f194
address some comments
panva Jan 3, 2023
c7af35a
address comments
panva Jan 3, 2023
39e40f0
address comments
panva Jan 3, 2023
91c8abd
address comments
panva Jan 3, 2023
833e286
remove more redundant checks
panva Jan 3, 2023
fcddb59
refactor JWK import validations
panva Jan 3, 2023
31b983c
upcoming linter rules fix
panva Jan 3, 2023
5d74979
webcrypto does not need defaultValue
panva Jan 4, 2023
44640fb
improve coverage
panva Jan 4, 2023
fb9d1c4
apply review feedback
panva Jan 5, 2023
42b9fb2
address more prototype pollution
panva Jan 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update unrecognized name message and assertions
panva committed Jan 3, 2023
commit c5bab068ad264413e1fe62ac84e9bc99fec770e1
2 changes: 1 addition & 1 deletion lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
@@ -182,7 +182,7 @@ async function asyncDigest(algorithm, data) {
data));
}

throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
}

module.exports = {
2 changes: 1 addition & 1 deletion lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
@@ -306,7 +306,7 @@ function normalizeAlgorithm(algorithm, op) {
}
}
if (desiredType === undefined)
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');

// Fast path everything below if the registered dictionary is null
if (desiredType === null)
12 changes: 6 additions & 6 deletions lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
@@ -161,7 +161,7 @@ async function generateKey(
.aesGenerateKey(algorithm, extractable, keyUsages);
break;
default:
throw lazyDOMException('Unrecognized name.');
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
}

if (
@@ -222,7 +222,7 @@ async function deriveBits(algorithm, baseKey, length) {
return require('internal/crypto/pbkdf2')
.pbkdf2DeriveBits(algorithm, baseKey, length);
}
throw lazyDOMException('Unrecognized name.');
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
}

function getKeyLength({ name, length, hash }) {
@@ -313,7 +313,7 @@ async function deriveKey(
.pbkdf2DeriveBits(algorithm, baseKey, length);
break;
default:
throw lazyDOMException('Unrecognized name.');
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
}

return ReflectApply(
@@ -649,7 +649,7 @@ async function importKey(
keyUsages);
}

throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
}

// subtle.wrapKey() is essentially a subtle.exportKey() followed
@@ -812,7 +812,7 @@ function signVerify(algorithm, key, data, signature) {
return require('internal/crypto/mac')
.hmacSignVerify(key, data, algorithm, signature);
}
throw lazyDOMException('Unrecognized named.', 'NotSupportedError');
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
}

async function sign(algorithm, key, data) {
@@ -898,7 +898,7 @@ async function cipherOrWrap(mode, algorithm, key, data, op) {
.aesCipher(mode, key, data, algorithm);
}
}
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
}

async function encrypt(algorithm, key, data) {
16 changes: 12 additions & 4 deletions test/parallel/test-webcrypto-derivebits-hkdf.js
Original file line number Diff line number Diff line change
@@ -296,7 +296,8 @@ async function testDeriveBitsBadHash(
...algorithm,
hash: hash.substring(0, 3) + hash.substring(4)
}, baseKeys[size], 256), {
message: /Unrecognized name/
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
}),
assert.rejects(
subtle.deriveBits(
@@ -305,7 +306,8 @@ async function testDeriveBitsBadHash(
hash: 'PBKDF2'
},
baseKeys[size], 256), {
message: /Unrecognized name/
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
}),
]);
}
@@ -435,7 +437,10 @@ async function testDeriveKeyBadHash(
keyType,
true,
usages),
{ message: /Unrecognized name/ }),
{
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
}),
assert.rejects(
subtle.deriveKey(
{
@@ -446,7 +451,10 @@ async function testDeriveKeyBadHash(
keyType,
true,
usages),
{ message: /Unrecognized name/ }),
{
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
}),
]);
}

10 changes: 8 additions & 2 deletions test/parallel/test-webcrypto-digest.js
Original file line number Diff line number Diff line change
@@ -67,10 +67,16 @@ const kData = (new TextEncoder()).encode('hello');
})().then(common.mustCall());

Promise.all([1, null, undefined].map((i) =>
assert.rejects(subtle.digest(i, Buffer.alloc(0)), { message: /Unrecognized name/ })
assert.rejects(subtle.digest(i, Buffer.alloc(0)), {
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
})
)).then(common.mustCall());

assert.rejects(subtle.digest('', Buffer.alloc(0)), { message: /Unrecognized name/ }).then(common.mustCall());
assert.rejects(subtle.digest('', Buffer.alloc(0)), {
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
}).then(common.mustCall());

Promise.all([1, [], {}, null, undefined].map((i) =>
assert.rejects(subtle.digest('SHA-256', i), {
9 changes: 6 additions & 3 deletions test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
@@ -164,7 +164,8 @@ const vectors = {
// The extractable and usages values are invalid here also,
// but the unrecognized algorithm name should be caught first.
subtle.generateKey(algorithm, 7, []), {
message: /Unrecognized name/
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
});
}

@@ -345,7 +346,8 @@ const vectors = {
publicExponent,
hash
}, true, usages), {
message: /Unrecognized name/
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
});
}));

@@ -564,7 +566,8 @@ const vectors = {
[1, false, null].forEach(async (hash) => {
await assert.rejects(
subtle.generateKey({ name: 'HMAC', length, hash }, true, usages), {
message: /Unrecognized name/
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
});
});
}
3 changes: 2 additions & 1 deletion test/parallel/test-webcrypto-sign-verify-ecdsa.js
Original file line number Diff line number Diff line change
@@ -135,7 +135,8 @@ async function testVerify({ name,

await assert.rejects(
subtle.verify({ name, hash: 'sha256' }, publicKey, signature, copy), {
message: /Unrecognized name/
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
});
}

16 changes: 12 additions & 4 deletions test/pummel/test-webcrypto-derivebits-pbkdf2.js
Original file line number Diff line number Diff line change
@@ -483,7 +483,8 @@ async function testDeriveBitsBadHash(
...algorithm,
hash: hash.substring(0, 3) + hash.substring(4)
}, baseKeys[size], 256), {
message: /Unrecognized name/
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
}),
assert.rejects(
subtle.deriveBits(
@@ -492,7 +493,8 @@ async function testDeriveBitsBadHash(
hash: 'HKDF'
},
baseKeys[size], 256), {
message: /Unrecognized name/
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
}),
]);
}
@@ -569,7 +571,10 @@ async function testDeriveKeyBadHash(
keyType,
true,
usages),
{ message: /Unrecognized name/ }),
{
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
}),
assert.rejects(
subtle.deriveKey(
{
@@ -580,7 +585,10 @@ async function testDeriveKeyBadHash(
keyType,
true,
usages),
{ message: /Unrecognized name/ }),
{
message: /Unrecognized algorithm name/,
name: 'NotSupportedError',
}),
]);
}