mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 15:05:51 +00:00
feat(browser): count credential-named members in HAR capture inspection (#8708)
* feat(browser): count credential-named members in HAR capture inspection inspect_web_network_capture now runs the credential-name scan that environment-build already applies over the selected HAR records and reports matches as limitations by pointer, never by value, together with the bodies the scan could not read. Values the decoder already excluded are not counted, and nothing is excluded on a name. Refs #8207 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(browser): bound the listed credential-name pointers The limitation lists the pointers that fit within 1 KiB of text and says how many counted names are not listed, so one long body member name cannot inflate the limitation that summaries and record pages repeat. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(environment): bound the credential scan's values before parsing a body A body under the scan's 4 MiB text limit could still expand into millions of JSON values or form fields when parsed in the application process, and the inspection now runs the scan on every HAR capture. The scan counts the characters that can start a value (`[`, `{`, `,` for JSON, `&` for forms) before parsing and skips a body that could hold more than 524288, reporting it like a body over the text limit. Both consumers' messages and docs name the bound. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(web): charge every body and name/value list to one credential-scan budget The credential-name scan bounded each body's values on its own, so a capture of many compact bodies still expanded millions of values in the application process, and HAR header, query and form-parameter lists were copied twice without any bound. One 524288-value budget now covers the whole scan: a body's values are bounded from its text before it is parsed and a list is charged by its length before it is read in place, so what would pass the budget is reported as not scanned instead. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(web): read credential names in place and charge nothing for empty text The credential-name scan split each member, header and query name with two regex replacements, a lowercase copy and a split into a segment array before matching. A near-4 MiB alternating-case member name (`x-aAaA…`) therefore expanded into about two million segments: more than 192 MiB of heap in the application process after the decoder's containment had ended. `credentialNamed` now reads the name once in place with the same segmentation (case changes, digits, separators, and `toLowerCase`'s KELVIN SIGN and dotted capital I), keeping at most two segments no longer than the longest credential word, so a name of any length allocates no copies. `valueBound` started every text at one value, so an empty query, a bare `?`, an empty form and whitespace-only JSON were each charged a value they do not hold; once earlier content spent the budget, they were reported as unscanned in `credential-scan-incomplete`. JSON now holds its first value only after a non-whitespace character, and a form holds one field per non-empty `&`-separated piece after an optional leading `?`, as `URLSearchParams` and the query reader split it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(browser): point the environment build link at the physical guide The guide lives at docs/agent-environments.md, so the relative link from docs/guides/web must climb two levels. The Documentation job failed on the dead link; vitepress build now completes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * test: drop the hard-coded timeout from the heap-bound name test The test finishes in about half a second, so the shared test deadline is enough, and a literal 60_000 bypasses the CI and coverage deadline scaling. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * fix(web-network-capture): name an unavailable response source in the scan limitation The credential-name scan counts a response body whose retained source is unavailable as not scanned, and WebNetworkCaptureService reuses that scan. Its limitation text still listed only a cut body, text over 4 MiB, or past the value budget, so CLI and MCP reports gave none of those reasons for such a body. Name the unavailable source in the text, matching the environment-build warning, and cover it with a test that fails on the old wording. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
a16669ab2a
commit
3c7ea1ce85
8 files changed
+825
-81
No files matched your search
@@ -650,3 +650,230 @@ it("rejects relative descriptor paths before starting a capture decoder", async
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
/** HAR-shaped record whose credential-named members hold values the decoder retains. */
|
||||
const credentialNamedReport = (): WebNetworkCapture => {
|
||||
const report = fixture();
|
||||
report.records[0]!.reported = {
|
||||
request: {
|
||||
method: "POST",
|
||||
url: "https://example.test/login?api_key=demo-key",
|
||||
headers: [
|
||||
{ name: "Authorization", value: null },
|
||||
{ name: "X-Demo-Token", value: "demo-token" },
|
||||
{ name: "Accept", value: "application/json" },
|
||||
],
|
||||
queryString: [{ name: "api_key", value: "demo-key" }],
|
||||
postData: {
|
||||
mimeType: "application/json",
|
||||
text: '{"user":"demo","password":"demo-pass"}',
|
||||
},
|
||||
},
|
||||
response: {
|
||||
status: 200,
|
||||
headers: [{ name: "Set-Cookie", value: null }],
|
||||
content: {
|
||||
mimeType: "application/json",
|
||||
text: '{"session_token":"demo-session"}',
|
||||
},
|
||||
},
|
||||
};
|
||||
return report;
|
||||
};
|
||||
|
||||
const credentialNameLimitation = (limitations: readonly string[]) =>
|
||||
limitations.filter((text) => text.includes("look like credentials"));
|
||||
|
||||
it("counts credential-named members that hold a retained value, by pointer and never by value", async () => {
|
||||
const result = await new WebNetworkCaptureService({
|
||||
inspect: () => Promise.resolve(ok(credentialNamedReport())),
|
||||
}).inspect({ capture_path: "/capture.har", format: "har" });
|
||||
if (!result.ok) throw result.error;
|
||||
const [limitation, ...others] = credentialNameLimitation(
|
||||
result.value.limitations,
|
||||
);
|
||||
expect(others).toEqual([]);
|
||||
expect(limitation).toMatch(/^4 /);
|
||||
for (const pointer of [
|
||||
"record 0 request header X-Demo-Token",
|
||||
"record 0 request query api_key",
|
||||
"record 0 request /password",
|
||||
"record 0 response /session_token",
|
||||
])
|
||||
expect(limitation).toContain(pointer);
|
||||
// Redacted standard credentials and ordinary names are not counted.
|
||||
expect(limitation).not.toContain("Authorization");
|
||||
expect(limitation).not.toContain("Accept");
|
||||
for (const value of ["demo-token", "demo-key", "demo-pass", "demo-session"])
|
||||
expect(limitation).not.toContain(value);
|
||||
expect(result.value.normalized_result).toMatchObject({
|
||||
limitations: expect.arrayContaining([limitation]),
|
||||
});
|
||||
expect(parseEvidence(result.value)).toEqual(result.value);
|
||||
});
|
||||
|
||||
it("bounds the listed pointers so a long member name cannot inflate the limitation", async () => {
|
||||
const report = credentialNamedReport();
|
||||
const long = "k".repeat(2048);
|
||||
report.records[0]!.reported = {
|
||||
request: {
|
||||
method: "POST",
|
||||
url: "https://example.test/login",
|
||||
postData: {
|
||||
mimeType: "application/json",
|
||||
text: JSON.stringify({
|
||||
[long]: { token: "demo-token" },
|
||||
password: "p",
|
||||
}),
|
||||
},
|
||||
},
|
||||
response: { status: 204 },
|
||||
};
|
||||
const result = await new WebNetworkCaptureService({
|
||||
inspect: () => Promise.resolve(ok(report)),
|
||||
}).inspect({ capture_path: "/capture.har", format: "har" });
|
||||
if (!result.ok) throw result.error;
|
||||
const [limitation] = credentialNameLimitation(result.value.limitations);
|
||||
expect(limitation).toMatch(/^2 /);
|
||||
expect(limitation).toContain("record 0 request /password");
|
||||
expect(limitation).toContain("1 more not listed");
|
||||
expect(limitation).not.toContain(long);
|
||||
expect(limitation!.length).toBeLessThan(1024 + 400);
|
||||
});
|
||||
|
||||
it("adds no credential-name limitation when no counted name holds a value", async () => {
|
||||
const result = await new WebNetworkCaptureService({
|
||||
inspect: () => Promise.resolve(ok(fixture())),
|
||||
}).inspect({ capture_path: "/capture.har", format: "har" });
|
||||
if (!result.ok) throw result.error;
|
||||
expect(credentialNameLimitation(result.value.limitations)).toEqual([]);
|
||||
});
|
||||
|
||||
it("scans only HAR records for credential names", async () => {
|
||||
const report = credentialNamedReport();
|
||||
report.format = "mitmproxy";
|
||||
const result = await new WebNetworkCaptureService({
|
||||
inspect: () => Promise.resolve(ok(report)),
|
||||
}).inspect({ capture_path: "/capture.har", format: "mitmproxy" });
|
||||
if (!result.ok) throw result.error;
|
||||
expect(credentialNameLimitation(result.value.limitations)).toEqual([]);
|
||||
});
|
||||
|
||||
it("reports a body whose values would expand past the credential-name scan's bound", async () => {
|
||||
const report = credentialNamedReport();
|
||||
report.records[0]!.reported = {
|
||||
request: { method: "GET", url: "https://example.test/expansive" },
|
||||
response: {
|
||||
status: 200,
|
||||
content: {
|
||||
mimeType: "application/json",
|
||||
text: `[${"[],".repeat(300_000)}{"password":"x"}]`,
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = await new WebNetworkCaptureService({
|
||||
inspect: () => Promise.resolve(ok(report)),
|
||||
}).inspect({ capture_path: "/capture.har", format: "har" });
|
||||
if (!result.ok) throw result.error;
|
||||
expect(credentialNameLimitation(result.value.limitations)).toEqual([]);
|
||||
const skipped = result.value.limitations.filter((text) =>
|
||||
text.includes("were not scanned"),
|
||||
);
|
||||
expect(skipped).toHaveLength(1);
|
||||
expect(skipped[0]).toMatch(/^1 /);
|
||||
expect(skipped[0]).toContain("record 0 response");
|
||||
});
|
||||
|
||||
it("shares the credential-name scan's value budget across the selected records", async () => {
|
||||
const expansive = (path: string) => ({
|
||||
request: { method: "GET", url: `https://example.test/${path}` },
|
||||
response: {
|
||||
status: 200,
|
||||
content: {
|
||||
mimeType: "application/json",
|
||||
text: `[${"1,".repeat(300_000)}{"password":"x"}]`,
|
||||
},
|
||||
},
|
||||
});
|
||||
const base = credentialNamedReport();
|
||||
const first = { ...base.records[0]!, reported: expansive("first") };
|
||||
const report: WebNetworkCapture = {
|
||||
...base,
|
||||
total_records: 2,
|
||||
records: [
|
||||
first,
|
||||
{
|
||||
...first,
|
||||
ordinal: 1,
|
||||
location: { kind: "json-pointer", pointer: "/log/entries/1" },
|
||||
reported: expansive("second"),
|
||||
},
|
||||
],
|
||||
};
|
||||
const result = await new WebNetworkCaptureService({
|
||||
inspect: () => Promise.resolve(ok(report)),
|
||||
}).inspect({ capture_path: "/capture.har", format: "har" });
|
||||
if (!result.ok) throw result.error;
|
||||
const [found] = credentialNameLimitation(result.value.limitations);
|
||||
expect(found).toMatch(/^1 /);
|
||||
expect(found).toContain("record 0 response /300000/password");
|
||||
const skipped = result.value.limitations.filter((text) =>
|
||||
text.includes("were not scanned"),
|
||||
);
|
||||
expect(skipped).toHaveLength(1);
|
||||
expect(skipped[0]).toMatch(/^1 /);
|
||||
expect(skipped[0]).toContain("record 1 response");
|
||||
});
|
||||
|
||||
it("reports a response body whose retained source is unavailable", async () => {
|
||||
const report = credentialNamedReport();
|
||||
// A base64 body with no retained binary field has no source to decode.
|
||||
report.records[0]!.reported = {
|
||||
request: { method: "GET", url: "https://example.test/unretained" },
|
||||
response: {
|
||||
status: 200,
|
||||
content: {
|
||||
mimeType: "application/json",
|
||||
text: "eyJwYXNzd29yZCI6IngifQ==",
|
||||
encoding: "base64",
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = await new WebNetworkCaptureService({
|
||||
inspect: () => Promise.resolve(ok(report)),
|
||||
}).inspect({ capture_path: "/capture.har", format: "har" });
|
||||
if (!result.ok) throw result.error;
|
||||
expect(credentialNameLimitation(result.value.limitations)).toEqual([]);
|
||||
const skipped = result.value.limitations.filter((text) =>
|
||||
text.includes("were not scanned"),
|
||||
);
|
||||
expect(skipped).toHaveLength(1);
|
||||
expect(skipped[0]).toMatch(/^1 /);
|
||||
expect(skipped[0]).toContain("record 0 response");
|
||||
expect(skipped[0]).toContain("retained source is unavailable");
|
||||
});
|
||||
|
||||
it("reports bodies the credential-name scan could not read", async () => {
|
||||
const report = credentialNamedReport();
|
||||
report.records[0]!.reported = {
|
||||
request: { method: "GET", url: "https://example.test/large" },
|
||||
response: {
|
||||
status: 200,
|
||||
content: {
|
||||
mimeType: "application/json",
|
||||
text: `{"password":"${"x".repeat(4 * 1024 * 1024)}"}`,
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = await new WebNetworkCaptureService({
|
||||
inspect: () => Promise.resolve(ok(report)),
|
||||
}).inspect({ capture_path: "/capture.har", format: "har" });
|
||||
if (!result.ok) throw result.error;
|
||||
expect(credentialNameLimitation(result.value.limitations)).toEqual([]);
|
||||
const skipped = result.value.limitations.filter((text) =>
|
||||
text.includes("were not scanned"),
|
||||
);
|
||||
expect(skipped).toHaveLength(1);
|
||||
expect(skipped[0]).toMatch(/^1 /);
|
||||
expect(skipped[0]).toContain("record 0 response");
|
||||
});
|
||||
Reference in new issue
Block a user