claude-secret-mask: a Claude Code plugin that masks secrets in .env files and command output
claude-secret-mask is a Claude Code plugin with a PostToolUse hook. It redacts secrets, such as .env values, API tokens and passwords, in the results of Read, Bash and Grep before the model reads them.
The problem
Claude Code reads files and runs commands for you. When it reads a .env file, or runs a command that prints a token, the secret goes into the model's context. It is also stored in the session transcript and drawn in the terminal.
This plugin replaces the secret with a notice before it gets there. The key name stays, so the model knows a value exists and was hidden on purpose.
Install
Needs Node 22.18 or newer on your PATH. The hook runs the TypeScript sources with Node directly, with no build step.
Inside Claude Code:
/plugin marketplace add ejklock/claude-secret-mask
/plugin install secret-mask@secret-mask
On Claude Code 2.1.275 or later, one step does both:
/plugin install secret-mask --marketplace ejklock/claude-secret-mask
From a shell:
claude plugin marketplace add ejklock/claude-secret-mask
claude plugin install secret-mask@secret-mask
To try it from a local clone without installing: claude --plugin-dir /path/to/claude-secret-mask.
Example
A .env file with fake values:
DATABASE_HOST=db.example.test
API_KEY=fake-api-key-value
APP_NAME=demo
What the model sees when it reads that file:
DATABASE_HOST=[MASKED by secret-mask: original value hidden for security]
API_KEY=[MASKED by secret-mask: original value hidden for security]
APP_NAME=[MASKED by secret-mask: original value hidden for security]
Every value of an env file is masked, not only the ones that look secret.
What it covers
The rules run in order over every string of a Read, Bash or Grep result.
-
Env files. Every value in a .env, .env.example or .env.* file is masked. This applies to a Read, to the rows of a Grep in content mode, and to the output of a Bash command that names such a file (a word of the command whose basename is .env or starts with .env.). The file is judged by its path alone.
-
Token shapes. Well-known token shapes are replaced whole, anywhere in the text: AWS access key ids, GitHub, GitLab, Slack (and Slack webhook URLs), Stripe, Google, Anthropic, OpenAI and npm tokens, JWTs, and private key blocks (a block with no end is masked to the end of the text). In a URL with credentials only the password is hidden. In an Authorization header only the value after Bearer, Basic or Token is hidden. The shapes are a curated subset of the default rules of gitleaks (MIT).
-
Secret-named assignments. The literal value of an assignment is masked when its name is secret-named, and the name and the operator stay. A name is secret-named when one of its words is secret, token, password, pass, credential, apikey, dsn, salt and similar, or its last word is key. It covers NAME=value, a quoted string after : or = (JSON, YAML, code) and a YAML name: value line. The YAML form is skipped in source-code files, and for type names and values ending in ; or ,. Empty values and references such as $NAME are left alone.
-
Known values. A secret value held by an env file directly inside the working directory, or by an environment variable of the hook process, is masked wherever it appears in a result, longest first. Only the first 1 MiB of each file is read. A value is selected when:
- its name is secret-named and it has 8 or more characters; or
- under any name, it has 16 or more characters, no whitespace, no
://, no leading /, ./, ../ or ~/, and mixes at least 3 of lowercase, uppercase, digit and other characters (other counts only characters outside : / . - _ @).
References ($NAME, {{ ... }}), PWD, OLDPWD and text inside the notices are never selected. Short ordinary values such as production or localhost stay visible in other files, because masking them everywhere would hide ordinary words. An env file is already masked whole.
-
Fail closed. If the content of an env-file Read result cannot be read, the whole result is replaced by [WITHHELD by secret-mask: this output could not be checked, so it was hidden for security].
A mask already in the text is never masked again. A result with nothing to hide is left untouched.
Limits
Measured on Claude Code 2.1.300 (see how it was verified):
| Surface | Sees the original? |
|---|
| The model's context | No |
The session transcript (.jsonl) | No |
| The terminal render | No |
| OpenTelemetry tool spans (per the Claude Code docs) | Yes |
| Other PostToolUse hooks | Yes |
| What you type in a prompt | Yes; a hook cannot rewrite a prompt |
More limits: