Skip to content

Truly independent web browser

LibWeb: Apply [EnforceRange] to the deriveBits() length

Shannon Boothab78f0f09623
diff --git a/Libraries/LibWeb/Crypto/SubtleCrypto.idl b/Libraries/LibWeb/Crypto/SubtleCrypto.idl
index 73bcfb844e0..a3cc72bcc74 100644
--- a/Libraries/LibWeb/Crypto/SubtleCrypto.idl
+++ b/Libraries/LibWeb/Crypto/SubtleCrypto.idl
@@ -70,8 +70,7 @@ interface SubtleCrypto {
[NeedsCallerRealm] Promise<(CryptoKey or CryptoKeyPair)> generateKey(AlgorithmIdentifier algorithm, boolean extractable, sequence<KeyUsage> keyUsages);
[NeedsCallerRealm] Promise<CryptoKey> deriveKey(AlgorithmIdentifier algorithm, CryptoKey baseKey, AlgorithmIdentifier derivedKeyType, boolean extractable, sequence<KeyUsage> keyUsages);
- // FIXME: Promise<ArrayBuffer> deriveBits(AlgorithmIdentifier algorithm, CryptoKey baseKey, optional unsigned long? length = null);
- [NeedsCallerRealm] Promise<ArrayBuffer> deriveBits(AlgorithmIdentifier algorithm, CryptoKey baseKey, optional unsigned long? length);
+ [NeedsCallerRealm] Promise<ArrayBuffer> deriveBits(AlgorithmIdentifier algorithm, CryptoKey baseKey, optional [EnforceRange] unsigned long? length = null);
[NeedsCallerRealm] Promise<CryptoKey> importKey(KeyFormat format, (BufferSource or JsonWebKey) keyData, AlgorithmIdentifier algorithm, boolean extractable, sequence<KeyUsage> keyUsages);
[NeedsCallerRealm] Promise<(ArrayBuffer or JsonWebKey)> exportKey(KeyFormat format, CryptoKey key);
diff --git a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any.txt
index b5307ef9ac8..8f8e3267066 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any.txt
@@ -1,8 +1,8 @@
Harness status: OK
-Found 29 tests
+Found 45 tests
-29 Pass
+45 Pass
Pass setup - define tests
Pass HKDF derivation with 256 as 'length' parameter
Pass HKDF derivation with 384 as 'length' parameter
@@ -11,6 +11,10 @@ Pass HKDF derivation with 0 as 'length' parameter
Pass HKDF derivation with null as 'length' parameter
Pass HKDF derivation with undefined as 'length' parameter
Pass HKDF derivation with omitted as 'length' parameter
+Pass HKDF derivation with NaN as 'length' parameter
+Pass HKDF derivation with Infinity as 'length' parameter
+Pass HKDF derivation with -8 as 'length' parameter
+Pass HKDF derivation with 4294967304 as 'length' parameter
Pass PBKDF2 derivation with 256 as 'length' parameter
Pass PBKDF2 derivation with 384 as 'length' parameter
Pass PBKDF2 derivation with 230 as 'length' parameter
@@ -18,6 +22,10 @@ Pass PBKDF2 derivation with 0 as 'length' parameter
Pass PBKDF2 derivation with null as 'length' parameter
Pass PBKDF2 derivation with undefined as 'length' parameter
Pass PBKDF2 derivation with omitted as 'length' parameter
+Pass PBKDF2 derivation with NaN as 'length' parameter
+Pass PBKDF2 derivation with Infinity as 'length' parameter
+Pass PBKDF2 derivation with -8 as 'length' parameter
+Pass PBKDF2 derivation with 4294967304 as 'length' parameter
Pass ECDH derivation with 256 as 'length' parameter
Pass ECDH derivation with 384 as 'length' parameter
Pass ECDH derivation with 230 as 'length' parameter
@@ -25,10 +33,18 @@ Pass ECDH derivation with 0 as 'length' parameter
Pass ECDH derivation with null as 'length' parameter
Pass ECDH derivation with undefined as 'length' parameter
Pass ECDH derivation with omitted as 'length' parameter
+Pass ECDH derivation with NaN as 'length' parameter
+Pass ECDH derivation with Infinity as 'length' parameter
+Pass ECDH derivation with -8 as 'length' parameter
+Pass ECDH derivation with 4294967304 as 'length' parameter
Pass X25519 derivation with 256 as 'length' parameter
Pass X25519 derivation with 384 as 'length' parameter
Pass X25519 derivation with 230 as 'length' parameter
Pass X25519 derivation with 0 as 'length' parameter
Pass X25519 derivation with null as 'length' parameter
Pass X25519 derivation with undefined as 'length' parameter
-Pass X25519 derivation with omitted as 'length' parameter
\ No newline at end of file
+Pass X25519 derivation with omitted as 'length' parameter
+Pass X25519 derivation with NaN as 'length' parameter
+Pass X25519 derivation with Infinity as 'length' parameter
+Pass X25519 derivation with -8 as 'length' parameter
+Pass X25519 derivation with 4294967304 as 'length' parameter
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length.js b/Tests/LibWeb/Text/input/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length.js
index 5a7ed7eb50a..39b4ddce7fa 100644
--- a/Tests/LibWeb/Text/input/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length.js
+++ b/Tests/LibWeb/Text/input/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length.js
@@ -17,16 +17,16 @@ function define_tests() {
derivedBits = await subtle.deriveBits(testData.deriveAlg, privateKey);
else
derivedBits = await subtle.deriveBits(testData.deriveAlg, privateKey, testParam.length);
- if (testParam.expected === undefined) {
- assert_unreached("deriveBits should have thrown an OperationError exception.");
+ if (testParam.expected === "TypeError" || testParam.expected === "OperationError") {
+ assert_unreached("deriveBits should have thrown an " + testParam.expected + " exception.");
}
assert_array_equals(new Uint8Array(derivedBits), testParam.expected, "Derived bits do not match the expected result.");
} catch (err) {
- if (err instanceof AssertionError || testParam.expected !== undefined) {
+ if (err instanceof AssertionError || !(testParam.expected === "TypeError" || testParam.expected === "OperationError")) {
throw err;
}
assert_true(privateKey !== undefined, "Key should be valid.");
- assert_equals(err.name, "OperationError", "deriveBits correctly threw OperationError: " + err.message);
+ assert_equals(err.name, testParam.expected, "deriveBits correctly threw " + testParam.expected + ": " + err.message);
}
}, algorithm + " derivation with " + testParam.length + " as 'length' parameter");
});
diff --git a/Tests/LibWeb/Text/input/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length_testcases.js b/Tests/LibWeb/Text/input/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length_testcases.js
index 2679fa79e2a..e368ac86697 100644
--- a/Tests/LibWeb/Text/input/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length_testcases.js
+++ b/Tests/LibWeb/Text/input/wpt-import/WebCryptoAPI/derive_bits_keys/derived_bits_length_testcases.js
@@ -1,38 +1,49 @@
+var enforceRangeTestCases = [
+ // These cases should throw an error in all algorithms due to [EnforceRange]
+ {length: NaN, expected: "TypeError"},
+ {length: Infinity, expected: "TypeError"},
+ {length: -8, expected: "TypeError"},
+ {length: 2**32 + 8, expected: "TypeError"},
+];
var testCases = {
"HKDF": [
{length: 256, expected: algorithms["HKDF"].derivation},
{length: 384, expected: algorithms["HKDF"].derivation384},
- {length: 230, expected: undefined}, // should throw an exception, not multiple of 8
+ {length: 230, expected: "OperationError"}, // should throw an exception, not multiple of 8
{length: 0, expected: emptyArray},
- {length: null, expected: undefined }, // should throw an exception
- {length: undefined, expected: undefined }, // should throw an exception
- {length: "omitted", expected: undefined }, // default value is null, so should throw
+ {length: null, expected: "OperationError"}, // should throw an exception
+ {length: undefined, expected: "OperationError"}, // should throw an exception
+ {length: "omitted", expected: "OperationError"}, // default value is null, so should throw
+ ...enforceRangeTestCases,
],
"PBKDF2": [
{length: 256, expected: algorithms["PBKDF2"].derivation},
{length: 384, expected: algorithms["PBKDF2"].derivation384},
- {length: 230, expected: undefined}, // should throw an exception, not multiple of 8
+ {length: 230, expected: "OperationError"}, // should throw an exception, not multiple of 8
{length: 0, expected: emptyArray},
- {length: null, expected: undefined }, // should throw an exception
- {length: undefined, expected: undefined }, // should throw an exception
- {length: "omitted", expected: undefined }, // default value is null, so should throw
+ {length: null, expected: "OperationError"}, // should throw an exception
+ {length: undefined, expected: "OperationError"}, // should throw an exception
+ {length: "omitted", expected: "OperationError"}, // default value is null, so should throw
+ ...enforceRangeTestCases,
],
"ECDH": [
{length: 256, expected: algorithms["ECDH"].derivation},
- {length: 384, expected: undefined}, // should throw an exception, bigger than the output size
+ {length: 384, expected: "OperationError"}, // should throw an exception, bigger than the output size
{length: 230, expected: algorithms["ECDH"].derivation230},
{length: 0, expected: emptyArray},
{length: null, expected: algorithms["ECDH"].derivation},
{length: undefined, expected: algorithms["ECDH"].derivation},
- {length: "omitted", expected: algorithms["ECDH"].derivation }, // default value is null
+ {length: "omitted", expected: algorithms["ECDH"].derivation}, // default value is null
+ ...enforceRangeTestCases,
],
"X25519": [
{length: 256, expected: algorithms["X25519"].derivation},
- {length: 384, expected: undefined}, // should throw an exception, bigger than the output size
+ {length: 384, expected: "OperationError"}, // should throw an exception, bigger than the output size
{length: 230, expected: algorithms["X25519"].derivation230},
{length: 0, expected: emptyArray},
{length: null, expected: algorithms["X25519"].derivation},
{length: undefined, expected: algorithms["X25519"].derivation},
- {length: "omitted", expected: algorithms["X25519"].derivation }, // default value is null
+ {length: "omitted", expected: algorithms["X25519"].derivation}, // default value is null
+ ...enforceRangeTestCases,
],
}