Lightpanda: the headless browser designed for AI and automation
Merge remote-tracking branch 'origin/main' into crypto-shared-buffers
orezraey4013c15becd5
diff --cc src/browser/tests/crypto.htmlindex b837a608e,27ec81277..fc438dc45--- a/src/browser/tests/crypto.html+++ b/src/browser/tests/crypto.html@@@ -2993,102 -2994,79 +2993,179 @@@});</script>+ <script id=crypto-key-cached-slots type=module>+ const state = await testing.async();+ const subtle = crypto.subtle;+ const aes = await subtle.generateKey({ name: "AES-CTR", length: 256 }, true, ["encrypt", "decrypt"]);+ const pair = await subtle.generateKey({ name: "ECDSA", namedCurve: "P-256" }, true, ["sign", "verify"]);+ const hmac = await subtle.generateKey({ name: "HMAC", hash: "SHA-256" }, true, ["sign"]);+ const rsa = await subtle.generateKey({ name: "RSA-PSS", hash: "SHA-256", modulusLength: 1024, publicExponent: new Uint8Array([1, 0, 1]) }, true, ["sign", "verify"]);++ // algorithm and usages return the key's cached object every time (Chrome+ // 152 still builds a new one per read). A change the page makes to it shows+ // on the next read, but not in what the key does.+ const algorithm = aes.algorithm;+ const usages = aes.usages;+ algorithm.name = "HMAC";+ algorithm.length = 1;+ usages.push("sign");+ const counter = new Uint8Array(16);+ const encrypted = await subtle.encrypt({ name: "AES-CTR", counter, length: 64 }, aes, new Uint8Array(4));+ const results = {+ algorithmSame: aes.algorithm === algorithm,+ usagesSame: aes.usages === usages,+ algorithmSeen: `${aes.algorithm.name}/${aes.algorithm.length}`,+ usagesSeen: aes.usages.join(),+ stillEncrypts: encrypted.byteLength,+ stillNotSigning: await subtle.sign("HMAC", aes, new Uint8Array(1)).then(() => "signed", (e) => e.name),+ exportedAlg: (await subtle.exportKey("jwk", aes)).alg,+ // Each key has its own objects, even the two keys of a pair.+ pairAlgorithmsDiffer: pair.publicKey.algorithm !== pair.privateKey.algorithm,+ pairUsagesDiffer: pair.publicKey.usages !== pair.privateKey.usages,+ publicAlgorithmSame: pair.publicKey.algorithm === pair.publicKey.algorithm,+ privateUsagesSame: pair.privateKey.usages === pair.privateKey.usages,+ // So are its nested members.+ hmacHashSame: hmac.algorithm.hash === hmac.algorithm.hash,+ rsaHashSame: rsa.publicKey.algorithm.hash === rsa.publicKey.algorithm.hash,+ rsaExponentSame: rsa.publicKey.algorithm.publicExponent === rsa.publicKey.algorithm.publicExponent,+ };++ state.resolve();+ await state.done(() => {+ testing.expectEqual({+ algorithmSame: true,+ usagesSame: true,+ algorithmSeen: "HMAC/1",+ usagesSeen: "encrypt,decrypt,sign",+ stillEncrypts: 4,+ stillNotSigning: "InvalidAccessError",+ exportedAlg: "A256CTR",+ pairAlgorithmsDiffer: true,+ pairUsagesDiffer: true,+ publicAlgorithmSame: true,+ privateUsagesSame: true,+ hmacHashSame: true,+ rsaHashSame: true,+ rsaExponentSame: true,+ }, results);+ });+ </script>++ <script id=key-usages-sequence type=module>+ // keyUsages is a sequence<KeyUsage>: any iterable, and required+ const state = await testing.async();+ const algo = { name: "HMAC", hash: "SHA-256" };+ const key = await crypto.subtle.generateKey(algo, true, new Set(["sign", "verify"]));+ let missing;+ try {+ await crypto.subtle.generateKey(algo, true);+ } catch (e) {+ missing = e.name;+ }++ state.resolve();+ await state.done(() => {+ testing.expectEqual(["sign", "verify"], key.usages);+ testing.expectEqual("TypeError", missing);+ });+ </script>+++<script id=shared-buffers type=module>+ const state = await testing.async();+ const subtle = crypto.subtle;+ const outcome = async (fn) => {+ try {+ const r = await fn();+ return r instanceof ArrayBuffer ? `bytes:${r.byteLength}` : r instanceof CryptoKey ? "key" : typeof r;+ } catch (err) {+ return err.name;+ }+ };+ // None of SubtleCrypto's BufferSource arguments or members is+ // [AllowShared]: a SharedArrayBuffer, or a view onto one, is a TypeError.+ const sab = new SharedArrayBuffer(16);+ const view = new Uint8Array(sab);+ const data = new Uint8Array(16);+ const gcm = await subtle.generateKey({ name: "AES-GCM", length: 128 }, false, ["encrypt", "decrypt"]);+ const kw = await subtle.generateKey({ name: "AES-KW", length: 128 }, false, ["wrapKey", "unwrapKey"]);+ const hmac = await subtle.generateKey({ name: "HMAC", hash: "SHA-256" }, false, ["sign", "verify"]);+ const hkdf = await subtle.importKey("raw", data, "HKDF", false, ["deriveBits"]);+ const pbkdf2 = await subtle.importKey("raw", data, "PBKDF2", false, ["deriveBits"]);+ const iv = new Uint8Array(12);+ const results = {};+ for (const [kind, buffer] of [["sab", sab], ["view", view], ["dataView", new DataView(sab)]]) {+ results[`digest ${kind}`] = await outcome(() => subtle.digest("SHA-256", buffer));+ results[`encrypt data ${kind}`] = await outcome(() => subtle.encrypt({ name: "AES-GCM", iv }, gcm, buffer));+ results[`encrypt iv ${kind}`] = await outcome(() => subtle.encrypt({ name: "AES-GCM", iv: buffer }, gcm, data));+ results[`encrypt aad ${kind}`] = await outcome(() => subtle.encrypt({ name: "AES-GCM", iv, additionalData: buffer }, gcm, data));+ results[`decrypt data ${kind}`] = await outcome(() => subtle.decrypt({ name: "AES-GCM", iv }, gcm, buffer));+ results[`sign data ${kind}`] = await outcome(() => subtle.sign("HMAC", hmac, buffer));+ results[`verify signature ${kind}`] = await outcome(() => subtle.verify("HMAC", hmac, buffer, data));+ results[`import ${kind}`] = await outcome(() => subtle.importKey("raw", buffer, "AES-GCM", false, ["encrypt"]));+ results[`unwrap ${kind}`] = await outcome(() => subtle.unwrapKey("raw", buffer, kw, "AES-KW", "AES-GCM", false, ["encrypt"]));+ results[`hkdf salt ${kind}`] = await outcome(() => subtle.deriveBits({ name: "HKDF", hash: "SHA-256", salt: buffer, info: data }, hkdf, 128));+ results[`pbkdf2 salt ${kind}`] = await outcome(() => subtle.deriveBits({ name: "PBKDF2", hash: "SHA-256", salt: buffer, iterations: 1 }, pbkdf2, 128));+ }+ // The same calls with a non-shared buffer.+ results["digest plain"] = await outcome(() => subtle.digest("SHA-256", data.buffer));+ results["encrypt plain"] = await outcome(() => subtle.encrypt({ name: "AES-GCM", iv, additionalData: data }, gcm, data));+ results["import plain"] = await outcome(() => subtle.importKey("raw", data, "AES-GCM", false, ["encrypt"]));+ // Any view of a non-shared buffer is a BufferSource, whatever its element type.+ results["digest Uint32Array"] = await outcome(() => subtle.digest("SHA-256", new Uint32Array([1, 2])));+ results["digest Float64Array"] = await outcome(() => subtle.digest("SHA-256", new Float64Array([1])));+ results["encrypt Uint16Array"] = await outcome(() => subtle.encrypt({ name: "AES-GCM", iv: new Uint16Array(6) }, gcm, new Uint16Array(8)));+ results["import Uint16Array"] = await outcome(() => subtle.importKey("raw", new Uint16Array(8), "AES-GCM", false, ["encrypt"]));+ results["import DataView"] = await outcome(() => subtle.importKey("raw", new DataView(new ArrayBuffer(16)), "AES-GCM", false, ["encrypt"]));+ results["sign Int32Array"] = await outcome(() => subtle.sign("HMAC", hmac, new Int32Array(3)));+ // RSA's publicExponent is a BigInteger: a Uint8Array, no other view or buffer.+ const rsa = (publicExponent) => outcome(() => subtle.generateKey({ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256", modulusLength: 1024, publicExponent }, false, ["sign"]));+ results["publicExponent Uint8Array"] = await rsa(new Uint8Array([1, 0, 1]));+ results["publicExponent Uint16Array"] = await rsa(new Uint16Array([1, 1]));+ results["publicExponent DataView"] = await rsa(new DataView(new Uint8Array([1, 0, 1]).buffer));+ results["publicExponent ArrayBuffer"] = await rsa(new Uint8Array([1, 0, 1]).buffer);+ results["publicExponent shared"] = await rsa(new Uint8Array(new SharedArrayBuffer(3)));+ // A shared keyData is a TypeError of its conversion, before the algorithm.+ results["import view, unknown algorithm"] = await outcome(() => subtle.importKey("raw", view, "NOPE", false, ["encrypt"]));+ // A SharedArrayBuffer is a BufferSource that fails its conversion, not a+ // JsonWebKey, in any format.+ results["import jwk sab"] = await outcome(() => subtle.importKey("jwk", sab, "AES-GCM", false, ["encrypt"]));+ results["import jwk sab, unknown algorithm"] = await outcome(() => subtle.importKey("jwk", sab, "NOPE", false, ["encrypt"]));+ results["import jwk {}"] = await outcome(() => subtle.importKey("jwk", {}, "AES-GCM", false, ["encrypt"]));+ // getRandomValues' ArrayBufferView isn't [AllowShared] either.+ const random = (array) => { try { return crypto.getRandomValues(array) === array; } catch (err) { return err.name; } };+ results["getRandomValues view"] = random(view);+ results["getRandomValues plain"] = random(new Uint8Array(4));++ const expected = {};+ for (const kind of ["sab", "view", "dataView"]) {+ for (const name of ["digest", "encrypt data", "encrypt iv", "encrypt aad", "decrypt data", "sign data",+ "verify signature", "import", "unwrap", "hkdf salt", "pbkdf2 salt"]) {+ expected[`${name} ${kind}`] = "TypeError";+ }+ }+ expected["digest plain"] = "bytes:32";+ expected["encrypt plain"] = "bytes:32";+ expected["import plain"] = "key";+ expected["digest Uint32Array"] = "bytes:32";+ expected["digest Float64Array"] = "bytes:32";+ expected["encrypt Uint16Array"] = "bytes:32";+ expected["import Uint16Array"] = "key";+ expected["import DataView"] = "key";+ expected["sign Int32Array"] = "bytes:32";+ expected["publicExponent Uint8Array"] = "object";+ expected["publicExponent Uint16Array"] = "TypeError";+ expected["publicExponent DataView"] = "TypeError";+ expected["publicExponent ArrayBuffer"] = "TypeError";+ expected["publicExponent shared"] = "TypeError";+ expected["import view, unknown algorithm"] = "TypeError";+ expected["import jwk sab"] = "TypeError";+ expected["import jwk sab, unknown algorithm"] = "TypeError";+ expected["import jwk {}"] = "DataError";+ expected["getRandomValues view"] = "TypeError";+ expected["getRandomValues plain"] = true;++ state.resolve();+ await state.done(() => {+ testing.expectEqual(expected, results);+ });+</script>