Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/memory/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\]/);
});
});
21 changes: 19 additions & 2 deletions src/memory/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
);
}
Expand Down
Loading