Skip to content

The headless browser for AI agents and web scraping

fix(obscura-js): don't move the document URL before navigation commits (#940)

Andrey Mnatsakanov4778192fb413
diff --git a/crates/obscura-js/src/ops.rs b/crates/obscura-js/src/ops.rs
index da58ec50..17da92fc 100644
--- a/crates/obscura-js/src/ops.rs
+++ b/crates/obscura-js/src/ops.rs
@@ -3935,7 +3935,11 @@ fn op_navigate(
) {
let gs = realm_state(scope, state);
let mut gs = gs.borrow_mut();
- gs.url = url.to_string();
+ // Only queue the navigation — do NOT change the realm URL here. The URL is
+ // updated on commit via `set_url` once the navigation is actually performed.
+ // Moving it early let synchronous JS run between two navigations read and
+ // write another origin's cookies through document.cookie, whose ops derive
+ // the cookie domain from this URL (SOP bypass, #940).
gs.pending_navigation = Some((url.to_string(), method.to_string(), body.to_string()));
}
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 1211d43e..aabfa33b 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -15468,6 +15468,30 @@ mod tests {
);
}
+ // #940: op_navigate must not move the document's cookie context before the
+ // navigation actually commits. A queued navigation to another origin must
+ // not let document.cookie read that origin's cookies (SOP bypass).
+ #[test]
+ fn queued_navigation_does_not_expose_another_origins_cookies() {
+ let (mut rt, jar) = setup_runtime_with_cookies("<html><body></body></html>");
+ // A cookie belonging to a different origin than the current page
+ // (the harness page is http://example.com/test).
+ let victim = url::Url::parse("https://victim.example/").unwrap();
+ jar.set_cookie("secret=victimtoken; Path=/", &victim);
+
+ // Start navigating to the victim origin. This only *queues* the
+ // navigation; it must not retroactively move the cookie context.
+ rt.evaluate("location.href = 'https://victim.example/'").unwrap();
+
+ let result = rt.evaluate("document.cookie").unwrap();
+ let cookie_str = result.as_str().unwrap();
+ assert!(
+ !cookie_str.contains("victimtoken"),
+ "document.cookie must not expose another origin's cookies after a queued navigation, got: {}",
+ cookie_str
+ );
+ }
+
#[test]
fn test_document_cookie_delete_via_max_age() {
let (mut rt, jar) = setup_runtime_with_cookies("<html><body></body></html>");