Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(location): make prototype immutable

ldm0fa30d1b468b7
diff --git a/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs b/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
index 34c0d2f57..8160b40bc 100644
--- a/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
+++ b/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
@@ -22,6 +22,7 @@ use super::super::{
geometry_runtime::{dom_matrix_constructor_callback, dom_point_constructor_callback},
idle_detection::idle_detector_constructor_callback,
image_data::image_data_constructor_callback,
+ location_runtime::build_location_constructor_template,
media_cues::{
media_error_constructor_callback, text_track_cue_constructor_callback,
vtt_cue_constructor_callback,
@@ -493,6 +494,7 @@ pub(in crate::context_bootstrap) fn build_constructor_template<'s>(
ConstructorKind::History | ConstructorKind::Navigation => {
v8::FunctionTemplate::builder(illegal_constructor_callback).build(scope)
}
+ ConstructorKind::Location => build_location_constructor_template(scope),
ConstructorKind::MediaError
| ConstructorKind::TextTrack
| ConstructorKind::TextTrackList
diff --git a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/materialize.rs b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/materialize.rs
index 898a63b3e..2b5b44dfc 100644
--- a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/materialize.rs
+++ b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/materialize.rs
@@ -138,6 +138,25 @@ pub(crate) fn ensure_intrinsic_interface_constructor<'s>(
.map_err(|_| anyhow!("intrinsic constructor `{name}` is not a Function"))
}
+pub(crate) fn build_intrinsic_interface_instance<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ name: &str,
+) -> Result<v8::Local<'s, v8::Object>> {
+ let _ = ensure_intrinsic_interface_constructor(scope, name)?;
+ let registry = ExposedInterfaceTemplateRegistry::current(scope)
+ .ok_or_else(|| anyhow!("exposed interface template registry is unavailable"))?;
+ let id = registry
+ .id_by_name(name)
+ .ok_or_else(|| anyhow!("unknown exposed interface `{name}`"))?;
+ let template = registry
+ .ready_template(scope, id)
+ .ok_or_else(|| anyhow!("missing FunctionTemplate for `{name}`"))?;
+ template
+ .instance_template(scope)
+ .new_instance(scope)
+ .ok_or_else(|| anyhow!("failed to instantiate `{name}` instance template"))
+}
+
pub(crate) fn ensure_intrinsic_interface_prototype<'s>(
scope: &mut v8::PinScope<'s, '_>,
name: &str,
diff --git a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/mod.rs b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/mod.rs
index 913aa9183..6155abb6c 100644
--- a/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/mod.rs
+++ b/moli-renderer-v8/src/context_bootstrap/exposed_interfaces/mod.rs
@@ -20,8 +20,8 @@ pub(crate) use install::{
storage_interface_materialization_count,
};
pub(crate) use materialize::{
- ensure_intrinsic_interface_constructor, ensure_intrinsic_interface_prototype,
- object_is_intrinsic_interface_instance,
+ build_intrinsic_interface_instance, ensure_intrinsic_interface_constructor,
+ ensure_intrinsic_interface_prototype, object_is_intrinsic_interface_instance,
};
pub(crate) use metadata::RealmKind;
pub(in crate::context_bootstrap) use metadata::TemplateBuildProfile;
diff --git a/moli-renderer-v8/src/context_bootstrap/location_runtime.rs b/moli-renderer-v8/src/context_bootstrap/location_runtime.rs
index 52605ad25..f56648123 100644
--- a/moli-renderer-v8/src/context_bootstrap/location_runtime.rs
+++ b/moli-renderer-v8/src/context_bootstrap/location_runtime.rs
@@ -13,7 +13,7 @@ mod slots;
mod surface;
pub(super) use install::{
- build_location_runtime_object, ensure_location_constructor_runtime_state,
+ build_location_constructor_template, build_location_runtime_object,
install_location_runtime_state,
};
pub(super) use navigation::{
diff --git a/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs b/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs
index 3b5954a28..2efd59f5f 100644
--- a/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs
+++ b/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs
@@ -1,3 +1,4 @@
+use super::super::constructors::illegal_constructor_callback;
use super::super::navigation_window::{
navigation_document_has_opaque_origin, runtime_window_owner,
};
@@ -11,9 +12,10 @@ use super::methods::{
};
use super::slots::{location_href_slot, sync_location_object_fields};
use super::*;
+use crate::context_bootstrap::exposed_interfaces::build_intrinsic_interface_instance;
use crate::util::{callback_data_index_value, callback_data_item};
use anyhow::{Result, anyhow};
-use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
+use moli_webapi_declare::WebApiObject;
#[derive(Clone, Copy)]
enum LocationAttribute {
@@ -158,54 +160,21 @@ struct LocationOwnSurfaceDeclaration {
to_string: (),
}
-#[derive(WebApiFunctionTemplate)]
-#[webapi(name = "Location", constructor = "illegal", constructor_length = 0)]
-struct LocationConstructorTemplateDeclaration {}
-
-#[derive(WebApiObject)]
-#[webapi(interface = "Object")]
-struct LocationConstructorGlobalDeclaration<'scope> {
- #[webapi(data_property = "Location")]
- constructor: v8::Local<'scope, v8::Function>,
-}
-
-#[derive(Default, WebApiObject)]
-#[webapi(interface = "Location")]
-struct LocationPrototypeMetadataDeclaration {
- #[webapi(to_string_tag, init = string("Location"))]
- to_string_tag: (),
-}
-
-pub(in crate::context_bootstrap) fn ensure_location_constructor_runtime_state<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- global: v8::Local<'s, v8::Object>,
-) -> Result<()> {
- if global_constructor_prototype(scope, "Location").is_some() {
- return Ok(());
- }
-
- let template = LocationConstructorTemplateDeclaration::build(scope);
- let constructor = template
- .get_function(scope)
- .ok_or_else(|| anyhow!("failed to build Location constructor"))?;
- LocationConstructorGlobalDeclaration::new(constructor)
- .initialize(scope, global)
- .map_err(|error| anyhow!("failed to initialize Location constructor global: {error}"))?;
-
- let prototype = constructor
- .get(scope, v8str(scope, "prototype").into())
- .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
- .ok_or_else(|| anyhow!("failed to extract Location.prototype"))?;
- LocationPrototypeMetadataDeclaration::default()
- .initialize(scope, prototype)
- .map_err(|error| anyhow!("failed to initialize Location prototype metadata: {error}"))?;
- Ok(())
+pub(in crate::context_bootstrap) fn build_location_constructor_template<'s>(
+ scope: &mut v8::PinScope<'s, '_, ()>,
+) -> v8::Local<'s, v8::FunctionTemplate> {
+ let template = v8::FunctionTemplate::builder(illegal_constructor_callback)
+ .length(0)
+ .build(scope);
+ let instance = template.instance_template(scope);
+ configure_location_instance_template(scope, instance);
+ template
}
-pub(in crate::context_bootstrap) fn build_location_runtime_object<'s>(
- scope: &mut v8::PinScope<'s, '_>,
-) -> Result<v8::Local<'s, v8::Object>> {
- let template = v8::ObjectTemplate::new(scope);
+fn configure_location_instance_template(
+ scope: &mut v8::PinScope<'_, '_, ()>,
+ template: v8::Local<'_, v8::ObjectTemplate>,
+) {
let locked_property_attributes = || {
v8::PropertyAttribute::READ_ONLY
| v8::PropertyAttribute::DONT_ENUM
@@ -231,9 +200,13 @@ pub(in crate::context_bootstrap) fn build_location_runtime_object<'s>(
.getter(location_same_origin_named_property_getter)
.flags(v8::PropertyHandlerFlags::ONLY_INTERCEPT_STRINGS),
);
- template
- .new_instance(scope)
- .ok_or_else(|| anyhow!("failed to instantiate Location object template"))
+ template.set_immutable_proto();
+}
+
+pub(in crate::context_bootstrap) fn build_location_runtime_object<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+) -> Result<v8::Local<'s, v8::Object>> {
+ build_intrinsic_interface_instance(scope, "Location")
}
fn location_same_origin_named_property_getter(
@@ -250,9 +223,6 @@ pub(in crate::context_bootstrap) fn install_location_runtime_state<'s>(
location: v8::Local<'s, v8::Object>,
href: &str,
) -> Result<()> {
- if let Some(prototype) = global_constructor_prototype(scope, "Location") {
- let _ = location.set_prototype(scope, prototype.into());
- }
sync_location_object_fields(scope, location, href);
// Location's legacy-unforgeable own properties are non-configurable.
// Window resets refresh the backing slots on the existing object without
diff --git a/moli-renderer-v8/src/context_bootstrap/runtime_state.rs b/moli-renderer-v8/src/context_bootstrap/runtime_state.rs
index a14b5b773..e51459d47 100644
--- a/moli-renderer-v8/src/context_bootstrap/runtime_state.rs
+++ b/moli-renderer-v8/src/context_bootstrap/runtime_state.rs
@@ -9,10 +9,7 @@ use super::{
css_runtime::install_css_runtime_state,
exposed_interfaces::is_lazy_exposed_interface,
indexed_db::ensure_indexed_db_runtime_state,
- location_runtime::{
- ensure_location_constructor_runtime_state, location_href_slot,
- sync_document_location_runtime_state_from_window,
- },
+ location_runtime::{location_href_slot, sync_document_location_runtime_state_from_window},
navigation_bootstrap::install_window_location_history_navigation_runtime_state,
navigator_runtime::{bind_window_navigator_identity_seed, install_navigator_runtime_state},
performance_runtime::install_default_window_performance_seed,
@@ -1973,7 +1970,6 @@ fn install_window_runtime_state<'s>(
v8::Boolean::new(scope, secure_context_available).into(),
);
install_date_locale_runtime_state(scope, global)?;
- ensure_location_constructor_runtime_state(scope, global)?;
install_webassembly_runtime_state(scope, global)?;
install_webidl_collection_iterator_intrinsics(scope, global)?;
diff --git a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
index a871a6842..fe000ff68 100644
--- a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
+++ b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
@@ -696,7 +696,7 @@ const CONSTRUCTOR_SPECS_BEFORE_STREAMS: &[ConstructorSpec] = &[
ConstructorSpec {
name: "Location",
parent: None,
- kind: ConstructorKind::Illegal,
+ kind: ConstructorKind::Location,
},
ConstructorSpec {
name: "Navigation",
diff --git a/moli-renderer-v8/src/context_bootstrap/specs/types.rs b/moli-renderer-v8/src/context_bootstrap/specs/types.rs
index d295aeffb..dcfbab49a 100644
--- a/moli-renderer-v8/src/context_bootstrap/specs/types.rs
+++ b/moli-renderer-v8/src/context_bootstrap/specs/types.rs
@@ -130,6 +130,7 @@ pub(in crate::context_bootstrap) enum ConstructorKind {
VTTCue,
Selection,
History,
+ Location,
Navigation,
NavigationHistoryEntry,
NavigationActivation,
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
index 0d0986d17..023446c74 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc.rs
@@ -23287,6 +23287,61 @@ fn location_conversion_hooks_and_prevent_extensions_match_location_exotic_semant
);
}
+#[test]
+fn location_prototype_is_immutable_while_same_prototype_assignments_succeed() {
+ let mut vm = new_storage_test_vm("https://example.com/path");
+
+ let result = vm
+ .eval(
+ r#"
+ (() => {
+ "use strict";
+ const original = Object.getPrototypeOf(location);
+ const replacement = {};
+ const throwsName = callback => {
+ try {
+ callback();
+ return "returned";
+ } catch (error) {
+ return error && error.name;
+ }
+ };
+
+ const objectDifferent = throwsName(() => {
+ Object.setPrototypeOf(location, replacement);
+ });
+ const dunderDifferent = throwsName(() => {
+ location.__proto__ = replacement;
+ });
+ const reflectDifferent = Reflect.setPrototypeOf(location, replacement);
+ const unchanged = Object.getPrototypeOf(location) === original;
+ const objectSame = Object.setPrototypeOf(location, original) === location;
+ const dunderSame = throwsName(() => {
+ location.__proto__ = original;
+ });
+ const reflectSame = Reflect.setPrototypeOf(location, original);
+
+ return JSON.stringify({
+ objectDifferent,
+ dunderDifferent,
+ reflectDifferent,
+ unchanged,
+ objectSame,
+ dunderSame,
+ reflectSame,
+ instanceofLocation: location instanceof Location,
+ });
+ })()
+ "#,
+ )
+ .expect("Location immutable prototype probe should evaluate");
+
+ assert_eq!(
+ result,
+ r#"{"objectDifferent":"TypeError","dunderDifferent":"TypeError","reflectDifferent":false,"unchanged":true,"objectSame":true,"dunderSame":"returned","reflectSame":true,"instanceofLocation":true}"#
+ );
+}
+
#[test]
fn location_href_backing_slot_ignores_reflection_and_spoofing() {
let mut vm = new_storage_test_vm("https://example.com/path?x=1#frag");