Skip to content

The headless browser for AI agents and web scraping

fix(js): coerce Location navigation values

xripfe26417be51c
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index e8495742..6c64175f 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -5904,6 +5904,7 @@ globalThis.self = globalThis;
globalThis.document = null;
function _resolveUrl(url) {
+ url = String(url);
if (!url) return url;
if (url.startsWith('http://') || url.startsWith('https://') || url.startsWith('about:')) return url;
try { return new URL(url, _domParse("document_url") || "about:blank").href; } catch(e) { return url; }
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 5e593cf9..c825a88d 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -12639,6 +12639,39 @@ mod tests {
);
}
+ #[test]
+ fn test_location_navigation_coerces_url_objects() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let hrefs = rt
+ .evaluate(
+ r#"(() => {
+ location.href = new URL('/from-href', location.href);
+ const href = location.href;
+ location.assign(new URL('/from-assign', location.href));
+ const assigned = location.href;
+ location.replace(new URL('/from-replace', location.href));
+ return [href, assigned, location.href];
+ })()"#,
+ )
+ .unwrap();
+ assert_eq!(
+ hrefs,
+ serde_json::json!([
+ "http://example.com/from-href",
+ "http://example.com/from-assign",
+ "http://example.com/from-replace"
+ ])
+ );
+ assert_eq!(
+ rt.take_pending_navigation(),
+ Some((
+ "http://example.com/from-replace".to_string(),
+ "GET".to_string(),
+ "".to_string()
+ ))
+ );
+ }
+
#[test]
fn test_submit_button_click_handler_can_prevent_default_and_navigate() {
let mut rt =