mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
494 lines
14 KiB
TypeScript
494 lines
14 KiB
TypeScript
import { expect, it, vi } from "vitest";
|
|
import { changedEvidenceSubject } from "../../../tests/fixtures/evidenceSubject.js";
|
|
import { createEvidence } from "../evidence.js";
|
|
import {
|
|
freezeJsonSnapshot,
|
|
isImmutableJsonSnapshot,
|
|
} from "../immutableJson.js";
|
|
import * as webNetworkCapture from "../webNetworkCapture.js";
|
|
import { ANALYSIS_VIEW_PAGE_BYTES } from "./analysisViewErrors.js";
|
|
import { projectAnalysisView } from "./analysisView.js";
|
|
import * as protobufCaptureView from "./protobufCaptureView.js";
|
|
|
|
const capture = async () => {
|
|
const report = webNetworkCapture.webNetworkCaptureSchema.parse({
|
|
artifact: { path: "/recordings/a.har", bytes: 100, sha256: "b".repeat(64) },
|
|
decoder: {
|
|
id: "projection-fixture",
|
|
name: "Original projection facts",
|
|
version: "1",
|
|
},
|
|
container: {
|
|
reported: null,
|
|
numeric_literals: [],
|
|
redactions: [],
|
|
records_pointer: "/log/entries",
|
|
},
|
|
format: "har",
|
|
total_records: 1,
|
|
runtime_attribution: "unknown",
|
|
limitations: [],
|
|
records: [
|
|
{
|
|
ordinal: 0,
|
|
location: { kind: "json-pointer", pointer: "/log/entries/0" },
|
|
reported: null,
|
|
binary_fields: [],
|
|
numeric_literals: [],
|
|
redactions: [],
|
|
limitations: [],
|
|
protobuf: [
|
|
{
|
|
decoder: "rea-protobuf-wire-v1",
|
|
pointer: "/response/content/text",
|
|
content_type: "application/grpc-web+proto",
|
|
body_sha256: "c".repeat(64),
|
|
framing: "grpc-web",
|
|
offset_basis: "retained-body",
|
|
decoded_bytes: 37,
|
|
status: "complete",
|
|
examined_bytes: 37,
|
|
limitations: [],
|
|
frames: [
|
|
{
|
|
index: 0,
|
|
kind: "data",
|
|
offset: 0,
|
|
payload_offset: 5,
|
|
bytes: 11,
|
|
compressed: false,
|
|
status: "complete",
|
|
examined_bytes: 11,
|
|
},
|
|
{
|
|
index: 1,
|
|
kind: "trailer",
|
|
offset: 16,
|
|
payload_offset: 21,
|
|
bytes: 16,
|
|
compressed: false,
|
|
status: "complete",
|
|
examined_bytes: 16,
|
|
},
|
|
],
|
|
fields: [
|
|
{
|
|
index: 0,
|
|
frame_index: 0,
|
|
parent_index: null,
|
|
field_id: "offset:5",
|
|
parent_field_id: null,
|
|
number: 1,
|
|
wire_type: "len",
|
|
offset: 5,
|
|
bytes: 7,
|
|
payload_offset: 7,
|
|
payload_bytes: 5,
|
|
text: "hello",
|
|
interpretations: ["utf8-string", "packed-varint", "bytes"],
|
|
},
|
|
{
|
|
index: 1,
|
|
frame_index: 0,
|
|
parent_index: null,
|
|
field_id: "offset:12",
|
|
parent_field_id: null,
|
|
number: 2,
|
|
wire_type: "len",
|
|
offset: 12,
|
|
bytes: 4,
|
|
payload_offset: 14,
|
|
payload_bytes: 2,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
const evidence = createEvidence(
|
|
{ ...report.artifact, sha256: "b".repeat(64), format: "file" },
|
|
report.decoder,
|
|
{
|
|
operation: "inspect_web_network_capture",
|
|
parameters: { record_ordinals: [0] },
|
|
result: report,
|
|
confidence: "observed",
|
|
authority: "historical-reference",
|
|
},
|
|
);
|
|
return {
|
|
report,
|
|
parent: {
|
|
predicateType: evidence.predicate_type,
|
|
evidenceId: evidence.evidence_id,
|
|
operation: evidence.operation,
|
|
normalizedResult: report,
|
|
subject: evidence.subject,
|
|
limitations: evidence.limitations,
|
|
},
|
|
};
|
|
};
|
|
|
|
it("keeps coordinate identity and coverage with empty fields, batch filters and offset paging", async () => {
|
|
const { parent } = await capture();
|
|
const viewed = projectAnalysisView(parent, {
|
|
kind: "protobuf",
|
|
collection: "fields",
|
|
field_ids: ["offset:5", "offset:12"],
|
|
fields: [],
|
|
offset: 1,
|
|
limit: 1,
|
|
});
|
|
if (!viewed.ok) throw viewed.error;
|
|
expect(viewed.value).toMatchObject({
|
|
kind: "protobuf",
|
|
coverage: { total: 2, examined: 1, exhausted: true },
|
|
});
|
|
if (viewed.value.kind !== "protobuf")
|
|
throw new Error("Expected Protobuf view");
|
|
const identity = {
|
|
record_ordinal: 0,
|
|
body_pointer: "/response/content/text",
|
|
body_sha256: "c".repeat(64),
|
|
body_status: "complete",
|
|
offset_basis: "retained-body",
|
|
};
|
|
expect(viewed.value.items).toStrictEqual([
|
|
{
|
|
...identity,
|
|
index: 1,
|
|
frame_index: 0,
|
|
field_id: "offset:12",
|
|
parent_field_id: null,
|
|
offset: 12,
|
|
payload_offset: 14,
|
|
payload_bytes: 2,
|
|
},
|
|
]);
|
|
for (const collection of ["bodies", "frames"] as const) {
|
|
const page = projectAnalysisView(parent, {
|
|
kind: "protobuf",
|
|
collection,
|
|
fields: [],
|
|
offset: 0,
|
|
limit: 1,
|
|
});
|
|
if (!page.ok) throw page.error;
|
|
if (page.value.kind !== "protobuf")
|
|
throw new Error("Expected Protobuf view");
|
|
expect(page.value.items).toStrictEqual([
|
|
collection === "bodies" ? identity : { ...identity, index: 0, offset: 0 },
|
|
]);
|
|
}
|
|
const empty = projectAnalysisView(parent, {
|
|
kind: "protobuf",
|
|
collection: "fields",
|
|
field_numbers: [],
|
|
offset: 0,
|
|
limit: 1,
|
|
});
|
|
if (!empty.ok) throw empty.error;
|
|
expect(empty.value).toMatchObject({
|
|
items: [],
|
|
coverage: { total: 0, exhausted: true },
|
|
});
|
|
});
|
|
|
|
it("omits large values by default, returns requested text inline and preserves parser uncertainty", async () => {
|
|
const { parent, report } = await capture();
|
|
const brief = projectAnalysisView(parent, {
|
|
kind: "protobuf",
|
|
collection: "fields",
|
|
offset: 0,
|
|
limit: 1,
|
|
});
|
|
if (!brief.ok) throw brief.error;
|
|
expect(brief.value).toMatchObject({
|
|
items: [{ interpretations: ["utf8-string", "packed-varint", "bytes"] }],
|
|
});
|
|
expect(JSON.stringify(brief.value)).not.toContain('"hello"');
|
|
const preview = projectAnalysisView(parent, {
|
|
kind: "capture",
|
|
detail: "preview",
|
|
offset: 0,
|
|
limit: 1,
|
|
});
|
|
if (!preview.ok) throw preview.error;
|
|
expect(preview.value).toMatchObject({
|
|
items: [
|
|
{
|
|
protobuf_summary: {
|
|
bodies: 1,
|
|
frames: 2,
|
|
fields: 2,
|
|
incomplete_bodies: 0,
|
|
},
|
|
},
|
|
],
|
|
});
|
|
expect(JSON.stringify(preview.value)).not.toContain('"field_id"');
|
|
report.records[0]!.protobuf![0]!.status = "partial";
|
|
const full = projectAnalysisView(parent, {
|
|
kind: "protobuf",
|
|
collection: "fields",
|
|
fields: ["text"],
|
|
offset: 0,
|
|
limit: 1,
|
|
});
|
|
if (!full.ok) throw full.error;
|
|
expect(full.value).toMatchObject({
|
|
source_status: "partial",
|
|
items: [{ text: "hello", body_status: "partial" }],
|
|
});
|
|
const changedSubject = projectAnalysisView(
|
|
{
|
|
...parent,
|
|
subject: changedEvidenceSubject(parent.subject, {
|
|
sha256: "e".repeat(64),
|
|
}),
|
|
},
|
|
{ kind: "protobuf", collection: "bodies", offset: 0, limit: 1 },
|
|
);
|
|
expect(changedSubject.ok).toBe(false);
|
|
report.records[0]!.protobuf![0]!.fields[0]!.offset = 1000;
|
|
expect(
|
|
projectAnalysisView(parent, {
|
|
kind: "protobuf",
|
|
collection: "fields",
|
|
offset: 0,
|
|
limit: 1,
|
|
}).ok,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("reuses one frozen capture snapshot across repeated webNetworkCapture and protobuf pages", async () => {
|
|
const { report, parent } = await capture();
|
|
const sharedReported = {
|
|
request: { method: "GET", url: "https://example.test" },
|
|
};
|
|
const record0 = report.records[0]!;
|
|
const multi = webNetworkCapture.webNetworkCaptureSchema.parse({
|
|
...report,
|
|
total_records: 2,
|
|
records: [
|
|
{ ...record0, ordinal: 0, reported: sharedReported },
|
|
{
|
|
...record0,
|
|
ordinal: 1,
|
|
location: { kind: "json-pointer", pointer: "/log/entries/1" },
|
|
reported: sharedReported,
|
|
},
|
|
],
|
|
});
|
|
const sealedSnapshot = freezeJsonSnapshot({
|
|
...multi,
|
|
records: multi.records.map((entry) => ({
|
|
...entry,
|
|
reported: sharedReported,
|
|
})),
|
|
});
|
|
const sealedParent = {
|
|
...parent,
|
|
sealedResult: true,
|
|
normalizedResult: sealedSnapshot,
|
|
};
|
|
const unsealedParent = {
|
|
...parent,
|
|
sealedResult: false,
|
|
normalizedResult: structuredClone(multi),
|
|
};
|
|
const protobufPage = (offset: number) => ({
|
|
kind: "protobuf" as const,
|
|
collection: "fields" as const,
|
|
field_numbers: [1, 2],
|
|
offset,
|
|
limit: 2,
|
|
});
|
|
const capturePreview = () => ({
|
|
kind: "capture" as const,
|
|
detail: "preview" as const,
|
|
offset: 0,
|
|
limit: 2,
|
|
});
|
|
|
|
const parseSpy = vi.spyOn(
|
|
webNetworkCapture.webNetworkCaptureSchema,
|
|
"safeParse",
|
|
);
|
|
const passedReports: unknown[] = [];
|
|
const originalProject = protobufCaptureView.projectProtobufCapturePage;
|
|
const protobufSpy = vi
|
|
.spyOn(protobufCaptureView, "projectProtobufCapturePage")
|
|
.mockImplementation((parsedReport, view, shared) => {
|
|
passedReports.push(parsedReport);
|
|
return originalProject(parsedReport, view, shared);
|
|
});
|
|
try {
|
|
const summary = projectAnalysisView(sealedParent, { kind: "summary" });
|
|
const page1 = projectAnalysisView(sealedParent, protobufPage(0));
|
|
const page2 = projectAnalysisView({ ...sealedParent }, protobufPage(2));
|
|
const preview = projectAnalysisView(sealedParent, capturePreview());
|
|
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
expect(protobufSpy).toHaveBeenCalledTimes(2);
|
|
expect(passedReports).toHaveLength(2);
|
|
expect(isImmutableJsonSnapshot(passedReports[0])).toBe(true);
|
|
expect(passedReports[1]).toBe(passedReports[0]);
|
|
|
|
expect(summary).toEqual(
|
|
projectAnalysisView(unsealedParent, { kind: "summary" }),
|
|
);
|
|
expect(page1).toEqual(projectAnalysisView(unsealedParent, protobufPage(0)));
|
|
expect(page2).toEqual(projectAnalysisView(unsealedParent, protobufPage(2)));
|
|
expect(preview).toEqual(
|
|
projectAnalysisView(unsealedParent, capturePreview()),
|
|
);
|
|
} finally {
|
|
parseSpy.mockRestore();
|
|
protobufSpy.mockRestore();
|
|
}
|
|
});
|
|
|
|
it("continues byte-limited protobuf pages across decoded bodies", async () => {
|
|
const { parent, report } = await capture();
|
|
const original = report.records[0]!.protobuf![0]!;
|
|
const payloadBytes = 80_004;
|
|
const fieldBytes = payloadBytes + 4;
|
|
const frameBytes = 6 * fieldBytes;
|
|
const records = Array.from({ length: 3 }, (_, ordinal) => ({
|
|
...report.records[0]!,
|
|
ordinal,
|
|
protobuf: [
|
|
{
|
|
...original,
|
|
decoded_bytes: frameBytes + 5,
|
|
examined_bytes: frameBytes + 5,
|
|
frames: [
|
|
{
|
|
...original.frames[0]!,
|
|
bytes: frameBytes,
|
|
examined_bytes: frameBytes,
|
|
},
|
|
],
|
|
fields: Array.from({ length: 6 }, (_, index) => {
|
|
const offset = 5 + index * fieldBytes;
|
|
return {
|
|
...original.fields[0]!,
|
|
index,
|
|
offset,
|
|
field_id: `offset:${String(offset)}`,
|
|
bytes: fieldBytes,
|
|
payload_offset: offset + 4,
|
|
payload_bytes: payloadBytes,
|
|
text: `${String(ordinal)}/${String(index)}:${"é".repeat(40_000)}`,
|
|
};
|
|
}),
|
|
},
|
|
],
|
|
}));
|
|
const source = {
|
|
...parent,
|
|
normalizedResult: { ...report, records, total_records: records.length },
|
|
};
|
|
const read = (offset: number) => {
|
|
const result = projectAnalysisView(source, {
|
|
kind: "protobuf",
|
|
collection: "fields",
|
|
fields: ["text"],
|
|
offset,
|
|
limit: 18,
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
if (result.value.kind !== "protobuf")
|
|
throw new Error("Expected protobuf page");
|
|
expect(Buffer.byteLength(JSON.stringify(result.value))).toBeLessThanOrEqual(
|
|
ANALYSIS_VIEW_PAGE_BYTES,
|
|
);
|
|
return result.value;
|
|
};
|
|
const first = read(0);
|
|
expect(first.coverage.total).toBe(18);
|
|
const next = first.coverage.next_offset;
|
|
if (next === null) throw new Error("Expected protobuf continuation");
|
|
expect(next).toBe(first.items.length);
|
|
expect(next).toBeGreaterThan(6);
|
|
const second = read(next);
|
|
expect(second.coverage.next_offset).toBeNull();
|
|
expect(
|
|
[...first.items, ...second.items].map((row) =>
|
|
"text" in row ? row.text : null,
|
|
),
|
|
).toEqual(
|
|
records.flatMap((record) =>
|
|
record.protobuf[0]!.fields.map((field) => field.text),
|
|
),
|
|
);
|
|
});
|
|
|
|
it("names the parent for an oversized field and permits a narrower projection", async () => {
|
|
const { parent, report } = await capture();
|
|
const body = report.records[0]!.protobuf![0]!;
|
|
const payloadBytes = ANALYSIS_VIEW_PAGE_BYTES;
|
|
const fieldBytes = payloadBytes + 4;
|
|
const source = {
|
|
...parent,
|
|
normalizedResult: {
|
|
...report,
|
|
records: [
|
|
{
|
|
...report.records[0]!,
|
|
protobuf: [
|
|
{
|
|
...body,
|
|
decoded_bytes: fieldBytes + 5,
|
|
examined_bytes: fieldBytes + 5,
|
|
frames: [
|
|
{
|
|
...body.frames[0]!,
|
|
bytes: fieldBytes,
|
|
examined_bytes: fieldBytes,
|
|
},
|
|
],
|
|
fields: [
|
|
{
|
|
...body.fields[0]!,
|
|
bytes: fieldBytes,
|
|
payload_offset: 9,
|
|
payload_bytes: payloadBytes,
|
|
text: "x".repeat(payloadBytes),
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
};
|
|
const view = {
|
|
kind: "protobuf",
|
|
collection: "fields",
|
|
offset: 0,
|
|
limit: 1,
|
|
} as const;
|
|
expect(
|
|
projectAnalysisView(source, { ...view, fields: ["text"] }),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
_tag: "AnalysisResourceConstraintError",
|
|
reportedLimits: {
|
|
collection: "fields",
|
|
parent_evidence_id: parent.evidenceId,
|
|
limit_bytes: ANALYSIS_VIEW_PAGE_BYTES,
|
|
},
|
|
},
|
|
});
|
|
expect(projectAnalysisView(source, { ...view, fields: [] })).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
items: [{ field_id: "offset:5", record_ordinal: 0 }],
|
|
coverage: { total: 1, next_offset: null },
|
|
},
|
|
});
|
|
});
|