Files
reverse-engineering-anything/src/domain/explicitSensitiveValues.test.ts
T
morluto a97a5b823d refactor(domain): redact declared sensitive values and transport credentials through one owner (#11327)
* refactor(domain): redact declared sensitive values and transport credentials through one owner

src/domain/explicitSensitiveValues.ts becomes the single owner of how a
declared value (a caller's explicit sensitive value or a REA-minted
transport credential) is found and replaced:

- redactExplicitText gains a preferred replacement marker (still refused
  when it would spell a declaration) and budget cut-off handling, and
  redactExplicitOutput applies cut-off from a stream's observed/retained
  bytes.
- explicitValuesOccurIn and explicitUtf8Spellings replace CaptureRedaction's
  private literal checks; empty declarations no longer match.
- TRANSPORT_CREDENTIAL_HEADERS / isTransportCredentialHeader replace three
  copies of the RFC 9110/6265 header table (capture, Playwright network,
  IDA registration). IDA now also treats Set-Cookie as an authentication
  header.

Migrated: process/ProviderDiagnosticRedaction.ts (deleted; Ghidra client,
diagnostics and launcher, Hopper launcher outcome), AgentEnvComputer's
control-token replaceAll, CaptureRedaction, QLOG, Playwright network
content, IDA registration and diagnostic redactor header checks.

* fix(ida): redact a configured Set-Cookie value without its attributes

* refactor(domain): spell and match declared values through the redaction owner (#11370)

* refactor(domain): spell and match declared values through the redaction owner

* fix(domain): yield declared-value spans lazily so budgeted callers stop early
2026-11-16 03:17:39 +08:00

175 lines
5.1 KiB
TypeScript

import { expect, it } from "vitest";
import {
explicitTextSpellings,
explicitUtf16Spellings,
explicitUtf8Spellings,
explicitValueSpans,
lowercasePercentEscapes,
explicitValuesOccurIn,
isTransportCredentialHeader,
redactExplicitOutput,
redactExplicitText,
} from "./explicitSensitiveValues.js";
it.each([
["secret context", ["secret"], "[REDACTED] context"],
["secret context", ["secret", "REDACTED"], "… context"],
["secret context", ["secret", "[", "…"], " context"],
// Overlapping declarations redact as one span, leaving no fragment between.
["secretable context", ["secret", "able", "table"], "[REDACTED] context"],
["secretsecret context", ["secret", "…", "REDACTED"], " context"],
] as const)(
"keeps declared literals out of replacement text: %s",
(input, values, expected) => {
const result = redactExplicitText(input, values);
expect(result).toBe(expected);
for (const value of values) expect(result).not.toContain(value);
},
);
it("does not guess sensitivity or treat empty declarations as matches", () => {
expect(redactExplicitText("API_SECRET ordinary evidence", [""])).toBe(
"API_SECRET ordinary evidence",
);
expect(explicitValuesOccurIn("API_SECRET ordinary evidence", [""])).toBe(
false,
);
});
it("uses a caller's marker unless it would spell a declaration", () => {
expect(
redactExplicitText("token=abc", ["abc"], {
replacement: "[control token]",
}),
).toBe("token=[control token]");
expect(
redactExplicitText("token=con", ["con"], {
replacement: "[control token]",
}),
).toBe("token=[REDACTED]");
});
it.each([
[
"complete output keeps an unrelated tail",
false,
"log private",
"log private",
],
[
"cut output replaces a declared prefix",
true,
"log private-tr",
"log [REDACTED]",
],
[
"cut output replaces whole values before the tail",
true,
"private-transport-token then private-",
"[REDACTED] then [REDACTED]",
],
[
"cut output without a declared prefix is unchanged",
true,
"log done",
"log done",
],
] as const)("%s", (_name, cutOff, text, expected) => {
expect(
redactExplicitText(text, ["private-transport-token"], { cutOff }),
).toBe(expected);
});
it("treats output as cut when more bytes were observed than retained", () => {
const token = "private-transport-token";
expect(
redactExplicitOutput(
{ text: token.slice(0, 8), bytes: 8, observedBytes: token.length },
[token],
),
).toBe("[REDACTED]");
expect(
redactExplicitOutput({ text: "priv", bytes: 4, observedBytes: 4 }, [token]),
).toBe("priv");
});
it("spells declarations in UTF-8 and skips values with no UTF-8 form", () => {
expect(explicitUtf8Spellings(["é", "\ud800", "", "é"])).toEqual([
Buffer.from("é", "utf8"),
]);
});
it("recognizes protocol-defined credential headers in any case only", () => {
for (const name of [
"Authorization",
"proxy-authorization",
"Cookie",
"SET-COOKIE",
])
expect(isTransportCredentialHeader(name)).toBe(true);
for (const name of ["x-api-key", "x-auth-token", "location"])
expect(isTransportCredentialHeader(name)).toBe(false);
});
it("merges overlapping occurrences of one or several declarations", () => {
expect([...explicitValueSpans("aaaa", ["aa"])]).toEqual([
{ start: 0, end: 4 },
]);
expect([
...explicitValueSpans("abcdef xx cd", ["abcd", "cdef", "cd"]),
]).toEqual([
{ start: 0, end: 6 },
{ start: 10, end: 12 },
]);
expect([...explicitValueSpans("abab", ["ab"])]).toEqual([
{ start: 0, end: 2 },
{ start: 2, end: 4 },
]);
expect([...explicitValueSpans("text", [""])]).toEqual([]);
});
it("spells a declared value in each selected encoding", () => {
expect(
explicitTextSpellings("a b'/é", [
"uri-component",
"uri-query",
"uri-plus-space",
"form",
]),
).toEqual([
"a%20b'%2F%C3%A9",
"a%20b%27%2F%C3%A9",
"a+b'%2F%C3%A9",
"a+b%27%2F%C3%A9",
]);
expect(explicitTextSpellings('q"\\n', ["json-escaped"])).toEqual([
'q\\"\\\\n',
]);
expect(explicitTextSpellings("ok", ["json-escaped"])).toEqual([]);
expect(explicitTextSpellings("\u00ff", ["hex"])).toEqual(["c3bf", "C3BF"]);
const base64 = explicitTextSpellings("secret-value", ["base64"]);
for (const prefix of ["", "x", "xy"])
expect(
base64.some((spelling) =>
Buffer.from(`${prefix}secret-value`)
.toString("base64")
.includes(spelling),
),
).toBe(true);
// URL serializers write an unpaired surrogate as U+FFFD.
expect(explicitTextSpellings("\ud800", ["uri-component"])).toEqual([
"%EF%BF%BD",
]);
expect(explicitUtf16Spellings("ab")).toEqual([
Buffer.from([0x61, 0, 0x62, 0]),
Buffer.from([0, 0x61, 0, 0x62]),
]);
expect(lowercasePercentEscapes("a%2Fb%C3")).toBe("a%2fb%c3");
});
it("finds spans lazily, so a budgeted caller stops matching early", () => {
const spans = explicitValueSpans("token ".repeat(100_000), ["token"]);
expect(spans.next().value).toEqual({ start: 0, end: 5 });
expect(spans.next().value).toEqual({ start: 6, end: 11 });
});