Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: CryptoKey algorithm and usages return their cached object

orezraey900f420a9a32
diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zig
index 69ec063e2..fa7d020dc 100644
--- a/src/browser/js/Env.zig
+++ b/src/browser/js/Env.zig
@@ -1022,15 +1022,21 @@ const PrivateSymbols = struct {
const Private = @import("Private.zig");
child_nodes: Private,
+ crypto_key_algorithm: Private,
+ crypto_key_usages: Private,
fn init(isolate: *v8.Isolate) PrivateSymbols {
return .{
.child_nodes = Private.init(isolate, "child_nodes"),
+ .crypto_key_algorithm = Private.init(isolate, "crypto_key_algorithm"),
+ .crypto_key_usages = Private.init(isolate, "crypto_key_usages"),
};
}
fn deinit(self: *PrivateSymbols) void {
self.child_nodes.deinit();
+ self.crypto_key_algorithm.deinit();
+ self.crypto_key_usages.deinit();
}
};
diff --git a/src/browser/tests/crypto.html b/src/browser/tests/crypto.html
index 6171892d1..1481e58f0 100644
--- a/src/browser/tests/crypto.html
+++ b/src/browser/tests/crypto.html
@@ -2992,3 +2992,52 @@
}, results);
});
</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"]);
+
+ // 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,
+ };
+
+ 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,
+ }, results);
+ });
+</script>
diff --git a/src/browser/webapi/CryptoKey.zig b/src/browser/webapi/CryptoKey.zig
index 7aec0019a..b0491e6a3 100644
--- a/src/browser/webapi/CryptoKey.zig
+++ b/src/browser/webapi/CryptoKey.zig
@@ -246,6 +246,8 @@ pub const JsApi = struct {
pub const @"type" = bridge.accessor(CryptoKey.getType, null, .{});
pub const extractable = bridge.accessor(CryptoKey.getExtractable, null, .{});
- pub const algorithm = bridge.accessor(CryptoKey.getAlgorithm, null, .{});
- pub const usages = bridge.accessor(CryptoKey.getUsages, null, .{});
+ // Each returns its cached object: the same one on every read, as the
+ // spec's [[algorithm_cached]] and [[usages_cached]] slots are.
+ pub const algorithm = bridge.accessor(CryptoKey.getAlgorithm, null, .{ .cache = .{ .private = "crypto_key_algorithm" } });
+ pub const usages = bridge.accessor(CryptoKey.getUsages, null, .{ .cache = .{ .private = "crypto_key_usages" } });
};