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
make webidl require a noop after first use
panva committed Jan 3, 2023
commit 28a3c4f5f77344e6b5f2677a2ab0e2c09f278445
4 changes: 3 additions & 1 deletion lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
@@ -271,11 +271,13 @@ function validateMaxBufferLength(data, name) {
}
}

let webidl;

function normalizeAlgorithm(algorithm, op) {
if (typeof algorithm === 'string')
return normalizeAlgorithm({ name: algorithm }, op);

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');

// 1.
const registeredAlgorithms = kSupportedAlgorithms[op];
28 changes: 15 additions & 13 deletions lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
@@ -64,10 +64,12 @@ const {
randomUUID: _randomUUID,
} = require('internal/crypto/random');

let webidl;

async function digest(algorithm, data) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'digest' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 2, { prefix });
algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {
@@ -95,7 +97,7 @@ async function generateKey(
keyUsages) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'generateKey' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 3, { prefix });
algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {
@@ -179,7 +181,7 @@ async function generateKey(
async function deriveBits(algorithm, baseKey, length) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'deriveBits' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 3, { prefix });
algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {
@@ -257,7 +259,7 @@ async function deriveKey(
keyUsages) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'deriveKey' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 5, { prefix });
algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {
@@ -500,7 +502,7 @@ async function exportKeyJWK(key) {
async function exportKey(format, key) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'exportKey' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 2, { prefix });
format = webidl.converters.KeyFormat(format, {
@@ -577,7 +579,7 @@ async function importKey(
keyUsages) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'importKey' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 4, { prefix });
format = webidl.converters.KeyFormat(format, {
@@ -655,7 +657,7 @@ async function importKey(
async function wrapKey(format, key, wrappingKey, algorithm) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'wrapKey' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 4, { prefix });
format = webidl.converters.KeyFormat(format, {
@@ -710,7 +712,7 @@ async function unwrapKey(
keyUsages) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'unwrapKey' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 7, { prefix });
format = webidl.converters.KeyFormat(format, {
@@ -816,7 +818,7 @@ function signVerify(algorithm, key, data, signature) {
async function sign(algorithm, key, data) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'sign' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 3, { prefix });
algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {
@@ -838,7 +840,7 @@ async function sign(algorithm, key, data) {
async function verify(algorithm, key, signature, data) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'verify' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 4, { prefix });
algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {
@@ -902,7 +904,7 @@ async function cipherOrWrap(mode, algorithm, key, data, op) {
async function encrypt(algorithm, key, data) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'encrypt' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 3, { prefix });
algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {
@@ -925,7 +927,7 @@ async function encrypt(algorithm, key, data) {
async function decrypt(algorithm, key, data) {
if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'decrypt' on 'SubtleCrypto'";
webidl.requiredArguments(arguments.length, 3, { prefix });
algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {
@@ -970,7 +972,7 @@ const crypto = ReflectConstruct(function() {}, [], Crypto);
function getRandomValues(array) {
if (this !== crypto) throw new ERR_INVALID_THIS('Crypto');

const webidl = require('internal/crypto/webidl');
webidl ??= require('internal/crypto/webidl');
const prefix = "Failed to execute 'getRandomValues' on 'Crypto'";
webidl.requiredArguments(arguments.length, 1, { prefix });