# Secret Read Guard

> Refuse tool calls that would print a secret file, helper output, keychain or environment; use masked reader.

- Skill: `etanhey/secret-read-guard` (Agent Skill, multi-file: 11 files)
- Install (CLI): `npx get-skill add etanhey/secret-read-guard`
- Raw SKILL.md: https://api.skillmd.com/api/skills/etanhey/secret-read-guard/raw
- Safety review: pending
- Works with: any agent that reads SKILL.md (Claude Code, Claude.ai, Cursor, Codex, Windsurf, 60+ more)
- Category: Coding & Dev Tools
- Author: EtanHey (https://skillmd.com/u/etanhey)
- Updated: 2026-12-01
- Page: https://skillmd.com/skills/etanhey/secret-read-guard

---

---
name: secret-read-guard
description: Refuse tool calls that would print a secret file, helper output, keychain or environment; use masked reader.
---

# Secret read guard

Every other golems gate guards writes. This one guards reads: on 2026-11-17 a
Codex worker printed the Claude-proxy key with a plain `cat` of a settings
file, and nothing stopped it.

## What it refuses

A Bash, Monitor, Read or Grep call that would put a secret on screen:

- **A listed file.** The list is data: `secret-files.json` (paths, basenames,
  Claude settings whose `env` block holds a secret-named key, and every
  `*.bak-*`, `~`, `.orig`, `.old` and `.swp` backup), matched the way APFS
  names files: case-insensitive and
  Unicode-normalization-insensitive (`.ENV` is `.env`; composed `é` is
  `e` + U+0301).
  `cat`, `head`/`tail`, `less`, `bat`, any `sed` (in-place included: its
  `w` command can print), `awk`, `grep` (not `-c`/`-l`/`-L`/`-q`),
  `jq` (except a key-only filter such as `keys`), `base64`, `xxd`, `strings`,
  `diff`, editors, `git show HEAD:<file>`, interpreters given the file,
  `curl -d @file`, `< file` into anything but a counter or hash, and a copy or
  move of the file to a path the list does not cover (`cp .env /dev/stdout`,
  `cp auth.json notes.txt`). Any command not known to be safe is refused when
  an operand resolves to a listed file.
- **MCP configs.** `.mcp.json` in any folder, `~/.claude.json` and Claude's
  `.claude.json.backup*` copies are listed names, so every shape above
  applies to them: `cat`/`head`/`sed`/`grep`/`jq .` over them, `python3 -c`
  code that opens them, `python3 -m json.tool`, the Read tool and content
  Grep. Absolute paths, `$HOME/.claude.json`, other folders and symlinks to
  them are covered (the link's real path is checked). Servers keep tokens
  inline under `mcpServers.<name>.env` or `headers`, which is how a live
  token reached transcripts 49 times in the 30 Nov 2026 leak review. Safe:
  `jq '.mcpServers | keys' .mcp.json`, `ls`, `wc -c`, `.mcp.json.example`,
  and the masked reader, which keeps server names, `command`, `args`, `type`
  and `url` readable. A content Grep over a folder that holds a `.mcp.json`
  is refused like one that holds a `.env`: pass a `glob` or a narrower path.
- **Process command lines.** `ps` forms that print an argument column print
  every key passed on a command line: `ps aux`, `ps -ef`, bare `ps`, `ps -A`,
  `ps -p <pid>` (macOS's default format ends in the full command line),
  `-o`/`--format` lists with `args`, `command` or `cmd`, `-O`, and any
  default-format flag (`-f`, `-l`, `-j`, `-v`, `-u`, BSD `u`/`v`/`j`/`l`/`s`)
  next to `-o`. Also `pgrep`/`pkill` with `-a`/`--list-full`, or `-l` with
  `-f`, and `/proc/<pid>/cmdline`. Safe forms, which the refusal names:
  `pgrep -x <name>` or `pgrep -f <pattern>` (pids only), `pgrep -l <name>`
  without `-f`/`-a` (pid and name), `ps -axo pid,ppid,stat,etime,comm` (any
  `-o` list without an argument column), and the masked listing
  `python3 <reader> --ps [filter]`.
- **Links and moves.** `ln`, `ln -s`, `cp -l`/`-s` of a listed file, and `mv`
  from or onto a listed path. The whole command is judged before any of it
  runs, so a link made in it is invisible to a later read: every name this
  command links (or copies a listed file to) is an alias, and reading it later
  in the same command is refused.
- **A file it can't name.** A printing command whose file is only known at
  run time (`f=$(grep -l ...); cat "$f"`, xargs input, `while read f`) is
  refused: fail-closed. The refusal says to run the lookup first and name the
  file literally. Globs, `~`, `$HOME`, inherited environment variables,
  variables the command assigns itself (`F=x.md; cat "$F"`), `for f in a b`
  lists, `cd` targets, and `find -name '*.ts' -exec head {} +` (a name pattern
  that can't match a listed file, outside secret folders) are resolved. For
  `find ... -exec <printer>`, a literal listed name (`-name .mcp.json`) is
  refused outright; a wildcard filter is checked against the real tree: the
  start folders are walked (every depth, case-folded matching, symlinks
  judged by their target) and the command is refused if any matching file is
  listed, so `-name '*.bak-review'` meets `.mcp.json.bak-review`. A pattern
  can't be proved disjoint from the list in the abstract (`.env.*` and the
  `.bak-*` backup suffix absorb any extension), which is why the tree is
  walked. Over 1.5 s or 200,000 entries, character classes and escapes fail
  closed.
- **Commandless reads.** `$(< file)` substitutes the file's content and zsh
  pages a bare `< file`, so `echo $(< .env)`, `cat <<< $(< .mcp.json)` and
  `< ~/.claude.json` are judged like `cat`. Output-only redirects
  (`> out.txt`) stay allowed.
- **Anything over 32 KiB.** A command too large for the policy parser is
  refused whatever it names (split it, or pass long content by file path);
  git-guardian refuses those too.
- **A recursive search into a secret folder** (`grep -r`, `rg`, content-mode
  Grep) that prints lines, including a symlink into that folder. Content-mode
  Grep over a directory enumerates with `rg --files --hidden --no-follow`,
  excludes `.git`, `.svn`, `.hg`, `.bzr`, `.jj` and `.sl`, and uses Claude's
  whitespace/comma glob splitting (brace alternations stay together) and type.
  Any listed candidate refuses the search regardless of the content pattern.
  Resolution tries PATH, Claude's embedded rg (`CLAUDE_CODE_EXECPATH` with
  argv0 `rg`), Homebrew locations, then vendored rg under Claude's install.
  Without rg, a bounded Python superset walk includes hidden and gitignored
  files, ignores glob/type filters, prunes those six VCS dirs and skips links.
  Clean trees allow on either path. Enumeration fails closed after 2 seconds,
  40,000 entries or 2 MiB of names, or on a tool error. Brace/class and
  specimen pre-checks apply only when the path is not a directory.
- **A secret helper:** running `claude-proxy-token` (the `apiKeyHelper`).
- **The keychain:** `security find-generic-password` / `find-internet-password`
  with `-w` or `-g`, and `security dump-keychain`.
- **Environment dumps, in any form or pipeline, no exceptions:** `env` with no
  command (`env -0` too), `printenv` (any argument), `export` bare or `-p`,
  bare `set`, `declare`/`typeset` bare or with `-p`/`-x`, `compgen -e`;
  `echo`/`printf`/`launchctl getenv` of a secret-named variable; `ps -E`,
  `ps e`, `ps eww`; `launchctl print|export|procinfo`; `/proc/*/environ`.
  `env NAME=value cmd` (env as a wrapper) still runs. Variable names only?
  Use a script file.
- **Inline interpreter code that touches the environment at all.** For
  python, node, ruby, perl, php, deno, bun, osascript and the rest: inline
  code in any spelling (`-c`/`-e`, clustered `-Ic`/`-sc`, attached `-cCODE`,
  `--eval=CODE`, `-p`, `-r`, `deno eval`, `-`, a heredoc or here-string with
  no script file) is refused if it references `os.environ`, `environ`,
  `getenv`, `process.env`, `ENV`, `%ENV`, `Deno.env`, `Bun.env`, AppleScript
  `system attribute` / `do shell script`, Swift `ProcessInfo`/`.environment`,
  Scala `sys.env`, or Tcl `$env(`, `env(`,
  `array get env`, `parray env`. The same goes for awk-family `ENVIRON` and
  jq/yq `env`, `$ENV`, `strenv` (a `.env` field is fine). Code piped on stdin
  into any interpreter or shell (`echo 'puts $env(K)' | tclsh`,
  `echo printenv | sh`, `cat <<EOF | node`) is inline code too; a variable in
  it means the code only exists at run time, so it is refused. Output of
  another command piped in (`cat tool.py | python3`) counts as a script file.
  A heredoc or here-string fed to any shell, `eval`'s arguments and
  `source /dev/stdin <<EOF` are judged as shell text; `source <(...)` is
  refused. `busybox`/`toybox <applet>` is judged as the applet.
  No name matching: aliases (`e=os.environ; print(e)`) leave nothing to match. It is
  also refused if it names a listed path, opens a listed name
  (`open('.env')`) or runs the helper. Code that needs a key belongs in a
  script file; `( set -a; . x.env; set +a; python3 tool.py )` keeps working.

## The masked reader

```bash
python3 ~/.claude/hooks/secret-read-guard/scripts/masked-read.py <file>
```

Exactly that argv: `python3 <reader> <files>`. Any interpreter option
(`-I`, `-X`, `-c`, `-m`, `-W`...) or other shape, direct execution included,
is not exempt. It prints the file's structure with every value replaced by
`<redacted len=N sha=xxxxxxxx>` (JSON, YAML, env and TOML aware; unknown
formats print key names only; comments are dropped). Equal values share a
sha, so two files can be compared without showing either.

MCP configs stay auditable: inside any JSON `mcpServers` object, server
names and plain fields (`command`, `args`, `type`, `url`, `cwd`, numbers)
print as they are, while everything under `env` and `headers`, any field
whose key names a credential (token, secret, password, api key, auth,
cookie...), the whole argv element after a credential-named flag in `args`
(even one starting with `-`), the whole value of a credential-named
`name=value` / `Name: value` element (spaces included), URL passwords,
query values whose percent-decoded name names a credential (`api%5Fkey`),
explicit `Bearer`/`Basic`/`Token` values at any length, and strings with a
credential shape (`sk-`, `ghp_`, `sbp_`, `xox*-`, `AKIA`, JWTs, long random
tokens) are redacted. Once inside `env`, `headers` or a credential-named key,
every descendant is redacted; a `mcpServers` nested there never becomes
readable. A `.claude.json` prints only its non-empty `mcpServers` objects,
keyed by path (`projects > /repo > mcpServers`); its other keys hold
history and account data.

```bash
python3 ~/.claude/hooks/secret-read-guard/scripts/masked-read.py --ps [filter...]
```

prints `pid ppid command` for every process, with the same credential shapes
redacted in each command line. A flattened command line has lost its argv
boundaries, so from the first credential marker on (a dash-flag naming a
credential, `NAME=` / `Name:` naming one, or `Bearer`/`Basic`) the rest of
the line is redacted whole: a dash-leading, quoted or spaced value can't be
told apart from the next argument. Filters are case-insensitive substrings
matched against the redacted line, so a filter can't probe a hidden value.
Shapes are a heuristic: a key with no recognizable shape, passed positionally
after a flag whose name doesn't say credential, still prints. Prefer the pid
and name forms when they answer the question.

The guard exempts exactly this file (by real path), not a look-alike. The refusal message names
the reader's path.

## Allowed

`ls`, `stat`, `test -f`, `wc -c`, `shasum`, `chmod`, `grep -c`, `jq 'keys'`,
`. file.env` / `source` with a script or binary after it (the sanctioned way
to use a key), `env NAME=value cmd`,
`cp x.env x.env.bak-<date>`, reading `.env.example`, `jq '.mcpServers | keys'
.mcp.json`, `pgrep -x`/`-f`/`-l` (without `-f` and `-l` together, and without
`-a`), `ps -axo pid,ppid,stat,etime,comm`, and every command that names no
listed file.

## Known limits

**This guard is a speed bump plus an audit trail, not a sandbox.** It parses
shell text and cannot enumerate every interpreter, applet, encoding or
indirection an agent could use to read a file or the environment. The primary
control is keeping secrets out of agent-readable files: the keyout lane's
`apiKeyHelper` plus the macOS keychain. Treat a refusal as a nudge toward the
masked reader or a script file, and a pass as "no known shape matched", never
as proof that nothing leaked.

Specific gaps known today:

- Other process viewers that print command lines (`pstree`, `htop`,
  `procs`, `sysctl kern.procargs2`) are not parsed; `ps` and `pgrep` cover
  the shapes seen in transcripts. The `ps` parser judges macOS and procps
  spellings and fails closed on run-time input, but an exotic option spelling
  it misreads can still pass. It also over-refuses a few harmless forms
  (`ps -axo pid,comm -X`, `ps -axo pid,args -c`); use a plain `-o` list.
- The find walk sees the tree as it is when the hook runs; a file created
  between the check and the run is not seen (the same race content Grep
  has). A start folder too big to walk inside the bound is refused; name a
  narrower folder.

- Interpreter **scripts** (`python3 tool.py`) are not read; only their operands
  and inline code are checked. Languages with only file runners (`go run`)
  are the same.
- Shell text is judged with bash meanings: fish's `set -x` / `set --show`
  list exported variables there, but `set -x` is tracing in bash, so it is
  not refused. Paths built at run time in code
  (`os.path.join(home, ".codex", "auth.json")`) are out of reach.
- `source x.env && echo "$VAR"` is caught only when the variable name looks
  secret (`token`, `secret`, `password`, `api_key`, `credential`...).
- Bash recursive searches inside ordinary repos are not walked; `rg` skips
  hidden files by default, `grep -r` does not. Directory content-Grep now
  checks hidden candidates with Claude's rg flags and retains rg's ignore
  rules. Without rg the superset walk may deny a glob/type-filtered search
  because it also checks ignored or excluded files. Repos over the enumeration
  cap refuse content Grep; use a narrower path. Filesystem changes between
  the check and the actual search remain a TOCTOU race.
- Editing a listed file needs a path that never prints it: Claude's Edit tool
  needs a Read first (refused), and inline code that opens the file is
  refused. Use a script file or the tool that owns the file.
- These limits are why the keyout lane also takes secrets out of readable
  files. This guard narrows the accident; it is not a sandbox.

## Measured

Replaying distinct real Bash commands from local Claude transcripts (scratch
HOME, no real file read): round 0, 369 of 25,702 refused (1.44%); round 1
(stricter env, sed and inline-code rules), 379 of 24,904 (1.52%); round 2
(final class rules: any env reference in inline code, no env-dump
exceptions), 431 of 24,892 (1.73%); round 3 (awk/jq/AppleScript/Tcl env
reads, piped stdin code), 432 of 24,880 (1.74%). About 60% of refusals name a file only at
run time; most of the rest touch a listed file or the environment. Hook median
81 ms.

## Wiring

Claude: PreToolUse on `Bash|Monitor|Read|Grep`, fail-closed launcher.
Codex 0.171: native `PreToolUse` hook on `^Bash$` through
`scripts/hooks/codex-policy-hook.py` (Codex has no Read tool; shell is its
read path). Codex runs a new hook only after the owner trusts it from plain
`codex` via `/hooks`. Each denial appends `ts`, `input_sha256` and `rule` to
`~/.claude/logs/secret-read-guard-ledger.jsonl` (override:
`SECRET_READ_GUARD_LEDGER`), never the command text.

