fix(storage): use first-party keys for popups opened by frames

An iframe-created about:blank popup used a synthetic popup:<hash> site,
which survived adoption and HTTP navigation in a browser-owned Page.
Same-origin Service Worker registrations and first-party storage were
therefore invisible in the new top-level context.

Use the inherited tuple origin to construct its real first-party key,
clearing iframe partition and ancestor metadata while preserving opaque
origin handling. Cover same-origin sharing and seed a genuinely shared
first-party store in the cross-site isolation regression.
This commit is contained in:
ldm0
2026-11-13 00:39:59 +08:00
parent 468ac5b754
commit 22f81b8293
2 changed files with 56 additions and 25 deletions
@@ -49,7 +49,6 @@ use crate::{
},
};
use anyhow::Result;
use moli_crypto::sha256_hex;
use moli_fetch::Request;
use moli_storage_key::{MoliStorageKey, StoragePartitionRelation, site_for_url};
use moli_webapi_declare::{ObjectLiteralDeclaration, WebApiObject};
@@ -1674,7 +1673,7 @@ impl JsContextHost {
if opener_child_handle.is_none() {
return opener_scope;
}
let storage_key = web_storage_key_for_child_about_blank_popup(&opener_scope);
let storage_key = popup_first_party_storage_key(&opener_scope);
let area_key = web_storage_area_key_for_storage_key(&storage_key);
return LightweightPopupStorageScope::new(
opener_scope.origin().to_owned(),
@@ -6207,24 +6206,12 @@ fn trackable_lightweight_popup_window_name(target_name: &str) -> Option<String>
Some(target_name.to_owned())
}
fn web_storage_key_for_child_about_blank_popup(
opener_scope: &LightweightPopupStorageScope,
) -> MoliStorageKey {
let popup_top_level_site = format!(
"popup:{}",
sha256_hex(
opener_scope
.storage_key()
.serialized_storage_key()
.as_bytes()
)
);
MoliStorageKey::new(
opener_scope.origin().to_owned(),
popup_top_level_site,
None,
StoragePartitionRelation::ThirdParty,
)
fn popup_first_party_storage_key(opener_scope: &LightweightPopupStorageScope) -> MoliStorageKey {
// The popup is a new top-level context. Its inherited tuple origin becomes
// its own site, without the iframe's embedding partition or ancestor bit.
let origin =
Url::parse(opener_scope.origin()).expect("a non-opaque popup creator has a URL origin");
MoliStorageKey::first_party_from_url(&origin, None)
}
fn lightweight_popup_initial_base_url(target_url: &Url, creator_base_url: Url) -> Url {
@@ -1503,7 +1503,7 @@ async fn third_party_web_storage_is_partitioned_by_top_level_site() {
}
#[tokio::test]
async fn third_party_about_blank_popup_does_not_reuse_opener_or_first_party_storage_area() {
async fn third_party_about_blank_popup_uses_first_party_storage_and_keeps_opener_partition() {
let (child_origin, server) = spawn_about_blank_popup_storage_child_server().await;
let child_url = format!("{child_origin}/popup-child.html");
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
@@ -1511,6 +1511,14 @@ async fn third_party_about_blank_popup_does_not_reuse_opener_or_first_party_stor
"http://top-about-blank-popup-partition.test/page.html",
&loader,
);
let web_storage = crate::RendererWebStorageHandles::ephemeral();
vm.set_web_storage_handles(&web_storage);
let mut child_first_party_vm =
new_storage_test_vm_with_loader(&format!("{child_origin}/first-party.html"), &loader);
child_first_party_vm.set_web_storage_handles(&web_storage);
child_first_party_vm
.eval("localStorage.setItem('popup-scope', 'existing-first-party')")
.expect("seed the actual shared first-party storage area");
let child_url_literal = serde_json::to_string(&child_url).expect("child url should serialize");
vm.eval(&format!(
@@ -1543,7 +1551,7 @@ async fn third_party_about_blank_popup_does_not_reuse_opener_or_first_party_stor
assert_eq!(
vm.eval("globalThis.__aboutBlankPopupStorageMessage || 'missing'")
.expect("about:blank popup message should evaluate"),
r#"{"popupBefore":null,"popupAfter":"popup-first-party","childAfter":"child-partition","opener":true}"#
r#"{"popupBefore":"existing-first-party","popupAfter":"popup-first-party","childAfter":"child-partition","opener":true}"#
);
assert_eq!(
vm.eval("localStorage.getItem('popup-scope')")
@@ -1551,13 +1559,11 @@ async fn third_party_about_blank_popup_does_not_reuse_opener_or_first_party_stor
"top"
);
let mut child_first_party_vm =
new_storage_test_vm_with_loader(&format!("{child_origin}/first-party.html"), &loader);
assert_eq!(
child_first_party_vm
.eval("localStorage.getItem('popup-scope')")
.expect("child first-party localStorage should evaluate"),
"null"
"popup-first-party"
);
let requests = server.await.expect("popup child server should finish");
@@ -1569,6 +1575,44 @@ async fn third_party_about_blank_popup_does_not_reuse_opener_or_first_party_stor
);
}
#[tokio::test]
async fn same_origin_iframe_about_blank_popup_shares_first_party_local_storage() {
let (child_origin, server) = spawn_about_blank_popup_storage_child_server().await;
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
let mut vm = new_storage_page_task_executor_test_vm_with_loader(
&format!("{child_origin}/top.html"),
&loader,
);
let child_url = serde_json::to_string(&format!("{child_origin}/popup-child.html")).unwrap();
vm.eval(&format!(
r#"
globalThis.popupStorageResult = null;
addEventListener('message', event => {{ popupStorageResult = String(event.data); }});
const frame = document.createElement('iframe');
frame.src = {child_url};
(document.body || document.documentElement || document).appendChild(frame);
"#
))
.unwrap();
advance_page_task_executor_until_eval_equals(
&mut vm,
&loader,
"String(globalThis.popupStorageResult !== null)",
"true",
"same-origin iframe popup storage result",
)
.await;
assert_eq!(
vm.eval("popupStorageResult").unwrap(),
r#"{"popupBefore":"child-partition","popupAfter":"popup-first-party","childAfter":"popup-first-party","opener":true}"#
);
assert_eq!(
vm.eval("localStorage.getItem('popup-scope')").unwrap(),
"popup-first-party"
);
assert_eq!(server.await.unwrap().len(), 1);
}
#[tokio::test]
async fn document_domain_exact_self_assignment_keeps_storage_event_delivery() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");