Skip to content

The headless browser for AI agents and web scraping

fix(cdp): implement Storage.clearCookies

xrip835bc1b92382
diff --git a/crates/obscura-cdp/src/dispatch.rs b/crates/obscura-cdp/src/dispatch.rs
index 997078d2..283d59e5 100644
--- a/crates/obscura-cdp/src/dispatch.rs
+++ b/crates/obscura-cdp/src/dispatch.rs
@@ -405,6 +405,7 @@ fn is_v8_free_method(method: &str) -> bool {
| "IO.close"
| "Storage.getCookies"
| "Storage.setCookies"
+ | "Storage.clearCookies"
| "Storage.deleteCookies"
)
}
diff --git a/crates/obscura-cdp/src/domains/storage.rs b/crates/obscura-cdp/src/domains/storage.rs
index 91a09847..4ffcab68 100644
--- a/crates/obscura-cdp/src/domains/storage.rs
+++ b/crates/obscura-cdp/src/domains/storage.rs
@@ -40,6 +40,10 @@ pub async fn handle(
}
Ok(json!({}))
}
+ "clearCookies" => {
+ cookie_jar_for(ctx, params, session_id)?.clear();
+ Ok(json!({}))
+ }
"deleteCookies" => {
if let Some(filter) = parse_delete_cookies_params(params) {
cookie_jar_for(ctx, params, session_id)?.delete_cookies_filtered(
@@ -53,3 +57,68 @@ pub async fn handle(
_ => Ok(json!({})),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use obscura_net::CookieInfo;
+
+ fn sample_cookie(value: &str) -> CookieInfo {
+ CookieInfo {
+ name: "sid".to_string(),
+ value: value.to_string(),
+ domain: "example.com".to_string(),
+ path: "/".to_string(),
+ secure: false,
+ http_only: false,
+ same_site: String::new(),
+ expires: None,
+ }
+ }
+
+ #[tokio::test]
+ async fn clear_cookies_is_scoped_to_browser_context() {
+ let mut ctx = CdpContext::new();
+ let browser_context_id = ctx.create_browser_context();
+ ctx.browser_context(&browser_context_id)
+ .unwrap()
+ .cookie_jar
+ .set_cookies_from_cdp(vec![sample_cookie("isolated")]);
+ ctx.default_context
+ .cookie_jar
+ .set_cookies_from_cdp(vec![sample_cookie("default")]);
+
+ handle(
+ "clearCookies",
+ &json!({ "browserContextId": browser_context_id }),
+ &mut ctx,
+ &None,
+ )
+ .await
+ .unwrap();
+
+ assert!(ctx
+ .browser_context(&browser_context_id)
+ .unwrap()
+ .cookie_jar
+ .get_all_cookies()
+ .is_empty());
+ let default = ctx.default_context.cookie_jar.get_all_cookies();
+ assert_eq!(default.len(), 1);
+ assert_eq!(default[0].value, "default");
+ }
+
+ #[tokio::test]
+ async fn clear_cookies_without_context_clears_default_context() {
+ let mut ctx = CdpContext::new();
+ ctx.default_context
+ .cookie_jar
+ .set_cookies_from_cdp(vec![sample_cookie("default")]);
+
+ handle("clearCookies", &json!({}), &mut ctx, &None)
+ .await
+ .unwrap();
+
+ assert!(ctx.default_context.cookie_jar.get_all_cookies().is_empty());
+ }
+}