Files
node/test/parallel/test-webcrypto-webidl-brand.js
Filip Skokan 0a9225dd16 lib: use Web IDL interface brand checks
Use explicit brand predicates for interface conversion instead of
prototype ancestry. Update CryptoKey and AbortSignal together with the
shared converter contract.

Read internal AbortSignal state during composition. Preserve genuine
signals after prototype changes without invoking shadowed getters.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
Assisted-by: GitHub Copilot
PR-URL: https://github.com/nodejs/node/pull/65846
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Aviv Keller <me@aviv.sh>
2026-09-08 08:50:21 +00:00

39 lines
1.1 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const { subtle } = globalThis.crypto;
const { CryptoKey } = require('internal/crypto/keys');
const { converters } = require('internal/crypto/webidl');
async function main() {
const bytes = new Uint8Array(16);
const key = await subtle.importKey('raw', bytes, 'AES-GCM', true, ['encrypt']);
for (const value of [
{ __proto__: CryptoKey.prototype },
{ __proto__: key },
Object.create(CryptoKey.prototype, Object.getOwnPropertyDescriptors(key)),
new Proxy(key, {}),
]) {
assert.throws(() => converters.CryptoKey(value), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
});
await assert.rejects(subtle.exportKey('raw', value), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
});
}
Object.setPrototypeOf(key, null);
assert.strictEqual(converters.CryptoKey(key), key);
assert.deepStrictEqual(new Uint8Array(await subtle.exportKey('raw', key)), bytes);
}
main().then(common.mustCall());