Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(url): create query objects with intrinsic bindings

ldm0a46e4678b459
diff --git a/moli-renderer-v8/src/context_bootstrap/url_search_params_runtime/storage.rs b/moli-renderer-v8/src/context_bootstrap/url_search_params_runtime/storage.rs
index fc905ec6a..f651102a9 100644
--- a/moli-renderer-v8/src/context_bootstrap/url_search_params_runtime/storage.rs
+++ b/moli-renderer-v8/src/context_bootstrap/url_search_params_runtime/storage.rs
@@ -18,15 +18,7 @@ pub(in crate::context_bootstrap) fn new_url_search_params_object<'s>(
initial_pairs: Option<Vec<SearchParamPair>>,
) -> Option<v8::Local<'s, v8::Object>> {
let pairs = initial_pairs.as_deref().unwrap_or(&[]);
- let global = scope.get_current_context().global(scope);
- if let Some(object) = global
- .get(scope, v8str(scope, "URLSearchParams").into())
- .and_then(|value| v8::Local::<v8::Function>::try_from(value).ok())
- .and_then(|ctor| ctor.new_instance(scope, &[]))
- {
- initialize_url_search_params_object(scope, object, owner, pairs);
- return Some(object);
- }
+ // Platform-created queries use the declaration's intrinsic realm prototype.
UrlSearchParamsObjectDeclaration::new(
owner
.map(Into::into)
diff --git a/moli-renderer-v8/src/script_vm/tests/webidl_fetch/url_and_initializer_semantics.rs b/moli-renderer-v8/src/script_vm/tests/webidl_fetch/url_and_initializer_semantics.rs
index 898729aff..ffb261e36 100644
--- a/moli-renderer-v8/src/script_vm/tests/webidl_fetch/url_and_initializer_semantics.rs
+++ b/moli-renderer-v8/src/script_vm/tests/webidl_fetch/url_and_initializer_semantics.rs
@@ -1516,3 +1516,24 @@ fn fetched_null_bodies_discard_payloads_without_registering_pending_streams() {
})
.expect("null response body checks should complete");
}
+
+#[test]
+fn url_search_params_internal_objects_ignore_public_constructor_replacements() {
+ let mut vm = new_storage_test_vm("https://url-intrinsic-query.test/");
+ vm.eval(include_str!(
+ "../../../../tests/fixtures/url-intrinsic-query-objects.js"
+ ))
+ .expect("URL intrinsic query fixture should execute");
+ let facts: serde_json::Value = serde_json::from_str(
+ &vm.eval("JSON.stringify(__uiEventResults)")
+ .expect("URL intrinsic query results should serialize"),
+ )
+ .expect("URL intrinsic query results should be valid JSON");
+ let checks = facts["checks"].as_array().expect("query checks");
+ let failures = checks
+ .iter()
+ .filter(|check| check["passed"] != true)
+ .collect::<Vec<_>>();
+ assert!(failures.is_empty(), "{failures:#?}");
+ assert_eq!(checks.len(), 10);
+}
diff --git a/moli-renderer-v8/tests/fixtures/url-intrinsic-query-objects.js b/moli-renderer-v8/tests/fixtures/url-intrinsic-query-objects.js
new file mode 100644
index 000000000..202308f82
--- /dev/null
+++ b/moli-renderer-v8/tests/fixtures/url-intrinsic-query-objects.js
@@ -0,0 +1,55 @@
+(() => {
+ const checks = [];
+ globalThis.__blobReadingProgress = { stage: "URL intrinsic query objects", checks };
+ const NativeURL = URL;
+ const publicDescriptor = Object.getOwnPropertyDescriptor(globalThis, "URLSearchParams");
+ let NativeParams;
+ const restore = () => Object.defineProperty(globalThis, "URLSearchParams", publicDescriptor);
+ const factories = [
+ ["constructor", input => new NativeURL(input)],
+ ["parse", input => NativeURL.parse(input)]
+ ];
+ for (const [factory, create] of factories) {
+ for (const scenario of ["throwing getter", "throwing constructor", "shared author object", "deleted global", "conversion replacement"]) {
+ let calls = 0, conversions = 0;
+ const marker = new Error("author URLSearchParams must not run");
+ const shared = {};
+ let detail;
+ const poisonGetter = () => Object.defineProperty(globalThis, "URLSearchParams", {
+ configurable: true, get() { calls++; throw marker; }
+ });
+ try {
+ let input = "https://query-intrinsic.test/?a=1";
+ if (scenario === "throwing getter") poisonGetter();
+ else if (scenario === "throwing constructor") Object.defineProperty(globalThis, "URLSearchParams", {
+ configurable: true, writable: true, value: function () { calls++; throw marker; }
+ });
+ else if (scenario === "shared author object") Object.defineProperty(globalThis, "URLSearchParams", {
+ configurable: true, writable: true, value: function () { calls++; return shared; }
+ });
+ else if (scenario === "deleted global") delete globalThis.URLSearchParams;
+ else input = { toString() { conversions++; poisonGetter(); return "https://query-intrinsic.test/?a=1"; } };
+ const url = create(input), params = url.searchParams;
+ const second = scenario === "shared author object" ? create("https://query-intrinsic.test/?a=2") : null;
+ restore(); NativeParams ??= globalThis.URLSearchParams;
+ let sharedRejected = false;
+ try { NativeParams.prototype.get.call(shared, "a"); }
+ catch (error) { sharedRejected = error instanceof TypeError; }
+ const initial = NativeParams.prototype.get.call(params, "a");
+ params.append("b", "2");
+ const appended = url.search === "?a=1&b=2";
+ url.search = "?c=3";
+ detail = { calls, conversions, intrinsicPrototype: Object.getPrototypeOf(params) === NativeParams.prototype,
+ initial, appended, sameObject: params === url.searchParams, updated: params.get("c"), sharedRejected,
+ independent: second === null || second.searchParams !== params && second.searchParams.get("a") === "2" };
+ checks.push({ name: `${factory}: ${scenario}`, passed: calls === 0 && detail.intrinsicPrototype && initial === "1"
+ && appended && detail.sameObject && detail.updated === "3" && sharedRejected && detail.independent
+ && conversions === (scenario === "conversion replacement" ? 1 : 0), detail });
+ } catch (error) {
+ checks.push({ name: `${factory}: ${scenario}`, passed: false,
+ detail: { calls, conversions, error: error === marker ? "author marker" : String(error.stack || error) } });
+ } finally { restore(); NativeParams ??= globalThis.URLSearchParams; }
+ }
+ }
+ globalThis.__uiEventResults = { checks };
+})();