Lightpanda: the headless browser designed for AI and automation
webapi: exportKey and wrapKey check export support first
orezraeyd4057e3e59aa
diff --git a/src/browser/tests/crypto.html b/src/browser/tests/crypto.htmlindex 95a0f01ce..537a667c5 100644--- a/src/browser/tests/crypto.html+++ b/src/browser/tests/crypto.html@@ -2908,3 +2908,79 @@}, results);});</script>++<script id=export-and-format-order 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}` : typeof r;+ } catch (err) {+ return err.name;+ }+ };+ const hkdf = await subtle.importKey("raw", new Uint8Array(16), "HKDF", false, ["deriveBits"]);+ const pbkdf2 = await subtle.importKey("raw", new Uint8Array(16), "PBKDF2", false, ["deriveBits"]);+ const aesLocked = await subtle.importKey("raw", new Uint8Array(16), "AES-CBC", false, ["encrypt"]);+ const aes = await subtle.importKey("raw", new Uint8Array(16), "AES-CBC", true, ["encrypt"]);+ const kw = await subtle.importKey("raw", new Uint8Array(16), "AES-KW", false, ["wrapKey", "unwrapKey"]);+ const unwrapOnly = await subtle.importKey("raw", new Uint8Array(16), "AES-KW", false, ["unwrapKey"]);++ const results = {};+ // HKDF and PBKDF2 have no export operation: NotSupportedError comes before+ // the extractable check (Chrome 152 still gives InvalidAccessError).+ for (const format of ["raw", "jwk", "spki", "pkcs8"]) {+ results[`hkdf ${format}`] = await outcome(() => subtle.exportKey(format, hkdf));+ results[`pbkdf2 ${format}`] = await outcome(() => subtle.exportKey(format, pbkdf2));+ }+ results["wrap hkdf"] = await outcome(() => subtle.wrapKey("raw", hkdf, kw, "AES-KW"));+ results["wrap pbkdf2"] = await outcome(() => subtle.wrapKey("raw", pbkdf2, kw, "AES-KW"));+ // The wrapping key's checks come first.+ results["wrap hkdf, unwrap-only key"] = await outcome(() => subtle.wrapKey("raw", hkdf, unwrapOnly, "AES-KW"));+ // An exportable algorithm still checks extractable before the format.+ results["aes locked raw"] = await outcome(() => subtle.exportKey("raw", aesLocked));+ results["aes locked spki"] = await outcome(() => subtle.exportKey("spki", aesLocked));+ results["aes spki"] = await outcome(() => subtle.exportKey("spki", aes));+ results["wrap aes locked"] = await outcome(() => subtle.wrapKey("raw", aesLocked, kw, "AES-KW"));+ // An unknown format is a TypeError before anything else; a raw-* one of the+ // Modern Algorithms draft is a known format, but not supported here.+ results["export bogus"] = await outcome(() => subtle.exportKey("bogus", aesLocked));+ results["export Raw"] = await outcome(() => subtle.exportKey("Raw", aes));+ results["export bogus hkdf"] = await outcome(() => subtle.exportKey("bogus", hkdf));+ results["import bogus"] = await outcome(() => subtle.importKey("bogus", new Uint8Array(16), "NOPE", true, ["encrypt"]));+ results["import raw-public aes"] = await outcome(() => subtle.importKey("raw-public", new Uint8Array(16), "AES-GCM", true, ["encrypt"]));+ results["export raw-secret aes"] = await outcome(() => subtle.exportKey("raw-secret", aes));+ results["wrap bogus"] = await outcome(() => subtle.wrapKey("bogus", aes, kw, "NOPE"));+ results["unwrap bogus"] = await outcome(() => subtle.unwrapKey("bogus", new Uint8Array(24), kw, "NOPE", "AES-GCM", true, ["encrypt"]));++ state.resolve();+ await state.done(() => {+ testing.expectEqual({+ "hkdf raw": "NotSupportedError",+ "pbkdf2 raw": "NotSupportedError",+ "hkdf jwk": "NotSupportedError",+ "pbkdf2 jwk": "NotSupportedError",+ "hkdf spki": "NotSupportedError",+ "pbkdf2 spki": "NotSupportedError",+ "hkdf pkcs8": "NotSupportedError",+ "pbkdf2 pkcs8": "NotSupportedError",+ "wrap hkdf": "NotSupportedError",+ "wrap pbkdf2": "NotSupportedError",+ "wrap hkdf, unwrap-only key": "InvalidAccessError",+ "aes locked raw": "InvalidAccessError",+ "aes locked spki": "InvalidAccessError",+ "aes spki": "NotSupportedError",+ "wrap aes locked": "InvalidAccessError",+ "export bogus": "TypeError",+ "export Raw": "TypeError",+ "export bogus hkdf": "TypeError",+ "import bogus": "TypeError",+ "import raw-public aes": "NotSupportedError",+ // Chrome supports raw-secret for AES.+ "export raw-secret aes": "NotSupportedError",+ "wrap bogus": "TypeError",+ "unwrap bogus": "TypeError",+ }, results);+ });+</script>diff --git a/src/browser/webapi/SubtleCrypto.zig b/src/browser/webapi/SubtleCrypto.zigindex b5e38fd6f..8f99c4e2b 100644--- a/src/browser/webapi/SubtleCrypto.zig+++ b/src/browser/webapi/SubtleCrypto.zig@@ -121,6 +121,7 @@ pub fn importKey(exec: *const Execution,) !js.Promise {const local = exec.js.local.?;+ try checkFormat(format, local);// keyData converts first (a JWK's members are read then), but a buffer's// bytes are only read after the algorithm is normalized (bufferBytes).var key_data = local.jsValueToZig(algorithm.KeyData, key_data_value) catch |err| {@@ -306,6 +307,7 @@ pub fn exportKey(exec: *const Execution,) !js.Promise {const local = exec.js.local.?;+ try checkFormat(format, local);return switch (try exportAs(format, key, exec)) {.bytes => |bytes| local.resolvePromise(js.ArrayBuffer{ .values = bytes }),.jwk => |jwk| local.resolvePromise(try jwk.toJs(local)),@@ -352,7 +354,11 @@ const Jwk = union(enum) {/// The "export key" steps shared by exportKey and wrapKey.fn exportAs(format: []const u8, key: *CryptoKey, exec: *const Execution) !Exported {- const local = exec.js.local.?;+ // HKDF and PBKDF2 have no export operation: that's a NotSupportedError,+ // checked before the extractable flag (their keys never are).+ if (key._type == .derive) {+ return error.NotSupported;+ }if (!key.canExportKey()) {return error.InvalidAccessError;}@@ -390,7 +396,8 @@ fn exportAs(format: []const u8, key: *CryptoKey, exec: *const Execution) !Exportreturn error.NotSupported;}- return local.typeError("invalid format");+ // A raw-* format of the Modern Algorithms draft.+ return error.NotSupported;}/// The JSON Web Key returned for symmetric ("oct") keys. Members are listed@@ -571,6 +578,7 @@ pub fn wrapKey(exec: *const Execution,) !js.Promise {const local = exec.js.local.?;+ try checkFormat(format, local);const algo = try algorithm.Encrypt.fromJs(algo_value, local, true);try checkWrappingKey(algo.algoName(), wrapping_key, .wrap);@@ -596,6 +604,7 @@ pub fn unwrapKey(exec: *const Execution,) !js.Promise {const local = exec.js.local.?;+ try checkFormat(format, local);try checkBufferSource(wrapped_key_value, local);const algo = try algorithm.Encrypt.fromJs(algo_value, local, true);// The unwrapped key's algorithm is normalized next, before the key checks.@@ -613,6 +622,20 @@ pub fn unwrapKey(return importNormalized(format, key_data, unwrapped_key_algo, extractable, key_usages, exec);}+/// A KeyFormat, including the raw-* ones of the Modern Algorithms draft (which+/// Chrome recognizes; none is supported here, so they're NotSupportedError+/// later). Any other string fails the WebIDL enum conversion of the format+/// argument, a TypeError before any other step.+fn checkFormat(format: []const u8, local: *const js.Local) !void {+ const formats = [_][]const u8{ "raw", "spki", "pkcs8", "jwk", "raw-public", "raw-private", "raw-seed", "raw-secret" };+ for (formats) |known| {+ if (std.mem.eql(u8, format, known)) {+ return;+ }+ }+ return local.typeError("invalid key format");+}+/// A BufferSource argument taken as a value. Its type is checked when the/// operation is called (a TypeError, as WebIDL's conversion gives), but its/// bytes are read with `bufferBytes` only once the algorithm is normalized: