Skip to content

Commit 663a0eb

Browse files
committed
fixup! lib: improve Web Cryptography key validation ordering
1 parent 9ce84b8 commit 663a0eb

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

test/parallel/test-webcrypto-wrap-unwrap.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ function testWrapping(name, keys) {
379379
await Promise.all(variations);
380380
})().then(common.mustCall());
381381

382-
// Test that wrapKey validates the wrapping key's algorithm and usage
383-
// before attempting to export the key to be wrapped.
382+
// Test that wrapKey/unwrapKey validate the wrapping/unwrapping key's
383+
// algorithm and usage before proceeding.
384384
// Spec: https://w3c.github.io/webcrypto/#SubtleCrypto-method-wrapKey
385385
// Steps 9-10 (wrapping key checks) must precede step 12 (exportKey).
386386
(async function() {
@@ -442,4 +442,28 @@ function testWrapping(name, keys) {
442442
// exportKey('spki', privateKey) throws NotSupportedError
443443
name: 'NotSupportedError',
444444
});
445+
446+
// --- unwrapKey validation tests ---
447+
448+
const ciphertext = new Uint8Array(32); // Dummy ciphertext
449+
450+
// Wrong algorithm: unwrapping key is HMAC but algorithm says AES-GCM.
451+
await assert.rejects(
452+
subtle.unwrapKey('raw', ciphertext, hmacKey, {
453+
name: 'AES-GCM',
454+
iv: new Uint8Array(12),
455+
}, { name: 'AES-GCM', length: 128 }, true, ['encrypt']), {
456+
name: 'InvalidAccessError',
457+
message: 'The requested operation is not valid for the provided key',
458+
});
459+
460+
// Missing unwrapKey usage: aesKey only has encrypt/decrypt, not unwrapKey.
461+
await assert.rejects(
462+
subtle.unwrapKey('raw', ciphertext, aesKey, {
463+
name: 'AES-GCM',
464+
iv: new Uint8Array(12),
465+
}, { name: 'AES-GCM', length: 128 }, true, ['encrypt']), {
466+
name: 'InvalidAccessError',
467+
message: 'The requested operation is not valid for the provided key',
468+
});
445469
})().then(common.mustCall());

0 commit comments

Comments
 (0)