The headless browser for AI agents and web scraping
fix(js): enforce same-origin in history.pushState/replaceState
Andrey Mnatsakanov3549eadb0a52
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex 82576e07..3ed14338 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -11080,12 +11080,37 @@ globalThis.atob = globalThis.atob || ((s) => {const stack = [{state: null, url: undefined}]; // initial entry; url=undefined means "use document URL"let idx = 0;const historyToken = Symbol("History");- const resolveOrFallback = (url) => {+ const resolveHistoryUrl = (url, method) => {// A missing url (pushState/replaceState called with < 3 args) keeps the// current document URL per the HTML spec — capture it so the entry does not// reset location back to the original document URL.if (url === null || url === undefined) return __currentUrl();- try { return new URL(String(url), __currentUrl()).href; } catch (e) { return String(url); }+ const base = __currentUrl();+ let target;+ try {+ target = new URL(String(url), base);+ } catch (e) {+ // HTML spec: a URL that fails to parse throws a SecurityError.+ throw new DOMException(+ "Failed to execute '" + method + "' on 'History': Invalid URL '" + String(url) + "'.",+ "SecurityError"+ );+ }+ // Same-origin restriction (HTML spec): pushState/replaceState may only+ // rewrite the URL within the document's origin. Without this a page could+ // spoof its own URL to a cross-origin or file:// value, which the host then+ // adopts as page.url via sync_virtual_url (#1055). Opaque-origin documents+ // (about:blank, file:) have no comparable origin, so they are left as-is.+ let baseUrl = null;+ try { baseUrl = new URL(base); } catch (e) {}+ if (baseUrl && baseUrl.origin && baseUrl.origin !== "null" && target.origin !== baseUrl.origin) {+ throw new DOMException(+ "Failed to execute '" + method + "' on 'History': A history state object with URL '" ++ target.href + "' cannot be created in a document with origin '" + baseUrl.origin + "'.",+ "SecurityError"+ );+ }+ return target.href;};const applyVirtual = () => {const entry = stack[idx];@@ -11118,7 +11143,7 @@ globalThis.atob = globalThis.atob || ((s) => {}pushState(state, _title, url) {const prevUrl = __currentUrl();- const resolved = resolveOrFallback(url);+ const resolved = resolveHistoryUrl(url, 'pushState');// Truncate forward entries (real Chrome drops the forward stack on a// new push) then append + advance.stack.length = idx + 1;@@ -11129,7 +11154,7 @@ globalThis.atob = globalThis.atob || ((s) => {}replaceState(state, _title, url) {const prevUrl = __currentUrl();- const resolved = resolveOrFallback(url);+ const resolved = resolveHistoryUrl(url, 'replaceState');stack[idx] = {state: state ?? null, url: resolved};applyVirtual();fireHashChangeIfNeeded(prevUrl);diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 53440fea..a8b1e734 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -5783,6 +5783,38 @@ mod tests {assert_eq!(path, serde_json::json!("/a"));}+ // #1055: pushState/replaceState must reject a cross-origin (or file:) URL+ // with a SecurityError rather than adopting it. Otherwise a page can spoof+ // its own URL, which the host adopts as page.url via sync_virtual_url.+ #[test]+ fn push_and_replace_state_reject_cross_origin_urls() {+ let mut rt = setup_runtime("<html><body></body></html>");+ let result = rt+ .evaluate(+ r#"(function(){+ function attempt(fn) {+ try { fn(); return 'no-throw'; }+ catch (e) { return e && e.name; }+ }+ return {+ push_cross: attempt(() => history.pushState({}, '', 'http://evil.example/x')),+ push_file: attempt(() => history.pushState({}, '', 'file:///etc/passwd')),+ replace_cross: attempt(() => history.replaceState({}, '', 'https://evil.example/x')),+ // Same-origin routing still works.+ same_origin: attempt(() => history.pushState({}, '', '/dashboard')),+ href: location.href,+ };+ })()"#,+ )+ .unwrap();+ assert_eq!(result["push_cross"], serde_json::json!("SecurityError"));+ assert_eq!(result["push_file"], serde_json::json!("SecurityError"));+ assert_eq!(result["replace_cross"], serde_json::json!("SecurityError"));+ assert_eq!(result["same_origin"], serde_json::json!("no-throw"));+ // The rejected cross-origin URLs never became the page URL.+ assert_eq!(result["href"], serde_json::json!("http://example.com/dashboard"));+ }+#[test]fn history_exposes_the_web_platform_constructor_and_prototype() {let mut rt = setup_runtime("<html><body></body></html>");