Skip to content
Merged
Changes from 1 commit
Commits
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
fixup! lib: improve Web Cryptography key validation ordering
  • Loading branch information
panva committed Apr 15, 2026
commit 663a0ebb49163f1c4247ede531401ed81e85993f
28 changes: 26 additions & 2 deletions test/parallel/test-webcrypto-wrap-unwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ function testWrapping(name, keys) {
await Promise.all(variations);
})().then(common.mustCall());

// Test that wrapKey validates the wrapping key's algorithm and usage
// before attempting to export the key to be wrapped.
// Test that wrapKey/unwrapKey validate the wrapping/unwrapping key's
// algorithm and usage before proceeding.
// Spec: https://w3c.github.io/webcrypto/#SubtleCrypto-method-wrapKey
// Steps 9-10 (wrapping key checks) must precede step 12 (exportKey).
(async function() {
Expand Down Expand Up @@ -442,4 +442,28 @@ function testWrapping(name, keys) {
// exportKey('spki', privateKey) throws NotSupportedError
name: 'NotSupportedError',
});

// --- unwrapKey validation tests ---

const ciphertext = new Uint8Array(32); // Dummy ciphertext

// Wrong algorithm: unwrapping key is HMAC but algorithm says AES-GCM.
await assert.rejects(
subtle.unwrapKey('raw', ciphertext, hmacKey, {
name: 'AES-GCM',
iv: new Uint8Array(12),
}, { name: 'AES-GCM', length: 128 }, true, ['encrypt']), {
name: 'InvalidAccessError',
message: 'The requested operation is not valid for the provided key',
});

// Missing unwrapKey usage: aesKey only has encrypt/decrypt, not unwrapKey.
await assert.rejects(
subtle.unwrapKey('raw', ciphertext, aesKey, {
name: 'AES-GCM',
iv: new Uint8Array(12),
}, { name: 'AES-GCM', length: 128 }, true, ['encrypt']), {
name: 'InvalidAccessError',
message: 'The requested operation is not valid for the provided key',
});
})().then(common.mustCall());
Loading