diff --git a/src/memory/db.test.ts b/src/memory/db.test.ts index 4f00e7df..3529f903 100644 --- a/src/memory/db.test.ts +++ b/src/memory/db.test.ts @@ -165,3 +165,35 @@ describe("memory db", () => { db.close(); }); }); + +describe("redaction-at-capture (KIT_MEMORY_REDACT)", () => { + const SECRET = ["sk", "live", "Z".repeat(40)].join("_"); + function insertAndRead(redact: boolean): string { + const prev = process.env.KIT_MEMORY_REDACT; + if (redact) process.env.KIT_MEMORY_REDACT = "1"; + else delete process.env.KIT_MEMORY_REDACT; + try { + const db = openMemoryDb(":memory:"); + upsertSession(db, { sessionId: "s", harness: "claude-code" }); + insertMessage(db, { uuid: "u", sessionId: "s", type: "user", content: `key ${SECRET}` }); + const row = db.prepare("SELECT content FROM messages WHERE uuid = ?").get("u") as { + content: string; + }; + db.close(); + return row.content; + } finally { + if (prev === undefined) delete process.env.KIT_MEMORY_REDACT; + else process.env.KIT_MEMORY_REDACT = prev; + } + } + + it("stores raw content by default (opt-in off)", () => { + assert.ok(insertAndRead(false).includes(SECRET)); + }); + + it("masks secrets at capture when KIT_MEMORY_REDACT=1", () => { + const c = insertAndRead(true); + assert.ok(!c.includes(SECRET), "secret must not be persisted"); + assert.match(c, /\[REDACTED\]/); + }); +}); diff --git a/src/memory/db.ts b/src/memory/db.ts index 448eb2a2..41ab0876 100644 --- a/src/memory/db.ts +++ b/src/memory/db.ts @@ -17,9 +17,26 @@ import { homedir } from "node:os"; import { join } from "node:path"; import { existsSync, mkdirSync, chmodSync, statSync } from "node:fs"; import type { MemoryStats, MessageInput, SearchHit, SessionInput, ToolUseInput } from "./types.js"; +import { redactSecrets } from "../utils/redactSecrets.js"; export const SCHEMA_VERSION = 3; +/** + * Opt-in redaction-at-capture (KIT_MEMORY_REDACT=1). The memory store is raw by + * default; a regulated/air-gapped deployment can have secret-shaped substrings + * masked BEFORE they ever land in memory.db, so a leaked key in a transcript is + * never persisted (spillage prevention). Off by default — no behavior change. + */ +function captureRedactEnabled(): boolean { + return ["1", "true", "yes"].includes((process.env.KIT_MEMORY_REDACT ?? "").trim().toLowerCase()); +} + +/** Apply capture-time redaction to a stored text field when enabled. */ +function captureText(text: string | null | undefined): string | null { + if (text == null) return null; + return captureRedactEnabled() ? redactSecrets(text) : text; +} + export function getMemoryDir(): string { return process.env.KIT_MEMORY_DIR ?? join(homedir(), ".kit"); } @@ -235,7 +252,7 @@ export function insertMessage(db: DatabaseSync, m: MessageInput): boolean { m.parentUuid ?? null, m.type, m.role ?? null, - m.content ?? null, + captureText(m.content), m.model ?? null, m.inputTokens ?? null, m.outputTokens ?? null, @@ -261,7 +278,7 @@ export function insertToolUse(db: DatabaseSync, t: ToolUseInput): void { t.messageUuid ?? null, t.sessionId ?? null, t.toolName, - t.toolInput ?? null, + captureText(t.toolInput), t.timestamp ?? null, ); }