Files
seal-use/src/core/util/redact.test.ts
T
gitea a189f49223 Group core utility modules into util, skill, and update
Move duration, redact, runtime, secure-file, and semver under core/util; embedded-skill and skill-version under core/skill; update-cache under core/update. Move the dev-override test next to the script it covers, and update the embedded-skill generator output path and regenerated manifest. No behavior change.
2026-11-19 16:31:50 +08:00

58 lines
2.1 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { redactSensitiveText } from "./redact.js";
describe("redactSensitiveText", () => {
test("redacts credential query parameters while preserving the rest of the URL", () => {
const input = "https://seal.example/api/hosecloud/outbound?config_id=keep-me&signKey=super-secret&z=1";
const output = redactSensitiveText(input);
expect(output).toContain("config_id=keep-me");
expect(output).toContain("signKey=<redacted>");
expect(output).not.toContain("super-secret");
expect(output).toContain("z=1");
});
test("redacts Bearer tokens", () => {
const output = redactSensitiveText("Authorization failed for Bearer abc.def.ghi");
expect(output).toContain("Bearer <redacted>");
expect(output).not.toContain("abc.def.ghi");
});
test("redacts credential fields in JSON without corrupting the document", () => {
const input = `{"signKey":"abc123","appSecurity":"sec","name":"keep"}`;
const output = redactSensitiveText(input);
expect(output).toContain(`"signKey":"<redacted>"`);
expect(output).toContain(`"appSecurity":"<redacted>"`);
expect(output).toContain(`"name":"keep"`);
expect(output).not.toContain("abc123");
// The output must still be parseable: a broken replacement would leave a
// literal `$2`/`$3` and drop the closing quote.
expect(() => JSON.parse(output)).not.toThrow();
expect(output).not.toMatch(/\$\d/);
});
test("leaves text without secrets untouched", () => {
const input = "plain text with no credentials at all";
expect(redactSensitiveText(input)).toBe(input);
});
test("covers every credential field name the schema allows", () => {
const fields = [
"appSecurity",
"password",
"secret",
"signKey",
"closeToken",
"refreshToken",
"accessToken",
"authorization"
];
for (const field of fields) {
const output = redactSensitiveText(`{"${field}":"value-should-vanish"}`);
expect(output).toContain(`"${field}":"<redacted>"`);
expect(output).not.toContain("value-should-vanish");
}
});
});