mirror of
https://github.com/lexmount/moli.git
synced 2026-11-18 00:09:07 +00:00
fix(network): scope cookie deletion and validate partition keys
This commit is contained in:
@@ -17,12 +17,13 @@ use url::Url;
|
||||
use super::model::{
|
||||
BrowserCookieFacadeContext, CookieSource, NetworkCookieRequestContext,
|
||||
NetworkCookieRequestType, NetworkSameSiteContext, NetworkSameSiteHttpMethod,
|
||||
NetworkSameSiteRedirectType, StoredCookie, StoredCookieQueryReport, StoredCookieSameSite,
|
||||
StoredCookieSetRejectionReason, StoredCookieSetReport, StoredCookieSetStatus,
|
||||
StoredCookieSourceScheme, core_browser_site_context_from_facade,
|
||||
core_cookie_partition_key_for_url, core_partition_key_from_stored,
|
||||
core_same_site_context_metadata_from_stored, core_source_scheme_from_stored,
|
||||
has_invalid_cookie_octets, stored_query_report_from_core, stored_set_report_from_core,
|
||||
NetworkSameSiteRedirectType, StoredCookie, StoredCookiePartitionFilter,
|
||||
StoredCookieQueryReport, StoredCookieSameSite, StoredCookieSetRejectionReason,
|
||||
StoredCookieSetReport, StoredCookieSetStatus, StoredCookieSourceScheme,
|
||||
core_browser_site_context_from_facade, core_cookie_partition_key_for_url,
|
||||
core_partition_key_from_stored, core_same_site_context_metadata_from_stored,
|
||||
core_source_scheme_from_stored, has_invalid_cookie_octets, stored_query_report_from_core,
|
||||
stored_set_report_from_core,
|
||||
};
|
||||
|
||||
/// Thread-shareable cookie store used by browser/runtime components.
|
||||
@@ -383,15 +384,41 @@ impl BrowserCookieStore {
|
||||
path: Option<&str>,
|
||||
url_host: Option<&str>,
|
||||
partition_key: Option<&super::model::StoredCookiePartitionKey>,
|
||||
) -> usize {
|
||||
let partition_filter = partition_key.cloned().map_or(
|
||||
StoredCookiePartitionFilter::All,
|
||||
StoredCookiePartitionFilter::Partition,
|
||||
);
|
||||
self.delete_cookies_with_partition_filter(name, domain, path, url_host, &partition_filter)
|
||||
}
|
||||
|
||||
/// Deletes matching cookies in all partitions, only the unpartitioned
|
||||
/// store, or one exact CHIPS partition.
|
||||
pub fn delete_cookies_with_partition_filter(
|
||||
&mut self,
|
||||
name: Option<&str>,
|
||||
domain: Option<&str>,
|
||||
path: Option<&str>,
|
||||
url_host: Option<&str>,
|
||||
partition_filter: &StoredCookiePartitionFilter,
|
||||
) -> usize {
|
||||
self.purge_expired();
|
||||
let partition_key = partition_key.map(core_partition_key_from_stored);
|
||||
let partition_key = match partition_filter {
|
||||
StoredCookiePartitionFilter::Partition(key) => {
|
||||
Some(core_partition_key_from_stored(key))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let removed = self.full_core.delete_matching(&CookieDeleteFilter {
|
||||
name,
|
||||
domain,
|
||||
path,
|
||||
url_host,
|
||||
partition_key: partition_key.as_ref(),
|
||||
unpartitioned_only: matches!(
|
||||
partition_filter,
|
||||
StoredCookiePartitionFilter::Unpartitioned
|
||||
),
|
||||
});
|
||||
if removed > 0 {
|
||||
self.bump_document_cookie_generation();
|
||||
|
||||
@@ -15,10 +15,10 @@ pub use model::{
|
||||
CookieSiteDataScope, CookieSiteDataSummary, CookieSource, CookieStorageClearReport,
|
||||
CookieStorageClearTarget, CookieStorageStateDiff, CookieStorageStateSnapshot,
|
||||
NetworkCookieRequestContext, NetworkStorageAccessStatus, StoredCookie,
|
||||
StoredCookieEffectiveSameSite, StoredCookiePartitionKey, StoredCookieSameSite,
|
||||
StoredCookieSetRejectionReason, StoredCookieSetReport, StoredCookieSetStatus,
|
||||
StoredCookieSetWarningReason, StoredCookieSourceScheme, advance_cookie_request_context,
|
||||
redirect_types_for_request, site_context_downgrade_type,
|
||||
StoredCookieEffectiveSameSite, StoredCookiePartitionFilter, StoredCookiePartitionKey,
|
||||
StoredCookieSameSite, StoredCookieSetRejectionReason, StoredCookieSetReport,
|
||||
StoredCookieSetStatus, StoredCookieSetWarningReason, StoredCookieSourceScheme,
|
||||
advance_cookie_request_context, redirect_types_for_request, site_context_downgrade_type,
|
||||
};
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
pub use model::{
|
||||
|
||||
@@ -39,7 +39,8 @@ pub use site_data::{
|
||||
CookieStorageClearPreviewReport,
|
||||
};
|
||||
pub use stored_cookie::{
|
||||
StoredCookie, StoredCookiePartitionKey, StoredCookieSameSite, StoredCookieSourceScheme,
|
||||
StoredCookie, StoredCookiePartitionFilter, StoredCookiePartitionKey, StoredCookieSameSite,
|
||||
StoredCookieSourceScheme,
|
||||
};
|
||||
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
|
||||
@@ -20,6 +20,14 @@ pub enum StoredCookiePartitionKey {
|
||||
},
|
||||
}
|
||||
|
||||
/// Partition scope for cookie deletion, independent of the calling protocol.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum StoredCookiePartitionFilter {
|
||||
All,
|
||||
Unpartitioned,
|
||||
Partition(StoredCookiePartitionKey),
|
||||
}
|
||||
|
||||
impl StoredCookiePartitionKey {
|
||||
pub fn site(top_level_site: String, has_cross_site_ancestor: bool) -> Self {
|
||||
Self::Site {
|
||||
|
||||
@@ -5,9 +5,9 @@ use super::test_support::{
|
||||
use super::{
|
||||
CookiePriority, CookieSource, NetworkCookieRequestContext, NetworkSiteContextMetadata,
|
||||
NetworkStorageAccessStatus, StoredCookie, StoredCookieAccessSemantics,
|
||||
StoredCookieEffectiveSameSite, StoredCookieExclusionReason, StoredCookiePartitionKey,
|
||||
StoredCookieQueryReport, StoredCookieRequestSameSiteContext, StoredCookieSameSite,
|
||||
StoredCookieSameSiteContextDowngradeType, StoredCookieSameSiteHttpMethod,
|
||||
StoredCookieEffectiveSameSite, StoredCookieExclusionReason, StoredCookiePartitionFilter,
|
||||
StoredCookiePartitionKey, StoredCookieQueryReport, StoredCookieRequestSameSiteContext,
|
||||
StoredCookieSameSite, StoredCookieSameSiteContextDowngradeType, StoredCookieSameSiteHttpMethod,
|
||||
StoredCookieSameSiteRedirectType, StoredCookieScopeSemantics, StoredCookieSetRejectionReason,
|
||||
StoredCookieSetWarningReason, StoredCookieSiteContextBasis, StoredCookieSourceScheme,
|
||||
StoredCookieStorageAccessStatus, StoredCookieWarningReason, advance_cookie_request_context,
|
||||
@@ -4345,7 +4345,7 @@ fn third_party_response_cookie_is_scoped_to_top_level_site() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_cookies_with_partition_key_keeps_other_top_level_sites() {
|
||||
fn cookie_deletion_distinguishes_partition_scopes() {
|
||||
let mut store = BrowserCookieStore::default();
|
||||
let response_url = parse("https://widget.example/resource");
|
||||
let top_a = parse("https://first.example/page");
|
||||
@@ -4364,6 +4364,13 @@ fn delete_cookies_with_partition_key_keeps_other_top_level_sites() {
|
||||
context,
|
||||
);
|
||||
}
|
||||
store.store_response_headers(
|
||||
&response_url,
|
||||
&[(
|
||||
"set-cookie".to_owned(),
|
||||
b"chip=global; Secure; Path=/".to_vec(),
|
||||
)],
|
||||
);
|
||||
|
||||
let removed = store.delete_cookies_with_partition_key(
|
||||
Some("chip"),
|
||||
@@ -4379,12 +4386,32 @@ fn delete_cookies_with_partition_key_keeps_other_top_level_sites() {
|
||||
assert_eq!(removed, 1);
|
||||
assert_eq!(
|
||||
store.cookie_header_for_request(&response_url, context_a),
|
||||
None
|
||||
Some("chip=global".to_owned())
|
||||
);
|
||||
assert_eq!(
|
||||
store.cookie_header_for_request(&response_url, context_b),
|
||||
Some("chip=two".to_owned())
|
||||
Some("chip=two; chip=global".to_owned())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
store.delete_cookies_with_partition_filter(
|
||||
Some("chip"),
|
||||
Some("widget.example"),
|
||||
Some("/"),
|
||||
None,
|
||||
&StoredCookiePartitionFilter::Unpartitioned,
|
||||
),
|
||||
1
|
||||
);
|
||||
assert_eq!(store.cookies().len(), 1);
|
||||
assert_eq!(store.cookies()[0].value, "two");
|
||||
|
||||
// The existing API keeps its all-partitions meaning for None.
|
||||
assert_eq!(
|
||||
store.delete_cookies_with_partition_key(Some("chip"), None, None, None, None),
|
||||
1
|
||||
);
|
||||
assert!(store.cookies().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -93,6 +93,10 @@ pub(super) fn cookie_matches_delete_filter(
|
||||
return false;
|
||||
}
|
||||
|
||||
if filter.unpartitioned_only && cookie.partition_key().is_some() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if filter
|
||||
.partition_key
|
||||
.is_some_and(|wanted| cookie.partition_key() != Some(wanted))
|
||||
|
||||
@@ -70,6 +70,9 @@ pub struct CookieDeleteFilter<'a> {
|
||||
/// When present, delete only cookies in this exact partition. `None` keeps
|
||||
/// the compatibility behavior of deleting all matching partitions.
|
||||
pub partition_key: Option<&'a crate::CookiePartitionKey>,
|
||||
/// Exclude partitioned cookies. Defaults to false so an omitted partition
|
||||
/// key continues to match all partitions unless explicitly constrained.
|
||||
pub unpartitioned_only: bool,
|
||||
}
|
||||
|
||||
/// Browser-oriented cookie store limits.
|
||||
|
||||
@@ -24,6 +24,7 @@ fn delete_matching_removes_matching_domain_cookie_but_not_host_only_cookie() {
|
||||
path: None,
|
||||
url_host: Some("deep.sub.example.com"),
|
||||
partition_key: None,
|
||||
unpartitioned_only: false,
|
||||
});
|
||||
|
||||
assert_eq!(removed, 1);
|
||||
@@ -55,6 +56,7 @@ fn delete_matching_url_host_filter_respects_host_only_semantics() {
|
||||
path: None,
|
||||
url_host: Some("deep.sub.example.com"),
|
||||
partition_key: None,
|
||||
unpartitioned_only: false,
|
||||
});
|
||||
|
||||
assert_eq!(removed, 1);
|
||||
|
||||
@@ -544,6 +544,7 @@ fn partitioned_cookies_coexist_and_query_by_exact_partition_key() {
|
||||
path: Some("/foo"),
|
||||
url_host: None,
|
||||
partition_key: Some(&key_a),
|
||||
unpartitioned_only: false,
|
||||
}),
|
||||
1
|
||||
);
|
||||
|
||||
@@ -93,7 +93,7 @@ pub(crate) fn bidi_delete_cookies_command(
|
||||
url: None,
|
||||
domain: filter.as_ref().and_then(|filter| filter.domain.clone()),
|
||||
path: filter.as_ref().and_then(|filter| filter.path.clone()),
|
||||
partition_key: None,
|
||||
partition_filter: moli_protocol::automation::StoredCookiePartitionFilter::All,
|
||||
filter,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ pub fn delete_all_cookies_command(
|
||||
url: Some(current_url.into()),
|
||||
domain: None,
|
||||
path: None,
|
||||
partition_key: None,
|
||||
partition_filter: moli_protocol::automation::StoredCookiePartitionFilter::All,
|
||||
filter: None,
|
||||
})
|
||||
}
|
||||
@@ -97,7 +97,7 @@ pub fn delete_cookie_command(
|
||||
url: Some(current_url.into()),
|
||||
domain: None,
|
||||
path: None,
|
||||
partition_key: None,
|
||||
partition_filter: moli_protocol::automation::StoredCookiePartitionFilter::All,
|
||||
filter: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
|
||||
use std::{fmt, str::FromStr, sync::Arc};
|
||||
|
||||
use moli_cookie_jar::{StoredCookiePartitionKey, StoredCookieQueryReport};
|
||||
pub use moli_cookie_jar::StoredCookiePartitionFilter;
|
||||
use moli_cookie_jar::StoredCookieQueryReport;
|
||||
use moli_core::page::{
|
||||
DomScrollIntoViewRect, SelectedFile, SubresourceResourceType, is_renderer_backend_node_id,
|
||||
};
|
||||
@@ -822,7 +823,7 @@ pub struct DevToolsDeleteCookiesCommand {
|
||||
pub url: Option<String>,
|
||||
pub domain: Option<String>,
|
||||
pub path: Option<String>,
|
||||
pub partition_key: Option<StoredCookiePartitionKey>,
|
||||
pub partition_filter: StoredCookiePartitionFilter,
|
||||
pub filter: Option<DevToolsCookieFilter>,
|
||||
}
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ async fn automation_command_executes_storage_cookie_commands_for_webdriver_conte
|
||||
url: Some(cookie_url.clone()),
|
||||
domain: None,
|
||||
path: None,
|
||||
partition_key: None,
|
||||
partition_filter: moli_cookie_jar::StoredCookiePartitionFilter::All,
|
||||
filter: None,
|
||||
},
|
||||
))
|
||||
|
||||
@@ -1499,19 +1499,31 @@ impl BrowserContext {
|
||||
path: Option<&str>,
|
||||
url_host: Option<&str>,
|
||||
) {
|
||||
self.delete_cookies_with_partition_key(name, domain, path, url_host, None);
|
||||
self.delete_cookies_with_partition_filter(
|
||||
name,
|
||||
domain,
|
||||
path,
|
||||
url_host,
|
||||
&moli_cookie_jar::StoredCookiePartitionFilter::All,
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn delete_cookies_with_partition_key(
|
||||
pub(crate) fn delete_cookies_with_partition_filter(
|
||||
&mut self,
|
||||
name: Option<&str>,
|
||||
domain: Option<&str>,
|
||||
path: Option<&str>,
|
||||
url_host: Option<&str>,
|
||||
partition_key: Option<&moli_cookie_jar::StoredCookiePartitionKey>,
|
||||
partition_filter: &moli_cookie_jar::StoredCookiePartitionFilter,
|
||||
) {
|
||||
let mut cookie_store = self.storage_partition.cookie_store().lock();
|
||||
cookie_store.delete_cookies_with_partition_key(name, domain, path, url_host, partition_key);
|
||||
cookie_store.delete_cookies_with_partition_filter(
|
||||
name,
|
||||
domain,
|
||||
path,
|
||||
url_host,
|
||||
partition_filter,
|
||||
);
|
||||
}
|
||||
|
||||
pub(crate) fn active_target_id(&self) -> Option<&str> {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use moli_cookie_jar::StoredCookiePartitionFilter;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
use url::Url;
|
||||
@@ -8,7 +9,8 @@ use crate::automation::{
|
||||
DevToolsGetCookiesCommand, DevToolsSetCookiesCommand,
|
||||
};
|
||||
use crate::domains::storage::{
|
||||
StorageCommandTaskStep, normalize_partition_key, start_devtools_storage_command,
|
||||
PartitionKeyError, StorageCommandTaskStep, normalize_partition_key,
|
||||
start_devtools_storage_command,
|
||||
};
|
||||
|
||||
pub(super) fn start_get_cookies_command(
|
||||
@@ -99,7 +101,7 @@ pub(super) fn start_clear_browser_cookies_command(
|
||||
url: None,
|
||||
domain: None,
|
||||
path: None,
|
||||
partition_key: None,
|
||||
partition_filter: StoredCookiePartitionFilter::All,
|
||||
filter: None,
|
||||
};
|
||||
start_devtools_storage_command(conn, cmd.id, AutomationCommand::DeleteCookies(command))
|
||||
@@ -117,8 +119,16 @@ fn build_cdp_network_delete_cookies_command(
|
||||
// nothing, since no stored cookie can be in that partition. Puppeteer relies
|
||||
// on this: page.setCookie()/deleteCookie() send a malformed topLevelSite
|
||||
// (`https//example.com`) and fail outright if the command is rejected.
|
||||
let Ok(partition_key) = normalize_partition_key(params.partition_key.as_ref(), false) else {
|
||||
return Err(CommandOutputPlan::success());
|
||||
// Read the raw field so an explicit null remains a structural error rather
|
||||
// than being deserialized as an omitted Option.
|
||||
let partition_key = cmd.params.and_then(|params| params.get("partitionKey"));
|
||||
let partition_filter = match normalize_partition_key(partition_key, false) {
|
||||
Ok(Some(key)) => StoredCookiePartitionFilter::Partition(key),
|
||||
Ok(None) => StoredCookiePartitionFilter::Unpartitioned,
|
||||
Err(PartitionKeyError::InvalidSite) => return Err(CommandOutputPlan::success()),
|
||||
Err(PartitionKeyError::InvalidStructure) => {
|
||||
return Err(CommandOutputPlan::error(-32602, "InvalidParams"));
|
||||
}
|
||||
};
|
||||
let (_, target_id) = conn
|
||||
.target_owner_identity_for_session(cmd.session_id)
|
||||
@@ -134,7 +144,7 @@ fn build_cdp_network_delete_cookies_command(
|
||||
url: params.url,
|
||||
domain: params.domain,
|
||||
path: params.path,
|
||||
partition_key,
|
||||
partition_filter,
|
||||
filter: None,
|
||||
})
|
||||
}
|
||||
@@ -335,11 +345,13 @@ mod protocol_neutral_tests {
|
||||
assert_eq!(command.domain.as_deref(), Some("example.com"));
|
||||
assert_eq!(command.path.as_deref(), Some("/"));
|
||||
assert_eq!(
|
||||
command.partition_key,
|
||||
Some(moli_cookie_jar::StoredCookiePartitionKey::site(
|
||||
"https://top.example".to_owned(),
|
||||
true,
|
||||
))
|
||||
command.partition_filter,
|
||||
moli_cookie_jar::StoredCookiePartitionFilter::Partition(
|
||||
moli_cookie_jar::StoredCookiePartitionKey::site(
|
||||
"https://top.example".to_owned(),
|
||||
true,
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -398,6 +410,55 @@ mod protocol_neutral_tests {
|
||||
assert_eq!(out, vec![json!({ "id": 149, "result": {} })]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cdp_network_delete_cookies_rejects_invalid_partition_key_structure() {
|
||||
for partition_key in [
|
||||
Value::Null,
|
||||
json!("https://example.com"),
|
||||
json!(42),
|
||||
json!(false),
|
||||
json!([]),
|
||||
json!({}),
|
||||
json!({ "topLevelSite": "https://example.com" }),
|
||||
json!({ "hasCrossSiteAncestor": false }),
|
||||
json!({ "topLevelSite": 42, "hasCrossSiteAncestor": false }),
|
||||
json!({ "topLevelSite": null, "hasCrossSiteAncestor": false }),
|
||||
json!({ "topLevelSite": "https://example.com", "hasCrossSiteAncestor": "false" }),
|
||||
json!({ "topLevelSite": "https://example.com", "hasCrossSiteAncestor": null }),
|
||||
// Structural errors take priority even when the site cannot be parsed.
|
||||
json!({ "topLevelSite": "https//example.com" }),
|
||||
json!({ "topLevelSite": "https//example.com", "hasCrossSiteAncestor": 0 }),
|
||||
] {
|
||||
let mut conn = CdpConnection::new();
|
||||
let params = json!({
|
||||
"name": "sid",
|
||||
"url": "https://example.com/",
|
||||
"partitionKey": partition_key,
|
||||
});
|
||||
let cmd = Cmd::for_test(
|
||||
Some(150),
|
||||
"Network.deleteCookies",
|
||||
¶ms,
|
||||
Some("SID-network"),
|
||||
r#"{"id":150,"method":"Network.deleteCookies"}"#,
|
||||
);
|
||||
let StorageCommandTaskStep::Complete(plan) =
|
||||
start_delete_cookies_command(&mut conn, &cmd)
|
||||
else {
|
||||
panic!("invalid partitionKey structure must fail before storage dispatch");
|
||||
};
|
||||
let mut out = Vec::new();
|
||||
plan.emit_into(&mut out, cmd.id, cmd.session_id);
|
||||
assert_eq!(out[0]["error"]["code"], json!(-32602), "{params}");
|
||||
assert_eq!(
|
||||
out[0]["error"]["message"],
|
||||
json!("InvalidParams"),
|
||||
"{params}"
|
||||
);
|
||||
assert_eq!(out[0]["sessionId"], json!("SID-network"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn network_clear_browser_cookies_routes_to_shared_storage_entry() {
|
||||
let mut conn = CdpConnection::new();
|
||||
|
||||
@@ -1,5 +1,131 @@
|
||||
use super::*;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn cookie_deletion_preserves_the_requested_partition_scope() {
|
||||
for (method, params, mut expected_values) in [
|
||||
(
|
||||
"Network.deleteCookies",
|
||||
json!({ "name": "sid", "url": "https://example.com/" }),
|
||||
vec!["cross-site", "other-site", "same-site", "unrelated"],
|
||||
),
|
||||
(
|
||||
"Network.deleteCookies",
|
||||
json!({ "name": "sid", "domain": "example.com" }),
|
||||
vec!["cross-site", "other-site", "same-site", "unrelated"],
|
||||
),
|
||||
(
|
||||
"Network.deleteCookies",
|
||||
json!({
|
||||
"name": "sid", "url": "https://example.com/",
|
||||
"partitionKey": {
|
||||
"topLevelSite": "https://site-a.com", "hasCrossSiteAncestor": true
|
||||
}
|
||||
}),
|
||||
vec!["other-site", "same-site", "unpartitioned", "unrelated"],
|
||||
),
|
||||
(
|
||||
"Network.deleteCookies",
|
||||
json!({
|
||||
"name": "sid", "url": "https://example.com/",
|
||||
"partitionKey": {
|
||||
"topLevelSite": "https://site-a.com", "hasCrossSiteAncestor": false
|
||||
}
|
||||
}),
|
||||
vec!["cross-site", "other-site", "unpartitioned", "unrelated"],
|
||||
),
|
||||
(
|
||||
"Network.deleteCookies",
|
||||
json!({
|
||||
"name": "sid", "url": "https://example.com/",
|
||||
"partitionKey": {
|
||||
"topLevelSite": "https//site-a.com", "hasCrossSiteAncestor": true
|
||||
}
|
||||
}),
|
||||
vec![
|
||||
"cross-site",
|
||||
"other-site",
|
||||
"same-site",
|
||||
"unpartitioned",
|
||||
"unrelated",
|
||||
],
|
||||
),
|
||||
(
|
||||
"Network.deleteCookies",
|
||||
json!({
|
||||
"name": "sid", "url": "https://example.com/",
|
||||
"partitionKey": {
|
||||
"topLevelSite": "https://missing.com", "hasCrossSiteAncestor": true
|
||||
}
|
||||
}),
|
||||
vec![
|
||||
"cross-site",
|
||||
"other-site",
|
||||
"same-site",
|
||||
"unpartitioned",
|
||||
"unrelated",
|
||||
],
|
||||
),
|
||||
(
|
||||
"Storage.deleteCookies",
|
||||
json!({ "name": "sid", "domain": "example.com" }),
|
||||
vec!["unrelated"],
|
||||
),
|
||||
("Network.clearBrowserCookies", json!({}), vec![]),
|
||||
("Storage.clearCookies", json!({}), vec![]),
|
||||
] {
|
||||
let mut ctx = TestContext::new();
|
||||
ctx.conn.browser_context = Some(BrowserContext::new("BID-cookie-partitions".into()));
|
||||
ctx.process_async(json!({
|
||||
"id": 1,
|
||||
"method": "Network.setCookies",
|
||||
"params": { "cookies": [
|
||||
{ "name": "sid", "value": "unpartitioned", "url": "https://example.com/" },
|
||||
{
|
||||
"name": "sid", "value": "same-site", "url": "https://example.com/",
|
||||
"secure": true, "sameSite": "None",
|
||||
"partitionKey": {
|
||||
"topLevelSite": "https://site-a.com", "hasCrossSiteAncestor": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "sid", "value": "cross-site", "url": "https://example.com/",
|
||||
"secure": true, "sameSite": "None",
|
||||
"partitionKey": {
|
||||
"topLevelSite": "https://site-a.com", "hasCrossSiteAncestor": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "sid", "value": "other-site", "url": "https://example.com/",
|
||||
"secure": true, "sameSite": "None",
|
||||
"partitionKey": {
|
||||
"topLevelSite": "https://site-b.com", "hasCrossSiteAncestor": true
|
||||
}
|
||||
},
|
||||
{ "name": "other", "value": "unrelated", "url": "https://example.com/" }
|
||||
] }
|
||||
}))
|
||||
.await;
|
||||
ctx.expect_result(1, json!({ "success": true }), None);
|
||||
|
||||
ctx.process_async(json!({ "id": 2, "method": method, "params": params }))
|
||||
.await;
|
||||
ctx.expect_result(2, json!({}), None);
|
||||
|
||||
ctx.process_async(json!({ "id": 3, "method": "Network.getAllCookies" }))
|
||||
.await;
|
||||
let response = ctx.take_response_by_id(3);
|
||||
let mut values = response["result"]["cookies"]
|
||||
.as_array()
|
||||
.expect("all cookies")
|
||||
.iter()
|
||||
.map(|cookie| cookie["value"].as_str().expect("cookie value"))
|
||||
.collect::<Vec<_>>();
|
||||
values.sort_unstable();
|
||||
expected_values.sort_unstable();
|
||||
assert_eq!(values, expected_values, "{method}: {params}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Network.getCookies returns an empty list.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn get_cookies_returns_empty() {
|
||||
|
||||
@@ -5,7 +5,7 @@ mod reports;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
pub(crate) use normalize::normalize_partition_key;
|
||||
pub(crate) use normalize::{PartitionKeyError, normalize_partition_key};
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use ops::set_cookies_for_browser_context;
|
||||
|
||||
@@ -76,8 +76,8 @@ pub(super) fn normalize_cookie_param(
|
||||
param.partition_key_opaque.unwrap_or(false),
|
||||
) {
|
||||
Ok(partition_key) => partition_key,
|
||||
Err(reason) => {
|
||||
rejection_reasons.push(reason);
|
||||
Err(_) => {
|
||||
rejection_reasons.push(StoredCookieSetRejectionReason::InvalidPartitionKey);
|
||||
None
|
||||
}
|
||||
};
|
||||
@@ -194,12 +194,17 @@ pub(super) fn normalize_cookie_param(
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) enum PartitionKeyError {
|
||||
InvalidStructure,
|
||||
InvalidSite,
|
||||
}
|
||||
|
||||
pub(crate) fn normalize_partition_key(
|
||||
value: Option<&serde_json::Value>,
|
||||
partition_key_opaque: bool,
|
||||
) -> Result<Option<StoredCookiePartitionKey>, StoredCookieSetRejectionReason> {
|
||||
) -> Result<Option<StoredCookiePartitionKey>, PartitionKeyError> {
|
||||
if partition_key_opaque {
|
||||
return Err(StoredCookieSetRejectionReason::InvalidPartitionKey);
|
||||
return Err(PartitionKeyError::InvalidSite);
|
||||
}
|
||||
let Some(value) = value else {
|
||||
return Ok(None);
|
||||
@@ -207,15 +212,14 @@ pub(crate) fn normalize_partition_key(
|
||||
let top_level_site = value
|
||||
.get("topLevelSite")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.ok_or(StoredCookieSetRejectionReason::InvalidPartitionKey)?;
|
||||
.ok_or(PartitionKeyError::InvalidStructure)?;
|
||||
let has_cross_site_ancestor = value
|
||||
.get("hasCrossSiteAncestor")
|
||||
.and_then(serde_json::Value::as_bool)
|
||||
.ok_or(StoredCookieSetRejectionReason::InvalidPartitionKey)?;
|
||||
let url = Url::parse(top_level_site)
|
||||
.map_err(|_| StoredCookieSetRejectionReason::InvalidPartitionKey)?;
|
||||
.ok_or(PartitionKeyError::InvalidStructure)?;
|
||||
let url = Url::parse(top_level_site).map_err(|_| PartitionKeyError::InvalidSite)?;
|
||||
if !matches!(url.scheme(), "http" | "https") || url.host_str().is_none() {
|
||||
return Err(StoredCookieSetRejectionReason::InvalidPartitionKey);
|
||||
return Err(PartitionKeyError::InvalidSite);
|
||||
}
|
||||
Ok(Some(StoredCookiePartitionKey::site(
|
||||
moli_storage_key::site_for_url(&url),
|
||||
|
||||
@@ -221,7 +221,7 @@ pub(crate) fn build_cdp_storage_clear_cookies_command(
|
||||
url: None,
|
||||
domain: None,
|
||||
path: None,
|
||||
partition_key: None,
|
||||
partition_filter: moli_cookie_jar::StoredCookiePartitionFilter::All,
|
||||
filter: None,
|
||||
})
|
||||
}
|
||||
@@ -280,7 +280,10 @@ pub(crate) fn build_cdp_storage_delete_cookies_command(
|
||||
url: params.url,
|
||||
domain: params.domain,
|
||||
path: params.path,
|
||||
partition_key,
|
||||
partition_filter: partition_key.map_or(
|
||||
moli_cookie_jar::StoredCookiePartitionFilter::All,
|
||||
moli_cookie_jar::StoredCookiePartitionFilter::Partition,
|
||||
),
|
||||
filter: None,
|
||||
})
|
||||
}
|
||||
@@ -1097,7 +1100,7 @@ pub(crate) fn complete_devtools_delete_cookies_result(
|
||||
command.filter.as_ref(),
|
||||
)
|
||||
} else {
|
||||
delete_cookies_for_browser_context_with_normalized_partition_key(
|
||||
delete_cookies_for_browser_context_with_partition_filter(
|
||||
conn,
|
||||
DeleteCookiesParams {
|
||||
name: command.name,
|
||||
@@ -1107,7 +1110,7 @@ pub(crate) fn complete_devtools_delete_cookies_result(
|
||||
partition_key: None,
|
||||
browser_context_id,
|
||||
},
|
||||
command.partition_key,
|
||||
command.partition_filter,
|
||||
)
|
||||
};
|
||||
match result {
|
||||
@@ -1415,13 +1418,17 @@ pub(crate) fn delete_cookies_for_browser_context(
|
||||
) -> Result<(), (i32, &'static str)> {
|
||||
let partition_key = normalize_partition_key(params.partition_key.as_ref(), false)
|
||||
.map_err(|_| (-32602, "InvalidParams"))?;
|
||||
delete_cookies_for_browser_context_with_normalized_partition_key(conn, params, partition_key)
|
||||
let partition_filter = partition_key.map_or(
|
||||
moli_cookie_jar::StoredCookiePartitionFilter::All,
|
||||
moli_cookie_jar::StoredCookiePartitionFilter::Partition,
|
||||
);
|
||||
delete_cookies_for_browser_context_with_partition_filter(conn, params, partition_filter)
|
||||
}
|
||||
|
||||
fn delete_cookies_for_browser_context_with_normalized_partition_key(
|
||||
fn delete_cookies_for_browser_context_with_partition_filter(
|
||||
conn: &mut CdpConnection,
|
||||
params: DeleteCookiesParams,
|
||||
partition_key: Option<moli_cookie_jar::StoredCookiePartitionKey>,
|
||||
partition_filter: moli_cookie_jar::StoredCookiePartitionFilter,
|
||||
) -> Result<(), (i32, &'static str)> {
|
||||
let bc = match params.browser_context_id.as_deref() {
|
||||
Some(wanted_id) => conn
|
||||
@@ -1443,12 +1450,12 @@ fn delete_cookies_for_browser_context_with_normalized_partition_key(
|
||||
.and_then(|parsed| parsed.host_str().map(str::to_ascii_lowercase))
|
||||
});
|
||||
|
||||
bc.delete_cookies_with_partition_key(
|
||||
bc.delete_cookies_with_partition_filter(
|
||||
params.name.as_deref(),
|
||||
normalized_domain.as_deref(),
|
||||
params.path.as_deref(),
|
||||
normalized_url_host.as_deref(),
|
||||
partition_key.as_ref(),
|
||||
&partition_filter,
|
||||
);
|
||||
|
||||
Ok(())
|
||||
@@ -1575,11 +1582,13 @@ mod protocol_neutral_tests {
|
||||
assert_eq!(command.domain.as_deref(), Some("example.com"));
|
||||
assert_eq!(command.path.as_deref(), Some("/"));
|
||||
assert_eq!(
|
||||
command.partition_key,
|
||||
Some(moli_cookie_jar::StoredCookiePartitionKey::site(
|
||||
"https://top.example".to_owned(),
|
||||
false,
|
||||
))
|
||||
command.partition_filter,
|
||||
moli_cookie_jar::StoredCookiePartitionFilter::Partition(
|
||||
moli_cookie_jar::StoredCookiePartitionKey::site(
|
||||
"https://top.example".to_owned(),
|
||||
false,
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user