feat(browser): compare scenario and storage evidence
This commit is contained in:
@@ -27,11 +27,11 @@ import {
|
||||
type WebMcpDiscovery,
|
||||
} from "../domain/webMcpDiscovery.js";
|
||||
import {
|
||||
compareWebCaptures,
|
||||
webCaptureDiffSchema,
|
||||
type CompareWebCapturesInput,
|
||||
type WebCaptureDiff,
|
||||
} from "../domain/webCaptureDiff.js";
|
||||
browserCaptureComparisonSchema,
|
||||
compareBrowserCaptures,
|
||||
type BrowserCaptureComparison,
|
||||
type BrowserCaptureComparisonInput,
|
||||
} from "../domain/browserCaptureComparison.js";
|
||||
import {
|
||||
webScreenshotDiffSchema,
|
||||
webScreenshotSchema,
|
||||
@@ -248,10 +248,12 @@ export class CdpBrowserProvider implements BrowserObservationPort {
|
||||
}
|
||||
|
||||
async compareCaptures(
|
||||
input: CompareWebCapturesInput,
|
||||
): Promise<Result<WebCaptureDiff, AnalysisError>> {
|
||||
input: BrowserCaptureComparisonInput,
|
||||
): Promise<Result<BrowserCaptureComparison, AnalysisError>> {
|
||||
try {
|
||||
return ok(webCaptureDiffSchema.parse(compareWebCaptures(input)));
|
||||
return ok(
|
||||
browserCaptureComparisonSchema.parse(compareBrowserCaptures(input)),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
return err(providerError(cause, "compare_web_captures"));
|
||||
}
|
||||
|
||||
@@ -232,10 +232,17 @@ export const captureAccessibility = (
|
||||
readonly total: number;
|
||||
readonly nodes: WebPageInspection["accessibility"]["nodes"];
|
||||
readonly textCapture: WebPageInspection["accessibility"]["text_capture"];
|
||||
readonly treeIncomplete: boolean;
|
||||
} => {
|
||||
const all = results.flatMap((result) =>
|
||||
recordsValue(requiredRecord(result).nodes),
|
||||
);
|
||||
const nodeIds = new Set(
|
||||
all.flatMap((node) => {
|
||||
const nodeId = stringValue(node.nodeId);
|
||||
return nodeId === undefined ? [] : [nodeId];
|
||||
}),
|
||||
);
|
||||
let retainedBytes = 0;
|
||||
let excludedFields = 0;
|
||||
let truncatedFields = 0;
|
||||
@@ -269,6 +276,7 @@ export const captureAccessibility = (
|
||||
name: captureText(node.name),
|
||||
description: captureText(node.description),
|
||||
ignored: node.ignored === true,
|
||||
states: accessibilityStates(node.properties),
|
||||
};
|
||||
});
|
||||
for (const node of all.slice(maximum)) {
|
||||
@@ -281,6 +289,14 @@ export const captureAccessibility = (
|
||||
return {
|
||||
total: all.length,
|
||||
nodes,
|
||||
treeIncomplete: all.some(
|
||||
(node) =>
|
||||
Array.isArray(node.childIds) &&
|
||||
node.childIds.some((childId) => {
|
||||
const id = stringValue(childId);
|
||||
return id !== undefined && !nodeIds.has(id);
|
||||
}),
|
||||
),
|
||||
textCapture: {
|
||||
status: options.unavailable
|
||||
? "unavailable"
|
||||
@@ -296,6 +312,56 @@ export const captureAccessibility = (
|
||||
};
|
||||
};
|
||||
|
||||
const ACCESSIBILITY_STATE_NAMES = new Set([
|
||||
"atomic",
|
||||
"autocomplete",
|
||||
"busy",
|
||||
"checked",
|
||||
"disabled",
|
||||
"editable",
|
||||
"expanded",
|
||||
"focusable",
|
||||
"focused",
|
||||
"hasPopup",
|
||||
"hidden",
|
||||
"invalid",
|
||||
"level",
|
||||
"live",
|
||||
"modal",
|
||||
"multiline",
|
||||
"multiselectable",
|
||||
"orientation",
|
||||
"pressed",
|
||||
"readonly",
|
||||
"required",
|
||||
"selected",
|
||||
"valuemax",
|
||||
"valuemin",
|
||||
]);
|
||||
|
||||
const accessibilityStates = (
|
||||
value: unknown,
|
||||
): WebPageInspection["accessibility"]["nodes"][number]["states"] => {
|
||||
const states = new Map<string, boolean | number | string>();
|
||||
for (const property of recordsValue(value)) {
|
||||
const name = stringValue(property.name);
|
||||
if (name === undefined || !ACCESSIBILITY_STATE_NAMES.has(name)) continue;
|
||||
const raw = recordValue(property.value)?.value;
|
||||
const state =
|
||||
typeof raw === "boolean"
|
||||
? raw
|
||||
: typeof raw === "number"
|
||||
? numberValue(raw)
|
||||
: typeof raw === "string"
|
||||
? raw.slice(0, 256)
|
||||
: undefined;
|
||||
if (state !== undefined) states.set(name, state);
|
||||
}
|
||||
return [...states]
|
||||
.sort(([left], [right]) => left.localeCompare(right))
|
||||
.map(([name, state]) => ({ name, value: state }));
|
||||
};
|
||||
|
||||
const axText = (value: unknown): string | null =>
|
||||
boundedText(recordValue(value)?.value, 1_024);
|
||||
|
||||
|
||||
@@ -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[] =>
|
||||
|
||||
@@ -0,0 +1,501 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import canonicalize from "canonicalize";
|
||||
|
||||
import type {
|
||||
InspectWebPageInput,
|
||||
WebPageInspection,
|
||||
} from "../domain/browserObservation.js";
|
||||
import { CdpConnection } from "./CdpConnection.js";
|
||||
import { optionalCdpCommand } from "./CdpOptionalCommand.js";
|
||||
import { recordValue, recordsValue, stringValue } from "./CdpCaptureValues.js";
|
||||
|
||||
const MAX_CACHE_BODY_BYTES = 64 * 1_024;
|
||||
|
||||
type StorageFingerprint =
|
||||
WebPageInspection["storage"]["content_fingerprints"][number];
|
||||
|
||||
export interface CapturedStorageItems {
|
||||
readonly items: readonly {
|
||||
readonly key: string;
|
||||
readonly value: string;
|
||||
}[];
|
||||
readonly complete: boolean;
|
||||
}
|
||||
|
||||
export interface CapturedCache {
|
||||
readonly name: string;
|
||||
readonly id: string;
|
||||
}
|
||||
|
||||
interface StorageFingerprintContext {
|
||||
readonly connection: CdpConnection;
|
||||
readonly sessionId: string | undefined;
|
||||
readonly input: InspectWebPageInput;
|
||||
readonly signal?: AbortSignal;
|
||||
}
|
||||
|
||||
interface FingerprintState {
|
||||
readonly items: StorageFingerprint[];
|
||||
readonly maximum: number;
|
||||
complete: boolean;
|
||||
truncated: boolean;
|
||||
}
|
||||
|
||||
interface FingerprintRun {
|
||||
readonly context: StorageFingerprintContext;
|
||||
readonly limitations: string[];
|
||||
readonly state: FingerprintState;
|
||||
}
|
||||
|
||||
interface CaptureStorageFingerprintInput {
|
||||
readonly context: StorageFingerprintContext;
|
||||
readonly origin: string;
|
||||
readonly local: CapturedStorageItems;
|
||||
readonly session: CapturedStorageItems;
|
||||
readonly indexedDbNames: readonly string[];
|
||||
readonly caches: readonly CapturedCache[];
|
||||
readonly limitations: string[];
|
||||
}
|
||||
|
||||
/** Capture stable hashes of approved storage content without returning values. */
|
||||
export const captureStorageFingerprints = async ({
|
||||
context,
|
||||
origin,
|
||||
local,
|
||||
session,
|
||||
indexedDbNames,
|
||||
caches,
|
||||
limitations,
|
||||
}: CaptureStorageFingerprintInput): Promise<{
|
||||
readonly items: readonly StorageFingerprint[];
|
||||
readonly complete: boolean;
|
||||
readonly truncated: boolean;
|
||||
}> => {
|
||||
const state: FingerprintState = {
|
||||
items: [],
|
||||
maximum: context.input.limits.max_storage_keys,
|
||||
complete: local.complete && session.complete,
|
||||
truncated: false,
|
||||
};
|
||||
const run = { context, limitations, state };
|
||||
addStorageItems(state, "local_storage", local.items);
|
||||
addStorageItems(state, "session_storage", session.items);
|
||||
await addCookies(run, origin);
|
||||
await addIndexedDb(run, origin, indexedDbNames);
|
||||
await addCaches(run, caches);
|
||||
state.items.sort((left, right) =>
|
||||
compareCodePoints(
|
||||
`${left.scope}:${left.identity_sha256}`,
|
||||
`${right.scope}:${right.identity_sha256}`,
|
||||
),
|
||||
);
|
||||
return {
|
||||
items: state.items,
|
||||
complete:
|
||||
state.complete &&
|
||||
!state.truncated &&
|
||||
state.items.every(({ complete }) => complete),
|
||||
truncated: state.truncated,
|
||||
};
|
||||
};
|
||||
|
||||
const addStorageItems = (
|
||||
state: FingerprintState,
|
||||
scope: "local_storage" | "session_storage",
|
||||
items: CapturedStorageItems["items"],
|
||||
): void => {
|
||||
for (const item of items)
|
||||
addFingerprint(state, {
|
||||
scope,
|
||||
identity: item.key,
|
||||
value: item.value,
|
||||
complete: true,
|
||||
});
|
||||
};
|
||||
|
||||
const addCookies = async (
|
||||
run: FingerprintRun,
|
||||
origin: string,
|
||||
): Promise<void> => {
|
||||
const raw = await optionalCdpCommand(
|
||||
run.context,
|
||||
"Network.getCookies",
|
||||
{ urls: [origin] },
|
||||
run.limitations,
|
||||
);
|
||||
if (raw === undefined) {
|
||||
run.state.complete = false;
|
||||
return;
|
||||
}
|
||||
const cookieValues = recordValue(raw)?.cookies;
|
||||
if (!Array.isArray(cookieValues)) {
|
||||
run.state.complete = false;
|
||||
return;
|
||||
}
|
||||
const cookies = recordsValue(cookieValues);
|
||||
if (cookies.length !== cookieValues.length) run.state.complete = false;
|
||||
for (const cookie of cookies) {
|
||||
const name = stringValue(cookie.name);
|
||||
const value = stringValue(cookie.value);
|
||||
const domain = stringValue(cookie.domain);
|
||||
const path = stringValue(cookie.path);
|
||||
if (
|
||||
name === undefined ||
|
||||
value === undefined ||
|
||||
domain === undefined ||
|
||||
path === undefined
|
||||
) {
|
||||
run.state.complete = false;
|
||||
continue;
|
||||
}
|
||||
addFingerprint(run.state, {
|
||||
scope: "cookie",
|
||||
identity: {
|
||||
domain,
|
||||
path,
|
||||
name,
|
||||
partition_key: cookie.partitionKey ?? null,
|
||||
},
|
||||
value,
|
||||
complete: true,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const addIndexedDb = async (
|
||||
run: FingerprintRun,
|
||||
origin: string,
|
||||
names: readonly string[],
|
||||
): Promise<void> => {
|
||||
for (const databaseName of names) {
|
||||
if (run.state.items.length >= run.state.maximum) {
|
||||
run.state.truncated = true;
|
||||
return;
|
||||
}
|
||||
const raw = await optionalCdpCommand(
|
||||
run.context,
|
||||
"IndexedDB.requestDatabase",
|
||||
{ securityOrigin: origin, databaseName },
|
||||
run.limitations,
|
||||
);
|
||||
const database = recordValue(recordValue(raw)?.databaseWithObjectStores);
|
||||
if (raw === undefined || database === undefined) {
|
||||
run.state.complete = false;
|
||||
continue;
|
||||
}
|
||||
if (stringValue(database.name) !== databaseName) {
|
||||
run.state.complete = false;
|
||||
continue;
|
||||
}
|
||||
if (!Array.isArray(database.objectStores)) {
|
||||
run.state.complete = false;
|
||||
continue;
|
||||
}
|
||||
const stores = recordsValue(database.objectStores);
|
||||
if (stores.length !== database.objectStores.length)
|
||||
run.state.complete = false;
|
||||
addFingerprint(run.state, {
|
||||
scope: "indexed_db_schema",
|
||||
identity: databaseName,
|
||||
value: stores.map(indexedDbStoreSchema),
|
||||
complete: indexedDbSchemaComplete(stores),
|
||||
});
|
||||
for (const store of stores) {
|
||||
const objectStoreName = stringValue(store.name);
|
||||
if (objectStoreName === undefined) {
|
||||
run.state.complete = false;
|
||||
continue;
|
||||
}
|
||||
await addIndexedDbRecords(run, {
|
||||
origin,
|
||||
databaseName,
|
||||
objectStoreName,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const indexedDbStoreSchema = (store: Readonly<Record<string, unknown>>) => ({
|
||||
name: stringValue(store.name) ?? null,
|
||||
key_path: stableValue(store.keyPath),
|
||||
auto_increment: store.autoIncrement === true,
|
||||
indexes: recordsValue(store.indexes).map((index) => ({
|
||||
name: stringValue(index.name) ?? null,
|
||||
key_path: stableValue(index.keyPath),
|
||||
unique: index.unique === true,
|
||||
multi_entry: index.multiEntry === true,
|
||||
})),
|
||||
});
|
||||
|
||||
const indexedDbSchemaComplete = (
|
||||
stores: readonly Readonly<Record<string, unknown>>[],
|
||||
): boolean =>
|
||||
stores.every(
|
||||
(store) =>
|
||||
stringValue(store.name) !== undefined &&
|
||||
stableValueComplete(store.keyPath) &&
|
||||
typeof store.autoIncrement === "boolean" &&
|
||||
Array.isArray(store.indexes) &&
|
||||
recordsValue(store.indexes).length === store.indexes.length &&
|
||||
recordsValue(store.indexes).every(
|
||||
(index) =>
|
||||
stringValue(index.name) !== undefined &&
|
||||
stableValueComplete(index.keyPath) &&
|
||||
typeof index.unique === "boolean" &&
|
||||
typeof index.multiEntry === "boolean",
|
||||
),
|
||||
);
|
||||
|
||||
const addIndexedDbRecords = async (
|
||||
run: FingerprintRun,
|
||||
input: {
|
||||
readonly origin: string;
|
||||
readonly databaseName: string;
|
||||
readonly objectStoreName: string;
|
||||
},
|
||||
): Promise<void> => {
|
||||
const remaining = run.state.maximum - run.state.items.length;
|
||||
if (remaining <= 0) {
|
||||
run.state.truncated = true;
|
||||
return;
|
||||
}
|
||||
const raw = await optionalCdpCommand(
|
||||
run.context,
|
||||
"IndexedDB.requestData",
|
||||
{
|
||||
securityOrigin: input.origin,
|
||||
databaseName: input.databaseName,
|
||||
objectStoreName: input.objectStoreName,
|
||||
indexName: "",
|
||||
skipCount: 0,
|
||||
pageSize: remaining,
|
||||
},
|
||||
run.limitations,
|
||||
);
|
||||
const result = recordValue(raw);
|
||||
if (raw === undefined || result === undefined) {
|
||||
run.state.complete = false;
|
||||
return;
|
||||
}
|
||||
if (
|
||||
!Array.isArray(result.objectStoreDataEntries) ||
|
||||
typeof result.hasMore !== "boolean"
|
||||
) {
|
||||
run.state.complete = false;
|
||||
return;
|
||||
}
|
||||
const entries = recordsValue(result.objectStoreDataEntries);
|
||||
if (entries.length !== result.objectStoreDataEntries.length)
|
||||
run.state.complete = false;
|
||||
for (const entry of entries) {
|
||||
const key = stableValue(entry.key);
|
||||
const primaryKey = stableValue(entry.primaryKey);
|
||||
const value = stableValue(entry.value);
|
||||
addFingerprint(run.state, {
|
||||
scope: "indexed_db_record",
|
||||
identity: {
|
||||
database: input.databaseName,
|
||||
store: input.objectStoreName,
|
||||
key,
|
||||
primaryKey,
|
||||
},
|
||||
value,
|
||||
complete: remoteObjectComplete(entry.value),
|
||||
});
|
||||
}
|
||||
if (result.hasMore === true || entries.length > remaining)
|
||||
run.state.truncated = true;
|
||||
};
|
||||
|
||||
const addCaches = async (
|
||||
run: FingerprintRun,
|
||||
caches: readonly CapturedCache[],
|
||||
): Promise<void> => {
|
||||
for (const cache of caches) {
|
||||
const remaining = run.state.maximum - run.state.items.length;
|
||||
if (remaining <= 0) {
|
||||
run.state.truncated = true;
|
||||
return;
|
||||
}
|
||||
const raw = await optionalCdpCommand(
|
||||
run.context,
|
||||
"CacheStorage.requestEntries",
|
||||
{ cacheId: cache.id, skipCount: 0, pageSize: remaining },
|
||||
run.limitations,
|
||||
);
|
||||
const result = recordValue(raw);
|
||||
if (raw === undefined || result === undefined) {
|
||||
run.state.complete = false;
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
!Array.isArray(result.cacheDataEntries) ||
|
||||
typeof result.returnCount !== "number"
|
||||
) {
|
||||
run.state.complete = false;
|
||||
continue;
|
||||
}
|
||||
const entries = recordsValue(result.cacheDataEntries);
|
||||
if (entries.length !== result.cacheDataEntries.length)
|
||||
run.state.complete = false;
|
||||
for (const entry of entries) await addCacheEntry(run, cache, entry);
|
||||
if (result.returnCount > entries.length) run.state.truncated = true;
|
||||
}
|
||||
};
|
||||
|
||||
const addCacheEntry = async (
|
||||
run: FingerprintRun,
|
||||
cache: CapturedCache,
|
||||
entry: Readonly<Record<string, unknown>>,
|
||||
): Promise<void> => {
|
||||
const requestURL = stringValue(entry.requestURL);
|
||||
const requestMethod = stringValue(entry.requestMethod);
|
||||
if (requestURL === undefined || requestMethod === undefined) {
|
||||
run.state.complete = false;
|
||||
return;
|
||||
}
|
||||
if (!Array.isArray(entry.requestHeaders)) {
|
||||
run.state.complete = false;
|
||||
return;
|
||||
}
|
||||
const headerRecords = recordsValue(entry.requestHeaders);
|
||||
const requestHeaders = headerRecords.flatMap((header) => {
|
||||
const name = stringValue(header.name);
|
||||
const value = stringValue(header.value);
|
||||
return name === undefined || value === undefined ? [] : [{ name, value }];
|
||||
});
|
||||
if (
|
||||
headerRecords.length !== entry.requestHeaders.length ||
|
||||
requestHeaders.length !== headerRecords.length
|
||||
) {
|
||||
run.state.complete = false;
|
||||
return;
|
||||
}
|
||||
const raw = await optionalCdpCommand(
|
||||
run.context,
|
||||
"CacheStorage.requestCachedResponse",
|
||||
{ cacheId: cache.id, requestURL, requestHeaders },
|
||||
run.limitations,
|
||||
);
|
||||
const body = stringValue(recordValue(recordValue(raw)?.response)?.body);
|
||||
const decoded = body === undefined ? null : decodeBase64(body);
|
||||
const complete =
|
||||
decoded !== null && decoded.byteLength <= MAX_CACHE_BODY_BYTES;
|
||||
if (!complete) run.state.complete = false;
|
||||
addFingerprint(run.state, {
|
||||
scope: "cache_entry",
|
||||
identity: { cache: cache.name, requestURL, requestMethod, requestHeaders },
|
||||
value: decoded ?? undefined,
|
||||
complete,
|
||||
});
|
||||
};
|
||||
|
||||
const addFingerprint = (
|
||||
state: FingerprintState,
|
||||
input: {
|
||||
readonly scope: StorageFingerprint["scope"];
|
||||
readonly identity: unknown;
|
||||
readonly value: unknown;
|
||||
readonly complete: boolean;
|
||||
},
|
||||
): void => {
|
||||
if (state.items.length >= state.maximum) {
|
||||
state.truncated = true;
|
||||
return;
|
||||
}
|
||||
state.items.push({
|
||||
scope: input.scope,
|
||||
identity_sha256: digest(input.identity),
|
||||
value_sha256: input.value === undefined ? null : digest(input.value),
|
||||
complete:
|
||||
input.complete &&
|
||||
stableValueComplete(input.identity) &&
|
||||
stableValueComplete(input.value),
|
||||
});
|
||||
};
|
||||
|
||||
const digest = (value: unknown): string =>
|
||||
createHash("sha256").update(encodedValue(value)).digest("hex");
|
||||
|
||||
const encodedValue = (value: unknown): Uint8Array | string => {
|
||||
if (value instanceof Uint8Array) return value;
|
||||
const encoded = canonicalize(stableValue(value));
|
||||
if (encoded === undefined) throw new TypeError("Expected canonical JSON");
|
||||
return encoded;
|
||||
};
|
||||
|
||||
const stableValue = (value: unknown, depth = 0): unknown => {
|
||||
if (
|
||||
value === null ||
|
||||
typeof value === "string" ||
|
||||
typeof value === "number" ||
|
||||
typeof value === "boolean"
|
||||
)
|
||||
return value;
|
||||
if (depth >= 8) return "<depth-limit>";
|
||||
if (Array.isArray(value))
|
||||
return value.slice(0, 64).map((item) => stableValue(item, depth + 1));
|
||||
const record = recordValue(value);
|
||||
if (record === undefined) return String(value);
|
||||
return Object.fromEntries(
|
||||
Object.entries(record)
|
||||
.filter(([key]) => key !== "objectId" && key !== "customPreview")
|
||||
.sort(([left], [right]) => compareCodePoints(left, right))
|
||||
.slice(0, 64)
|
||||
.map(([key, item]) => [key, stableValue(item, depth + 1)]),
|
||||
);
|
||||
};
|
||||
|
||||
const stableValueComplete = (value: unknown, depth = 0): boolean => {
|
||||
if (value instanceof Uint8Array) return true;
|
||||
if (
|
||||
value === null ||
|
||||
value === undefined ||
|
||||
typeof value === "string" ||
|
||||
typeof value === "number" ||
|
||||
typeof value === "boolean"
|
||||
)
|
||||
return true;
|
||||
if (depth >= 8) return false;
|
||||
if (Array.isArray(value))
|
||||
return (
|
||||
value.length <= 64 &&
|
||||
value.every((item) => stableValueComplete(item, depth + 1))
|
||||
);
|
||||
const record = recordValue(value);
|
||||
if (record === undefined) return false;
|
||||
const entries = Object.entries(record).filter(
|
||||
([key]) => key !== "objectId" && key !== "customPreview",
|
||||
);
|
||||
return (
|
||||
entries.length <= 64 &&
|
||||
entries.every(([, item]) => stableValueComplete(item, depth + 1))
|
||||
);
|
||||
};
|
||||
|
||||
const remoteObjectComplete = (value: unknown): boolean => {
|
||||
const record = recordValue(value);
|
||||
if (record === undefined) return true;
|
||||
return (
|
||||
record.objectId === undefined &&
|
||||
record.preview === undefined &&
|
||||
record.customPreview === undefined &&
|
||||
(record.type !== "object" || record.value !== undefined) &&
|
||||
stableValueComplete(record)
|
||||
);
|
||||
};
|
||||
|
||||
const decodeBase64 = (value: string): Buffer | null => {
|
||||
if (
|
||||
value.length % 4 !== 0 ||
|
||||
!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/u.test(
|
||||
value,
|
||||
)
|
||||
)
|
||||
return null;
|
||||
return Buffer.from(value, "base64");
|
||||
};
|
||||
|
||||
const compareCodePoints = (left: string, right: string): number =>
|
||||
left < right ? -1 : left > right ? 1 : 0;
|
||||
@@ -386,6 +386,7 @@ const accessibilityForFrames = async (
|
||||
);
|
||||
if (capture.textCapture.status === "truncated")
|
||||
events.completeness.truncate("accessibility");
|
||||
if (capture.treeIncomplete) events.completeness.truncate("accessibility");
|
||||
return capture;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user