fix(browser): report complete storage key inventory changes (#2861)
* fix(browser): report complete storage key inventory changes * fix(browser): preserve unavailable storage inventory coverage Reuse the key-coverage propagation and independent fingerprint mapping proposed by chAng-L19 in morluto/rea#2491. Propagate failed or malformed inventories into the canonical completeness ledger before comparing membership; preserve independently proven value changes. Add producer-to-comparison regressions and document recapture requirements.
This commit is contained in:
@@ -14,6 +14,185 @@ import {
|
||||
} from "../../fixtures/fakeCdpBrowser.js";
|
||||
|
||||
const browsers: FakeCdpBrowser[] = [];
|
||||
|
||||
const inspectStorage = async (
|
||||
browser: FakeCdpBrowser,
|
||||
fingerprints = false,
|
||||
) => {
|
||||
const captured = await new CdpBrowserProvider().inspectPage(
|
||||
inspectWebPageInputSchema.parse({
|
||||
cdp_endpoint: browser.endpoint,
|
||||
allowed_origins: [browser.allowedOrigin],
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 0,
|
||||
include_storage_keys: true,
|
||||
include_storage_fingerprints: fingerprints,
|
||||
}),
|
||||
);
|
||||
if (!captured.ok) throw captured.error;
|
||||
return captured.value;
|
||||
};
|
||||
|
||||
const compareStorage = (left: WebPageInspection, right: WebPageInspection) =>
|
||||
compareWebCaptures(
|
||||
compareWebCapturesInputSchema.parse({
|
||||
before: { inspection: left },
|
||||
after: { inspection: right },
|
||||
}),
|
||||
).dimensions.storage;
|
||||
|
||||
it.each([
|
||||
["local", "DOMStorage.getDOMStorageItems", { entries: [null] }],
|
||||
[
|
||||
"session",
|
||||
"DOMStorage.getDOMStorageItems",
|
||||
{ entries: [["known", "value"], null] },
|
||||
],
|
||||
["indexed", "IndexedDB.requestDatabaseNames", { databaseNames: [null] }],
|
||||
["cache", "CacheStorage.requestCacheNames", { caches: [null] }],
|
||||
] as const)(
|
||||
"does not invent membership changes after an incomplete %s inventory",
|
||||
async (kind, method, malformed) => {
|
||||
let failure: "unsupported" | "malformed" | undefined;
|
||||
const matches = (command: {
|
||||
method: string;
|
||||
params: Record<string, unknown>;
|
||||
}) => {
|
||||
if (command.method !== method) return false;
|
||||
if (kind !== "local" && kind !== "session") return true;
|
||||
return (
|
||||
(command.params.storageId as { isLocalStorage: boolean })
|
||||
.isLocalStorage ===
|
||||
(kind === "local")
|
||||
);
|
||||
};
|
||||
const browser = await startFakeCdpBrowser({
|
||||
commandError: (command) =>
|
||||
failure === "unsupported" && matches(command)
|
||||
? { code: -32601, message: "Method not found" }
|
||||
: undefined,
|
||||
commandResult: (command) =>
|
||||
failure === "malformed" && matches(command) ? malformed : undefined,
|
||||
});
|
||||
browsers.push(browser);
|
||||
const before = await inspectStorage(browser);
|
||||
expect(before.completeness.unavailable_sections).not.toContain(
|
||||
"storage_keys",
|
||||
);
|
||||
for (const [mode, reason] of [
|
||||
["unsupported", "provider_unavailable"],
|
||||
["malformed", "invalid_protocol_value"],
|
||||
] as const) {
|
||||
failure = mode;
|
||||
const after = await inspectStorage(browser);
|
||||
expect(after.completeness.excluded).toContainEqual({
|
||||
section: "storage_keys",
|
||||
reason,
|
||||
count: null,
|
||||
});
|
||||
expect(after.completeness.unavailable_sections).toContain("storage_keys");
|
||||
for (const [left, right] of [
|
||||
[before, after],
|
||||
[after, before],
|
||||
] as const)
|
||||
expect(compareStorage(left, right)).toMatchObject({
|
||||
status: "unknown",
|
||||
total_changes: 0,
|
||||
changes: [],
|
||||
});
|
||||
if (kind === "session" && mode === "malformed")
|
||||
expect(after.storage.session_storage_keys).toEqual(["known"]);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it("preserves shared content changes when a peer inventory is unavailable", async () => {
|
||||
let value = "before";
|
||||
const browser = await startFakeCdpBrowser({
|
||||
unsupportedMethods: ["CacheStorage.requestCacheNames"],
|
||||
commandResult: (command) =>
|
||||
command.method === "DOMStorage.getDOMStorageItems"
|
||||
? { entries: [["shared-key", value]] }
|
||||
: undefined,
|
||||
});
|
||||
browsers.push(browser);
|
||||
const before = await inspectStorage(browser, true);
|
||||
expect(before.completeness.unavailable_sections).toContain("storage_keys");
|
||||
expect(compareStorage(before, before)).toMatchObject({
|
||||
status: "unknown",
|
||||
total_changes: 0,
|
||||
});
|
||||
value = "after";
|
||||
const after = await inspectStorage(browser, true);
|
||||
for (const [left, right] of [
|
||||
[before, after],
|
||||
[after, before],
|
||||
] as const)
|
||||
expect(compareStorage(left, right)).toMatchObject({
|
||||
status: "changed",
|
||||
total_changes: 2,
|
||||
changes: [
|
||||
expect.objectContaining({ change: "modified" }),
|
||||
expect.objectContaining({ change: "modified" }),
|
||||
],
|
||||
});
|
||||
for (const fingerprint of after.storage.content_fingerprints)
|
||||
if (["local_storage", "session_storage"].includes(fingerprint.scope))
|
||||
fingerprint.complete = false;
|
||||
expect(compareStorage(before, after)).toMatchObject({
|
||||
status: "unknown",
|
||||
total_changes: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("reports complete storage key additions and removals without content fingerprints", async () => {
|
||||
const browser = await startFakeCdpBrowser();
|
||||
browsers.push(browser);
|
||||
const before = await inspectStorage(browser);
|
||||
expect(before.storage.fingerprints_complete).toBe(false);
|
||||
const after = structuredClone(before);
|
||||
after.storage.local_storage_keys.push("new-local");
|
||||
after.storage.session_storage_keys.push("new-session");
|
||||
after.storage.indexed_db_names.push("new-db");
|
||||
after.storage.cache_names.push("new-cache");
|
||||
const identities = [
|
||||
"storage:cache:new-cache",
|
||||
"storage:indexed_db:new-db",
|
||||
"storage:local_storage:new-local",
|
||||
"storage:session_storage:new-session",
|
||||
];
|
||||
for (const [left, right, change] of [
|
||||
[before, after, "added"],
|
||||
[after, before, "removed"],
|
||||
] as const) {
|
||||
expect(compareStorage(left, right)).toMatchObject({
|
||||
status: "changed",
|
||||
total_changes: 4,
|
||||
changes: identities.map((identity) => ({ identity, change })),
|
||||
});
|
||||
}
|
||||
expect(compareStorage(after, after)).toMatchObject({
|
||||
status: "unknown",
|
||||
total_changes: 0,
|
||||
});
|
||||
const missingQuota = structuredClone(after);
|
||||
missingQuota.storage.quota_bytes = null;
|
||||
expect(compareStorage(before, missingQuota)).toMatchObject({
|
||||
status: "changed",
|
||||
total_changes: 4,
|
||||
});
|
||||
const partialInventory = structuredClone(after);
|
||||
partialInventory.completeness.unavailable_sections.push("storage_keys");
|
||||
expect(compareStorage(before, partialInventory)).toMatchObject({
|
||||
status: "unknown",
|
||||
total_changes: 0,
|
||||
});
|
||||
expect(compareStorage(partialInventory, before)).toMatchObject({
|
||||
status: "unknown",
|
||||
total_changes: 0,
|
||||
});
|
||||
});
|
||||
|
||||
const expectInvalidDiff = (input: unknown): void => {
|
||||
expect(webCaptureDiffSchema.safeParse(input).success).toBe(false);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user