diff --git a/CHANGELOG.md b/CHANGELOG.md index a26651bc..383e1ad4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). ## [Unreleased] +## [1.25.0] - 2026-06-24 + +### Added + +- **Opt-in redaction-at-capture — `KIT_MEMORY_REDACT=1` (#91).** The memory store is raw by default (a key in any transcript is persisted to `memory.db` in cleartext — a stolen-laptop / backup-spillage risk). With `KIT_MEMORY_REDACT=1`, secret-shaped substrings in a message's `content` and a tool's `tool_input` are masked (via the same `redactSecrets` patterns) **before** they are written, so the secret never lands in the DB. Off by default → no behavior change; FTS still works over the redacted text for non-secret terms. + +### Removed + +- **Dead `Redacted` wrapper (#92).** The `src/utils/redacted.ts` wrapper (added in 1.15.0) had no importers; removed it and its test. (Secret masking is done by `redactSecrets`, which stays.) + ## [1.24.0] - 2026-06-24 Consolidated security-hardening batch (merged PRs #70–#94) + two recovered features. diff --git a/package-lock.json b/package-lock.json index 1ad8137d..055e1a31 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "sandstream-kit", - "version": "1.24.0", + "version": "1.25.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "sandstream-kit", - "version": "1.24.0", + "version": "1.25.0", "license": "MIT", "workspaces": [ "packages/*" diff --git a/package.json b/package.json index 864a8d6a..3fd739cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sandstream-kit", - "version": "1.24.0", + "version": "1.25.0", "description": "developer kit. zero LLM, local-first, multi-vault. one command from git clone to working dev environment.", "license": "MIT", "funding": "https://buymeacoffee.com/sandstream", diff --git a/src/memory/db.test.ts b/src/memory/db.test.ts index add71987..cc5f0f1e 100644 --- a/src/memory/db.test.ts +++ b/src/memory/db.test.ts @@ -246,3 +246,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 e9689d60..c6eaccff 100644 --- a/src/memory/db.ts +++ b/src/memory/db.ts @@ -18,9 +18,26 @@ import { join } from "node:path"; import { existsSync, mkdirSync, chmodSync, statSync } from "node:fs"; import type { MemoryStats, MessageInput, SearchHit, SessionInput, ToolUseInput } from "./types.js"; import { summarizeTokens } from "./stats.js"; +import { redactSecrets } from "../utils/redactSecrets.js"; export const SCHEMA_VERSION = 4; +/** + * 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"); } @@ -253,7 +270,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, @@ -281,7 +298,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, ); } diff --git a/src/utils/redacted.test.ts b/src/utils/redacted.test.ts deleted file mode 100644 index 3d1f5017..00000000 --- a/src/utils/redacted.test.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { describe, it } from "node:test"; -import assert from "node:assert/strict"; -import { inspect } from "node:util"; -import { Redacted, redacted, isRedacted } from "./redacted.js"; - -describe("Redacted", () => { - const secret = redacted("sk_live_supersecret"); - - it("exposes the real value only via .expose()", () => { - assert.equal(secret.expose(), "sk_live_supersecret"); - }); - - it("masks on String() / template / toString", () => { - assert.equal(String(secret), ""); - assert.equal(`${secret}`, ""); - }); - - it("masks in JSON.stringify, including when nested in an object", () => { - assert.equal(JSON.stringify(secret), '""'); - assert.equal(JSON.stringify({ apiKey: secret }), '{"apiKey":""}'); - }); - - it("masks under util.inspect / console.log", () => { - assert.match(inspect(secret), //); - assert.doesNotMatch(inspect(secret), /supersecret/); - }); - - it("does not expose the value via own keys / Object spread", () => { - assert.deepEqual(Object.keys(secret), []); - assert.doesNotMatch(JSON.stringify({ ...secret }), /supersecret/); - }); - - it("isRedacted narrows correctly", () => { - assert.equal(isRedacted(secret), true); - assert.equal(isRedacted("plain"), false); - assert.equal(isRedacted(new Redacted(123)), true); - }); - - it("works for non-string payloads", () => { - const r = redacted({ token: "abc", n: 1 }); - assert.deepEqual(r.expose(), { token: "abc", n: 1 }); - assert.equal(String(r), ""); - }); -}); diff --git a/src/utils/redacted.ts b/src/utils/redacted.ts deleted file mode 100644 index d1590b48..00000000 --- a/src/utils/redacted.ts +++ /dev/null @@ -1,45 +0,0 @@ -/** - * `Redacted` — a secret wrapper whose value never leaks through string - * coercion, `JSON.stringify`, `console.log`/`util.inspect`, or object-key - * enumeration. The only way to read the real value is the explicit `.expose()`. - * - * Borrowed from Effect's `Redacted` pattern — the *pattern*, not the framework - * (see issue: borrow the wrapper, don't adopt Effect). The value is held in a - * module-private WeakMap, so it is not an instance property and cannot be - * reached by reflection/serialization. - */ -const store = new WeakMap, unknown>(); - -const MASK = ""; - -export class Redacted { - constructor(value: T) { - store.set(this, value); - } - - /** The only path to the underlying value. Call sites become grep-able. */ - expose(): T { - return store.get(this) as T; - } - - toString(): string { - return MASK; - } - - toJSON(): string { - return MASK; - } - - // console.log / util.inspect - [Symbol.for("nodejs.util.inspect.custom")](): string { - return MASK; - } -} - -export function redacted(value: T): Redacted { - return new Redacted(value); -} - -export function isRedacted(x: unknown): x is Redacted { - return x instanceof Redacted; -}