feat(browser): compare scenario and storage evidence

This commit is contained in:
morluto
2026-07-24 13:16:18 +00:00
parent 1ac3118aa2
commit f27dd745b8
24 changed files with 2668 additions and 234 deletions
+89 -52
View File
@@ -4,6 +4,11 @@ import type {
} from "../domain/browserObservation.js";
import { CdpConnection } from "./CdpConnection.js";
import { optionalCdpCommand } from "./CdpOptionalCommand.js";
import {
captureStorageFingerprints,
type CapturedCache,
type CapturedStorageItems,
} from "./CdpCaptureStorageFingerprints.js";
import {
numberValue,
recordValue,
@@ -34,77 +39,109 @@ export const captureStorage = async (
),
);
const local = context.input.include_storage_keys
? await storageKeys(context, origin, true, limitations)
: [];
? await storageItems(context, origin, true, limitations)
: emptyStorageItems;
const session = context.input.include_storage_keys
? await storageKeys(context, origin, false, limitations)
: [];
const indexed = context.input.include_storage_keys
? stringArray(
recordValue(
await optionalCdpCommand(
context,
"IndexedDB.requestDatabaseNames",
{ securityOrigin: origin },
limitations,
),
)?.databaseNames,
? await storageItems(context, origin, false, limitations)
: emptyStorageItems;
const indexedRaw = context.input.include_storage_keys
? await optionalCdpCommand(
context,
"IndexedDB.requestDatabaseNames",
{ securityOrigin: origin },
limitations,
)
: [];
const caches = context.input.include_storage_keys
? recordsValue(
recordValue(
await optionalCdpCommand(
context,
"CacheStorage.requestCacheNames",
{ securityOrigin: origin },
limitations,
),
)?.caches,
).flatMap((cache) => {
const name = stringValue(cache.cacheName);
return name === undefined ? [] : [name.slice(0, 1_024)];
})
: [];
: undefined;
const indexedValues = recordValue(indexedRaw)?.databaseNames;
const indexed = stringArray(indexedValues);
const cacheRaw = context.input.include_storage_keys
? await optionalCdpCommand(
context,
"CacheStorage.requestCacheNames",
{ securityOrigin: origin },
limitations,
)
: undefined;
const cacheValues = recordsValue(recordValue(cacheRaw)?.caches);
const caches = cacheValues.flatMap((cache) => {
const name = stringValue(cache.cacheName);
const id = stringValue(cache.cacheId);
return name === undefined || id === undefined
? []
: [{ name: name.slice(0, 1_024), id } satisfies CapturedCache];
});
const maximum = context.input.limits.max_storage_keys;
const fingerprints = context.input.include_storage_keys
? await captureStorageFingerprints({
context,
origin,
local,
session,
indexedDbNames: indexed.slice(0, maximum),
caches: caches.slice(0, maximum),
limitations,
})
: { items: [], complete: false, truncated: false };
const truncated =
[local.items, session.items, indexed, caches].some(
(items) => items.length > maximum,
) || fingerprints.truncated;
const structuredStorageComplete =
indexedRaw !== undefined &&
Array.isArray(indexedValues) &&
indexed.length === indexedValues.length &&
cacheRaw !== undefined &&
caches.length === cacheValues.length;
return {
value: {
origin,
usage_bytes: numberValue(quota?.usage) ?? null,
quota_bytes: numberValue(quota?.quota) ?? null,
local_storage_keys: local.slice(0, maximum),
session_storage_keys: session.slice(0, maximum),
local_storage_keys: local.items.map(({ key }) => key).slice(0, maximum),
session_storage_keys: session.items
.map(({ key }) => key)
.slice(0, maximum),
indexed_db_names: indexed.slice(0, maximum),
cache_names: caches.slice(0, maximum),
cache_names: caches.map(({ name }) => name).slice(0, maximum),
content_fingerprints: [...fingerprints.items],
fingerprint_algorithm: "sha256",
fingerprints_complete:
fingerprints.complete && structuredStorageComplete && !truncated,
values_redacted: true,
},
truncated: [local, session, indexed, caches].some(
(items) => items.length > maximum,
),
truncated,
};
};
const storageKeys = async (
const emptyStorageItems: CapturedStorageItems = {
items: [],
complete: false,
};
const storageItems = async (
context: Parameters<typeof captureStorage>[0],
origin: string,
isLocalStorage: boolean,
limitations: string[],
): Promise<readonly string[]> => {
const result = recordValue(
await optionalCdpCommand(
context,
"DOMStorage.getDOMStorageItems",
{ storageId: { securityOrigin: origin, isLocalStorage } },
limitations,
),
): Promise<CapturedStorageItems> => {
const raw = await optionalCdpCommand(
context,
"DOMStorage.getDOMStorageItems",
{ storageId: { securityOrigin: origin, isLocalStorage } },
limitations,
);
return Array.isArray(result?.entries)
? result.entries.flatMap((entry) => {
if (!Array.isArray(entry)) return [];
const key = stringValue(entry[0]);
return key === undefined ? [] : [key.slice(0, 1_024)];
})
: [];
const entries = recordValue(raw)?.entries;
if (raw === undefined || !Array.isArray(entries))
return { items: [], complete: false };
const items = entries.flatMap((entry) => {
if (!Array.isArray(entry)) return [];
const key = stringValue(entry[0]);
const value = stringValue(entry[1]);
return key === undefined || value === undefined
? []
: [{ key: key.slice(0, 1_024), value }];
});
return { items, complete: items.length === entries.length };
};
const stringArray = (value: unknown): readonly string[] =>