fix(web): keep unreadable cookie partitions incomplete (#13187)

This commit is contained in:
morluto authored and GitHub committed 2026-11-21 19:00:00 +00:00
1 parent 7a11884c08
commit c0963732a7
4 files changed
+207 -464

No files matched your search

+4 -3
View File
@@ -99,9 +99,10 @@ evidence, and inventory coverage separate:
Storage fingerprints decode supported CDP primitive/JSON values before hashing.
Debugger descriptions, previews, and remote references do not represent storage
content. Actual JSON properties with those names remain content. Reference-only
objects, unsupported values, and opaque cookie partition identities remain
incomplete. Previously exported fingerprints are not recomputed; recapture both
sides when comparing captures produced across this decoder change.
objects, unsupported values, and opaque or unreadable cookie partition
identities remain incomplete. Previously exported fingerprints are not
recomputed; recapture both sides when comparing captures produced across this
decoder change.
IndexedDB schema fingerprints carry `projection_version: 2` and include the
observed database version and object-store definitions. A missing, malformed, or
imprecise version leaves the schema fingerprint incomplete while retaining
-418
View File
@@ -1,418 +0,0 @@
import { expect, it } from "vitest";
import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
import type { WebPageInspection } from "../domain/browserObservationSchemas.js";
import {
browserCaptureComparisonInputSchema,
compareBrowserCaptures,
} from "../domain/browserCaptureComparison.js";
import { webCaptureDiffSchema } from "../domain/webCaptureDiffSchemas.js";
import { passiveNetworkCapture } from "../../tests/fixtures/passiveNetworkCapture.js";
import { captureStorage } from "./CdpCaptureStorage.js";
import { CdpCommandRejection } from "./CdpCommandRejection.js";
import type { CdpConnection } from "./CdpConnection.js";
import { fakeCdpSend } from "../../tests/fixtures/fakeCdpSend.js";
const NO_CACHE_COMMAND = Symbol("unsupported");
const captureWithCacheResult = async (
cacheResult: unknown,
storageEntries: unknown = [],
): Promise<Awaited<ReturnType<typeof captureStorage>>["value"]> => {
const connection = {
send: fakeCdpSend((method, params): Promise<unknown> => {
switch (method) {
case "Storage.getUsageAndQuota":
return Promise.resolve({ usage: 1, quota: 10 });
case "DOMStorage.getDOMStorageItems":
return Promise.resolve({ entries: storageEntries });
case "IndexedDB.requestDatabaseNames":
return Promise.resolve({ databaseNames: [] });
case "Network.getCookies":
return Promise.resolve({ cookies: [] });
case "CacheStorage.requestCacheNames":
return cacheResult === NO_CACHE_COMMAND
? Promise.reject(
new CdpCommandRejection(
"inspect_web_page",
method,
-32601,
"Method not found",
),
)
: Promise.resolve(cacheResult);
case "CacheStorage.requestEntries":
expect(params.cacheId).toEqual(expect.any(String));
return Promise.resolve({ cacheDataEntries: [], returnCount: 0 });
default:
return Promise.reject(new Error(`Unexpected command: ${method}`));
}
}),
} as unknown as CdpConnection;
const { value } = await captureStorage(
{
connection,
sessionId: undefined,
input: inspectWebPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "fixture-page",
include_storage_keys: true,
include_storage_fingerprints: true,
}),
},
"https://example.test/",
[],
);
return value;
};
it.each([
["an observed empty cache list", { caches: [] }, true],
["a response without a cache list", {}, false],
["a non-array cache list", { caches: "nope" }, false],
["a cache list with an unusable entry", { caches: [5] }, false],
["an unsupported cache command", NO_CACHE_COMMAND, false],
] as const)(
"reports fingerprint completeness for %s",
async (_label, cacheResult, complete) => {
const value = await captureWithCacheResult(cacheResult);
expect(value.cache_names).toEqual([]);
expect(value.fingerprints_complete).toBe(complete);
},
);
it.each([false, true])(
"preserves valid cache evidence beside an unusable entry: %s",
async (mixed) => {
const cache = { cacheName: "fixture-cache", cacheId: "fixture-cache-id" };
const value = await captureWithCacheResult({
caches: mixed ? [cache, 5] : [cache],
});
expect(value.cache_names).toEqual(["fixture-cache"]);
expect(value.fingerprints_complete).toBe(!mixed);
},
);
it("keeps an omitted IndexedDB remote value unknown", async () => {
const replies: Record<string, unknown> = {
"Storage.getUsageAndQuota": { usage: 0, quota: 1000 },
"DOMStorage.getDOMStorageItems": { entries: [] },
"IndexedDB.requestDatabaseNames": { databaseNames: ["db"] },
"CacheStorage.requestCacheNames": { caches: [] },
"Network.getCookies": { cookies: [] },
"IndexedDB.requestDatabase": {
databaseWithObjectStores: {
name: "db",
objectStores: [
{
name: "records",
keyPath: { type: "string", string: "id" },
autoIncrement: false,
indexes: [],
},
],
},
},
"IndexedDB.requestData": {
objectStoreDataEntries: [
{
key: { type: "number", value: 1 },
primaryKey: { type: "number", value: 1 },
},
],
hasMore: false,
},
};
const connection = {
send: fakeCdpSend(async (method): Promise<unknown> => {
if (!(method in replies)) throw new Error(method);
return replies[method];
}),
} as unknown as CdpConnection;
const { value } = await captureStorage(
{
connection,
sessionId: undefined,
input: inspectWebPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://example.test"],
target_id: "page-1",
include_storage_keys: true,
include_storage_fingerprints: true,
}),
},
"https://example.test/app",
[],
);
const record = value.content_fingerprints.find(
(item) => item.scope === "indexed_db_record",
);
expect(record).toMatchObject({ value_sha256: null, complete: false });
expect(value.fingerprints_complete).toBe(false);
});
it.each(["indexed_db", "cache"] as const)(
"advances %s pagination past malformed rows without repeating evidence",
async (scope) => {
const offsets: unknown[] = [];
const record = {
key: { type: "number", value: 1 },
primaryKey: { type: "number", value: 1 },
value: { type: "string", value: "first" },
};
const cacheEntry = {
requestURL: "https://example.test/first",
requestMethod: "GET",
requestHeaders: [],
};
const replies: Record<string, unknown> = {
"Storage.getUsageAndQuota": { usage: 0, quota: 1000 },
"DOMStorage.getDOMStorageItems": { entries: [] },
"IndexedDB.requestDatabaseNames": {
databaseNames: scope === "indexed_db" ? ["db"] : [],
},
"CacheStorage.requestCacheNames": {
caches:
scope === "cache" ? [{ cacheName: "cache", cacheId: "id" }] : [],
},
"Network.getCookies": { cookies: [] },
"IndexedDB.requestDatabase": {
databaseWithObjectStores: {
name: "db",
version: 1,
objectStores: [
{
name: "records",
keyPath: { type: "string", string: "id" },
autoIncrement: false,
indexes: [],
},
],
},
},
"CacheStorage.requestCachedResponse": { response: { body: "" } },
};
const connection = {
send: fakeCdpSend(async (method, params): Promise<unknown> => {
if (
method === "IndexedDB.requestData" ||
method === "CacheStorage.requestEntries"
) {
offsets.push(params.skipCount);
// The first page includes a malformed row; offset 1 repeats valid evidence.
const rows =
params.skipCount === 0
? [null, scope === "indexed_db" ? record : cacheEntry]
: params.skipCount === 1
? [scope === "indexed_db" ? record : cacheEntry]
: [
scope === "indexed_db"
? {
...record,
key: { type: "number", value: 2 },
primaryKey: { type: "number", value: 2 },
}
: {
...cacheEntry,
requestURL: "https://example.test/second",
},
];
return method === "IndexedDB.requestData"
? { objectStoreDataEntries: rows, hasMore: params.skipCount === 0 }
: { cacheDataEntries: rows, returnCount: 3 };
}
if (!(method in replies)) throw new Error(method);
return replies[method];
}),
} as unknown as CdpConnection;
const { value } = await captureStorage(
{
connection,
sessionId: undefined,
input: inspectWebPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "page",
include_storage_keys: true,
include_storage_fingerprints: true,
}),
},
"https://example.test/",
[],
);
expect(offsets).toEqual([0, 2]);
expect(
value.content_fingerprints.filter(
(item) =>
item.scope ===
(scope === "indexed_db" ? "indexed_db_record" : "cache_entry"),
),
).toHaveLength(2);
expect(value.fingerprints_complete).toBe(false);
},
);
const cookieBase = {
name: "fixture",
value: "same",
domain: "example.test",
path: "/",
expires: -1,
size: 11,
httpOnly: false,
secure: false,
session: true,
sameSite: "Lax",
};
const captureCookies = async (
cookies: readonly Record<string, unknown>[],
): Promise<WebPageInspection["storage"]> => {
const replies: Record<string, unknown> = {
"Storage.getUsageAndQuota": { usage: 0, quota: 1000 },
"DOMStorage.getDOMStorageItems": { entries: [] },
"IndexedDB.requestDatabaseNames": { databaseNames: [] },
"CacheStorage.requestCacheNames": { caches: [] },
"Network.getCookies": { cookies },
};
const connection = {
send: fakeCdpSend(async (method): Promise<unknown> => {
if (!(method in replies)) throw new Error(method);
return replies[method];
}),
} as unknown as CdpConnection;
const { value } = await captureStorage(
{
connection,
sessionId: undefined,
input: inspectWebPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "fixture-page",
include_storage_keys: true,
include_storage_fingerprints: true,
}),
},
"https://example.test/",
[],
);
return value;
};
const storageComparison = (
before: WebPageInspection["storage"],
after: WebPageInspection["storage"],
) => {
const inspection = (storage: WebPageInspection["storage"]) => {
const capture = passiveNetworkCapture();
capture.storage = storage;
return capture;
};
return webCaptureDiffSchema.parse(
compareBrowserCaptures(
browserCaptureComparisonInputSchema.parse({
before: { inspection: inspection(before) },
after: { inspection: inspection(after) },
}),
),
).dimensions.storage;
};
it("keeps an opaque cookie partition unknown without dropping its value", async () => {
const plain = await captureCookies([cookieBase]);
const opaque = await captureCookies([
{ ...cookieBase, partitionKeyOpaque: true },
]);
const known = await captureCookies([
{
...cookieBase,
partitionKey: {
topLevelSite: "https://top.example.test",
hasCrossSiteAncestor: false,
},
partitionKeyOpaque: false,
},
]);
const cookieFingerprint = (storage: WebPageInspection["storage"]) => {
const fingerprint = storage.content_fingerprints.find(
(item) => item.scope === "cookie",
);
expect(fingerprint).toBeDefined();
return fingerprint!;
};
const plainCookie = cookieFingerprint(plain);
const opaqueCookie = cookieFingerprint(opaque);
const knownCookie = cookieFingerprint(known);
expect(opaque.fingerprints_complete).toBe(false);
expect(opaqueCookie).toMatchObject({
complete: false,
value_sha256: plainCookie.value_sha256,
});
expect(opaqueCookie.identity_sha256).not.toBe(plainCookie.identity_sha256);
expect(known.fingerprints_complete).toBe(true);
expect(knownCookie).toMatchObject({ complete: true });
expect(knownCookie.identity_sha256).not.toBe(plainCookie.identity_sha256);
expect(storageComparison(plain, opaque)).toMatchObject({
status: "unknown",
total_changes: 0,
changes: [],
});
expect(storageComparison(opaque, opaque)).toMatchObject({
status: "unknown",
total_changes: 0,
changes: [],
});
expect(storageComparison(plain, known).status).toBe("changed");
});
it("retains known storage keys and cache names when content or locator metadata is malformed", async () => {
const storage = await captureWithCacheResult(
{ caches: [{ cacheName: "known-cache" }] },
[
["known-key", 42],
["valid-peer", "recorded"],
[42, "unknown identity"],
],
);
expect(storage.local_storage_keys).toEqual(["known-key", "valid-peer"]);
expect(storage.session_storage_keys).toEqual(["known-key", "valid-peer"]);
expect(storage.cache_names).toEqual(["known-cache"]);
expect(storage.fingerprints_complete).toBe(false);
const local = storage.content_fingerprints.filter(
({ scope }) => scope === "local_storage",
);
expect(local).toHaveLength(2);
expect(local).toContainEqual(
expect.objectContaining({ complete: false, value_sha256: null }),
);
expect(local).toContainEqual(
expect.objectContaining({
complete: true,
value_sha256: expect.any(String),
}),
);
});
it("retains cookie identity when its content is malformed without inventing a value fingerprint", async () => {
const known = await captureCookies([cookieBase]);
const unavailable = await captureCookies([
{ ...cookieBase, value: 42 },
{ ...cookieBase, name: 42 },
]);
const knownCookie = known.content_fingerprints.find(
({ scope }) => scope === "cookie",
);
if (knownCookie === undefined) throw new Error("Missing known cookie");
expect(unavailable.content_fingerprints).toEqual([
{
scope: "cookie",
identity_sha256: knownCookie.identity_sha256,
value_sha256: null,
complete: false,
},
]);
expect(unavailable.fingerprints_complete).toBe(false);
expect(storageComparison(known, unavailable)).toMatchObject({
status: "unknown",
total_changes: 0,
changes: [],
});
});
+17 -8
View File
@@ -138,22 +138,31 @@ const addCookies = async (
run.state.complete = false;
continue;
}
// CDP Cookie.partitionKeyOpaque means the partition exists but is not
// serializable. A missing partitionKey means unpartitioned, so hashing
// that absence would align an unresolved partition with a different cookie.
// https://chromedevtools.github.io/devtools-protocol/tot/Network/#type-Cookie
const partitionOpaque = cookie.partitionKeyOpaque === true;
const { partitionKey, partitionKeyOpaque } = cookie;
const partition = recordValue(partitionKey);
// Older Chrome reports a string partition key. A missing key with no
// opaque flag means unpartitioned; malformed metadata leaves it unknown.
const partitionKnown =
(partitionKeyOpaque === undefined || partitionKeyOpaque === false) &&
(partitionKey === undefined ||
typeof partitionKey === "string" ||
(typeof partition?.topLevelSite === "string" &&
typeof partition.hasCrossSiteAncestor === "boolean"));
addFingerprint(run.state, {
scope: "cookie",
identity: {
domain,
path,
name,
partition_key: cookie.partitionKey ?? null,
...(partitionOpaque ? { partition_opaque: true } : {}),
partition_key: partitionKey ?? null,
...(partitionKeyOpaque === true
? { partition_opaque: true }
: partitionKnown
? {}
: { partition_unreadable: true }),
},
value,
complete: value !== undefined && !partitionOpaque,
complete: value !== undefined && partitionKnown,
});
}
};
@@ -27,7 +27,7 @@ const inspect = async (options: FakeOptions, includeFingerprints = true) => {
if (!result.ok) throw result.error;
return result.value;
};
return { capture };
return { browser, capture };
};
const compareStorage = (before: WebPageInspection, after: WebPageInspection) =>
@@ -39,19 +39,24 @@ const compareStorage = (before: WebPageInspection, after: WebPageInspection) =>
).dimensions.storage;
it.each([
["CacheStorage.requestCacheNames", {}],
["CacheStorage.requestCacheNames", { caches: null }],
["CacheStorage.requestCacheNames", { caches: [null] }],
["CacheStorage.requestCacheNames", {}, []],
["CacheStorage.requestCacheNames", { caches: null }, []],
["CacheStorage.requestCacheNames", { caches: [null] }, []],
[
"CacheStorage.requestCacheNames",
{ caches: [{ cacheName: "assets-v1", cacheId: "secret-id" }, null] },
["assets-v1"],
],
["CacheStorage.requestCacheNames", { caches: [{ cacheName: "assets-v1" }] }],
["IndexedDB.requestDatabaseNames", { databaseNames: [null] }],
["DOMStorage.getDOMStorageItems", { entries: [null] }],
[
"CacheStorage.requestCacheNames",
{ caches: [{ cacheName: "assets-v1" }] },
["assets-v1"],
],
["IndexedDB.requestDatabaseNames", { databaseNames: [null] }, ["assets-v1"]],
["DOMStorage.getDOMStorageItems", { entries: [null] }, ["assets-v1"]],
] as const)(
"preserves unknown inventory coverage from %s: %j",
async (method, reply) => {
async (method, reply, cacheNames) => {
let malformed = false;
const { capture } = await inspect({
commandResult: (command) =>
@@ -61,6 +66,7 @@ it.each([
malformed = true;
const after = await capture();
expect(after.storage.fingerprints_complete).toBe(false);
expect(after.storage.cache_names).toEqual(cacheNames);
expect(after.completeness.unavailable_sections).toContain("storage_keys");
expect(after.completeness.excluded).toContainEqual({
section: "storage_keys",
@@ -399,7 +405,10 @@ it.each([
({ scope }) => scope === "indexed_db_record",
);
expect(records).toHaveLength(1);
expect(records[0]?.complete).toBe(complete);
expect(records[0]).toMatchObject({
complete,
value_sha256: complete ? expect.stringMatching(/^[a-f0-9]{64}$/u) : null,
});
expect(before.storage.fingerprints_complete).toBe(complete);
expect(compareStorage(before, after)).toMatchObject({
status: complete ? "unchanged" : "unknown",
@@ -457,38 +466,180 @@ it("compares decoded IndexedDB content independently of debugger metadata", asyn
});
});
it.each([undefined, false, true])(
"keeps opaque cookie partition identity incomplete: %s",
async (partitionKeyOpaque) => {
const { capture } = await inspect({
commandResult: (command) =>
command.method === "Network.getCookies"
? {
cookies: [
{
name: "cookie",
value: "value",
domain: "app.example.test",
path: "/",
partitionKeyOpaque,
},
],
}
: undefined,
const cookie = {
name: "cookie",
value: "value",
domain: "app.example.test",
path: "/",
};
const cookieFingerprint = (capture: WebPageInspection) => {
const fingerprint = capture.storage.content_fingerprints.find(
({ scope }) => scope === "cookie",
);
if (fingerprint === undefined) throw new Error("Missing cookie fingerprint");
return fingerprint;
};
it("preserves cookie value evidence across distinct and unknown partitions", async () => {
let cookies: readonly Record<string, unknown>[] = [cookie];
const { capture } = await inspect({
commandResult: ({ method }) =>
method === "Network.getCookies" ? { cookies } : undefined,
});
const before = await capture();
const plain = cookieFingerprint(before);
expect(plain).toMatchObject({
complete: true,
value_sha256: expect.any(String),
});
expect(compareStorage(before, await capture()).status).toBe("unchanged");
cookies = [{ ...cookie, partitionKeyOpaque: false }];
expect(cookieFingerprint(await capture())).toEqual(plain);
const topLevelSite = "https://top.example.test";
for (const partitionKey of [
topLevelSite,
{ topLevelSite, hasCrossSiteAncestor: false },
]) {
cookies = [{ ...cookie, partitionKey }];
const known = await capture();
expect(cookieFingerprint(known)).toMatchObject({
complete: true,
value_sha256: plain.value_sha256,
});
const before = await capture();
const after = await capture();
expect(compareStorage(before, known).status).toBe("changed");
}
for (const partition of [
{ partitionKeyOpaque: true },
{ partitionKeyOpaque: "true" },
{ partitionKey: null },
{ partitionKey: { topLevelSite: "https://top.example.test" } },
]) {
cookies = [{ ...cookie, ...partition }];
const unknown = await capture();
for (const first of [before, unknown])
expect(compareStorage(first, unknown)).toMatchObject({
status: "unknown",
total_changes: 0,
changes: [],
});
const fingerprint = cookieFingerprint(unknown);
expect(fingerprint).toMatchObject({
complete: false,
value_sha256: plain.value_sha256,
});
expect(fingerprint.identity_sha256).not.toBe(plain.identity_sha256);
expect(unknown.storage.fingerprints_complete).toBe(false);
}
cookies = [
{ ...cookie, value: 42 },
{ ...cookie, name: 42 },
];
const malformedValue = await capture();
expect(
malformedValue.storage.content_fingerprints.filter(
({ scope }) => scope === "cookie",
),
).toEqual([{ ...plain, complete: false, value_sha256: null }]);
expect(compareStorage(before, malformedValue)).toMatchObject({
status: "unknown",
total_changes: 0,
changes: [],
});
});
it.each([
["IndexedDB.requestData", "indexed_db_record"],
["CacheStorage.requestEntries", "cache_entry"],
] as const)(
"advances %s past malformed rows without repeating evidence",
async (command, scope) => {
const { browser, capture } = await inspect({
commandResult: ({ method, params }, origin) => {
if (method !== command) return undefined;
const row = (index: number) =>
method === "IndexedDB.requestData"
? {
key: { type: "number", value: index },
primaryKey: { type: "number", value: index },
value: { type: "string", value: `row-${index}` },
}
: {
requestURL: `${origin}/cache-${index}`,
requestMethod: "GET",
requestHeaders: [],
};
const rows =
params.skipCount === 0
? [null, row(1)]
: [row(params.skipCount === 1 ? 1 : 2)];
return method === "IndexedDB.requestData"
? { objectStoreDataEntries: rows, hasMore: params.skipCount === 0 }
: { cacheDataEntries: rows, returnCount: 3 };
},
});
const { storage } = await capture();
expect(
before.storage.content_fingerprints.find(
({ scope }) => scope === "cookie",
)?.complete,
).toBe(partitionKeyOpaque !== true);
expect(compareStorage(before, after).status).toBe(
partitionKeyOpaque === true ? "unknown" : "unchanged",
browser.commands
.filter(({ method }) => method === command)
.map(({ params }) => params.skipCount),
).toEqual([0, 2]);
const fingerprints = storage.content_fingerprints.filter(
(item) => item.scope === scope,
);
expect(fingerprints).toHaveLength(2);
expect(fingerprints).toMatchObject([
{ complete: true },
{ complete: true },
]);
expect(
new Set(fingerprints.map(({ identity_sha256 }) => identity_sha256)).size,
).toBe(2);
expect(storage.fingerprints_complete).toBe(false);
},
);
it("keeps storage keys and cache names when their values or locators are malformed", async () => {
const { capture } = await inspect({
commandResult: ({ method }) => {
if (method === "CacheStorage.requestCacheNames")
return { caches: [{ cacheName: "known-cache" }] };
if (method === "DOMStorage.getDOMStorageItems")
return {
entries: [
["known-key", 42],
["valid-peer", "recorded"],
[42, "unknown identity"],
],
};
return undefined;
},
});
const { storage } = await capture();
expect(storage).toMatchObject({
local_storage_keys: ["known-key", "valid-peer"],
session_storage_keys: ["known-key", "valid-peer"],
cache_names: ["known-cache"],
fingerprints_complete: false,
});
const local = storage.content_fingerprints.filter(
({ scope }) => scope === "local_storage",
);
expect(local).toHaveLength(2);
expect(local).toEqual(
expect.arrayContaining([
expect.objectContaining({ complete: false, value_sha256: null }),
expect.objectContaining({
complete: true,
value_sha256: expect.any(String),
}),
]),
);
});
it.each([
[undefined, false],
[{}, false],