Skip to content

The headless browser for AI agents and web scraping

Merge commit '8edfe480133b383f70e17f9d4ea55e3592cdfd24' into repair/1223-web-storage

SGavrla253ccbcc697
diff --cc crates/obscura-cli/src/main.rs
index 8c43d6ef,e895aff1..418971e2
--- a/crates/obscura-cli/src/main.rs
+++ b/crates/obscura-cli/src/main.rs
@@@ -594,15 -598,8 +599,16 @@@ async fn run_cli() -> anyhow::Result<()
proxy,
user_agent,
}) => {
+ configure_font_directories(&font_dirs)?;
let mcp_proxy = merge_proxy(global_proxy.clone(), proxy);
+ // `obscura mcp` builds its BrowserContext from the environment, so
+ // the global profile flag has to be handed over explicitly.
+ if let Some(ref dir) = global_storage_dir {
+ // SAFETY: single threaded here, before the MCP server starts.
+ unsafe {
+ std::env::set_var("OBSCURA_STORAGE_DIR", dir);
+ }
+ }
if http {
obscura_mcp::http::run(host, port, mcp_proxy, user_agent, stealth).await?;
} else {
diff --cc crates/obscura-js/src/ops.rs
index 5923e4c1,81f15ddd..66f4f01b
--- a/crates/obscura-js/src/ops.rs
+++ b/crates/obscura-js/src/ops.rs
@@@ -5028,111 -4565,51 +5099,156 @@@ mod tests
use crate::runtime::ObscuraJsRuntime;
use obscura_dom::parse_html;
+ // #678: Web Storage lives in Rust, keyed by origin, so these pin the
+ // behaviour the realm-local object could not provide.
+ #[test]
+ fn origin_storage_keeps_origins_apart_and_preserves_insertion_order() {
+ let store = OriginStorage::default();
+ assert!(store.set("https://a.example", "k1".into(), "one".into()));
+ assert!(store.set("https://a.example", "k2".into(), "two".into()));
+ assert!(store.set("https://b.example", "k1".into(), "other".into()));
+
+ // Storage.key() is defined in insertion order, not sorted order.
+ let order: Vec<String> = store
+ .snapshot("https://a.example")
+ .into_iter()
+ .map(|(key, _)| key)
+ .collect();
+ assert_eq!(order, vec!["k1".to_string(), "k2".to_string()]);
+
+ assert_eq!(store.get("https://a.example", "k1"), Some("one".to_string()));
+ assert_eq!(store.get("https://b.example", "k1"), Some("other".to_string()));
+ assert_eq!(store.get("https://c.example", "k1"), None);
+ }
+
+ #[test]
+ fn origin_storage_set_replaces_in_place_without_growing_the_quota() {
+ let store = OriginStorage::default();
+ let origin = "https://a.example";
+ assert!(store.set(origin, "k".into(), "x".repeat(1024).into()));
+ // Overwriting an existing key must not count the old bytes twice: a
+ // replacement stays within quota where a second key of the same size
+ // would still fit, and the entry count must not change.
+ assert!(store.set(origin, "k".into(), "y".repeat(1024).into()));
+ assert_eq!(store.snapshot(origin).len(), 1);
+ assert_eq!(store.get(origin, "k").map(|v| v.len()), Some(1024));
+
+ // A single value over the per-origin limit is rejected, and the
+ // rejected write leaves the store untouched.
+ let too_big = "z".repeat(LOCAL_STORAGE_ORIGIN_LIMIT + 1);
+ assert!(!store.set(origin, "big1".into(), too_big));
+ assert_eq!(store.snapshot(origin).len(), 1);
+ assert_eq!(store.get(origin, "k").map(|v| v.len()), Some(1024));
+ }
+
+ #[test]
+ fn origin_storage_remove_and_clear_return_the_quota() {
+ let store = OriginStorage::default();
+ let origin = "https://a.example";
+ assert!(store.set(origin, "k1".into(), "x".repeat(2048).into()));
+ assert!(store.set(origin, "k2".into(), "x".repeat(2048).into()));
+
+ store.remove(origin, "k1");
+ assert_eq!(store.get(origin, "k1"), None);
+ // The freed bytes are usable again, which is the part a naive counter
+ // gets wrong.
+ assert!(store.set(origin, "k3".into(), "x".repeat(2048).into()));
+
+ store.clear(origin);
+ assert!(store.snapshot(origin).is_empty());
+ assert!(store.set(origin, "after-clear".into(), "small".into()));
+ }
+
+ #[test]
+ fn origin_storage_total_and_origin_count_limits_are_enforced() {
+ let store = OriginStorage::default();
+
+ // Distinct origins share one total budget.
+ let per_origin = LOCAL_STORAGE_TOTAL_LIMIT / LOCAL_STORAGE_ORIGIN_COUNT_LIMIT + 1024;
+ let mut rejected = 0;
+ for index in 0..LOCAL_STORAGE_ORIGIN_COUNT_LIMIT {
+ let origin = format!("https://o{index}.example");
+ if !store.set(&origin, "k".into(), "q".repeat(per_origin).into()) {
+ rejected += 1;
+ }
+ }
+ assert!(
+ rejected > 0,
+ "the total budget must stop writes once {LOCAL_STORAGE_TOTAL_LIMIT} bytes are used"
+ );
+
+ // And the origin count is capped even when each origin is tiny.
+ let store = OriginStorage::default();
+ for index in 0..LOCAL_STORAGE_ORIGIN_COUNT_LIMIT {
+ assert!(store.set(&format!("https://o{index}.example"), "k".into(), "v".into()));
+ }
+ assert!(
+ !store.set("https://one-too-many.example", "k".into(), "v".into()),
+ "an origin past the count limit must be rejected"
+ );
+ }
+
+ #[test]
+ fn clearing_an_origin_marks_it_changed() {
+ let store = OriginStorage::default();
+ let origin = "https://a.example";
+ assert!(store.set(origin, "k".into(), "v".into()));
+ store.take_changed();
+ assert!(!store.is_dirty());
+ store.clear(origin);
+ assert!(store.is_dirty(), "a clear must mark the store dirty");
+ assert_eq!(
+ store.changed_origins(),
+ vec![origin.to_string()],
+ "a clear must name the origin so its file is removed, not left behind"
+ );
+ }
+
+ #[test]
+ fn scripted_requests_pause_only_for_matching_fetch_patterns() {
+ let url = "https://example.test/value/1";
+ assert!(!super::should_intercept_url(false, &[], url));
+ assert!(super::should_intercept_url(true, &[], url));
+ assert!(super::should_intercept_url(true, &["*".to_string()], url));
+ assert!(super::should_intercept_url(true, &["*/value/*".to_string()], url));
+ assert!(!super::should_intercept_url(true, &["*never-matches*".to_string()], url));
+ assert!(super::should_intercept_url(
+ true,
+ &["*never-matches*".to_string(), "https://example.test/*".to_string()],
+ url
+ ));
+ }
+
+ #[test]
+ fn interception_patterns_match_repeated_suffixes_and_escaped_wildcards() {
+ for (pattern, url, expected) in [
+ ("/a*abc", "/aabcabc", true),
+ ("*abc", "abcabc", true),
+ ("/a?c", "/abc", true),
+ ("/a?c", "/ac", false),
+ ("/a?c", "/abbc", false),
+ (r"/a\?c", "/a?c", true),
+ (r"/a\?c", "/abc", false),
+ (r"/a\*c", "/a*c", true),
+ (r"/a\*c", "/abc", false),
+ (r"/a\\c", r"/a\c", true),
+ ("/a?c", "/aéc", true),
+ ("", "", true),
+ ("", "/", false),
+ ("**", "", true),
+ ("/a*c", "/ab", false),
+ ("*ab*bc", "ababbbc", true),
+ ("*ab*bc", "abbcd", false),
+ ("*?*", "", false),
+ ("*?*", "abc", true),
+ ("ab*cd*ef", "abxxcdyyef", true),
+ ("ab*cd*ef", "abxxcdyyefzz", false),
+ ] {
+ assert_eq!(super::url_matches_cdp_pattern(pattern, url), expected,
+ "pattern {pattern:?}, URL {url:?}");
+ }
+ }
+
// #967 — a redirect must not forward the caller's credentials to a
// different origin, and a 301/302/303 GET downgrade drops the body headers.
#[test]