Lightpanda: the headless browser designed for AI and automation
Improve unforgeable detection
Karl Seguind42e8b4d8150
diff --git a/src/browser/js/Caller.zig b/src/browser/js/Caller.zigindex db1caa59b..eff8769e0 100644--- a/src/browser/js/Caller.zig+++ b/src/browser/js/Caller.zig@@ -719,9 +719,6 @@ pub const Function = struct {exposed: Exposed = .both,ce_reactions: bool = false,js_name: ?[:0]const u8 = null,- // Web IDL [LegacyUnforgeable]: the accessor is an own property of- // every instance (including subclass instances) instead of living on- // the interface prototype object.unforgeable: bool = false,pub const Exposed = enum { both, window, worker };diff --git a/src/browser/js/Snapshot.zig b/src/browser/js/Snapshot.zigindex 450f415ba..80b660de5 100644--- a/src/browser/js/Snapshot.zig+++ b/src/browser/js/Snapshot.zig@@ -734,74 +734,17 @@ pub fn generateConstructor(comptime JsApi: type, isolate: *v8.Isolate) *const v8// hard-coded special case for HtmlElement which can be extended but not// instantiated.fn inheritsFromHtmlElement(comptime JsApi: type) bool {- comptime {- var T = JsApi.bridge.type;- if (T == HtmlElement) {- return false;- }-- while (@hasField(T, "_proto")) {- const Parent = @typeInfo(std.meta.fieldInfo(T, ._proto).type).pointer.child;- if (Parent == HtmlElement) {- return true;- }- T = Parent;- }+ if (JsApi.bridge.type == HtmlElement) {return false;}+ return inheritsOrIs(JsApi, HtmlElement.JsApi);}-// Attach JsApi members to a template (public for reuse). This is called on all-// types. But, for globals (window, WGS) it's called twice. The first time, it's-// called like any other interface. The 2nd time, it's called with flatten == true-// and define_on != null. This is the "flattening" pass, and it defines all of-// the functions/accessors on directly on the global instance. Thus, globals have-// it defined on both their prototype (first pass) and their own instance (2nd pass).-fn attachAccessorProperty(comptime name: [:0]const u8, value: bridge.Accessor, isolate: *v8.Isolate, template: *const v8.FunctionTemplate, signature: anytype, target: anytype) void {- const js_name = v8.v8__String__NewFromUtf8(isolate, name.ptr, v8.kNormal, @intCast(name.len));- const getter_signature = if (value.static) null else signature;- const getter_callback = v8.v8__FunctionTemplate__New__Config(isolate, &.{- .callback = value.getter,- .signature = getter_signature,- }).?;- // WebIDL: getter function's .name should be "get X"- const getter_name_str = "get " ++ name;- const getter_name_v8 = v8.v8__String__NewFromUtf8(isolate, getter_name_str.ptr, v8.kNormal, @intCast(getter_name_str.len));- v8.v8__FunctionTemplate__SetClassName(getter_callback, getter_name_v8);-- const setter_callback = if (value.setter) |setter| blk: {- const cb = v8.v8__FunctionTemplate__New__Config(isolate, &.{- .callback = setter,- .signature = getter_signature,- .length = 1,- }).?;- const setter_name_str = "set " ++ name;- const setter_name_v8 = v8.v8__String__NewFromUtf8(isolate, setter_name_str.ptr, v8.kNormal, @intCast(setter_name_str.len));- v8.v8__FunctionTemplate__SetClassName(cb, setter_name_v8);- break :blk cb;- } else null;-- var attribute: v8.PropertyAttribute = 0;- if (value.setter == null) {- attribute |= v8.ReadOnly;- }- if (value.deletable == false) {- attribute |= v8.DontDelete;- }-- if (value.static) {- v8.v8__Template__SetAccessorProperty(@ptrCast(template), js_name, getter_callback, setter_callback, attribute);- } else {- // Web IDL: attributes on the interface prototype object- // (and mirrored onto [Global] instances) are enumerable.- v8.v8__ObjectTemplate__SetAccessorProperty__Config(target, &.{- .key = js_name,- .getter = getter_callback,- .setter = setter_callback,- .attribute = attribute,- });- }-}+const Unforgeable = struct {+ Owner: type,+ name: [:0]const u8,+ accessor: bridge.Accessor,+};fn attachClass(comptime JsApi: type, comptime flatten: bool, isolate: *v8.Isolate, template: *const v8.FunctionTemplate, define_on: ?*const v8.ObjectTemplate) void {const instance = v8.v8__FunctionTemplate__InstanceTemplate(template);@@ -838,10 +781,12 @@ fn attachClass(comptime JsApi: type, comptime flatten: bool, isolate: *v8.Isolatcontinue;}- // [LegacyUnforgeable] accessors live on the instances, not on- // the interface prototype object.- const target = if (value.unforgeable) instance else (define_on orelse prototype);- attachAccessorProperty(name, value, isolate, template, signature, target);+ if (comptime value.unforgeable) {+ // this accessor will be handled in the unforgeables loop+ // later in this function.+ continue;+ }+ attachAccessorProperty(name, value, isolate, template, signature, define_on orelse prototype);},bridge.Function => {if (value.wpt_only and wpt_extensions_enabled == false) {@@ -927,23 +872,13 @@ fn attachClass(comptime JsApi: type, comptime flatten: bool, isolate: *v8.Isolat}}- // Web IDL [LegacyUnforgeable]: unforgeable accessors of ancestor- // interfaces are own properties of every subclass instance too. v8's- // Inherit only chains prototype templates, not instance templates, so- // re-install them on this class's instance template.- if (comptime !flatten) {- comptime var T = JsApi.bridge.type;- inline while (comptime @hasField(T, "_proto")) {- const P = @typeInfo(std.meta.fieldInfo(T, ._proto).type).pointer.child;- inline for (@typeInfo(P.JsApi).@"struct".decls) |pd| {- const pvalue = @field(P.JsApi, pd.name);- if (comptime @TypeOf(pvalue) == bridge.Accessor) {- if (comptime (pvalue.unforgeable and !pvalue.static)) {- attachAccessorProperty(pd.name, pvalue, isolate, template, signature, instance);- }- }+ if (comptime flatten == false) {+ inline for (unforgeables) |u| {+ if (comptime inheritsOrIs(JsApi, u.Owner)) {+ // unforgeables attributes aren't only applied directly on the+ // instance, they're also applied directly on every child instance+ attachAccessorProperty(u.name, u.accessor, isolate, template, signature, instance);}- T = P;}}@@ -997,3 +932,88 @@ fn globalScopeChain(comptime GlobalScopeApi: type) []const type {return chain;}}++// Every [LegacyUnforgeable] accessor. We collect this once so we don't need to+// keep scanning for this in attachClass+const unforgeables: []const Unforgeable = blk: {+ @setEvalBranchQuota(100_000);+ var list: []const Unforgeable = &.{};+ for (JsApis) |Api| {+ for (@typeInfo(Api).@"struct".decls) |d| {+ const value = @field(Api, d.name);+ if (@TypeOf(value) == bridge.Accessor and value.unforgeable and !value.static) {+ list = list ++ &[_]Unforgeable{.{ .Owner = Api, .name = d.name, .accessor = value }};+ }+ }+ }+ break :blk list;+};++// Whether Child is Ancestor or inherits from it, following the _proto chain.+fn inheritsOrIs(comptime Child: type, comptime Ancestor: type) bool {+ comptime {+ const target = Ancestor.bridge.type;+ var T = Child.bridge.type;+ while (true) {+ if (T == target) {+ return true;+ }+ if (!@hasField(T, "_proto")) {+ return false;+ }+ T = @typeInfo(std.meta.fieldInfo(T, ._proto).type).pointer.child;+ }+ }+}++// Attach JsApi members to a template (public for reuse). This is called on all+// types. But, for globals (window, WGS) it's called twice. The first time, it's+// called like any other interface. The 2nd time, it's called with flatten == true+// and define_on != null. This is the "flattening" pass, and it defines all of+// the functions/accessors on directly on the global instance. Thus, globals have+// it defined on both their prototype (first pass) and their own instance (2nd pass).+fn attachAccessorProperty(comptime name: [:0]const u8, value: bridge.Accessor, isolate: *v8.Isolate, template: *const v8.FunctionTemplate, signature: anytype, target: anytype) void {+ const js_name = v8.v8__String__NewFromUtf8(isolate, name.ptr, v8.kNormal, @intCast(name.len));+ const getter_signature = if (value.static) null else signature;+ const getter_callback = v8.v8__FunctionTemplate__New__Config(isolate, &.{+ .callback = value.getter,+ .signature = getter_signature,+ }).?;+ // WebIDL: getter function's .name should be "get X"+ const getter_name_str = "get " ++ name;+ const getter_name_v8 = v8.v8__String__NewFromUtf8(isolate, getter_name_str.ptr, v8.kNormal, @intCast(getter_name_str.len));+ v8.v8__FunctionTemplate__SetClassName(getter_callback, getter_name_v8);++ const setter_callback = if (value.setter) |setter| blk: {+ const cb = v8.v8__FunctionTemplate__New__Config(isolate, &.{+ .callback = setter,+ .signature = getter_signature,+ .length = 1,+ }).?;+ const setter_name_str = "set " ++ name;+ const setter_name_v8 = v8.v8__String__NewFromUtf8(isolate, setter_name_str.ptr, v8.kNormal, @intCast(setter_name_str.len));+ v8.v8__FunctionTemplate__SetClassName(cb, setter_name_v8);+ break :blk cb;+ } else null;++ var attribute: v8.PropertyAttribute = 0;+ if (value.setter == null) {+ attribute |= v8.ReadOnly;+ }+ if (value.deletable == false) {+ attribute |= v8.DontDelete;+ }++ if (value.static) {+ v8.v8__Template__SetAccessorProperty(@ptrCast(template), js_name, getter_callback, setter_callback, attribute);+ } else {+ // Web IDL: attributes on the interface prototype object+ // (and mirrored onto [Global] instances) are enumerable.+ v8.v8__ObjectTemplate__SetAccessorProperty__Config(target, &.{+ .key = js_name,+ .getter = getter_callback,+ .setter = setter_callback,+ .attribute = attribute,+ });+ }+}diff --git a/src/browser/js/bridge.zig b/src/browser/js/bridge.zigindex d535b7e79..100e1520b 100644--- a/src/browser/js/bridge.zig+++ b/src/browser/js/bridge.zig@@ -230,7 +230,7 @@ pub const Accessor = struct {static: bool = false,deletable: bool = true,wpt_only: bool = false,- unforgeable: bool = false,+ unforgeable: bool = false, // Web IDL [LegacyUnforgeable]exposed: Caller.Function.Opts.Exposed = .both,cache: ?Caller.Function.Opts.Caching = null,getter: ?*const fn (?*const v8.FunctionCallbackInfo) callconv(.c) void = null,diff --git a/src/browser/webapi/Event.zig b/src/browser/webapi/Event.zigindex 173c1a23e..53179e125 100644--- a/src/browser/webapi/Event.zig+++ b/src/browser/webapi/Event.zig@@ -481,8 +481,6 @@ pub const JsApi = struct {pub const eventPhase = bridge.accessor(Event.getEventPhase, null, .{});pub const defaultPrevented = bridge.accessor(Event.getDefaultPrevented, null, .{});pub const timeStamp = bridge.accessor(Event.getTimeStamp, null, .{});- // [LegacyUnforgeable]: an own, non-configurable accessor of every- // Event (and subclass) instance.pub const isTrusted = bridge.accessor(Event.getIsTrusted, null, .{ .unforgeable = true, .deletable = false });pub const preventDefault = bridge.function(Event.preventDefault, .{});pub const stopPropagation = bridge.function(Event.stopPropagation, .{});diff --git a/src/browser/webapi/event/KeyboardEvent.zig b/src/browser/webapi/event/KeyboardEvent.zigindex b6fa83433..73ff74706 100644--- a/src/browser/webapi/event/KeyboardEvent.zig+++ b/src/browser/webapi/event/KeyboardEvent.zig@@ -284,7 +284,6 @@ pub const KeyboardEventOptions = struct {shiftKey: bool = false,altKey: bool = false,metaKey: bool = false,- // Legacy KeyboardEventInit members: returned as-is by synthetic events.charCode: u32 = 0,keyCode: u32 = 0,};