mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 22:15:50 +00:00
test(capture): consolidate redirect and URL admission checks (#13218)
This commit is contained in:
1 parent
9528005707
commit
0ee5f8461e
5 files changed
+136
-154
No files matched your search
@@ -708,6 +708,35 @@ it("redacts actual authentication fields and explicit binary values while preser
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects absolute HAR request spellings that would change when parsed", () => {
|
||||
const fixture = historicalHar();
|
||||
const entry = fixture.log.entries[0]!;
|
||||
for (const url of [
|
||||
"http:\\\\user:pass@host.test/",
|
||||
"https://host.test/a\tb",
|
||||
"https://host.test/a b",
|
||||
]) {
|
||||
expect(
|
||||
() =>
|
||||
decodeHarCapture(
|
||||
JSON.stringify({
|
||||
log: {
|
||||
...fixture.log,
|
||||
entries: [{ ...entry, request: { ...entry.request, url } }],
|
||||
},
|
||||
}),
|
||||
[],
|
||||
),
|
||||
url,
|
||||
).toThrow(
|
||||
expect.objectContaining({
|
||||
failure: expect.objectContaining({ kind: "malformed" }),
|
||||
pointer: "/log/entries/0/request/url",
|
||||
}),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves observed SaveHar null post-data without broadening arbitrary HAR producers", () => {
|
||||
const fixture = historicalHar();
|
||||
const first = fixture.log.entries[0];
|
||||
|
||||
@@ -106,6 +106,9 @@ cliTest(
|
||||
const evidence = parseEvidence(output.json);
|
||||
const capture = webNetworkCaptureSchema.parse(evidence.normalized_result);
|
||||
expect(capture.total_records).toBe(redirects.length);
|
||||
expect(capture.records.map(({ ordinal }) => ordinal)).toEqual([
|
||||
0, 1, 2, 3,
|
||||
]);
|
||||
for (const [
|
||||
ordinal,
|
||||
{ retained, retainedRedirect },
|
||||
@@ -121,18 +124,41 @@ cliTest(
|
||||
],
|
||||
},
|
||||
});
|
||||
if (ordinal < 2)
|
||||
expect(record.redactions).not.toContainEqual({
|
||||
pointer: "/response/headers/0/value",
|
||||
reason: "transport-credential",
|
||||
});
|
||||
const redirectRedactions = record.redactions.filter(
|
||||
({ pointer }) =>
|
||||
pointer === "/response/headers/0/value" ||
|
||||
pointer === "/response/redirectURL",
|
||||
);
|
||||
expect(redirectRedactions).toEqual(
|
||||
ordinal < 2
|
||||
? []
|
||||
: [
|
||||
{
|
||||
pointer: "/response/headers/0/value",
|
||||
reason: "transport-credential",
|
||||
},
|
||||
...(format === "har"
|
||||
? [
|
||||
{
|
||||
pointer: "/response/redirectURL",
|
||||
reason: "transport-credential",
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
);
|
||||
}
|
||||
expect(JSON.stringify(capture)).not.toMatch(/source-pass|redirect-pass/u);
|
||||
expect(JSON.stringify(output.json)).not.toMatch(
|
||||
/source-pass|redirect-pass/u,
|
||||
);
|
||||
const reply = await call("inspect_web_network_capture", {
|
||||
capture_path: path,
|
||||
format,
|
||||
});
|
||||
expect(reply.isError, JSON.stringify(reply)).not.toBe(true);
|
||||
expect(JSON.stringify(reply.structuredContent)).not.toMatch(
|
||||
/source-pass|redirect-pass/u,
|
||||
);
|
||||
expect(parseEvidence(reply.structuredContent).evidence_id).toBe(
|
||||
evidence.evidence_id,
|
||||
);
|
||||
|
||||
@@ -43,6 +43,24 @@ const startLifecycleBrowser = () =>
|
||||
milestone("loader-before-window-replayed-after-arm", 19),
|
||||
milestone("loader-current-window", 21),
|
||||
milestone("loader-malformed-clock", "21"),
|
||||
{
|
||||
method: "Network.requestWillBeSent",
|
||||
sessionId,
|
||||
params: {
|
||||
requestId: "redirect-chain",
|
||||
frameId: "frame-main",
|
||||
loaderId: "redirect-loader",
|
||||
type: "Document",
|
||||
timestamp: 22,
|
||||
request: { url: `${origin}/after`, method: "GET" },
|
||||
redirectResponse: {
|
||||
url: `${origin}/before?token=visible`,
|
||||
status: 302,
|
||||
headers: { Authorization: "discard-redirect-header" },
|
||||
body: "discard-redirect-body",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
method: "Page.frameNavigated",
|
||||
sessionId,
|
||||
@@ -62,7 +80,7 @@ const startLifecycleBrowser = () =>
|
||||
});
|
||||
|
||||
cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
"retains in-window navigation and lifecycle evidence through %s",
|
||||
"retains in-window navigation, redirects, and lifecycle evidence through %s",
|
||||
async (surface, { cli }) => {
|
||||
const browser = await startLifecycleBrowser();
|
||||
onTestFinished(() => browser.close());
|
||||
@@ -95,6 +113,7 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
evidence = response.structuredContent;
|
||||
}
|
||||
|
||||
expect(JSON.stringify(evidence)).not.toContain("discard-redirect-");
|
||||
const contract = toolContract("observe_web_session");
|
||||
const sourceEvidence = contract.outputSchema.parse(evidence);
|
||||
const normalized = z
|
||||
@@ -129,6 +148,49 @@ cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
detail: "backforwardcacherestore",
|
||||
}),
|
||||
);
|
||||
expect(
|
||||
normalized.timeline.filter(({ type }) => type === "redirect"),
|
||||
).toEqual([
|
||||
expect.objectContaining({
|
||||
frame_id: "frame-main",
|
||||
loader_id: "redirect-loader",
|
||||
request_id: "redirect-chain",
|
||||
timestamp: 22,
|
||||
url: `${browser.allowedOrigin}/after`,
|
||||
destination_scope: "approved",
|
||||
redirect_response: {
|
||||
url: `${browser.allowedOrigin}/before?token=visible`,
|
||||
status: 302,
|
||||
scope: "approved",
|
||||
},
|
||||
}),
|
||||
]);
|
||||
if (surface === "CLI") {
|
||||
const index = normalized.timeline.findIndex(
|
||||
({ type }) => type === "redirect",
|
||||
);
|
||||
const redirect = normalized.timeline[index]!;
|
||||
for (const altered of [
|
||||
{ ...redirect, redirect_response: undefined },
|
||||
{ ...redirect, type: "lifecycle" as const },
|
||||
]) {
|
||||
const timeline = normalized.timeline.with(index, altered);
|
||||
expect(
|
||||
webObservationSessionSchema.safeParse({ ...normalized, timeline }),
|
||||
).toMatchObject({
|
||||
success: false,
|
||||
error: {
|
||||
issues: [
|
||||
expect.objectContaining({
|
||||
path: ["timeline", index, "redirect_response"],
|
||||
message:
|
||||
"redirect_response is present exactly on redirect events.",
|
||||
}),
|
||||
],
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
expect(normalized.limitations).toContainEqual(
|
||||
expect.stringMatching(
|
||||
/Buffered events.*unknown timing.*context only.*do not establish.*observation window/iu,
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
import { expect, onTestFinished } from "vitest";
|
||||
import { z } from "zod";
|
||||
import { toolContract } from "../../src/contracts/toolContracts.js";
|
||||
import { webObservationSessionSchema } from "../../src/domain/browserSession.js";
|
||||
import { startFakeCdpBrowser } from "../fixtures/fakeCdpBrowser.js";
|
||||
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
|
||||
import { cliTest } from "../support/cli/cliFixture.js";
|
||||
|
||||
cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
"retains admitted redirect response facts exactly on redirect events through %s",
|
||||
async (surface, { cli }) => {
|
||||
const browser = await startFakeCdpBrowser({
|
||||
eventsBeforeReply: ["Page.setLifecycleEventsEnabled"],
|
||||
commandResult: (command) =>
|
||||
command.method === "Performance.getMetrics"
|
||||
? { metrics: [{ name: "Timestamp", value: 20 }] }
|
||||
: undefined,
|
||||
commandEvents: (command, origin) =>
|
||||
command.method === "Page.setLifecycleEventsEnabled"
|
||||
? [
|
||||
{
|
||||
method: "Network.requestWillBeSent",
|
||||
sessionId: command.sessionId,
|
||||
params: {
|
||||
requestId: "redirect-chain",
|
||||
frameId: "frame-main",
|
||||
type: "Document",
|
||||
timestamp: 21,
|
||||
request: { url: `${origin}/after`, method: "GET" },
|
||||
redirectResponse: {
|
||||
url: `${origin}/before?token=visible`,
|
||||
status: 302,
|
||||
headers: { Authorization: "discard-redirect-header" },
|
||||
body: "discard-redirect-body",
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
: undefined,
|
||||
});
|
||||
onTestFinished(() => browser.close());
|
||||
let evidence: unknown;
|
||||
if (surface === "CLI") {
|
||||
const reply = await cli.run({
|
||||
arguments: [
|
||||
"observe-web-session",
|
||||
browser.endpoint,
|
||||
"allowed-page",
|
||||
"--allowed-origins",
|
||||
browser.allowedOrigin,
|
||||
"--observation-ms",
|
||||
"5",
|
||||
"--json",
|
||||
],
|
||||
});
|
||||
expect(reply.exitCode, reply.stderr).toBe(0);
|
||||
evidence = reply.json;
|
||||
} else {
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
const reply = await call("observe_web_session", {
|
||||
cdp_endpoint: browser.endpoint,
|
||||
allowed_origins: [browser.allowedOrigin],
|
||||
target_id: "allowed-page",
|
||||
observation_ms: 5,
|
||||
});
|
||||
expect(reply.isError, JSON.stringify(reply.content)).not.toBe(true);
|
||||
evidence = reply.structuredContent;
|
||||
}
|
||||
const source = toolContract("observe_web_session").outputSchema.parse(
|
||||
evidence,
|
||||
);
|
||||
const result = z
|
||||
.object({ normalized_result: webObservationSessionSchema })
|
||||
.parse(source).normalized_result;
|
||||
expect(result.timeline).toContainEqual(
|
||||
expect.objectContaining({
|
||||
type: "redirect",
|
||||
request_id: "redirect-chain",
|
||||
timestamp: 21,
|
||||
url: `${browser.allowedOrigin}/after`,
|
||||
redirect_response: {
|
||||
url: `${browser.allowedOrigin}/before?token=visible`,
|
||||
status: 302,
|
||||
scope: "approved",
|
||||
},
|
||||
}),
|
||||
);
|
||||
expect(JSON.stringify(source)).not.toContain("discard-redirect-");
|
||||
const omitted = {
|
||||
...result,
|
||||
timeline: result.timeline.map(({ redirect_response, ...row }) => {
|
||||
void redirect_response;
|
||||
return row;
|
||||
}),
|
||||
};
|
||||
const misplaced = {
|
||||
...result,
|
||||
timeline: result.timeline.map((row) => ({
|
||||
...row,
|
||||
type: "lifecycle" as const,
|
||||
})),
|
||||
};
|
||||
for (const malformed of [omitted, misplaced])
|
||||
expect(webObservationSessionSchema.safeParse(malformed)).toMatchObject({
|
||||
success: false,
|
||||
error: {
|
||||
issues: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
message:
|
||||
"redirect_response is present exactly on redirect events.",
|
||||
}),
|
||||
]),
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -42,37 +42,24 @@ const BROWSER_SERIALIZED = [
|
||||
];
|
||||
|
||||
cliTest(
|
||||
"keeps admitting an RFC 3986 request URL that WHATWG would rewrite",
|
||||
async ({ cli }) => {
|
||||
const url = "https://EXAMPLE.test/a";
|
||||
expect(new URL(url).href).not.toBe(url);
|
||||
const har = await writeHar([url]);
|
||||
const inspected = await cli.run({
|
||||
arguments: ["inspect-web-network-capture", har, "har", "--json"],
|
||||
});
|
||||
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
|
||||
expect(inspected.json).toMatchObject({
|
||||
normalized_result: { records: [{ reported: { request: { url } } }] },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"inspects and builds HARs whose request URLs are browser serializations",
|
||||
"inspects and builds HARs with RFC 3986 and browser-serialized request URLs",
|
||||
async ({ cli }) => {
|
||||
for (const url of BROWSER_SERIALIZED)
|
||||
expect(new URL(url).href, "fixture must be a WHATWG serialization").toBe(
|
||||
url,
|
||||
);
|
||||
const har = await writeHar(BROWSER_SERIALIZED);
|
||||
const rewrittenByWhatwg = "https://EXAMPLE.test/a";
|
||||
expect(new URL(rewrittenByWhatwg).href).toBe("https://example.test/a");
|
||||
const urls = [rewrittenByWhatwg, ...BROWSER_SERIALIZED];
|
||||
const har = await writeHar(urls);
|
||||
const inspected = await cli.run({
|
||||
arguments: ["inspect-web-network-capture", har, "har", "--json"],
|
||||
});
|
||||
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
|
||||
expect(inspected.json).toMatchObject({
|
||||
normalized_result: {
|
||||
total_records: BROWSER_SERIALIZED.length,
|
||||
records: BROWSER_SERIALIZED.map((url) => ({
|
||||
total_records: urls.length,
|
||||
records: urls.map((url) => ({
|
||||
reported: { request: { url } },
|
||||
})),
|
||||
},
|
||||
@@ -83,21 +70,15 @@ cliTest(
|
||||
expect(built.exitCode, built.stdout + built.stderr).toBe(0);
|
||||
expect(built.json).toMatchObject({
|
||||
status: "planned",
|
||||
capture: { records: BROWSER_SERIALIZED.length, input: "har" },
|
||||
capture: { records: urls.length, input: "har" },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
cliTest.for([
|
||||
// WHATWG rewrites these spellings, so the recorded text and its reading differ.
|
||||
{ name: "backslash authority", url: "http:\\\\user:pass@host.test/" },
|
||||
{ name: "tab", url: "https://host.test/a\tb" },
|
||||
{ name: "space", url: "https://host.test/a b" },
|
||||
{ name: "relative reference", url: "/a|b" },
|
||||
])(
|
||||
"still rejects a request URL that is neither RFC 3986 nor a WHATWG serialization: $name",
|
||||
async ({ url }, { cli }) => {
|
||||
const har = await writeHar([url]);
|
||||
cliTest(
|
||||
"rejects a relative HAR request URL at the public capture path coordinate",
|
||||
async ({ cli }) => {
|
||||
const har = await writeHar(["/a|b"]);
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-web-network-capture", har, "har", "--json"],
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user