Files
reverse-engineering-anything/src/browser/BrowserScenarioSecrets.test.ts
T
morluto 2d1748117e fix(browser): redact literal credentials in browser scenarios unless retained (#15347)
* fix(browser): redact literal credentials in browser scenarios unless retained

capture_browser_scenario records the scenario in Evidence parameters as
written. A literal Authorization header value or cookie seed therefore stayed
verbatim in Evidence, while the same record's network events redacted it.

Literal values of transport-credential headers (isTransportCredentialHeader)
and cookie seeds are now redacted like declared secrets. One domain owner,
scenarioLiteralCredentials, lists them with their redaction identities; the
capture session adds them to its redaction set, and Evidence parameters and
scenario_sha256 record the redacted scenario. A caller that wants the literal
kept sets retain_literal_credentials: true. Other literals stay ordinary
evidence; secrecy is not inferred from names.

Refs #15340

* test(browser): assert whole-value redaction instead of any change

not.toBe(value) also passed for partial redaction, such as a marker appended
with the secret left in place. Assert that each redacted value is exactly one
marker and that no fragment of the secret or its JSON-escaped form remains in
text, bytes, URLs or protected response bodies.

Refs #15340

* fix(browser): redact the trimmed spelling of padded literal credentials and commit the schema summary

* fix(browser): treat a credential header's prefix as part of its literal credential and retain fingerprint fixtures

* fix(browser): keep the bare literal token redacted beside the prefixed header
2026-12-01 02:27:15 +00:00

224 lines
8.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { browserScenarioSchema } from "../domain/browserScenario.js";
import { BrowserScenarioSecrets } from "./BrowserScenarioSecrets.js";
import { readResponseValues } from "./BrowserScenarioResponseValues.js";
const scenario = browserScenarioSchema.parse({
browser: { mode: "launch", executable_path: "/fixture/chrome" },
start_url: { url: "https://example.test/" },
actions: [
{
step_id: "save",
action: "request",
method: "GET",
path: "/value",
save: [{ name: "token", json_pointer: "/token", secret: true }],
},
],
});
it.each([
'private-\ud800-"\\token',
'private-\udc00-"\\token',
'private-\ud83d\ude80-"\\token',
])(
"retains exact saved values and redacts their JSON, UTF-8 and URL representations: %j",
(token) => {
const resolved = BrowserScenarioSecrets.resolve(scenario, {});
if (!resolved.ok) throw resolved.error;
const secrets = resolved.value;
const action = scenario.actions[0]!;
if (action.action !== "request") throw new Error("Expected request");
const body = JSON.stringify({ token, other: "visible" });
const selected = readResponseValues(Buffer.from(body), action.save, "fail");
if (!selected.ok) throw new Error(selected.error.message);
expect(selected.value.values[0]!.value).toBe(token);
for (const { entry, value } of selected.value.values)
secrets.save("save", entry.name, value, entry.secret);
expect(secrets.savedValue("token")).toBe(token);
const scalar = new TextDecoder().decode(new TextEncoder().encode(token));
const marker = /^\[REDACTED:[^\]]+\]$/u;
for (const value of [token, scalar]) {
// The whole value becomes one marker; no fragment of it survives.
expect(secrets.redact(value)).toMatch(marker);
const escaped = JSON.stringify(value);
const redactedJson = secrets.redact(escaped);
expect(redactedJson).not.toContain(escaped.slice(1, -1));
expect(JSON.parse(redactedJson)).toMatch(marker);
expect(secrets.fingerprint(value)).toEqual({
value_state: "redacted-secret",
value_sha256: null,
});
}
const rawBytes = Buffer.from(token);
expect(secrets.redactBytes(rawBytes).includes(rawBytes)).toBe(false);
const destination = new URL("https://example.test/");
destination.searchParams.set("value", token);
expect(secrets.sanitizeUrl(destination.href)).toMatchObject({
redacted: true,
});
const sanitized = secrets.sanitizeUrl(destination.href).url;
expect(new URL(sanitized).searchParams.get("value")).toMatch(marker);
const request = {};
secrets.responseProtector("save", action.save)!(request);
const protectedBody = secrets.admitResponseSecrets(
request,
Buffer.from(body),
{ maxBytes: 4096, maxAllocationBytes: 65536 },
);
expect(JSON.parse(protectedBody.toString())).toMatchObject({
other: "visible",
});
expect(JSON.parse(protectedBody.toString()).token).toMatch(marker);
expect(secrets.redact("unrelated value")).toBe("unrelated value");
},
);
describe("browser scenario cursor redaction", () => {
it("redacts multiple occurrences, longest-match overlaps, percent case escapes, and reconstructed secrets", () => {
const resolved = BrowserScenarioSecrets.resolve(
browserScenarioSchema.parse({
browser: { mode: "launch", executable_path: "/fixture/chrome" },
start_url: { url: "https://example.test/" },
secrets: [
{ secret_id: "short", environment_variable: "SHORT_SECRET" },
{ secret_id: "long", environment_variable: "LONG_SECRET" },
{ secret_id: "pct", environment_variable: "PCT_SECRET" },
{ secret_id: "outer", environment_variable: "OUTER_SECRET" },
],
actions: [
{
step_id: "save",
action: "request",
method: "GET",
path: "/value",
save: [{ name: "token", json_pointer: "/token", secret: true }],
},
],
}),
{
SHORT_SECRET: "alpha-secret",
LONG_SECRET: "alpha-secret-extended",
PCT_SECRET: "tok/en",
OUTER_SECRET: "pre[REDACTED:short]post",
},
);
if (!resolved.ok) throw resolved.error;
const secrets = resolved.value;
const clean = "nothing-sensitive-present&foo=bar";
expect(secrets.redact(clean)).toBe(clean);
const multi =
"first=alpha-secret&second=alpha-secret-extended&url=tok%2fen&upper=tok%2Fen";
const redactedMulti = secrets.redact(multi);
expect(redactedMulti).toBe(
"first=[REDACTED:short]&second=[REDACTED:long]&url=[REDACTED:pct]&upper=[REDACTED:pct]",
);
const sanitized = secrets.sanitizeUrl(
"https://example.test/?q=tok%2Fen&long=alpha-secret-extended",
);
expect(sanitized.redacted).toBe(true);
expect(sanitized.url).not.toContain("tok");
expect(sanitized.url).not.toContain("alpha-secret");
// When marker substitution forms another declared secret, the post-replacement check wipes the output.
expect(secrets.redact("prealpha-secretpost")).toBe("");
});
});
describe("browser scenario literal credentials", () => {
const literalScenario = (
retain: boolean,
authorization = "fixture-bearer-token",
prefix?: string,
) =>
browserScenarioSchema.parse({
browser: { mode: "launch", executable_path: "/fixture/chrome" },
start_url: { url: "https://example.test/" },
retain_literal_credentials: retain,
actions: [
{
step_id: "call",
action: "request",
method: "GET",
path: "/api",
headers: [
{
name: "Authorization",
...(prefix === undefined ? {} : { prefix }),
value: { source: "literal", value: authorization },
},
],
},
],
storage: {
cookies: [
{
name: "session",
value: { source: "literal", value: "fixture-session-value" },
destination: { url: "https://example.test/" },
http_only: true,
secure: true,
same_site: "Lax",
},
],
},
});
it("redacts inline credentials from captured text like declared secrets", () => {
const resolved = BrowserScenarioSecrets.resolve(literalScenario(false), {});
if (!resolved.ok) throw resolved.error;
const echoed = "echo fixture-bearer-token and fixture-session-value";
const redacted = resolved.value.redact(echoed);
expect(redacted).not.toContain("fixture-bearer-token");
expect(redacted).not.toContain("fixture-session-value");
expect(redacted).toContain("[REDACTED:literal:call.authorization]");
});
it("redacts the trimmed spelling fetch sends for a padded inline credential", () => {
const resolved = BrowserScenarioSecrets.resolve(
literalScenario(false, " Bearer fixture-bearer-token\t"),
{},
);
if (!resolved.ok) throw resolved.error;
expect(resolved.value.redact("echo Bearer fixture-bearer-token.")).toBe(
"echo [REDACTED:literal:call.authorization].",
);
});
it("redacts a credential carried by the header prefix", () => {
const resolved = BrowserScenarioSecrets.resolve(
literalScenario(false, "", "Bearer fixture-prefix-token"),
{},
);
if (!resolved.ok) throw resolved.error;
expect(resolved.value.redact("echo Bearer fixture-prefix-token.")).toBe(
"echo [REDACTED:literal:call.authorization].",
);
});
it("redacts both the sent header and the bare token behind a prefix", () => {
const resolved = BrowserScenarioSecrets.resolve(
literalScenario(false, "fixture-bare-token", "Bearer "),
{},
);
if (!resolved.ok) throw resolved.error;
expect(
resolved.value.redact(
"header Bearer fixture-bare-token; body fixture-bare-token.",
),
).toBe(
"header [REDACTED:literal:call.authorization]; body [REDACTED:literal:call.authorization.value].",
);
});
it("leaves inline credentials visible when the caller retains them", () => {
const resolved = BrowserScenarioSecrets.resolve(literalScenario(true), {});
if (!resolved.ok) throw resolved.error;
const echoed = "echo fixture-bearer-token";
expect(resolved.value.redact(echoed)).toBe(echoed);
});
});