SLOPSHOPPER

secret-guard

Hides secrets in tool results before Claude reads them: the secret values of every .env and .env.* from your session's folder up to the drive root, and…

guardcommandstatusprompt
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · secret-guard
› fix the failing auth test and add an audit log call ⏺ Read(src/auth.ts) ⎿ Read 6 lines ⏺ Update(src/auth.ts) ⎿ Added 2 lines, removed 1 line ⏺ Bash(cat .env) ⎿ Denied by secret-guard: secret-guard: this prints a protected env file (.env), secrets included. To see it ● Done. refresh now rejects expired claims and logs an audit event. ✻ Worked for 42s · done 4:20 PM › /secret-guard ⎿ secret-guard: on · mode value; no env file found above this session; values that look like secrets are still hidden; 0 value ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts
README

claude-code-mods-kit

Eight small Claude Code mods, free to use under the MIT licence. A mod is a plugin made of function hooks: Claude Code calls it at every step (a tool call, a slash command, a redraw), and it can answer, change or watch that step.

Each mod has its own on/off switch, /<mod> on, /<mod> off and /<mod> status, kept across sessions. secret-guard starts on; the other seven start off, so installing one changes nothing until you turn it on. See Switching a mod on and off.

ModWhat it doesCommand
secret-guardReads every .env and .env.* from your session's folder up to the drive root when a session starts and hides their secret values (keys named like PASSWORD, SECRET, TOKEN, API_KEY) in every tool result before Claude reads it, along with anything that looks like a secret on its own: KEY=VALUE under such a name, the password in postgres://user:REDACTED@host, private keys, JWTs, AWS keys, Kubernetes Secret data. A secret reaches Claude as ‹hidden: DB_PASSWORD›. It tells Claude, in every conversation, to use $DB_PASSWORD instead of printing the value, and it refuses the few commands that would print a whole env file or a decrypted secret./secret-guard lists the protected files and key names; /secret-guard off / on
plan-meterA one-line band above the prompt that says how far your plan is: plan ▸ Ship the export · phases 1/3 · steps 3/6 (50%) · now: API · Claude's tasks 2/5. It reads your plan file (and the phase files it links to) in many formats, and Claude's own task list. It updates when a file changes./plan-meter on / off / status; /plan-meter opens a pane with the details; /plan-meter docs/roadmap.md picks a file
done-gateWhen Claude marks a task done while code it changed has not been tested since, it tells Claude, in the tool result Claude reads, and you, in a toast. A band shows the last test run: done-gate ▸ tests ✔ passed 4 min ago · 2 files changed since. It warns; it never blocks./done-gate on / off; /done-gate status lists the changed files and the last test command
context-meterA band above the prompt with what fills the context window, by category and in /context's colours (context ▸ 90k of 1M · 9% · compacts at 987k), and a countdown to when the prompt cache expires, which turns from green through amber to red: cache ▸ 41:07 left (1h TTL, assumed: subscription). A Compact button (or c while the band has focus) runs the same compaction as /compact./context-meter on / off / status; the button
idea-shelfPark an idea while Claude works, without interrupting it: /idea try the bulk endpoint later keeps it on this project's shelf, across sessions. The band counts them (ideas ▸ 3 ideas parked [Shelf]); the shelf lists them with Send, Edit and Delete. Send puts the idea in the prompt box, or sends it when the box is empty and Claude is idle. A parked idea never reaches the model until you send it./idea <text>; /idea opens the shelf; /idea-shelf on / off / status
session-monitorSee your other Claude Code sessions from this one: other sessions ▸ 1 waiting · 2 working · 1 done [List], waiting first, in the colour for "needs you". Sessions in this same folder get their own line: also here ▸ "Fix the export" · working. Waiting means a permission dialog or a question is open./session-monitor on / off / status; /session-monitor list opens the list
prompt-enhancerAn Enhance button above the prompt: Haiku rewrites your draft so it names the skills that fit, lists what Claude should ask you first, and says what done looks like. The rewrite replaces the draft; nothing is sent until you press Enter or Send./prompt-enhancer on / off / status; the buttons
assumption-checkAfter a turn that edited files or ran commands, Haiku lists the choices Claude made that you did not state (assumptions ▸ 6 to check [Review]): a date format, a default, a library. Mark each Right or Wrong, add a note, and Send corrections puts one prompt in the box that says what to change and what to keep./assumption-check on / off / status; /assumption-check review opens the list

Tested on Claude Code 2.1.296 on Windows 11 (secret-guard, plan-meter, done-gate and context-meter first on 2.1.291 to 2.1.295). Mods are an early-access feature, so the API can change between versions; if a mod stops loading after an update, check claude plugin validate on its folder.

Try one without installing anything

git clone https://github.com/vumichien/claude-code-mods-kit.git
claude --plugin-dir claude-code-mods-kit/plugins/plan-meter

--plugin-dir loads the mod for that session only. Repeat the flag to load more than one.

Install

claude plugin marketplace add vumichien/claude-code-mods-kit
claude plugin install secret-guard@chien-mods
claude plugin install plan-meter@chien-mods
claude plugin install done-gate@chien-mods
claude plugin install context-meter@chien-mods
claude plugin install idea-shelf@chien-mods
claude plugin install session-monitor@chien-mods
claude plugin install prompt-enhancer@chien-mods
claude plugin install assumption-check@chien-mods

Start a new session afterwards. To remove one: claude plugin uninstall plan-meter@chien-mods.

Switching a mod on and off

Every mod answers /<mod> on, /<mod> off and /<mod> status (a bare /<mod> is status). The switch is kept in the mod's own store, so it holds in every later session, in every folder, until you change it.

  • Off means off. Every hook passes its event on unchanged: no band, no pane, no file written, no model call, no note to Claude, no timer. The mod still registers its command, so that you can turn it on.
  • secret-guard starts on, because a guard that has to be remembered protects nothing. Off, the status line says secret-guard OFF: secrets not hidden, and its note to Claude and the hiding both stop.
  • The other seven start off. Turn on the ones you want: /plan-meter on, /idea-shelf on, and so on.
  • claude plugin disable <mod>@chien-mods still unloads a mod entirely, and each one's tests include one that feeds every hooked event to the mod while it is off and finds each one passed on unchanged.

Up to version 0.2 of plan-meter, done-gate and context-meter, on and off only showed or hid the band for the session; now they switch the whole mod, and the choice stays.

Options

Every option has a default, so all eight mods work without any. plan-meter, done-gate and context-meter share one: band, on (default) or off. With off, a mod that is switched on keeps working (the plan is read, done-gate still tells Claude, context-meter still measures) but draws no band. To change one, use /plugin configure <name>@chien-mods inside Claude Code, pass --config key=value to claude plugin install, or pipe a JSON object to claude plugin configure <name>@chien-mods --values-stdin. With --plugin-dir, put them in a settings file: --settings '{"pluginConfigs":{"done-gate":{"options":{"testCommands":"make ci"}}}}'.

secret-guard

  • mode: value (default) hides secrets in results and refuses the commands listed below. command instead refuses any call whose command or path names a protected env file, except to load it (source .env, --env-file .env), without reading values or changing results; it is simpler, but it blocks harmless commands and misses reads that don't name the file.
  • secretFiles (default .env, .env.*, !*.example): comma-separated globs of the env files to read. A file name is looked for in the session's folder and every folder above it, up to the drive root. A path is read where it points: ~/vault//.env (from your home folder; goes up to 8 folders deep and skips node_modules, .git, .venv, venv and __pycache__), an absolute path, or one relative to the session's folder. !glob leaves files out. Each file must be in .env format.
  • secretKeys: comma-separated key names to treat as secrets on top of the built-in rule. The rule: a key is a secret when a part of its name (split at _, -, . and camelCase) is PASS, PASSWD, PASSWORD, PW, PWD, SECRET, TOKEN, KEY, DSN, CREDENTIAL or PRIVATE, or ends with one of the first seven (APIKEY, DBPASS). So DB_PASSWORD, apiKey and AWS_SECRET_ACCESS_KEY are secrets; DB_NAME, DB_HOST, ACCOUNT_ID, MAX_TOKENS, TOKENIZER_PATH and the shell's own PWD are not.
  • identifierKeys: comma-separated keys that match the rule but hold names, not secrets (KMS_KEY_ID, SSH_KEY_NAME). They are never hidden.

Values shorter than 8 characters are never hidden (except the password in a URL such as postgres://user:REDACTED@host, which is always hidden), so PW=1 or TOKEN_TTL=60 does not mask every 1 or 60. The values of non-secret keys are never hidden at all, so database names, hosts, users and account ids stay readable.

In value mode it refuses these commands, each time naming a way to do the same without printing the secret: cat, type, Get-Content, less, more, head, tail or bat of a protected file (cat .env | cut -d= -f1 is allowed, it prints names); a bare env, printenv, set or Get-ChildItem env:; and, unless the output goes to a file or a variable (> out.json, VALUE=$(...)), aws ssm ... --with-decryption, aws secretsmanager get-secret-value and kubectl get secret ... -o yaml|json. It never refuses loading a file: source .env, . .env, set -a, --env-file .env.

Every conversation starts with a short # secret-guard note to Claude: the protected files and key names (names only), what ‹hidden: NAME› means, and the convention (reference $NAME, let scripts load the env file, never print a secret, report only whether a command worked). So you no longer need to remind each session. The note is rebuilt after a compaction or /clear. Messages Claude sends to another agent or session (SendMessage) are scrubbed like tool results. So is what Claude Code attaches to a message on its own, which never passes through a tool call: a file Claude read, attached again after a compaction; a file changed on disk; a file you @-mention; a settings hook's output.

plan-meter

  • plan: comma-separated paths, relative to the project, tried in order; the first one that matches a file wins. A * in any part matches anything, and among several matches the most recently changed file wins. Default: plans/*/plan.md, PLAN.md, plan.md, TODO.md, TASKS.md, ROADMAP.md, todo.txt, TODO.org.
  • refreshSeconds (default 15, at least 5): how often the plan is read again, so an edit you make in your own editor shows up too. Edits Claude makes show up at once.

The band shows only when there is a plan or a task list. The pane draws in the terminal and the desktop app; under claude -p, /plan-meter answers with the band's line. (The command was /plan up to 0.1.0; Claude Code 2.1.294 has a built-in /plan, which refused the name.)

context-meter

  • cacheTtl: auto (default), 5m or 1h. A mod cannot read the cache lifetime Claude Code asks for, so auto follows Claude Code's defaults: one hour on a Claude subscription (the session reports rate-limit windows), five minutes with an API key or a cloud provider. Set it when you know better: you set promptCacheTtl or ENABLE_PROMPT_CACHING_1H, or you are drawing on usage credits, where Claude Code drops to five minutes. The band always says which lifetime it assumed and why.
  • breakdown: summary (default) estimates the categories locally and sends nothing. full counts them with the token-count API after every turn, as /context does: more exact, one request per tool and memory file.

Every request of the main conversation that hits the cache resets its timer, so the clock restarts at each model request, from the moment it was sent, not only when a turn ends. While Claude works the band says the cache is being kept warm; the countdown runs between turns, from the last request. A subagent's requests have caches of their own and are left out. After a compaction it starts again with the next message. The button is hidden while a turn runs and before the conversation's first reply, when Claude Code refuses a compaction ("Not enough messages to compact"); if a compaction is refused or a hook vetoes it, the band says why, and so does a toast. If you set CLAUDE_AUTOCOMPACT_PCT_OVERRIDE, which can only bring auto-compaction earlier, the header shows that point and marks it as your setting (compacts at 500k (your 50% setting)), since the breakdown Claude Code returns may still give its default; that one environment variable is all context-meter reads. In the desktop app and the IDE extensions, which run Claude Code through its SDK, Claude Code cannot compact between turns yet, so there the button runs /compact as if you had typed it. Below the bar, every category and the free part get a coloured entry with their tokens and share of the window, wrapped onto as many rows as they need. Compacting is a model call: the band costs no tokens, the button does. With little room above the prompt the band keeps two rows, the fill and the cache clock.

done-gate

  • testCommands: comma-separated commands that run your tests, added to the usual runners. Example: ./scripts/check.sh, make ci. Each one counts when it is the command itself, with or without arguments, so check does not match git checkout.
  • ignore (default .md,.mdx,.markdown,.txt,.rst,.adoc,.org): file endings whose changes need no test run.

The usual runners it knows: pytest, python -m pytest or unittest, npm/pnpm/yarn/bun test or run test, vitest, jest, mocha, go test, cargo test, cargo nextest, mvn test or verify, gradle test, dotnet test, rspec, phpunit, mix test, swift test, ctest, make test or check, tox, nox, deno test, claude plugin test. A runner counts when it is a command in the line, after &&, ; or cd api && and behind FOO=1, npx, uv run or poetry run; a runner named inside an argument (echo pytest, git commit -m "fix pytest") does not.

idea-shelf, session-monitor, prompt-enhancer and assumption-check have no options.

The four newer mods: what each reads, sends and costs

Each paragraph ends with the mod's calls: line from claude plugin validate: every call it makes into Claude Code. Costs are what the author's checks measured on Claude Code 2.1.296 with Haiku 5.5, at Anthropic's list prices; on a Claude subscription the same calls count against your usage instead.

idea-shelf. /idea <text> runs at once, even while Claude works, and does not interrupt the turn. The shelf is one list per project (the session's root folder), at most 200 ideas of 2,000 characters, in the mod's store on your machine. It sends nothing to the model. The line /idea <text> leaves in the conversation would carry the idea to the model, so idea-shelf rewrites that line before it is stored: the model and the transcript read /idea (an idea, kept on the shelf); the shelf keeps the text. In the author's check, a word typed after /idea was absent from the messages the model reads before and after. An idea reaches the model only when you send it, and then it arrives as a prompt "from the idea-shelf plugin". The shelf's text box is in its pane, so it draws in the terminal and the desktop app; elsewhere /idea <text> still parks. Cost: none. calls: $.clock.now, $.command.register, $.prompt.fill, $.prompt.read, $.prompt.submit, $.session.root, $.state.get, $.state.set, $.store.get, $.store.set, $.ui.open, $.ui.resolve

session-monitor. Each session that has it on writes one small file, ~/.cache/claude-mods/session-monitor/<session id>.json: the session id, its folder, its state (working, waiting or done), when that state began, when the file was written, and a title, which is the title Claude Code gave the session, else the folder's name. No prompt or command text. The file is rewritten at every change of state and every 15 seconds, and each session reads the others' files on the same beat; that is the mod's only reach beyond its own session. Working runs from a prompt to the end of the turn; waiting, while a permission dialog is open or Claude's question waits for you (in auto mode the classifier answers most permission checks, so waiting is brief there); done, otherwise. A closing session marks its file ended; a file not rewritten for 90 seconds (a killed session) is skipped. Mods cannot delete files, so the files stay, one per session, about 240 bytes each: delete the folder whenever you like. It sends nothing to the model. Cost: none. calls: $.clock.every, $.clock.now, $.command.register, $.env.get (HOME, USERPROFILE), $.fs.list, $.fs.read, $.fs.write, $.session.id, $.session.root, $.state.get, $.state.set, $.store.get, $.store.set, $.ui.open, $.ui.resolve

prompt-enhancer. Nothing happens until you press Enhance (e after Ctrl+X, Tab gives the band the keyboard). Then it sends Haiku one request holding your draft, the name and the first 100 characters of the description of each skill and command your session has (built-in commands left out), and the first 40 lines of the project's CLAUDE.md. It never reads .env files. It sends nothing when the draft is empty, longer than 4,000 characters, or holds secret-guard's ‹hidden: …› placeholder or a value shaped like a key (a private key, sk-…, AKIA…, ghp_…, xox?-…, a JWT); the band says why. A rewrite arrives in a few seconds and replaces the draft. If you edited the draft meanwhile, your edit wins and the rewrite is dropped. After 20 seconds it gives up and the draft stays. It never submits: Enter or Send (n) does. A command cannot read the prompt box, so Enhance is a button only. Cost: one press with about 150 skills and commands in the session took 6,615 input and 661 output tokens, about $0.001; the band and /prompt-enhancer status show each press's tokens. calls: $.command.list, $.command.register, $.fs.read (CLAUDE.md), $.model.complete, $.prompt.fill, $.prompt.read, $.prompt.submit, $.session.root, $.state.get, $.state.set, $.store.get, $.store.set, $.ui.resolve

assumption-check. It counts the edits and commands of each turn. When a turn that edited a file or ran a command ends (not one that only read, nor one you interrupted), it sends Haiku one request: that turn's messages, from your last prompt on, each tool call's input cut to 1,500 characters, the whole at most 30,000. These are messages the model already read, after secret-guard (if on) hid what it hides. It asks for at most 8 assumptions as JSON, and asks once more if the answer does not parse. The check starts after the turn has ended, so the turn never waits for it. The list lives for the session only. Send corrections puts a prompt in the box ("You assumed: … That is wrong: <your note>", and the ones you marked right, to keep); pressed again, it sends that prompt, but never while Claude is working. In the author's check, Claude wrote a date parser that read 3/4/2026 day first; the list named that choice first; marked wrong with the note "our users are in the US", the correction changed the two date formats and nothing else. It lists what it finds; it does not prove the list complete. Cost: a median of 1,176 input and 1,339 output tokens per checked turn over 13 checks, about $0.0008; /assumption-check status shows the last one. calls: $.clock.after, $.command.register, $.model.complete, $.prompt.fill, $.prompt.read, $.prompt.submit, $.session.messages, $.state.get, $.state.set, $.store.get, $.store.set, $.ui.open, $.ui.resolve

Plan formats plan-meter reads

plan-meter does not ask you to write your plan its way. It recognises these, alone or mixed in one file:

FormatExampleWhat it counts
Checklist- [x] write the schemaOne step per item. Bullets -, *, +, 1., 1). Marks: x or X done; / or ~ in progress (Obsidian); - cancelled, left out of the count; a space, >, <, ! or ? still to do.
Status table`\Phase \Name \Status \ with a row \2 \API \🚧 In progress \`One phase per row. The status column is the one headed Status, State, Progress, Done or Done?, Trạng thái, Tình trạng, ステータス or 状態. The name comes from a Name, Title, Task, Item, What, Deliverable, Tên or Công việc column, else Phase, Step, Milestone, Stage or Giai đoạn, else the first cell with words in it (so a Phase column holding only 2 is skipped).
Headings as phases## Phase 2: API (in progress), ## Step 3 ✅Used when the file has no status table. A heading named Phase, Step, Stage, Milestone, Sprint, Part or Task with a number (also Giai đoạn 1, Bước 2, フェーズ1) is a phase. Its status comes from a mark in it, a status at its end ((done), [WIP], — done, : in progress), the checklist beneath it (all ticked is done, some ticked is in progress), or the phase file it links to. Any other heading counts only when it ends with a status alone: ## Setup (done).
Linked phase filesPhase 1Links in the plan (in text, tables or headings) to Markdown files whose name starts with phase, step, stage, milestone, sprint, part or task and goes on with a number or a dash: phase-01-schema.md, phase1.md, steps.md, part_2.md, but not department.md. Relative to the plan's folder, up to 30; a #section part is ignored, and two links to one file count it once. Their checklists add to the steps. A phase whose status the plan leaves blank or unknown takes the file's status from its frontmatter or status line, else from its checklist; a status the plan states, such as Pending, wins. A plan with no phases of its own takes one phase per linked file.
YAML frontmattertitle: Ship the export / status: in_progressThe plan's title and its own status.
Status lineStatus: Draft, phase 3 next, Status: …, Status (2026-10-07): …The plan's own status, shown as written when nothing in the file can be counted.
org-mode* TODO write the schema, ** DONE testsOne step per headline. DONE done; DOING, IN-PROGRESS, STARTED, WAITING, HOLD in progress; CANCELLED left out; TODO, NEXT to do.
todo.txtx 2026-10-01 call the bankA file named todo.txt (or *.todo.txt): one step per line, x at the start is done.

Status words it understands in tables, headings, status lines and frontmatter, in English, Vietnamese and Japanese. When a cell holds several, the first one wins, so Done (review pending) is done and Not started is to do.

  • Done: done, complete, completed, finished, shipped, merged, closed, resolved, delivered, passed, xong, hoàn thành, 完了, ✅ ✔ ☑ ✓ [x]
  • In progress: in progress (also in-progress, in_progress), WIP, doing, ongoing, active, started, running, in review, reviewing, blocked, đang, đang làm, 進行中, 🚧 🔄 ⏳ ▶
  • To do: not started, not done, not yet, incomplete, unfinished, todo, to do, pending, planned, backlog, queued, open, chưa, chưa làm, chưa xong, 未着手, ⬜ ☐ [ ]
  • Dropped, left out of the count: cancelled, canceled, dropped, won't do, won't fix, wontfix, skipped, obsolete, abandoned, n/a, hủy, bỏ qua, 中止, 🚫

The title is the frontmatter title:, else the first # heading (a leading Plan: is dropped). Anything inside a fenced code block is skipped.

Claude's own tasks. When Claude keeps a task list (its TodoWrite, TaskCreate and TaskUpdate tools), the band adds Claude's tasks done/total, and the

Source 5 files
hooks/register.ts 281 lines
1import type { Register } from 'claude-code'
2
3import { namesProtectedFile, refusal } from './commands'
4import { DEFAULT_SECRET_FILES, findSecretFiles, isProtectedName, parseFileRules } from './files'
5import { MARKER, Vault, failClosed, fill, marker, mergeSecrets, nameSet, parseEnv, resolveMarkers } from './secrets'
6import type { Secret } from './secrets'
7import { STORE_KEY, storedSwitch, switchWord } from './toggle'
8
9// The input fields a person's first rule would look at for a file name.
10const INPUT_FIELDS = ['command', 'file_path', 'path', 'pattern', 'glob'] as const
11const SHELLS = new Set(['Bash', 'PowerShell'])
12// The context block's name: Claude reads it as `# secret-guard` in the first message of every conversation.
13const BLOCK = 'secret-guard'
14const MAX_NAMES = 80
15
16const baseName = (path: string) => path.slice(Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')) + 1)
17const hasMarker = (text: string) => new RegExp(MARKER.source).test(text)
18
19type Input = Record<string, unknown>
20type Texts = { path: string; texts: { text: string; isOld: boolean }[]; fill: (values: ReadonlyMap<string, string>) => Input }
21
22// The text fields of a call that writes a file, and how to put values back into them.
23function writerTexts(input: Input): Texts | undefined {
24  const str = (v: unknown) => (typeof v === 'string' ? v : '')
25  const swap = (fields: string[]) => (values: ReadonlyMap<string, string>) =>
26    ({ ...input, ...Object.fromEntries(fields.map(f => [f, fill(str(input[f]), values)])) })
27  switch (input.tool) {
28    case 'Edit':
29      return { path: str(input.file_path), texts: [{ text: str(input.old_string), isOld: true }, { text: str(input.new_string), isOld: false }], fill: swap(['old_string', 'new_string']) }
30    case 'Write':
31      return { path: str(input.file_path), texts: [{ text: str(input.content), isOld: false }], fill: swap(['content']) }
32    case 'NotebookEdit':
33      return { path: str(input.notebook_path), texts: [{ text: str(input.new_source), isOld: false }], fill: swap(['new_source']) }
34    case 'MultiEdit': {
35      const edits = Array.isArray(input.edits) ? (input.edits as Input[]) : []
36      return {
37        path: str(input.file_path),
38        texts: edits.flatMap(e => [{ text: str(e.old_string), isOld: true }, { text: str(e.new_string), isOld: false }]),
39        fill: values => ({ ...input, edits: edits.map(e => ({ ...e, old_string: fill(str(e.old_string), values), new_string: fill(str(e.new_string), values) })) }),
40      }
41    }
42    default:
43      return undefined
44  }
45}
46
47// The status line and one log line per name hidden (names only, never a value).
48function report($: { ui: { status: (text: string) => unknown; log: (text: string) => unknown } }, hidden: number, found: ReadonlySet<string>, where: string): void {
49  $.ui.status(`secret-guard: ${hidden} hidden this session`)
50  for (const name of found) $.ui.log(`hid ${name} from ${where}`)
51}
52
53// Said in the status line for as long as the guard is off, so that nobody forgets it.
54const OFF_STATUS = 'secret-guard OFF: secrets not hidden'
55
56const RESTORE_TEXT = (tool: string, path: string) => `secret-guard: this ${tool} holds ‹hidden: …›, a placeholder for a secret value. secret-guard puts the real value back only into a file that already holds it, and ${path || 'the target file'} does not, or holds several values it could stand for. Edit around the value (pick an old_string without the placeholder), or write the file with a command that reads the value from its env file without printing it.`
57
58export const register: Register = (on, options) => {
59  const mode = options.mode === 'command' ? 'command' : 'value'
60  const rule = { extra: nameSet(options.secretKeys), identifiers: nameSet(options.identifierKeys) }
61  const fileRules = parseFileRules(String(options.secretFiles ?? '').trim() || DEFAULT_SECRET_FILES)
62  // Values live in the Vault only: never in $.ui, $.store, $.state, a log or the context block.
63  let fileSecrets: Secret[] = []
64  let vault = new Vault([], rule)
65  // The env files found, nearest first, and the one being looked at (named if loading fails).
66  const envPaths: string[] = []
67  let current: string | undefined
68  let hidden = 0
69  // 'ready' once the env files were looked for and read (or there are none). Until then, or after a failure,
70  // value mode refuses every call: a guard that silently loaded nothing would pass every value through.
71  let loading: 'pending' | 'ready' | 'failed' = 'pending'
72  let loadError = ''
73  // Files that were read but could not be parsed: skipped, the rest still protected. Name and reason only.
74  const skipped: { file: string; reason: string }[] = []
75  const skippedText = () => skipped.map(s => `${s.file} (${s.reason})`).join(', ')
76  // Results withheld because the check itself failed (the .catch below).
77  let withheld = 0
78  // The context block was built before the files were loaded, so it is rebuilt once they are.
79  let contextStale = false
80  // The switch (toggle.ts). The guard starts on; `/secret-guard off` turns it off for every session, and then each
81  // hook passes its event on unchanged: nothing is hidden or refused and Claude gets no note. The env files are
82  // still read at the start, so that `/secret-guard on` protects at once.
83  let isOn = true
84
85  const isProtected = (path: string) => {
86    const name = baseName(path)
87    return isProtectedName(name, fileRules) || envPaths.some(p => baseName(p) === name)
88  }
89
90  const contextText = (): string => {
91    const use = 'To use a secret, reference it as $NAME (PowerShell: $env:NAME), or let the command or script load the env file: source FILE, set -a; . FILE; set +a, or --env-file FILE.'
92    const never = 'Never echo, cat or otherwise print a protected file or a secret value. To check a credential, run the command that uses it and report only whether it worked.'
93    if (mode === 'command') return [`secret-guard refuses any tool call that names a protected env file (such as .env or .env.local), except to load it.`, use, never].join('\n')
94    if (loading === 'failed') return `secret-guard could not load ${current ?? 'an env file'}, so every tool call is refused this session. Tell the user; \`claude plugin disable secret-guard@chien-mods\` turns it off.`
95    const names = [...new Set(fileSecrets.map(s => s.name))]
96    const shown = names.slice(0, MAX_NAMES).join(', ') + (names.length > MAX_NAMES ? ` and ${names.length - MAX_NAMES} more` : '')
97    const head = loading === 'pending'
98      ? 'secret-guard is loading the env files of this session.'
99      : envPaths.length === 0
100        ? 'secret-guard found no env file above this session.'
101        : `secret-guard protects the secrets in ${envPaths.filter(p => !skipped.some(s => s.file === p)).join(', ') || 'no file'}. Protected keys (names only): ${shown || 'none'}.`
102    const warning = skipped.length > 0
103      ? [`secret-guard could not parse ${skippedText()}, so it does not protect the values in that file. Tell the user; never print that file.`]
104      : []
105    return [
106      head,
107      ...warning,
108      'In tool results a secret shows as ‹hidden: NAME›, where NAME is its key or a kind (jwt, private-key, dsn-password, aws-secret-access-key, k8s-secret). It is a placeholder, not the value: never put it in a command. In an Edit or Write, secret-guard puts the real value back only into a file that already holds it.',
109      use,
110      never,
111    ].join('\n')
112  }
113
114  on('session.start', async ($, e, next) => {
115    await $.command.register({ name: 'secret-guard', description: 'Show which env files and keys secret-guard protects (/secret-guard on or off switches it)', argumentHint: '[on|off|status]' })
116    isOn = storedSwitch(await $.store.get(STORE_KEY).catch(() => undefined), true)
117    if (!isOn) $.ui.status(OFF_STATUS)
118    // Command mode matches file names only, so it never reads the values.
119    if (mode === 'command') {
120      loading = 'ready'
121      return next(e)
122    }
123    try {
124      // session.start can fire again in one load (an enable, a worker respawn): start the list over.
125      envPaths.length = 0
126      loading = 'pending'
127      const home = fileRules.paths.some(p => p.startsWith('~/')) ? ((await $.env.get('USERPROFILE')) ?? (await $.env.get('HOME'))) : undefined
128      // Every protected file from the session's folder up to the root, so a project's own .env does not hide
129      // the workspace .env above it. The two starts usually share their upper folders, so each is looked at once.
130      envPaths.push(...await findSecretFiles(dir => $.fs.list(dir), [e.cwd, await $.session.root()], fileRules, home))
131      const lists: Secret[][] = []
132      skipped.length = 0
133      for (const file of envPaths) {
134        current = file
135        // A file that cannot be read fails the load; one that cannot be parsed is skipped and named.
136        const text = String(await $.fs.read(file))
137        try {
138          lists.push(parseEnv(text, rule))
139        } catch (err) {
140          skipped.push({ file, reason: err instanceof Error ? err.message : 'unknown error' })
141        }
142      }
143      fileSecrets = mergeSecrets(lists)
144      vault = new Vault(fileSecrets, rule)
145      loading = 'ready'
146      if (skipped.length > 0) $.ui.status(`secret-guard: skipped ${skippedText()}; its values are not protected`)
147    } catch (err) {
148      loading = 'failed'
149      loadError = err instanceof Error ? err.message : 'unknown error'
150      $.ui.status('secret-guard: could not load an env file, tool calls are refused')
151    }
152    if (contextStale) await $.ui.invalidate('prompt.context')
153    return next(e)
154  })
155
156  // A standing note in the first message of every conversation (and again after a compaction or /clear): the
157  // protected key names, what a marker means and how to use a secret without printing it. Names only.
158  on('prompt.context', async ($, e, next) => {
159    const below = await next(e)
160    if (!isOn) return below
161    contextStale = loading === 'pending'
162    return { ...below, blocks: [...below.blocks.filter(b => b.name !== BLOCK), { name: BLOCK, text: contextText() }] }
163  })
164
165  on('command.run', { command: 'secret-guard' }, async ($, e) => {
166    const word = switchWord(e.args)
167    if (word === 'on' || word === 'off') {
168      isOn = word === 'on'
169      await $.store.set(STORE_KEY, isOn)
170      $.ui.status(isOn ? (hidden > 0 ? `secret-guard: ${hidden} hidden this session` : undefined) : OFF_STATUS)
171      // The standing note for Claude is added again, or taken away, from the next message on.
172      await $.ui.invalidate('prompt.context')
173      return { text: isOn ? 'on: secrets in tool results are hidden again (/secret-guard off turns it off)' : 'off: secrets are not hidden and Claude gets no note, in every session, until /secret-guard on' }
174    }
175    if (!isOn) return { text: 'off: secrets are not hidden (/secret-guard on turns it on)' }
176    if (mode === 'command') return { text: 'on · mode command: refuses any call whose command or path names a protected env file, except to load it; values are not read' }
177    if (loading === 'failed') return { text: `on · could not load ${current ?? 'an env file'} (${loadError}), so value mode refuses every tool call` }
178    // The start hook has not finished: still running, or skipped by the engine (it overran its budget).
179    if (loading === 'pending') return { text: 'on · has not loaded the env files yet (its start hook did not finish), so value mode refuses every tool call' }
180    const counts = `${hidden} values hidden this session${withheld > 0 ? `; ${withheld} results withheld because they could not be checked` : ''}`
181    if (envPaths.length === 0) return { text: `on · mode value; no env file found above this session; values that look like secrets are still hidden; ${counts}` }
182    const names = [...new Set(fileSecrets.map(s => s.name))].join(', ') || 'none'
183    const files = envPaths.filter(p => !skipped.some(s => s.file === p)).join(', ') || 'no file'
184    const skips = skipped.length > 0 ? `; skipped ${skippedText()}, its values are not protected` : ''
185    return { text: `on · mode value; protects ${fileSecrets.length} values from ${files}: ${names}${skips}; ${counts}` }
186  })
187
188  on('tool.call', async ($, e, next) => {
189    if (!isOn) return next(e)
190    const input = e as unknown as Input
191    if (mode === 'command') {
192      const named = INPUT_FIELDS.some(field => typeof input[field] === 'string' && namesProtectedFile(input[field] as string, isProtected))
193      if (named) return { deny: 'secret-guard: this call names a protected env file. Print key names only: sed -E "s/=.*/=<hidden>/" .env, or load it without printing: source .env, --env-file .env' }
194      return next(e)
195    }
196
197    if (loading !== 'ready') return { deny: 'secret-guard has not loaded the env files, so this call was not run' }
198    if (SHELLS.has(e.tool)) {
199      const why = refusal(typeof input.command === 'string' ? input.command : '', isProtected, label => vault.values(label).length > 0)
200      if (why !== undefined) return { deny: why }
201    }
202    let call = e
203    const writer = writerTexts(input)
204    if (writer !== undefined && writer.texts.some(t => hasMarker(t.text))) {
205      let fileText = ''
206      try {
207        fileText = String(await $.fs.read(writer.path))
208      } catch {
209        // A new file holds no value to put back.
210      }
211      const values = resolveMarkers(writer.texts, fileText, label => vault.values(label))
212      if (values === undefined) return { deny: RESTORE_TEXT(e.tool, writer.path) }
213      call = writer.fill(values) as unknown as typeof e
214      // A marker the file holds as text stays text: only the values put back are counted.
215      const restored = [...values].filter(([label, value]) => value !== marker(label)).length
216      if (restored > 0) $.ui.log(`put ${restored} hidden values back into ${writer.path}`)
217    }
218
219    const ran = await next(call)
220    const found = new Set<string>()
221    const where = `a ${e.tool} result`
222    // A refusal from beneath is read by the model too, so its reason is checked like a result.
223    if (ran.deny !== undefined) {
224      const deny = vault.scrubText(ran.deny, found)
225      if (found.size === 0) return ran
226      hidden += found.size
227      report($, hidden, found, where)
228      return { deny }
229    }
230    // An errored result has no typed record to answer with, so its scrubbed text goes back as the error.
231    if (ran.isError) {
232      const text = vault.scrubText(ran.text ?? String(ran.result), found)
233      if (found.size === 0 && vault.find([ran.result]).length === 0) return ran
234      hidden += found.size
235      report($, hidden, found, where)
236      return { deny: text }
237    }
238    vault.scrubText(ran.text ?? '', found)
239    const result = vault.scrub(ran.result, found) as typeof ran.result
240    const context = ran.context?.map(c => vault.scrubText(c, found))
241    // A value left after scrubbing (an object key, which scrub keeps so the shape stays valid) withholds the result.
242    // So does a number, which cannot become a marker without breaking the result's shape.
243    if (vault.find([result, context ?? []]).length > 0) {
244      withheld += 1
245      return failClosed(true, 'throw')
246    }
247    if (found.size === 0) return ran
248    hidden += found.size
249    report($, hidden, found, where)
250    return { result, ...(context ? { context } : {}) }
251  }).catch(($, e, next) => {
252    if (next.called) withheld += 1
253    return failClosed(next.called, next.error.kind)
254  })
255
256  // What the engine attaches on its own never passes tool.call: a file read before a compaction (attached again,
257  // read fresh from disk), a file changed on disk, an @-mentioned file, a settings hook's output. Scrub it too.
258  on('prompt.attachment', async ($, e, next) => {
259    const below = await next(e)
260    if (!isOn || mode === 'command' || typeof below.text !== 'string') return below
261    const found = new Set<string>()
262    const text = vault.scrubText(below.text, found)
263    if (found.size === 0) return below
264    hidden += found.size
265    report($, hidden, found, `a ${e.type} attachment`)
266    return { ...below, text }
267  }).catch(() => ({ text: null }))
268
269  // A message to another agent or session (SendMessage) leaves this conversation: scrub it the same way.
270  on('session.send', async ($, e, next) => {
271    if (!isOn || mode === 'command') return next(e)
272    if (loading !== 'ready') return { isDelivered: false, reason: 'secret-guard has not loaded the env files, so this message was not sent' }
273    const found = new Set<string>()
274    const text = vault.scrubText(e.text, found)
275    if (found.size === 0) return next(e)
276    hidden += found.size
277    report($, hidden, found, 'a message sent out')
278    return next({ ...e, text })
279  }).catch(() => ({ isDelivered: false, reason: 'secret-guard could not check this message, so it was not sent' }))
280}
281
hooks/commands.ts 81 lines
1// The shell commands secret-guard refuses: in value mode the few that print a whole env file, the whole
2// environment or a decrypted secret to the screen, each refusal naming a way that does not print it; in
3// command mode any call that names a protected file. Loading a file (source, --env-file) is always allowed.
4
5import { MARKER } from './secrets'
6
7export type IsProtected = (path: string) => boolean
8
9const CAT_LIKE = new Set(['cat', 'type', 'get-content', 'gc', 'less', 'more', 'head', 'tail', 'bat'])
10// Commands that pass on everything they are given: a dump piped into one of these still reaches the screen.
11const DISPLAY = new Set(['cat', 'type', 'less', 'more', 'head', 'tail', 'bat', 'tee', 'sort', 'uniq', 'column', 'out-host', 'out-string', 'write-output', 'write-host', 'format-list', 'format-table'])
12// Words in front of the command itself.
13const PREFIXES = new Set(['sudo', 'command', 'exec', 'time', 'nohup', '&'])
14
15const words = (text: string): string[] => (text.match(/"[^"]*"|'[^']*'|\S+/g) ?? []).map(w => w.replace(/^(["'])(.*)\1$/, '$2'))
16const baseName = (path: string) => path.slice(Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\')) + 1)
17const commandName = (word: string) => baseName(word).toLowerCase().replace(/\.exe$/, '')
18
19// The command and its arguments, past `FOO=1` assignments and `sudo`.
20function parseStage(stage: string): { name: string; args: string[] } {
21  const all = words(stage)
22  let i = 0
23  while (i < all.length && (/^[A-Za-z_]\w*=/.test(all[i] ?? '') || PREFIXES.has(commandName(all[i] ?? '')))) i += 1
24  return { name: commandName(all[i] ?? ''), args: all.slice(i + 1) }
25}
26
27// Output sent to a file (`> out`, `>> out`, `| Out-File`), not `2>/dev/null` or `2>&1`.
28const redirected = (statement: string) => /(^|[^0-9&<>])>>?(?!&)\s*\S|&>|\|\s*(out-file|set-content|add-content)\b|-OutFile\b/i.test(statement)
29// Output captured into a variable: `X=$(...)`, `export X=$(...)`, PowerShell `$x = ...`.
30const captured = (statement: string) => /^\s*(?:(?:export|local|readonly)\s+|declare\s+(?:-\w+\s+)*)?[A-Za-z_]\w*=["']?(?:\$\(|`)|^\s*\$[\w:]+\s*=/.test(statement)
31
32const MARKER_TEXT = (label: string) => `secret-guard: this command holds ‹hidden: ${label}›, a placeholder for a secret, not its value. Reference the variable instead ($${label}, or $env:${label} in PowerShell), or let the command load its env file (source .env, set -a; . .env; set +a, or --env-file .env).`
33const FILE_TEXT = (file: string) => `secret-guard: this prints a protected env file (${file}), secrets included. To see its key names: sed -E 's/=.*/=<hidden>/' ${file}. To use the values without printing them: source ${file} (or set -a; . ${file}; set +a, or --env-file ${file}), then reference $NAME.`
34const ENV_TEXT = 'secret-guard: this prints every environment variable, secrets included. List the names only (env | cut -d= -f1; PowerShell: Get-ChildItem env: | Select-Object Name), or print one variable that is not a secret.'
35const SSM_TEXT = 'secret-guard: this prints a decrypted SSM parameter. Capture it without printing it, VALUE=$(aws ssm get-parameter --name NAME --with-decryption --query Parameter.Value --output text), or redirect it to a file, then report only whether it worked.'
36const SECRETS_MANAGER_TEXT = 'secret-guard: this prints a secret from Secrets Manager. Capture it without printing it, VALUE=$(aws secretsmanager get-secret-value --secret-id NAME --query SecretString --output text), or redirect it to a file, then report only whether it worked.'
37const KUBECTL_TEXT = "secret-guard: this prints a Kubernetes Secret's data. Redirect it to a file (> secret.yaml), or see its key names and sizes only: kubectl describe secret NAME."
38
39// Why a value-mode command is refused, or undefined to let it run. `hasValue` says whether a marker's label stands
40// for a value secret-guard knows; a marker that stands for none is plain text (a document quoting the format).
41export function refusal(command: string, isProtected: IsProtected, hasValue: (label: string) => boolean): string | undefined {
42  const label = [...command.matchAll(MARKER)].map(m => m[1] ?? '').find(hasValue)
43  if (label !== undefined) return MARKER_TEXT(label)
44  for (const statement of command.split(/\r?\n|;|&&|\|\|/)) {
45    const hidden = redirected(statement) || captured(statement)
46    const all = words(statement)
47    const lower = all.map(w => w.toLowerCase())
48    if (!hidden) {
49      if (lower.some(w => w.startsWith('--with-decryption'))) return SSM_TEXT
50      if (lower.includes('get-secret-value')) return SECRETS_MANAGER_TEXT
51      if (lower.some(w => commandName(w) === 'kubectl') && lower.includes('get') && lower.some(w => /^secrets?(\/|$)/.test(w))
52        && /(?:^|\s)(?:-o|--output)(?:\s+|=)?(?:yaml|json|jsonpath|go-template|template|custom-columns)/.test(lower.join(' '))) return KUBECTL_TEXT
53    }
54    const stages = statement.split('|').map(parseStage)
55    for (let i = 0; i < stages.length; i++) {
56      const { name, args } = stages[i] ?? { name: '', args: [] }
57      // Whatever this stage prints reaches the screen: nothing after it but commands that pass it all on.
58      const shown = !hidden && stages.slice(i + 1).every(s => DISPLAY.has(s.name))
59      if (!shown) continue
60      if (CAT_LIKE.has(name)) {
61        const file = args.find(a => !a.startsWith('-') && isProtected(a))
62        if (file !== undefined) return FILE_TEXT(file)
63      }
64      if ((name === 'env' || name === 'printenv' || name === 'set') && args.length === 0) return ENV_TEXT
65      if (['get-childitem', 'gci', 'dir', 'ls'].includes(name) && args.some(a => /^env:\\?\*?$/i.test(a))) return ENV_TEXT
66    }
67  }
68  return undefined
69}
70
71// The old rule (`.env` as a file token, also inside a glob such as `**/.env*`), not `.venv` or `.env.example`.
72const ENV_FILE = /(?<![\w.])\.env(?![\w.])/
73// Loading a file without printing it: `source X`, `. X`, `--env-file X`, `env_file: X`.
74const LOADS = /(?:\bsource|(?:^|[\s;&|(])\.)\s+\S+|--env-file(?:=|\s+)\S+|env_file:\s*\S+/g
75
76// Command mode: does any field of the call name a protected file, other than to load it?
77export function namesProtectedFile(text: string, isProtected: IsProtected): boolean {
78  const rest = text.replace(LOADS, ' ')
79  return ENV_FILE.test(rest) || rest.split(/[\s'"=;|&<>()`,]+/).some(token => token !== '' && isProtected(token))
80}
81
hooks/files.ts 157 lines
1// Which env files secret-guard reads: the `secretFiles` globs, looked for from the session's folder up to the
2// drive root (a bare file name) or at a path of their own (`~/secrets/**/.env`).
3
4export const DEFAULT_SECRET_FILES = '.env, .env.*, !*.example'
5
6export type FileRules = {
7  // File-name globs, looked for in every folder from the session's up to the root.
8  names: RegExp[]
9  // Path globs (they hold a `/`), from the home folder (`~/`), absolute, or relative to the session's folder.
10  paths: string[]
11  // `!glob`: a file whose name (or, for a glob with a `/`, whose path) matches is never read.
12  excludes: RegExp[]
13}
14
15// `*` and `?` stay inside one folder; `**` crosses folders.
16export function globRegExp(glob: string): RegExp {
17  let source = ''
18  for (let i = 0; i < glob.length; i++) {
19    const ch = glob[i] ?? ''
20    if (ch === '*' && glob[i + 1] === '*') {
21      source += '.*'
22      i += 1
23    } else if (ch === '*') source += '[^/]*'
24    else if (ch === '?') source += '[^/]'
25    else source += ch.replace(/[.+^${}()|[\]\\]/g, '\\$&')
26  }
27  return new RegExp(`^${source}$`)
28}
29
30const hasSlash = (text: string) => /[\\/]/.test(text)
31const slashes = (path: string) => path.replace(/\\/g, '/')
32
33export function parseFileRules(option: unknown): FileRules {
34  const rules: FileRules = { names: [], paths: [], excludes: [] }
35  for (const entry of String(option ?? '').split(',').map(e => e.trim()).filter(Boolean)) {
36    if (entry.startsWith('!')) rules.excludes.push(globRegExp(slashes(entry.slice(1))))
37    else if (hasSlash(entry)) rules.paths.push(slashes(entry))
38    else rules.names.push(globRegExp(entry))
39  }
40  return rules
41}
42
43export function excluded(path: string, rules: FileRules): boolean {
44  const full = slashes(path)
45  const name = full.slice(full.lastIndexOf('/') + 1)
46  return rules.excludes.some(re => re.test(name) || re.test(full))
47}
48
49// A file name (no folder) the name globs protect: what the command checks match a path's last part against.
50export function isProtectedName(name: string, rules: FileRules): boolean {
51  return rules.names.some(re => re.test(name)) && !excluded(name, rules)
52}
53
54// The folder itself, then each parent up to and including the root (`C:\`, `/`), nearest first.
55// Each keeps the start's own separator, so a path /secret-guard shows reads as one path.
56export function ancestorDirs(start: string): string[] {
57  const sep = start.includes('\\') ? '\\' : '/'
58  const out: string[] = []
59  let dir = start.replace(/[\\/]+$/, '')
60  for (;;) {
61    const cut = Math.max(dir.lastIndexOf('/'), dir.lastIndexOf('\\'))
62    if (cut < 0) {
63      // `C:` alone means the drive's current folder, so the root is listed as `C:\`; an empty dir is Unix's `/`.
64      out.push(`${dir}${sep}`)
65      return out
66    }
67    out.push(dir)
68    dir = dir.slice(0, cut)
69  }
70}
71
72export function join(dir: string, name: string): string {
73  const sep = dir.includes('\\') ? '\\' : '/'
74  return dir.endsWith(sep) ? `${dir}${name}` : `${dir}${sep}${name}`
75}
76
77export type Entry = { name: string; kind: string }
78export type Lister = (dir: string) => Promise<readonly Entry[]>
79
80// Folders a `**` never walks into, and how far it goes, so a broad glob cannot stall the session's start.
81const SKIP_DIRS = new Set(['node_modules', '.git', '.venv', 'venv', '__pycache__'])
82const MAX_DEPTH = 8
83const MAX_DIRS = 2000
84
85// A folder that cannot be listed (missing, no permission) holds nothing secret-guard could read either.
86async function listed(list: Lister, dir: string): Promise<readonly Entry[]> {
87  try {
88    return await list(dir)
89  } catch {
90    return []
91  }
92}
93
94// Every protected file, nearest first: the name globs in each folder from each start up to the root, then each
95// path glob. A file is listed once, by the first spelling found.
96export async function findSecretFiles(list: Lister, starts: readonly string[], rules: FileRules, home: string | undefined): Promise<string[]> {
97  const out: string[] = []
98  const seen = new Set<string>()
99  const add = (path: string) => {
100    const key = slashes(path)
101    if (seen.has(key) || excluded(path, rules)) return
102    seen.add(key)
103    out.push(path)
104  }
105  const looked = new Set<string>()
106  if (rules.names.length > 0) {
107    for (const start of starts) {
108      for (const dir of ancestorDirs(start)) {
109        if (looked.has(slashes(dir))) continue
110        looked.add(slashes(dir))
111        const names = (await listed(list, dir)).filter(e => e.kind !== 'dir' && rules.names.some(re => re.test(e.name))).map(e => e.name).sort()
112        for (const name of names) add(join(dir, name))
113      }
114    }
115  }
116  let budget = MAX_DIRS
117  const walk = async (dir: string, segs: readonly string[], depth: number): Promise<void> => {
118    const [seg, ...rest] = segs
119    if (seg === undefined || budget <= 0) return
120    budget -= 1
121    const entries = await listed(list, dir)
122    if (seg === '**') {
123      await walk(dir, rest, depth)
124      if (depth >= MAX_DEPTH) return
125      for (const e of entries) if (e.kind === 'dir' && !SKIP_DIRS.has(e.name)) await walk(join(dir, e.name), segs, depth + 1)
126      return
127    }
128    const re = globRegExp(seg)
129    for (const e of entries) {
130      if (!re.test(e.name)) continue
131      if (rest.length === 0) {
132        if (e.kind !== 'dir') add(join(dir, e.name))
133      } else if (e.kind === 'dir') await walk(join(dir, e.name), rest, depth)
134    }
135  }
136  for (const pattern of rules.paths) {
137    let base: string
138    let rel: string
139    if (pattern.startsWith('~/')) {
140      if (home === undefined) continue
141      base = home
142      rel = pattern.slice(2)
143    } else if (/^[A-Za-z]:\//.test(pattern)) {
144      base = `${pattern.slice(0, 2)}\\`
145      rel = pattern.slice(3)
146    } else if (pattern.startsWith('/')) {
147      base = '/'
148      rel = pattern.slice(1)
149    } else {
150      base = starts[0] ?? '.'
151      rel = pattern.replace(/^\.\//, '')
152    }
153    await walk(base, rel.split('/').filter(Boolean), 0)
154  }
155  return out
156}
157
hooks/secrets.ts 323 lines
1// Pure helpers for secret-guard: which key names hold secrets, parse an env file, and the Vault that finds
2// secret values in any text (the env files' own, and ones that only look like secrets) and hides them.
3
4export type Secret = { name: string; value: string }
5
6// Shorter values are never hidden, so `PW=1` or `TOKEN_TTL=60` does not mask every 1 or 60 in a result.
7export const MIN_SECRET_LEN = 8
8
9// A key holds a secret when one part of its name (split at `_`, `-`, `.` and camelCase) is one of these words,
10// or ends with one (APIKEY, DBPASS). Whole parts, so PWD's cousins TOKENIZER, MAX_TOKENS, PASSPORT, KEYBOARD
11// are not secrets.
12const SECRET_WORDS = ['PASS', 'PASSWD', 'PASSWORD', 'PW', 'PWD', 'SECRET', 'SECRETS', 'TOKEN', 'KEY', 'DSN', 'CREDENTIAL', 'CREDENTIALS', 'PRIVATE']
13const SECRET_ENDINGS = ['PASS', 'PASSWD', 'PASSWORD', 'PWD', 'SECRET', 'TOKEN', 'KEY']
14// The shell's own working-directory variables: paths, in every env listing.
15const SHELL_NAMES = new Set(['PWD', 'OLDPWD'])
16
17export type KeyRule = { extra: ReadonlySet<string>; identifiers: ReadonlySet<string> }
18
19// Comma-separated option text to an upper-case set.
20export function nameSet(option: unknown): Set<string> {
21  return new Set(String(option ?? '').split(',').map(k => k.trim().toUpperCase()).filter(Boolean))
22}
23
24export function keyParts(name: string): string[] {
25  return name.replace(/([a-z0-9])([A-Z])/g, '$1_$2').toUpperCase().split(/[^A-Z0-9]+/).filter(Boolean)
26}
27
28export function isSecretKey(name: string, rule: KeyRule): boolean {
29  const upper = name.toUpperCase()
30  if (rule.identifiers.has(upper) || SHELL_NAMES.has(upper)) return false
31  if (rule.extra.has(upper)) return true
32  return keyParts(name).some(part => SECRET_WORDS.includes(part) || SECRET_ENDINGS.some(word => part.endsWith(word)))
33}
34
35const DOUBLE_QUOTED_ESCAPES: Record<string, string> = { n: '\n', r: '\r', t: '\t', '"': '"', '\\': '\\' }
36
37// A quoted value as python-dotenv reads it: double quotes take backslash escapes, single quotes are literal,
38// and a comment may follow the closing quote. A quote that does not close on its line (a multiline value) is
39// not supported, so it throws: the guard then refuses calls rather than protect a wrong value.
40function quotedValue(value: string, lineNumber: number): string {
41  const quote = value[0]
42  let inner = ''
43  for (let i = 1; i < value.length; i++) {
44    const ch = value[i]
45    if (ch === '\\' && quote === '"' && i + 1 < value.length) {
46      const next = value[i + 1] ?? ''
47      inner += DOUBLE_QUOTED_ESCAPES[next] ?? `\\${next}`
48      i += 1
49    } else if (ch === quote) {
50      const rest = value.slice(i + 1).trim()
51      if (rest !== '' && !rest.startsWith('#')) throw new Error(`unsupported .env syntax on line ${lineNumber}`)
52      return inner
53    } else inner += ch
54  }
55  throw new Error(`unsupported .env syntax on line ${lineNumber} (a quoted value that does not close)`)
56}
57
58// The secret values of an env file: keys whose name holds a secret, values long enough to hide.
59export function parseEnv(text: string, rule: KeyRule): Secret[] {
60  const secrets: Secret[] = []
61  text.split(/\r?\n/).forEach((raw, index) => {
62    const line = raw.trim()
63    if (line === '' || line.startsWith('#')) return
64    const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(line)
65    if (match === null) return
66    const name = match[1] ?? ''
67    const rest = (match[2] ?? '').trim()
68    const value = rest.startsWith('"') || rest.startsWith("'") ? quotedValue(rest, index + 1) : rest.replace(/\s+#.*$/, '')
69    if (!isSecretKey(name, rule) || value.length < MIN_SECRET_LEN) return
70    secrets.push({ name, value })
71  })
72  return byLength(secrets)
73}
74
75// Longest first, so a value that contains another is replaced whole.
76function byLength(secrets: Secret[]): Secret[] {
77  return secrets.sort((a, b) => b.value.length - a.value.length)
78}
79
80// Several env files merged, nearest first: a value found in two files keeps the nearest file's name.
81export function mergeSecrets(lists: readonly (readonly Secret[])[]): Secret[] {
82  const byValue = new Map<string, Secret>()
83  for (const list of lists) for (const s of list) if (!byValue.has(s.value)) byValue.set(s.value, s)
84  return byLength([...byValue.values()])
85}
86
87export const marker = (label: string) => `‹hidden: ${label}›`
88// A marker as it reaches a tool's input; the label is a key name or a kind such as jwt.
89export const MARKER = /‹hidden: ([^›\n]{1,100})›/g
90
91// Values that name or point at a secret rather than hold one: `$VAR`, `${{ secrets.X }}`, `<your key>`, a type.
92const NOT_A_VALUE = new Set(['true', 'false', 'null', 'none', 'nil', 'undefined', 'string', 'number', 'boolean', 'unknown', 'object'])
93// `bare`: the value stood unquoted in the text, where code names a variable rather than a literal.
94export function isSecretLiteral(value: string, bare = false): boolean {
95  if (value.length < MIN_SECRET_LEN || NOT_A_VALUE.has(value.toLowerCase())) return false
96  if (/^[$%<‹@]|^\{\{|^#\{/.test(value)) return false
97  // A path to a key file is not the key: ~/.ssh/id_rsa, ./certs/app.pem, C:\keys\app.p12.
98  if (/^(~|\.{1,2})[\\/]|^[A-Za-z]:[\\/]/.test(value)) return false
99  if (/\$\{|\$\(|process\.env|os\.environ|getenv|ENV\[/.test(value)) return false
100  // An unquoted attribute in code, not a literal: `self.api_key = config.service_key`. Generated values hold digits.
101  if (bare && /^[A-Za-z_]+(\.[A-Za-z_]+)+$/.test(value)) return false
102  // A mask someone already put there: ********, xxxxxxxx, ........, or a kept prefix and a masked rest (sk_****).
103  return !/^([*xX.•])\1*$/.test(value) && !/[*•]{4,}$/.test(value)
104}
105
106// A detected value is hidden everywhere else in the session only when it looks generated (long, letters and
107// digits), so `sortKey: "createdAt"` does not hide every createdAt that follows.
108function looksGenerated(value: string): boolean {
109  return value.length >= 12 && /[0-9]/.test(value) && /[A-Za-z]/.test(value)
110}
111
112// KEY=VALUE, KEY: VALUE, "KEY": "VALUE", --key=VALUE. An unquoted value after `:` must end its line (YAML),
113// so `apiKey: string;` or `token: config.token,` in code is left alone; after `=` it must end at a space or
114// a shell separator, so `password=pw,` (a keyword argument) is too.
115const KEYED = /(?<![\w.$])(["']?)([A-Za-z_][\w.-]*)\1([ \t]*(?::|=(?![=>~]))[ \t]*)("(?:[^"\\\n]|\\.)*"|'[^'\n]*'|[^\s'"`,;(){}[\]<>=]+)/g
116const PRIVATE_KEY = /-----BEGIN ([A-Z0-9 ]*)PRIVATE KEY( BLOCK)?-----[\s\S]*?(?:-----END \1PRIVATE KEY\2-----|$)/g
117const JWT = /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]+/g
118// scheme://user:password@host: only the password goes.
119const DSN = /\b([a-z][a-z0-9+.-]*:\/\/)([^\s:/@'"]*):([^\s@/'"]+)@/gi
120const AWS_KEY_ID = /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/
121// A 40-character secret access key on the same line as an access key id (a credentials CSV, a log line).
122const AWS_SECRET = /(?<![A-Za-z0-9/+=])[A-Za-z0-9/+]{40}(?![A-Za-z0-9/+=])/g
123
124// An object key that names a field (`apiKey`, `db.password`, `X-Api-Key`), as KEYED's keys do: no spaces.
125const KEY_NAME = /^[A-Za-z_][\w.-]*$/
126
127// Holds every value secret-guard knows (the env files', and ones it found in results) in memory only, and
128// hides them. `found` collects the names of what was hidden.
129export class Vault {
130  private named: Secret[]
131  private readonly byLabel = new Map<string, Set<string>>()
132
133  constructor(fileSecrets: readonly Secret[], private readonly rule: KeyRule) {
134    this.named = [...fileSecrets]
135    for (const s of fileSecrets) this.note(s.name, s.value)
136  }
137
138  // The env files' secrets plus the generated-looking values found since.
139  get size(): number {
140    return this.named.length
141  }
142
143  // The values a marker can stand for, for putting them back into a file that already holds one.
144  values(label: string): string[] {
145    return [...(this.byLabel.get(label) ?? [])]
146  }
147
148  private note(label: string, value: string): void {
149    const set = this.byLabel.get(label) ?? new Set<string>()
150    set.add(value)
151    this.byLabel.set(label, set)
152  }
153
154  private remember(label: string, value: string, everywhere: boolean): void {
155    this.note(label, value)
156    if (everywhere && !this.named.some(s => s.value === value)) this.named = byLength([...this.named, { name: label, value }])
157  }
158
159  // A value already hidden (the text is scrubbed twice: the tool's text and its record) is left as it is.
160  private hide(label: string, value: string, found: Set<string>, everywhere: boolean): string {
161    if (value.includes('‹hidden: ')) return value
162    found.add(label)
163    this.remember(label, value, everywhere)
164    return marker(label)
165  }
166
167  private exact(text: string, found: Set<string>): string {
168    for (const s of this.named) {
169      if (!text.includes(s.value)) continue
170      found.add(s.name)
171      text = text.split(s.value).join(marker(s.name))
172    }
173    return text
174  }
175
176  scrubText(text: string, found: Set<string>): string {
177    text = this.exact(text, found)
178    text = text.replace(PRIVATE_KEY, block => this.hide('private-key', block, found, true))
179    text = this.kubernetes(text, found)
180    text = text.replace(KEYED, (all, quote: string, key: string, sep: string, raw: string, offset: number, whole: string) => {
181      const quoted = /^["']/.test(raw)
182      const value = quoted ? raw.slice(1, -1) : raw
183      if (!isSecretKey(key, this.rule) || !isSecretLiteral(value, !quoted)) return all
184      // A key inside a quoted string (`rg -c "API_SECRET=" deploy/*.ini`): the quote after it closes that
185      // string, and what follows up to the next quote is more command, not a value.
186      if (quoted && (whole.slice(whole.lastIndexOf('\n', offset) + 1, offset).split(raw[0] ?? '').length - 1) % 2 === 1) return all
187      // A name that is only `key` (a React key, a YAML selector) hides a value only when it looks generated.
188      if (/^keys?$/i.test(key) && !looksGenerated(value)) return all
189      const after = whole.slice(offset + all.length)
190      // Unquoted after `:`, the value must end its line (YAML); after `=`, it must end at a space, a quote or a
191      // shell separator (`password=pw_var,` and `f(token=t)` are code).
192      if (!quoted && sep.includes(':') && !/^[ \t]*(#.*)?(\r?\n|$)/.test(after)) return all
193      if (!quoted && sep.includes('=') && !/^(\s|["'`;&|]|$)/.test(after)) return all
194      const shown = quoted ? `${raw[0]}${marker(key)}${raw[0]}` : marker(key)
195      found.add(key)
196      this.remember(key, value, looksGenerated(value))
197      return `${quote}${key}${quote}${sep}${shown}`
198    })
199    text = text.replace(DSN, (all, scheme: string, user: string, password: string) => {
200      if (/^[$%<{*‹]/.test(password)) return all
201      found.add('dsn-password')
202      this.remember('dsn-password', password, looksGenerated(password))
203      return `${scheme}${user}:${marker('dsn-password')}@`
204    })
205    text = text.split('\n').map(line => {
206      if (!AWS_KEY_ID.test(line)) return line
207      const withSecret = line.replace(AWS_SECRET, token =>
208        /[A-Z]/.test(token) && /[a-z]/.test(token) && /[0-9]/.test(token) ? this.hide('aws-secret-access-key', token, found, true) : token)
209      return withSecret.replace(new RegExp(AWS_KEY_ID.source, 'g'), id => this.hide('aws-access-key-id', id, found, true))
210    }).join('\n')
211    text = text.replace(JWT, token => this.hide('jwt', token, found, true))
212    // A value found just now may also stand bare elsewhere in the same text.
213    return this.exact(text, found)
214  }
215
216  // The values under a Kubernetes Secret's `data:` / `stringData:`, whatever their names.
217  private kubernetes(text: string, found: Set<string>): string {
218    if (/^\s*kind:\s*Secret\s*$/m.test(text)) {
219      const lines = text.split('\n')
220      let blockIndent = -1
221      let valueIndent = -1
222      for (let i = 0; i < lines.length; i++) {
223        const line = lines[i] ?? ''
224        const indent = line.length - line.trimStart().length
225        if (line.trim() === '') continue
226        if (valueIndent >= 0 && indent > valueIndent) {
227          // The lines of a `key: |` block scalar.
228          lines[i] = `${line.slice(0, indent)}${this.hide('k8s-secret', line.trim(), found, false)}`
229          continue
230        }
231        valueIndent = -1
232        if (blockIndent >= 0 && indent > blockIndent) {
233          const entry = /^(\s*)([\w.-]+):[ \t]*(\S.*)$/.exec(line)
234          if (entry === null) continue
235          const [, lead, key, value] = entry as unknown as [string, string, string, string]
236          if (/^[|>][-+0-9]*$/.test(value.trim())) valueIndent = indent
237          else lines[i] = `${lead}${key}: ${this.hide('k8s-secret', value.trim(), found, looksGenerated(value.trim()))}`
238          continue
239        }
240        blockIndent = /^\s*(data|stringData):\s*$/.test(line) ? indent : -1
241      }
242      text = lines.join('\n')
243    }
244    if (/"kind"\s*:\s*"Secret"/.test(text)) {
245      text = text.replace(/("(?:data|stringData)"\s*:\s*\{)([^{}]*)\}/g, (_all, head: string, body: string) =>
246        `${head}${body.replace(/("(?:[^"\\]|\\.)*"\s*:\s*)"((?:[^"\\]|\\.)*)"/g, (_e, key: string, value: string) =>
247          `${key}"${this.hide('k8s-secret', value, found, looksGenerated(value))}"`)}}`)
248    }
249    return text
250  }
251
252  // Every string in a tool's result: object keys stay (the result keeps its shape), a value under a secret
253  // key (`{ "apiKey": "..." }` from an MCP tool) is hidden whole. Only a key shaped like a name counts: a key
254  // that is a sentence (AskUserQuestion keys each answer by its question, "Which API key should I use?") is
255  // text, and the answer under it is not a secret because the question mentions one.
256  scrub(value: unknown, found: Set<string>): unknown {
257    if (typeof value === 'string') return this.scrubText(value, found)
258    if (Array.isArray(value)) return value.map(v => this.scrub(v, found))
259    if (value !== null && typeof value === 'object') {
260      return Object.fromEntries(Object.entries(value).map(([k, v]) => {
261        if (typeof v === 'string' && KEY_NAME.test(k) && isSecretKey(k, this.rule) && isSecretLiteral(v) && !v.includes('‹hidden: ')) {
262          found.add(k)
263          this.remember(k, v, looksGenerated(v))
264          return [k, marker(k)]
265        }
266        return [k, this.scrub(v, found)]
267      }))
268    }
269    // Numbers stay numbers so the result keeps its shape; find still sees them, and the guard withholds.
270    return value
271  }
272
273  // The names of known values left in any string, number, array item, object value or object key.
274  find(values: readonly unknown[]): string[] {
275    const found = new Set<string>()
276    const visit = (value: unknown): void => {
277      if (typeof value === 'string') {
278        for (const s of this.named) if (value.includes(s.value)) found.add(s.name)
279      } else if (typeof value === 'number' || typeof value === 'bigint') {
280        // A numeric value (a 12-digit PIN) can sit in a result as a number, not as text.
281        visit(String(value))
282      } else if (Array.isArray(value)) value.forEach(visit)
283      else if (value !== null && typeof value === 'object') {
284        for (const [key, inner] of Object.entries(value)) {
285          visit(key)
286          visit(inner)
287        }
288      }
289    }
290    values.forEach(visit)
291    return [...found]
292  }
293}
294
295export function fill(text: string, values: ReadonlyMap<string, string>): string {
296  return text.replace(MARKER, (all, label: string) => values.get(label) ?? all)
297}
298
299// The value behind each marker in an Edit or Write, when the target file settles it: each value must already be
300// in the file (restoring, never spreading), and every old_string must then match the file. Undefined when a
301// marker has no such value or could stand for several. A marker whose label stands for no value secret-guard
302// knows (a document quoting the format) is plain text and stays as written; so does a marker the file already
303// holds as text.
304export function resolveMarkers(texts: readonly { text: string; isOld: boolean }[], fileText: string, values: (label: string) => readonly string[]): Map<string, string> | undefined {
305  const labels = [...new Set(texts.flatMap(t => [...t.text.matchAll(MARKER)].map(m => m[1] ?? '')))]
306    .filter(label => values(label).length > 0)
307  let combos: Map<string, string>[] = [new Map()]
308  for (const label of labels) {
309    const candidates = [...values(label), marker(label)].filter(v => fileText.includes(v))
310    combos = combos.flatMap(c => candidates.map(v => new Map([...c, [label, v]])))
311    if (combos.length > 64) return undefined
312  }
313  const fits = combos.filter(c => texts.every(t => !t.isOld || fileText.includes(fill(t.text, c))))
314  return fits.length === 1 ? fits[0] : undefined
315}
316
317// What secret-guard answers when its own hook failed: refuse before the tool ran, withhold after.
318export function failClosed(called: boolean, kind: string): { deny: string } {
319  return called
320    ? { deny: 'secret-guard could not check this result, so it was withheld' }
321    : { deny: `secret-guard could not check this call (${kind}), so it was not run` }
322}
323
hooks/toggle.ts 24 lines
1// The mod's on/off switch, `/<mod> on|off|status`. The value is kept in $.store under STORE_KEY: one store per
2// plugin, shared by every session and folder, so the switch holds across restarts. A session reads it as it
3// starts and when its own command changes it; a session already open elsewhere picks a change up when it next
4// starts. Pure helpers only: `claude plugin validate` follows `$` only within the file that uses it.
5export const STORE_KEY = 'enabled'
6
7export type SwitchWord = 'on' | 'off' | 'status'
8
9// The command's argument as a switch word: '' and 'status' ask, 'on' and 'off' set; anything else is the mod's own.
10export function switchWord(args: string | undefined): SwitchWord | undefined {
11  const word = (args ?? '').trim().toLowerCase()
12  if (word === '' || word === 'status') return 'status'
13  return word === 'on' || word === 'off' ? word : undefined
14}
15
16// The stored value, or the mod's default when it was never set (or holds something else).
17export function storedSwitch(value: unknown, byDefault: boolean): boolean {
18  return typeof value === 'boolean' ? value : byDefault
19}
20
21export function switchText(mod: string, isOn: boolean): string {
22  return isOn ? `on (/${mod} off turns it off)` : `off (/${mod} on turns it on)`
23}
24