fix(storage): reconcile WPT state with SQLite partitions

This commit is contained in:
ldm0
2026-11-22 02:02:07 +08:00
parent 78a40c5760
commit 03214ca7bf
8 changed files with 212 additions and 12 deletions
+2 -2
View File
@@ -35,7 +35,7 @@ impl From<&StoredCookie> for Key {
}
}
enum Change {
Put(StoredCookie),
Put(Box<StoredCookie>),
Delete,
}
#[derive(Default)]
@@ -48,7 +48,7 @@ impl Batch {
match change {
StoredCookieMutation::Upsert { cookie } => Self {
clear: false,
changes: BTreeMap::from([(Key::from(&cookie), Change::Put(cookie))]),
changes: BTreeMap::from([(Key::from(&cookie), Change::Put(Box::new(cookie)))]),
},
StoredCookieMutation::Delete(key) => Self {
clear: false,
+74
View File
@@ -485,3 +485,77 @@ fn version_one_cookie_database_migrates_without_granting_a_new_cookie_age() {
);
owner.close().unwrap();
}
#[test]
fn version_one_cookie_rows_migrate_with_unknown_creation_time() {
let root = tempfile::tempdir().unwrap();
let path = root.path().join("cookies.sqlite");
let connection = Connection::open(&path).unwrap();
connection.execute_batch("CREATE TABLE cookies(
partition_key BLOB NOT NULL,domain TEXT NOT NULL,path TEXT NOT NULL,name TEXT NOT NULL,value TEXT NOT NULL,
host_only INTEGER NOT NULL,secure INTEGER NOT NULL,http_only INTEGER NOT NULL,
expires_seconds INTEGER,expires_nanos INTEGER,same_site INTEGER NOT NULL,priority INTEGER,
source_scheme INTEGER NOT NULL,source_port INTEGER NOT NULL,
creation_index BLOB NOT NULL,last_access_index BLOB NOT NULL,
PRIMARY KEY(partition_key,domain,path,name)) WITHOUT ROWID;").unwrap();
connection
.pragma_update(None, "application_id", codec::APPLICATION_ID)
.unwrap();
connection.pragma_update(None, "user_version", 1).unwrap();
connection.execute("INSERT INTO cookies VALUES (?1,'cookie.test','/','legacy','value',1,1,0,NULL,NULL,0,NULL,2,443,?2,?3)",
params![vec![0_u8],7_u64.to_be_bytes().as_slice(),11_u64.to_be_bytes().as_slice()]).unwrap();
drop(connection);
let mut expected = cookie("legacy");
expected.creation_index = 7;
expected.last_access_index = 11;
let runtime = Runtime::new().unwrap();
for _ in 0..2 {
let (store, owner) =
CookieDatabase::open(path.clone(), OpenMode::Existing, runtime.handle()).unwrap();
assert_eq!(store.lock().unwrap().cookies(), vec![expected.clone()]);
assert_eq!(
owner.owner.client().query(codec::read).unwrap(),
vec![expected.clone()]
);
owner.close().unwrap();
}
let connection = Connection::open(&path).unwrap();
assert_eq!(
connection
.pragma_query_value(None, "user_version", |row| row.get::<_, i32>(0))
.unwrap(),
2
);
let timestamp: Option<i64> = connection
.query_row("SELECT creation_unix_micros FROM cookies", [], |row| {
row.get(0)
})
.unwrap();
assert_eq!(timestamp, None);
}
#[test]
fn invalid_persisted_cookie_creation_time_does_not_become_an_empty_jar() {
let root = tempfile::tempdir().unwrap();
let path = root.path().join("cookies.sqlite");
let runtime = Runtime::new().unwrap();
let (store, owner) = open(root.path(), &runtime);
put(&store, cookie("invalid-age"));
owner.close().unwrap();
drop(owner);
drop(store);
let connection = Connection::open(&path).unwrap();
connection
.execute(
"UPDATE cookies SET creation_unix_micros=?1",
["invalid timestamp"],
)
.unwrap();
drop(connection);
let (store, owner) =
CookieDatabase::deferred(path, OpenMode::Existing, runtime.handle()).unwrap();
let first = store.lock().unwrap_err().to_string();
assert!(first.contains("creation_unix_micros"), "{first}");
assert_eq!(store.lock().unwrap_err().to_string(), first);
owner.close().unwrap();
}
@@ -172,11 +172,8 @@ async fn retained_popup_storage_managers_use_bound_popup_owner() {
let host = vm.context_host_weak_for_test().upgrade().unwrap();
let store = host.borrow().storage_bucket_store();
let service = store.lock().storage_service();
let key = moli_storage_key::MoliStorageKey::first_party_from_url(
&url::Url::parse("https://popup-storage-owner-child.test").unwrap(),
None,
)
.serialized_storage_key();
let key = moli_storage_key::MoliStorageKey::first_party_from_url(&popup_url, None)
.serialized_storage_key();
let locator = moli_storage_service::StorageBucketLocator::default_bucket(key);
let bucket_key = moli_storage_service::StorageService::opfs_bucket_key(&locator).unwrap();
let root = service.ensure_opfs_root(&locator).unwrap();
@@ -38,8 +38,29 @@ async fn drain_storage(
#[tokio::test]
async fn local_storage_events_cross_pages_with_matching_origin_and_partition() {
assert_local_storage_events_cross_pages(crate::RendererWebStorageHandles::ephemeral()).await;
}
#[tokio::test]
async fn sqlite_local_storage_events_cross_pages_with_matching_origin_and_partition() {
let directory = tempfile::tempdir().unwrap();
let runtime = moli_sqlite::Runtime::new().unwrap();
let (local, owner) = moli_web_storage::new_sqlite_web_storage_store(
directory.path().join("localstorage.sqlite"),
moli_sqlite::OpenMode::Create,
runtime.handle(),
)
.unwrap();
assert_local_storage_events_cross_pages(crate::RendererWebStorageHandles::new(
local,
crate::new_shared_web_storage_store(),
))
.await;
owner.close().unwrap();
}
async fn assert_local_storage_events_cross_pages(storage: crate::RendererWebStorageHandles) {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
let storage = crate::RendererWebStorageHandles::ephemeral();
let other_storage = crate::RendererWebStorageHandles::new(
storage.local_storage(),
crate::new_shared_web_storage_store(),
@@ -97,6 +97,7 @@ async fn window_fetch_credentialless_credentials_follow_redirect_taint() {
.cookie_store();
cookies
.lock()
.unwrap()
.set_document_cookie(&same.base_url(), "seed=1; Path=/; SameSite=Lax");
// The same probe exercises Request metadata and redirects in Window and Worker realms.
vm.eval(&format!(
@@ -169,6 +170,7 @@ async fn window_fetch_credentialless_credentials_follow_redirect_taint() {
}
let actual_cookies = cookies
.lock()
.unwrap()
.cookies()
.into_iter()
.map(|cookie| cookie.name)
@@ -139,7 +139,7 @@ fn sqlite_areas_follow_window_lifetime_without_storage_wrapper_finalizers() {
.unwrap();
let handles =
crate::RendererWebStorageHandles::new(local, crate::new_shared_web_storage_store());
let mut vm = new_storage_test_vm("https://window-storage-lifetime.test/");
let mut vm = new_storage_page_task_executor_test_vm("https://window-storage-lifetime.test/");
vm.set_web_storage_handles(&handles);
let host = vm.context_host_weak_for_test().upgrade().unwrap();
assert_eq!(host.borrow().local_storage_area_count_for_test(), 0);
@@ -157,14 +157,36 @@ fn sqlite_areas_follow_window_lifetime_without_storage_wrapper_finalizers() {
vm.eval("globalThis.storagePopup = open('about:blank'); globalThis.retainedPopupStorage = storagePopup.localStorage; retainedPopupStorage.getItem('key');").unwrap();
assert_eq!(host.borrow().local_storage_area_count_for_test(), 2);
vm.eval("storagePopup.close()").unwrap();
assert_eq!(
host.borrow().local_storage_area_count_for_test(),
2,
"close() queues definite Window closure on the DOM-manipulation source"
);
assert_eq!(
vm.eval_after_selected_page_tasks(
"String(storagePopup.closed && storagePopup.opener === null)"
)
.unwrap(),
"true"
);
assert_eq!(
host.borrow().local_storage_area_count_for_test(),
1,
"a closed Window releases its area even while its Storage wrapper is retained"
);
vm.eval("globalThis.storageFrame = document.createElement('iframe'); document.body.appendChild(storageFrame);").unwrap();
materialize_single_child_default_realm_for_test(&mut vm, "storage lifetime child");
assert_eq!(
vm.eval("globalThis.storageFrame = document.createElement('iframe'); document.body.appendChild(storageFrame); String(storageFrame.contentWindow !== null)").unwrap(),
"true"
);
assert_eq!(
vm.eval_after_selected_page_tasks(
"String(storageFrame.contentDocument.defaultView === storageFrame.contentWindow)"
)
.unwrap(),
"true"
);
assert_eq!(vm.live_child_default_runtime_realm_inventory().len(), 1);
vm.eval("globalThis.retainedChildStorage = storageFrame.contentWindow.localStorage; retainedChildStorage.getItem('key');").unwrap();
assert_eq!(host.borrow().local_storage_area_count_for_test(), 2);
vm.eval("storageFrame.remove()").unwrap();
@@ -91,7 +91,7 @@ async fn check_worker_credentialless_fetch(intercept: bool) {
7,
));
let cookie_store = moli_cookie_jar::new_shared_browser_cookie_store();
cookie_store.lock().set_document_cookie(
cookie_store.lock().unwrap().set_document_cookie(
&url::Url::parse(&same_origin).unwrap(),
"seed=1; Path=/; SameSite=Lax",
);
@@ -201,6 +201,7 @@ async fn check_worker_credentialless_fetch(intercept: bool) {
}
let actual_cookies = cookie_store
.lock()
.unwrap()
.cookies()
.into_iter()
.map(|cookie| cookie.name)
+83
View File
@@ -141,3 +141,86 @@ impl WebStorageStore {
queued
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Default)]
struct Recipient {
events: Mutex<Vec<WebStorageEventData>>,
register: Mutex<Option<(Weak<WebStorageStore>, Arc<Recipient>)>>,
closed: bool,
}
impl WebStorageEventSink for Recipient {
fn queue_storage_event(
&self,
_source: &WebStorageEventSource,
_origin: &str,
_area_key: &str,
data: &WebStorageEventData,
) -> WebStorageEventDelivery {
self.events.lock().push(data.clone());
if self.closed {
return WebStorageEventDelivery::Closed;
}
if let Some((store, recipient)) = self.register.lock().take() {
store.upgrade().unwrap().register_event_recipient(recipient);
}
WebStorageEventDelivery::Queued
}
}
#[test]
fn event_recipients_are_weak_and_delivery_can_register_a_future_recipient() {
let store = new_shared_web_storage_store();
let future = Arc::new(Recipient::default());
let first = Arc::new(Recipient {
register: Mutex::new(Some((Arc::downgrade(&store), future.clone()))),
..Default::default()
});
let closed = Arc::new(Recipient {
closed: true,
..Default::default()
});
let expired = Arc::new(Recipient::default());
let weak = Arc::downgrade(&expired);
store.register_event_recipient(first.clone());
store.register_event_recipient(closed.clone());
store.register_event_recipient(expired.clone());
drop(expired);
assert!(weak.upgrade().is_none());
let identity = Arc::new(());
let source = WebStorageEventSource::new(identity.clone());
assert_eq!(source, WebStorageEventSource::new(identity));
let data = WebStorageEventData::new(
"https://source.test/".into(),
false,
Some(vec![0xd800]),
None,
Some(vec![0xdc00]),
);
assert_eq!(
store.queue_remote_storage_events(&source, "https://source.test", "area", &data),
1
);
assert!(future.events.lock().is_empty());
assert_eq!(
store.queue_remote_storage_events(&source, "https://source.test", "area", &data),
2
);
assert_eq!(*first.events.lock(), vec![data.clone(), data.clone()]);
assert_eq!(*future.events.lock(), vec![data.clone()]);
assert_eq!(*closed.events.lock(), vec![data]);
drop(first);
drop(future);
assert_eq!(store.event_recipients.lock().len(), 2);
let data = WebStorageEventData::new(String::new(), false, None, None, None);
assert_eq!(
store.queue_remote_storage_events(&source, "https://source.test", "area", &data),
0
);
assert!(store.event_recipients.lock().is_empty());
}
}