diff --git a/src/core/redact.test.ts b/src/core/redact.test.ts new file mode 100644 index 0000000..29f9712 --- /dev/null +++ b/src/core/redact.test.ts @@ -0,0 +1,57 @@ +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="); + 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 "); + 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":""`); + expect(output).toContain(`"appSecurity":""`); + 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}":""`); + expect(output).not.toContain("value-should-vanish"); + } + }); +}); diff --git a/src/core/redact.ts b/src/core/redact.ts index 5d9c656..2b3caeb 100644 --- a/src/core/redact.ts +++ b/src/core/redact.ts @@ -1,10 +1,15 @@ /** - * Redacts credential-bearing patterns from free text before it reaches any output channel - * (stdout JSON payloads, stderr messages, HTTP error responses). + * Redacts credential-bearing patterns from free text before it reaches an + * output channel. * - * Lives in core because domain handlers embed upstream error messages — which may contain - * full request URLs (`?token=...`) or echoed bodies (`"appSecurity":"..."`) — into their - * results; the CLI printer and the HTTP adapter apply the same rules at their boundaries. + * Applied to: error messages surfaced by the CLI runner (`src/cli/runner.ts`) and + * HTTP error responses (`src/http/routes.ts`). + * + * NOT applied to successful stdout payloads: those are redacted where they are + * built — handlers mask fields via `presentWebhookIntegration`, + * `redactIntegrationSecrets`, `maskHoseSecurity` and `formatSafeSourceDetail`. + * Blanket redaction of stdout would also destroy deliberate credential-delivery + * output (`auth link` tokens, `extract-keys --reveal`), so it stays per-handler. */ export function redactSensitiveText(value: string): string { return value @@ -12,6 +17,6 @@ export function redactSensitiveText(value: string): string { .replace(/(Bearer\s+)[A-Za-z0-9._~+/=-]+/gi, "$1") .replace( /("(?:appSecurity|password|secret|signKey|closeToken|refreshToken|accessToken|authorization)"\s*:\s*")[^"]*(")/gi, - "$1$3" + "$1$2" ); } diff --git a/src/domains/seal/tools.test.ts b/src/domains/seal/tools.test.ts index 49e3248..93298fd 100644 --- a/src/domains/seal/tools.test.ts +++ b/src/domains/seal/tools.test.ts @@ -1026,6 +1026,49 @@ describe("sealTools", () => { }); }); + test("deploy hosecloud status masks signKey alongside apiKey/apiSecret", async () => { + const tool = sealTools.find((item) => item.name === "seal_deploy_hosecloud_status"); + expect(tool).toBeDefined(); + + const handler = tool!.handler as ToolHandler; + const client = { + get: (url: string) => { + if (url === "api/v1/simulation/integration-status") { + return { json: async () => ({ data: { hasHosecloud: true } }) }; + } + if (url === "api/v1/agent/ai-approval/config") { + return { + json: async () => ({ + data: { + integrations: [ + { + type: "hosecloud", + signKey: "visible-sign-key-123", + apiKey: "ak-123", + apiSecret: "as-123", + apiOrigin: "production" + } + ] + } + }) + }; + } + throw new Error(`unexpected url: ${url}`); + } + } as unknown as KyInstance; + + const result = (await handler(client, {}, fakeContext())) as { + integrations: Array>; + }; + + // signKey rides in the outbound callback URL as config_id, so the + // diagnostic view must treat it like the other credentials. + expect(result.integrations[0]!.signKey).toBe("***"); + expect(result.integrations[0]!.apiKey).toBe("***"); + expect(result.integrations[0]!.apiSecret).toBe("***"); + expect(JSON.stringify(result)).not.toContain("visible-sign-key-123"); + }); + test("seal_approval_runs_count supports startDate and endDate and normalizes them", async () => { const tool = sealTools.find((item) => item.name === "seal_approval_runs_count"); expect(tool).toBeDefined(); diff --git a/src/domains/seal/tools/helpers.ts b/src/domains/seal/tools/helpers.ts index ed5f434..60e6e60 100644 --- a/src/domains/seal/tools/helpers.ts +++ b/src/domains/seal/tools/helpers.ts @@ -257,7 +257,13 @@ export function redactIntegrationSecrets(integration: Record) { return Object.fromEntries( Object.entries(integration).map(([key, value]) => [ key, - ["apiKey", "apiSecret", "secret"].includes(key) && typeof value === "string" ? "***" : value + // signKey participates in the outbound callback URL as config_id, so it + // behaves like a credential: the diagnostic views mask it the same way as + // apiKey/apiSecret. The operator still receives it through the stderr + // guidance lines (`👉 Next action: ... sync `) when they need it. + ["apiKey", "apiSecret", "secret", "signKey"].includes(key) && typeof value === "string" + ? "***" + : value ]) ); }