Lightpanda: the headless browser designed for AI and automation
match chrome validation ordering
Karl Seguin205a1349df1e
diff --git a/src/browser/tests/crypto.html b/src/browser/tests/crypto.htmlindex e3814d30b..23ce7c711 100644--- a/src/browser/tests/crypto.html+++ b/src/browser/tests/crypto.html@@ -2590,6 +2590,7 @@const pb = await s.importKey("raw", data, "PBKDF2", false, ["deriveKey"]);const pbp = { name: "PBKDF2", hash: "SHA-256", salt: data, iterations: 1 };const ecdh = await s.generateKey({ name: "ECDH", namedCurve: "P-256" }, false, ["deriveKey"]);+ const ecdhBits = await s.generateKey({ name: "ECDH", namedCurve: "P-256" }, false, ["deriveBits"]);const x = await s.generateKey("X25519", false, ["deriveKey"]);const dk = (type, ext, usages, algo = pbp, key = pb) => s.deriveKey(algo, key, type, ext, usages);await run("dk AES len 2^32+128", () => dk({ name: "AES-GCM", length: 2 ** 32 + 128 }, true, ["encrypt"]));@@ -2598,6 +2599,11 @@await run("dk AES string", () => dk("AES-GCM", true, ["encrypt"]));await run("dk AES stray hash", () => dk({ name: "AES-GCM", length: 128, hash: "SHA-999" }, true, ["encrypt"]));await run("dk HMAC length 0", () => dk({ name: "HMAC", hash: "SHA-256", length: 0 }, true, ["sign"]));+ // The base key checks come before "get key length".+ await run("dk AES len 100 wrong key", () => dk({ name: "AES-GCM", length: 100 }, true, ["encrypt"], { name: "ECDH", public: ecdh.publicKey }, pb));+ await run("dk HMAC length 0 no usage", () => dk({ name: "HMAC", hash: "SHA-256", length: 0 }, true, ["sign"], { name: "ECDH", public: ecdh.publicKey }, ecdhBits.privateKey));+ // But the dictionary's conversion comes before them.+ await run("dk HMAC length -1 wrong key", () => dk({ name: "HMAC", hash: "SHA-256", length: -1 }, true, ["sign"], { name: "ECDH", public: ecdh.publicKey }, pb));await run("dk HMAC ok", () => dk({ name: "HMAC", hash: "SHA-256" }, true, ["sign"]));await run("dk HKDF from PBKDF2", () => dk("HKDF", false, ["deriveBits"]));await run("dk HKDF from ECDH", () => dk("HKDF", false, ["deriveBits"], { name: "ECDH", public: ecdh.publicKey }, ecdh.privateKey));@@ -2647,6 +2653,9 @@"dk AES string=TypeError","dk AES stray hash=key:secret:AES-GCM:128","dk HMAC length 0=TypeError",+ "dk AES len 100 wrong key=InvalidAccessError",+ "dk HMAC length 0 no usage=InvalidAccessError",+ "dk HMAC length -1 wrong key=TypeError","dk HMAC ok=key:secret:HMAC:512","dk HKDF from PBKDF2=OperationError","dk HKDF from ECDH=key:secret:HKDF:",diff --git a/src/browser/webapi/SubtleCrypto.zig b/src/browser/webapi/SubtleCrypto.zigindex 3570a3906..056d8d3bb 100644--- a/src/browser/webapi/SubtleCrypto.zig+++ b/src/browser/webapi/SubtleCrypto.zig@@ -456,8 +456,15 @@ pub fn deriveKey(// The algorithm is normalized before derivedKeyType.const algo = try algorithm.Derive.fromJs(algo_value, local);const derived = try algorithm.DerivedKey.fromJs(derived_value, local);- // The base key's deriveKey usage (not deriveBits) gates this operation.+ // The base key checks come before "get key length": its algorithm must be+ // the one given, and it must have the deriveKey usage (not deriveBits).+ if (!eqlIgnoreCase(base_key._algorithm.name, algo.algoName())) {+ return error.InvalidAccessError;+ }const usage_ok = base_key.canDeriveKey();+ if (!usage_ok) {+ return error.InvalidAccessError;+ }switch (derived) {.aes => |a| {@@ -470,15 +477,14 @@ pub fn deriveKey(return AES.import(a.name, bits, extractable, key_usages, exec);},.hmac => |h| {- const requested = try HMAC.lengthParam(h.length, local);// "Get key length": a present length must be non-zero.- if (requested == 0) {+ if (h.length == 0) {return local.typeError("HMAC length must not be zero");}const hash_name = try h.hash.canonical(local);const hash_md = crypto.findDigest(hash_name) catch return error.NotSupported;// Default length, per spec, is the hash's block size (in bits).- const length: u32 = requested orelse @intCast(crypto.EVP_MD_block_size(hash_md) * 8);+ const length: u32 = h.length orelse @intCast(crypto.EVP_MD_block_size(hash_md) * 8);const bits = try deriveRaw(algo, base_key, length, usage_ok, exec);return HMAC.import(hash_name, bits, length, extractable, key_usages, exec);},diff --git a/src/browser/webapi/crypto/algorithm.zig b/src/browser/webapi/crypto/algorithm.zigindex 36ca0add7..d611b39d0 100644--- a/src/browser/webapi/crypto/algorithm.zig+++ b/src/browser/webapi/crypto/algorithm.zig@@ -163,6 +163,14 @@ pub const Derive = union(enum) {return error.NotSupported;}+ pub fn algoName(self: Derive) []const u8 {+ return switch (self) {+ .pbkdf2 => |p| p.name,+ .hkdf => |p| p.name,+ .ecdh_or_x25519 => |p| p.name,+ };+ }+pub const Pbkdf2Params = struct {hash: Hash,/// An [EnforceRange] unsigned long: converted once the name is known@@ -187,8 +195,9 @@ pub const Derive = union(enum) {/// has no key length, so it's NotSupportedError.pub const DerivedKey = union(enum) {aes: struct { length: js.EnforceRange(u16), name: []const u8 },- /// `length` as in Import.hmac.- hmac: struct { hash: Hash, length: ?js.Value = null, name: []const u8 },+ /// `length` converted as Import.hmac's is (HMAC.lengthParam): null when+ /// absent, 0 for null.+ hmac: struct { hash: Hash, length: ?u32, name: []const u8 },/// HKDF or PBKDF2, whose key length is null: the whole derivation.named: []const u8,@@ -198,9 +207,10 @@ pub const DerivedKey = union(enum) {return .{ .aes = try convert(@FieldType(DerivedKey, "aes"), value, name, local) };}if (isOneOf(name, &.{"HMAC"})) {- const params = try convert(@FieldType(DerivedKey, "hmac"), value, name, local);+ const params = try convert(struct { hash: Hash, length: ?js.Value = null, name: []const u8 }, value, name, local);+ const length = try rangeMember(u32, params.length, local);_ = try params.hash.canonical(local);- return .{ .hmac = params };+ return .{ .hmac = .{ .hash = params.hash, .length = length, .name = params.name } };}if (isOneOf(name, &.{ "HKDF", "PBKDF2" })) {return .{ .named = name };