@coo-quack/sensitive-canary 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +830 -0
- package/README.md +269 -43
- package/dist/lib/bash-commands.js +405 -0
- package/dist/lib/command-tables.js +462 -0
- package/dist/lib/default-config.json +570 -0
- package/dist/lib/encoding.js +123 -0
- package/dist/lib/fail-closed.js +31 -0
- package/dist/lib/inspector.js +0 -0
- package/dist/lib/rules.js +399 -0
- package/dist/lib/shapes.js +161 -0
- package/dist/lib/shell.js +436 -0
- package/dist/lib/tool-inputs.js +217 -0
- package/dist/lib/transcript.js +115 -0
- package/dist/lib/validators.js +435 -0
- package/dist/pre-tool-use-hook.js +773 -0
- package/dist/user-prompt-submit-hook.js +105 -0
- package/hooks/hooks.json +1 -1
- package/package.json +25 -11
- package/src/lib/bash-commands.ts +455 -0
- package/src/lib/command-tables.ts +518 -0
- package/src/lib/default-config.json +570 -0
- package/src/lib/encoding.ts +135 -0
- package/src/lib/fail-closed.ts +36 -0
- package/src/lib/inspector.ts +0 -0
- package/src/lib/rules.ts +482 -267
- package/src/lib/shapes.ts +175 -0
- package/src/lib/shell.ts +512 -0
- package/src/lib/tool-inputs.ts +235 -0
- package/src/lib/transcript.ts +142 -0
- package/src/lib/validators.ts +435 -0
- package/src/pre-tool-use-hook.ts +774 -198
- package/src/user-prompt-submit-hook.ts +60 -18
- package/src/__tests__/pre-tool-use-hook.test.ts +0 -779
- package/src/__tests__/user-prompt-submit-hook.test.ts +0 -297
- package/src/lib/__tests__/inspector.test.ts +0 -281
- package/src/lib/__tests__/rules.test.ts +0 -448
package/README.md
CHANGED
|
@@ -20,14 +20,17 @@ Claude Code is a powerful development tool, but file reads and command execution
|
|
|
20
20
|
| Without sensitive-canary | With sensitive-canary |
|
|
21
21
|
|--------------------------|----------------------|
|
|
22
22
|
| `cat .env` → full contents sent to Claude ❌ | Blocked by name before Claude reads it ✅ |
|
|
23
|
-
| Paste
|
|
24
|
-
|
|
|
23
|
+
| Paste a live AWS key in a prompt ❌ | Blocked before the API call is made ✅ |
|
|
24
|
+
| `Read customers.csv` full of email addresses ❌ | PII detected before Claude sees the file ✅ |
|
|
25
25
|
| `echo $API_KEY` with live key ❌ | Env var value scanned and blocked ✅ |
|
|
26
|
+
| `cat docker-compose.yml` with `POSTGRES_PASSWORD:` ❌ | Assignment detected in YAML and JSON too ✅ |
|
|
26
27
|
|
|
27
28
|
- **Two hooks** — `UserPromptSubmit` and `PreToolUse` cover both directions of risk
|
|
28
|
-
- **
|
|
29
|
+
- **76 detection rules** — sourced from gitleaks and TruffleHog detector definitions
|
|
30
|
+
- **Checksum validation** — credit cards (Luhn) and national ID numbers (JP My Number, FR NIR, IT Codice Fiscale, DE Steuer-IdNr., ES DNI/NIE, KR RRN/BRN, CN Resident ID)
|
|
31
|
+
- **Context gating** — the noisiest rules only fire when a label is nearby: non-US/JP phone numbers, ZIP, EU/KR and Chinese postal codes, public IPv4 and IPv6, and the Korean resident and business numbers. US and Japanese phone numbers and Japanese postal codes are matched without a label, since their shapes are specific enough. RFC 1918 private addresses are not matched at all — they are non-routable, they identify nothing outside the network they belong to, and they fill the inventories, manifests and ssh configs this tool is most often pointed at
|
|
32
|
+
- **Not everything that looks like a secret is one** — published test card numbers, RFC 2606 domains (`example.com`), a value that is a variable reference (`PASSWORD: ${VAR}`), an ssh or scp target (`git@github.com`, `deploy@host`, `user@host:path`), and `.env.example` and its siblings are left alone. Each was blocking ordinary work. A template is exempt only when its contents can be read whole. One holding a NUL byte, running past the per-file cut, reached after the call's budget or deadline, or that is not a regular file at all is blocked on its name, since the contents are what the exemption relies on. A template name that exists on no disk is not blocked — there is nothing to read and nothing to leak
|
|
29
33
|
- **Entropy filtering** — reduces false positives on low-entropy values
|
|
30
|
-
- **Luhn validation** — credit card numbers are validated, not just pattern-matched
|
|
31
34
|
- **Local only** — all scanning runs in your terminal; nothing is sent anywhere
|
|
32
35
|
|
|
33
36
|
---
|
|
@@ -55,7 +58,15 @@ Install in two commands from inside a Claude Code session:
|
|
|
55
58
|
/plugin install sensitive-canary@coo-quack
|
|
56
59
|
```
|
|
57
60
|
|
|
58
|
-
|
|
61
|
+
The hooks are enabled for every session started after this. A session that was
|
|
62
|
+
already running does not pick them up — it reports the plugin as enabled and
|
|
63
|
+
checks nothing — so start a new one.
|
|
64
|
+
|
|
65
|
+
**Then check that it blocks.** An installation that checks nothing looks exactly
|
|
66
|
+
like one that works, and only exit 2 stops a tool call, so a hook that fails to
|
|
67
|
+
start is silent. Write a file holding `AKIA` followed by `IOSFODNN7EXAMPLE` and
|
|
68
|
+
ask Claude to read it. It should refuse and say why. If it shows you the key, the
|
|
69
|
+
hooks are not running.
|
|
59
70
|
|
|
60
71
|
> **Keeping up to date:** Third-party marketplaces have auto-update disabled by default. To receive automatic updates, run `/plugin` → **Marketplaces** tab → select the marketplace → **Enable auto-update**. You can also update manually from the same tab. See [Discover and install plugins](https://docs.anthropic.com/en/docs/claude-code/discover-plugins) for details.
|
|
61
72
|
|
|
@@ -84,18 +95,18 @@ Then add to `~/.claude/settings.json`:
|
|
|
84
95
|
"hooks": [
|
|
85
96
|
{
|
|
86
97
|
"type": "command",
|
|
87
|
-
"command": "
|
|
98
|
+
"command": "node $(npm root -g)/@coo-quack/sensitive-canary/dist/user-prompt-submit-hook.js"
|
|
88
99
|
}
|
|
89
100
|
]
|
|
90
101
|
}
|
|
91
102
|
],
|
|
92
103
|
"PreToolUse": [
|
|
93
104
|
{
|
|
94
|
-
"matcher": "Read|Bash",
|
|
105
|
+
"matcher": "Read|NotebookRead|Bash|Grep|mcp__.*",
|
|
95
106
|
"hooks": [
|
|
96
107
|
{
|
|
97
108
|
"type": "command",
|
|
98
|
-
"command": "
|
|
109
|
+
"command": "node $(npm root -g)/@coo-quack/sensitive-canary/dist/pre-tool-use-hook.js"
|
|
99
110
|
}
|
|
100
111
|
]
|
|
101
112
|
}
|
|
@@ -104,7 +115,7 @@ Then add to `~/.claude/settings.json`:
|
|
|
104
115
|
}
|
|
105
116
|
```
|
|
106
117
|
|
|
107
|
-
> **Note:**
|
|
118
|
+
> **Note:** These point at the compiled JavaScript the package ships. Node refuses to strip types from a `.ts` file inside `node_modules`, and a hook that fails to start exits non-zero without blocking — so an installation wired to `src/` looks installed and checks nothing. The plugin install uses the `.ts` sources, which sit outside `node_modules` and work.
|
|
108
119
|
|
|
109
120
|
</details>
|
|
110
121
|
|
|
@@ -140,7 +151,7 @@ Then add to `~/.claude/settings.json`:
|
|
|
140
151
|
],
|
|
141
152
|
"PreToolUse": [
|
|
142
153
|
{
|
|
143
|
-
"matcher": "Read|Bash",
|
|
154
|
+
"matcher": "Read|NotebookRead|Bash|Grep|mcp__.*",
|
|
144
155
|
"hooks": [
|
|
145
156
|
{
|
|
146
157
|
"type": "command",
|
|
@@ -164,11 +175,14 @@ Then add to `~/.claude/settings.json`:
|
|
|
164
175
|
Prompts containing secrets or PII are blocked before being sent.
|
|
165
176
|
|
|
166
177
|
```
|
|
167
|
-
>
|
|
178
|
+
> Here is my deploy key:
|
|
179
|
+
> -----BEGIN RSA PRIVATE KEY-----
|
|
180
|
+
> MIIEowIBAAKCAQEAwK3vJ9m5Q8xY2nB4dF6hL0pR7sT1uV3wX5yZ8aC2eG4iK6mO
|
|
181
|
+
> Can you review the config that uses it?
|
|
168
182
|
|
|
169
|
-
|
|
183
|
+
🐦 sensitive-canary: sensitive data detected — blocked
|
|
170
184
|
|
|
171
|
-
[Secret]
|
|
185
|
+
[Secret] PEM Private Key (private-key): ---****KEY
|
|
172
186
|
|
|
173
187
|
To allow, add a tag to your prompt:
|
|
174
188
|
[allow-secret] — allow secrets
|
|
@@ -178,12 +192,12 @@ To allow, add a tag to your prompt:
|
|
|
178
192
|
To allow it through, add the suggested tag:
|
|
179
193
|
|
|
180
194
|
```
|
|
181
|
-
> [allow-secret]
|
|
195
|
+
> [allow-secret] Here is my deploy key: -----BEGIN RSA PRIVATE KEY-----
|
|
182
196
|
```
|
|
183
197
|
|
|
184
198
|
### .env file blocked
|
|
185
199
|
|
|
186
|
-
`.env`
|
|
200
|
+
`.env` and its siblings are blocked by filename, before anything is read. Template names — `.env.example`, `.env.sample`, `.env.template`, `.env.dist`, `.env.defaults` — are the exception: they are meant to be committed, so they are read and judged on their contents like any other file. A template that turns out to hold a real credential is still blocked, and one whose contents cannot be read whole falls back to the name. This name-based block is a secret guard and only applies while the `secret` category is enabled (the default).
|
|
187
201
|
|
|
188
202
|
```
|
|
189
203
|
> Read .env
|
|
@@ -224,7 +238,7 @@ To intentionally bypass a block, include the appropriate tag in your **current p
|
|
|
224
238
|
| `[allow-pii]` | Skip all PII-category checks |
|
|
225
239
|
| `[allow-all]` | Skip all sensitive-canary checks |
|
|
226
240
|
|
|
227
|
-
> **Note:** Tags are read from the **current user message only**. Tags in previous messages are ignored — there is no risk of an accidental persistent bypass. Tags are case-insensitive. `[allow-secret]` does not bypass PII blocks (and vice versa). The name-based block on `.env`/`.env.*` files
|
|
241
|
+
> **Note:** Tags are read from the **current user message only**. Tags in previous messages are ignored — there is no risk of an accidental persistent bypass. Tags are case-insensitive. `[allow-secret]` does not bypass PII blocks (and vice versa). The name-based block on `.env`/`.env.*` files is a secret guard, so `[allow-secret]` and `[allow-all]` lift it and `[allow-pii]` does not.
|
|
228
242
|
|
|
229
243
|
---
|
|
230
244
|
|
|
@@ -254,15 +268,118 @@ This is a persistent filter, unlike allow tags which apply per prompt. The categ
|
|
|
254
268
|
|
|
255
269
|
---
|
|
256
270
|
|
|
271
|
+
## Custom Rules
|
|
272
|
+
|
|
273
|
+
All detection rules are defined in `src/lib/default-config.json` as data, not code. You can add your own rules or override built-in ones by creating a config file.
|
|
274
|
+
|
|
275
|
+
### Config file location
|
|
276
|
+
|
|
277
|
+
Create `~/.config/sensitive-canary/config.json`, or point to a custom path with the `SENSITIVE_CANARY_CONFIG` environment variable. Set it in the `env` block of your Claude Code `settings.json`:
|
|
278
|
+
|
|
279
|
+
```json
|
|
280
|
+
{
|
|
281
|
+
"env": {
|
|
282
|
+
"SENSITIVE_CANARY_CONFIG": "/path/to/my-rules.json"
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
or export it in your shell:
|
|
288
|
+
|
|
289
|
+
```sh
|
|
290
|
+
export SENSITIVE_CANARY_CONFIG=/path/to/my-rules.json
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Adding a rule
|
|
294
|
+
|
|
295
|
+
Each rule is a JSON object with an `id`, `description`, `regex` (source string), and `category` (`"secret"` or `"pii"`):
|
|
296
|
+
|
|
297
|
+
```json
|
|
298
|
+
{
|
|
299
|
+
"rules": [
|
|
300
|
+
{
|
|
301
|
+
"id": "custom-api-key",
|
|
302
|
+
"description": "My Service API Key",
|
|
303
|
+
"regex": "MYSVC-[A-Za-z0-9]{32}",
|
|
304
|
+
"category": "secret"
|
|
305
|
+
}
|
|
306
|
+
]
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### Overriding a built-in rule
|
|
311
|
+
|
|
312
|
+
A user rule with the same `id` as a built-in rule replaces it. For example, to tighten the email regex:
|
|
313
|
+
|
|
314
|
+
```json
|
|
315
|
+
{
|
|
316
|
+
"rules": [
|
|
317
|
+
{
|
|
318
|
+
"id": "pii-email",
|
|
319
|
+
"description": "Internal Email",
|
|
320
|
+
"regex": "[A-Za-z0-9]+@internal\\.corp\\.(com|org)",
|
|
321
|
+
"category": "pii"
|
|
322
|
+
}
|
|
323
|
+
]
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Context gating and validators
|
|
328
|
+
|
|
329
|
+
User rules support the same fields as built-in rules:
|
|
330
|
+
|
|
331
|
+
| Field | Type | Description |
|
|
332
|
+
|---|---|---|
|
|
333
|
+
| `requireContext` | boolean | Only fire when a nearby context word is found |
|
|
334
|
+
| `contextWords` | string[] | Words that satisfy the context requirement |
|
|
335
|
+
| `excludeContext` | string[] | Words that, found nearby, say the match is not what the rule is after — the mirror of `contextWords` |
|
|
336
|
+
| `contextWindow` | number | Override the global context window (default: 3 tokens) |
|
|
337
|
+
| `entropyThreshold` | number | Skip matches below this Shannon entropy |
|
|
338
|
+
| `secretGroup` | number | Capture group holding the secret. Omit for the whole match — writing `0` is not the same as omitting it, see [Detection Rules](https://coo-quack.github.io/sensitive-canary/rules.html) |
|
|
339
|
+
| `validate` | string | Name of a built-in checksum validator (see below) |
|
|
340
|
+
| `flags` | string | Regex flags. `g` is added if left out; `y` makes a rule match only at the very start of the text |
|
|
341
|
+
|
|
342
|
+
Available validators (referenced by name in the `validate` field):
|
|
343
|
+
|
|
344
|
+
`luhn`, `aws-key`, `phone-jp`, `mynumber-jp`, `nir-fr`, `codice-fiscale-it`, `steuer-id-de`, `dni-nie-es`, `rrn-kr`, `brn-kr`, `resident-id-cn`, `public-ipv4`, `public-ipv6`
|
|
345
|
+
|
|
346
|
+
### Overriding the context window globally
|
|
347
|
+
|
|
348
|
+
Set `contextWindow` at the top level to change how many tokens of surrounding text are scanned for context words (default: 3):
|
|
349
|
+
|
|
350
|
+
```json
|
|
351
|
+
{
|
|
352
|
+
"contextWindow": 5,
|
|
353
|
+
"rules": []
|
|
354
|
+
}
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
Invalid rules (bad regex, wrong types, missing required fields) are skipped with a warning on stderr. The rest of the config still loads. Each rule is validated against a strict schema before compilation — `requireContext: true` without `contextWords` is also rejected, since empty `contextWords` would silently disable context gating and make the rule fire on every match.
|
|
358
|
+
|
|
359
|
+
---
|
|
360
|
+
|
|
257
361
|
## Detection rules
|
|
258
362
|
|
|
259
|
-
### Secrets (
|
|
363
|
+
### Secrets (52 rules)
|
|
260
364
|
|
|
261
365
|
| Rule ID | Description |
|
|
262
366
|
|---|---|
|
|
367
|
+
| `openai-service-key` | OpenAI Service Account / Admin Key (`sk-svcacct-`, `sk-admin-`, `sk-proj-` prefix) |
|
|
368
|
+
| `azure-storage-key` | Azure Storage Account Key (`AccountKey=` + 88-char base64) |
|
|
369
|
+
| `azure-sas-key` | Azure Shared Access Key for Service Bus, Event Hubs and IoT Hub (`SharedAccessKey=` + 44-char base64). Separate from the storage account key, which is 88 characters |
|
|
370
|
+
| `google-oauth-secret` | Google OAuth Client Secret (`GOCSPX-` prefix) |
|
|
371
|
+
| `flyio-token` | Fly.io API Token (`FlyV1 fm2_` prefix) |
|
|
372
|
+
| `databricks-token` | Databricks Personal Access Token (`dapi` + 32 hex) |
|
|
373
|
+
| `vault-token` | HashiCorp Vault Token (`hvs.` / `hvb.` prefix) |
|
|
374
|
+
| `shopify-token` | Shopify Access Token (`shpat_`, `shpss_`, `shpca_`, `shppa_` prefix) |
|
|
375
|
+
| `doppler-token` | Doppler Token (`dp.pt.`, `dp.st.`, … prefix) |
|
|
376
|
+
| `grafana-token` | Grafana Cloud / Service Account Token (`glc_`, `glsa_` prefix) |
|
|
377
|
+
| `notion-token` | Notion Integration Token (`ntn_` prefix) |
|
|
263
378
|
| `aws-access-key` | AWS Access Key ID |
|
|
264
379
|
| `gcp-api-key` | Google Cloud API Key |
|
|
265
380
|
| `private-key` | PEM Private Key (RSA / EC / DSA / PGP / OpenSSH) |
|
|
381
|
+
| `private-key-base64` | PEM private key that has been base64-encoded — how one appears in a kubeconfig, a Kubernetes Secret or a Terraform state, where the `-----BEGIN` header never shows in the text |
|
|
382
|
+
| `url-basic-auth` | Credentials in the userinfo field of an http(s) URL — a git remote, a `.netrc`, a private registry, a `curl` invocation. RFC 3986 deprecates the form for this reason |
|
|
266
383
|
| `github-pat` | GitHub Personal Access Token |
|
|
267
384
|
| `github-fine-grained` | GitHub Fine-Grained Token |
|
|
268
385
|
| `gitlab-pat` | GitLab Personal Access Token |
|
|
@@ -280,25 +397,63 @@ This is a persistent filter, unlike allow tags which apply per prompt. The categ
|
|
|
280
397
|
| `openai-key` | OpenAI API Key (legacy format) |
|
|
281
398
|
| `openai-project-key` | OpenAI Project API Key (`sk-proj-` prefix) *(entropy ≥ 3.5)* |
|
|
282
399
|
| `anthropic-key` | Anthropic API Key |
|
|
400
|
+
| `replicate-token` | Replicate API Token |
|
|
401
|
+
| `huggingface-token` | Hugging Face Access Token |
|
|
402
|
+
| `groq-key` | Groq API Key |
|
|
403
|
+
| `openrouter-key` | OpenRouter API Key |
|
|
404
|
+
| `xai-key` | xAI (Grok) API Key |
|
|
405
|
+
| `perplexity-key` | Perplexity API Key |
|
|
406
|
+
| `digitalocean-pat` | DigitalOcean Personal Access Token |
|
|
407
|
+
| `square-access-token` | Square Access Token |
|
|
408
|
+
| `mapbox-token` | Mapbox Token |
|
|
409
|
+
| `sentry-user-token` | Sentry User Auth Token |
|
|
410
|
+
| `sentry-org-token` | Sentry Organization Auth Token |
|
|
411
|
+
| `atlassian-token` | Atlassian API Token |
|
|
412
|
+
| `linear-key` | Linear API Key |
|
|
413
|
+
| `postman-key` | Postman API Key |
|
|
414
|
+
| `supabase-key` | Supabase Personal Access Token |
|
|
283
415
|
| `jwt` | JSON Web Token (JWT) |
|
|
284
416
|
| `generic-secret` | Generic API key / secret assignment *(entropy ≥ 3.5)* |
|
|
285
417
|
| `env-assignment` | `.env`-style secret assignment *(entropy ≥ 3.0)* |
|
|
286
418
|
| `connection-string` | Database connection string with embedded credentials |
|
|
287
419
|
|
|
288
|
-
### PII (
|
|
420
|
+
### PII (24 rules)
|
|
289
421
|
|
|
290
422
|
| Rule ID | Description | Validation |
|
|
291
423
|
|---|---|---|
|
|
292
424
|
| `pii-email` | Email address | — |
|
|
293
425
|
| `pii-credit-card` | Credit card number | Luhn check |
|
|
294
426
|
| `pii-ssn` | US Social Security Number | Invalid prefix exclusion |
|
|
427
|
+
| `pii-mynumber-jp` | Japanese Individual Number (My Number) | Checksum (weighted mod 11) |
|
|
428
|
+
| `pii-nir-fr` | French NIR / Social Security Number | Check key (mod 97) |
|
|
429
|
+
| `pii-codice-fiscale-it` | Italian Codice Fiscale | Control character (mod 26) |
|
|
430
|
+
| `pii-steuer-id-de` | German Steuer-Identifikationsnummer | MOD 11,10 |
|
|
431
|
+
| `pii-dni-nie-es` | Spanish DNI / NIE | Control letter (mod 23) |
|
|
295
432
|
| `pii-phone-us` | US phone number | — |
|
|
296
433
|
| `pii-phone-jp` | Japanese phone number | — |
|
|
434
|
+
| `pii-phone-fr` | French phone number | Context-gated |
|
|
435
|
+
| `pii-phone-it` | Italian phone number | Context-gated |
|
|
436
|
+
| `pii-phone-de` | German phone number | Context-gated |
|
|
437
|
+
| `pii-phone-es` | Spanish phone number | Context-gated |
|
|
297
438
|
| `pii-postal-jp` | Japanese postal code (`〒` prefix required) | — |
|
|
298
|
-
| `pii-
|
|
439
|
+
| `pii-postal-code` | Postal code (US ZIP / EU / KR) | Context-gated |
|
|
440
|
+
| `pii-rrn-kr` | Korean Resident Registration Number | Checksum (weighted mod 11) |
|
|
441
|
+
| `pii-brn-kr` | Korean Business Registration Number | Checksum (NTS standard algorithm) |
|
|
442
|
+
| `pii-resident-id-cn` | Chinese Resident Identity Card | Check digit (GB 11643 MOD 11-2) |
|
|
443
|
+
| `pii-phone-kr` | Korean phone number | Context-gated |
|
|
444
|
+
| `pii-phone-cn` | Chinese phone number | Context-gated |
|
|
445
|
+
| `pii-postal-cn` | Chinese postal code (6-digit) | Context-gated |
|
|
446
|
+
| `pii-ipv4-public` | Public IPv4 address | Context-gated, reserved ranges excluded |
|
|
447
|
+
| `pii-ipv6` | IPv6 address | Context-gated, reserved ranges excluded |
|
|
299
448
|
|
|
300
449
|
Detection patterns are based on rule definitions from [gitleaks](https://github.com/gitleaks/gitleaks) and [TruffleHog](https://github.com/trufflesecurity/trufflehog).
|
|
301
450
|
|
|
451
|
+
National ID checksum algorithms follow the official specs from each issuing authority: 地方公共団体情報システム機構 (J-LIS) for My Number, INSEE for NIR, Agenzia delle Entrate for Codice Fiscale, Bundeszentralamt für Steuern for Steuer-IdNr., the Ministerio del Interior for DNI/NIE, the Ministry of the Interior and Safety for the Korean RRN, GB 11643-1999 for the Chinese Resident Identity Card, and the NTS (Hometax) standard algorithm for the Korean BRN.
|
|
452
|
+
|
|
453
|
+
### Context gating
|
|
454
|
+
|
|
455
|
+
Phone numbers (IT, DE, FR, ES, KR, CN), bare 5/9-digit and Chinese 6-digit postal codes, and public IP addresses produce too many false positives on digit-only patterns. These rules carry a list of context words (phone, ZIP, PLZ, CAP, IP, etc. in the relevant languages) and only fire when one of those words appears near the match. National ID numbers rely on their checksums instead and do not need context. Japanese postal codes keep their `〒` prefix requirement, which is a stricter form of the same idea.
|
|
456
|
+
|
|
302
457
|
---
|
|
303
458
|
|
|
304
459
|
## How It Works
|
|
@@ -320,30 +475,87 @@ When blocked, the terminal shows what was detected and how to bypass it.
|
|
|
320
475
|
|
|
321
476
|
### ② PreToolUse hook
|
|
322
477
|
|
|
323
|
-
Runs just before Claude calls the `Read` or `
|
|
478
|
+
Runs just before Claude calls the `Read`, `Bash` or `Grep` tool, or any MCP tool.
|
|
324
479
|
|
|
325
480
|
```
|
|
326
|
-
Claude calls Read / Bash tool
|
|
481
|
+
Claude calls Read / Bash / Grep / MCP tool
|
|
327
482
|
↓
|
|
328
483
|
PreToolUse hook
|
|
329
484
|
↓
|
|
330
485
|
── Read tool ─────────────────────────────────────────────────────
|
|
331
486
|
│ 1. filename is .env / .env.* → blocked (secret category only)
|
|
332
487
|
│ 2. file contents contain secret / PII → blocked
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
488
|
+
│
|
|
489
|
+
├─ Bash tool ──────────────────────────────────────────────────────
|
|
490
|
+
│ 1. env var values referenced in the command contain secret / PII → blocked
|
|
491
|
+
│ 2. a bare env / printenv would print the whole environment → every
|
|
492
|
+
│ variable is scanned
|
|
493
|
+
│ 3. command string itself contains secret / PII (e.g. echo AKIA...) → blocked
|
|
494
|
+
│ 4. the command is located past any wrapper (sudo, env VAR=1, timeout,
|
|
495
|
+
│ nice, xargs) and any leading VAR=value assignment
|
|
496
|
+
│ 5. inline scripts (-c, -e, -pe) are parsed and scanned
|
|
497
|
+
│ 6. file paths from input redirections, command substitutions and chained
|
|
498
|
+
│ commands are extracted and scanned
|
|
499
|
+
│ 7. printing commands (cat, head, tail, sed, awk, grep, rg, cut, sort,
|
|
500
|
+
│ base64, xxd, strings, diff, comm, dd, and git subcommands) targeting
|
|
501
|
+
│ a named file → file contents scanned
|
|
502
|
+
│
|
|
503
|
+
└─ every other tool, Grep and mcp__* included ─────────────────────
|
|
504
|
+
1. input fields naming an existing file are scanned for
|
|
505
|
+
secret / PII → blocked
|
|
337
506
|
```
|
|
338
507
|
|
|
339
|
-
|
|
508
|
+
A value is scanned when either its field name says path or the value itself is shaped like one.
|
|
509
|
+
|
|
510
|
+
The field names are `path`, `paths`, `file`, `files`, `filepath`, `filename`, `filenames`, `absolutepath`, `notebookpath` and `sourcepath`, compared with separators and case removed — so `file_path`, `filePath` and `filepath` are one name. Beyond those, any value containing a `/` is treated as a path whatever its field is called, which is what covers a tool carrying its path under `target`, `document` or `uri`.
|
|
511
|
+
|
|
512
|
+
The `/` is what separates a path from a word, and it is there so that a search pattern is not read as a path: `{ "pattern": ".env" }` is a search for the text `.env`, not a read of the file, and `.env` exists in most checkouts. The cost is that a bare filename under an unlisted field name is still missed.
|
|
513
|
+
|
|
514
|
+
Values are found up to four levels down and inside arrays, both of strings and of objects, so `{ "path": "…" }`, `{ "paths": ["…"] }`, `{ "args": ["/abs/…"] }` and `{ "args": { "paths": [{ "path": "…" }] } }` are all covered. A field naming a directory is left alone.
|
|
515
|
+
|
|
516
|
+
Which tools reach the hook at all is the matcher's business, and the default (`Read|NotebookRead|Bash|Grep|mcp__.*`) sends it `Read`, `NotebookRead`, `Bash`, `Grep` and every MCP tool. Widen the matcher and the same field search applies to whatever else arrives.
|
|
517
|
+
|
|
518
|
+
Commands that only measure a file (`wc`, `cksum`, `sha256sum`) are not treated as reads, whether the file is named or fed in over `<`: they print counts and digests, never the bytes. Neither are the tools that surface no file contents — `Write`, `Edit`, `MultiEdit`, `NotebookEdit`, `TodoWrite`, `Glob`, `WebFetch`, `WebSearch`, `ExitPlanMode`, `AskUserQuestion` — nor any tool whose name leads with a write verb, such as `mcp__fs__write_file` or `createPage`.
|
|
519
|
+
|
|
520
|
+
Neither is a command that sends its result back to the file it was handed. `sed -i`, `perl -i` and `ruby -i` (bundled forms such as `perl -pi -e` and `perl -lpi` included) edit in place and write nothing to stdout. A bundle is read one letter at a time, continuing only past switches that command is known to accept without a value — so `sed -Ei` and `perl -lpi` are in-place edits, while `perl -Ilib -pe` and `perl -MList::Util -pe` are reads. A letter the list does not know stops the reading and the file is scanned, which is the safe way to be wrong. `git log <file>` is not a read either — it prints who changed the file and when — unless a patch is asked for with `-p`, `-u`, `--patch`, `-U<n>`, `--unified=<n>`, one of the merge-diff forms (`-c`, `-m`, `--cc`, `--diff-merges`), or `-L`, which prints the lines of one named file.
|
|
521
|
+
|
|
522
|
+
When blocked, the hook exits 2, which stops the tool call, and writes the reason to stderr, which is where Claude reads it from. The reason names what was detected and which allow tag lifts the block, and asks Claude to pass that on to the user.
|
|
340
523
|
The terminal also receives a direct message (via `/dev/tty`).
|
|
341
524
|
|
|
525
|
+
### Known Limitations
|
|
526
|
+
|
|
527
|
+
- **Heredoc bodies** — a heredoc body is treated as text, not as commands, so `cat > deploy.sh <<'EOF'` writing a script that mentions `.env` is not itself a read. The trade-off is that a heredoc which *feeds* commands to another shell (`ssh host <<'EOF'` with a `cat /etc/secrets` in the body) is not inspected either.
|
|
528
|
+
- **A tool that runs a command is read for the command, by field name** — `command`, `commands`, `cmd`, `script`, `code`, `commandline` and `shellcommand`, each read with punctuation and case ignored, so `command_line`, `command-line`, `commandLine` and `command.line` are the same name. A shell-running MCP server that names the field something else hands its command past unread.
|
|
529
|
+
- **Only the first and last 1 MiB of a file are scanned** — a file larger than the cut is read at both ends rather than to its end, because a hook that does not return is killed by the PreToolUse timeout, and a killed hook does not block the call. The cut is in bytes, so a file of multi-byte characters gives up sooner in characters. What is missed is the middle of a file larger than both windows, and a secret straddling either edge, since the cut lands mid-match; the 64 KB transcript tail read makes the same trade. What the cut does *not* bound is the work done on what it read: that is a property of each rule's pattern, and `docs/rules.md` covers why three of them carry length bounds.
|
|
530
|
+
- **A write-named tool that also returns contents** — the exemption reads a tool's name, and assumes a name led by a write verb means the tool surfaces no file contents. `update` and `copy` are where those two things come apart: `mcp__*__update_file` and `mcp__*__copy_file` open a file to do their work, and one that returned the result would not be scanned. Scanning them instead would block writing to a file that already holds a secret, which is not a leak, so the exemption stays as it is.
|
|
531
|
+
- **A bare filename under an unlisted field name** — a value is treated as a path when its field name says so or when it contains a `/`. A tool passing `{ "target": "secrets.txt" }` satisfies neither, so it is not scanned. Requiring the `/` is deliberate: without it, a search for the text `.env` would be blocked as though the file had been read.
|
|
532
|
+
- **git history references** — `git show HEAD:.env` and similar references to objects in git history (not on disk) are not scanned, since the object does not exist as a file path.
|
|
533
|
+
- **Unlisted commands** — the set of commands known to print file contents is a list, not an analysis of the command. A printing command that is not on the list is not caught.
|
|
534
|
+
- **A template holding a real credential is blocked** — `.env.example` and its siblings are exempt from the name guard, not from the scan. Placeholders (`your-token-here`, `REPLACE_ME`, `changeme`, `<token>`, `postgres://user:password@localhost/db`) are recognised and left alone, but a template committed with a live key is blocked like any other file, through printing commands (`grep KEY .env.example`) as much as through `Read`. Use `[allow-secret]` if that is deliberate.
|
|
535
|
+
- **A binary swept up by a directory being named** — the files under a directory are scanned because the directory was named, and one whose first four kilobytes read as neither text nor UTF-16 is skipped rather than ground through every rule. A file named outright is scanned whatever its bytes look like, and so is an `.env` in a swept directory: the name decides that one when the contents cannot.
|
|
536
|
+
- **Anything that is not a regular file** — a FIFO, a process substitution (`/dev/fd/63`) and `/dev/stdin` are not read, so `cat` of one is not scanned. Reading them can never reach the end of the file: `cat /dev/zero` held the hook open until Claude Code's PreToolUse timeout killed it, and a killed hook does not block the call. Not scanning them is the lesser of the two, since a hang lets the call through as well. A directory is not read either, but it is not left alone: the files directly under it are scanned, one level and no further.
|
|
537
|
+
- **A search that names no path is judged on names alone** — `rg pattern`, `grep -r pattern` and `Grep {pattern}` with no `path` all print from the working directory, so that directory is checked for an `.env` and its siblings. Its other files are not read. A directory the user named is one they asked about and its contents are scanned; a directory only implied by a search is every repository anyone works in, and reading those stopped a plain `rg TODO` in a third of the checkouts it was measured against.
|
|
538
|
+
- **`~user/…` is not expanded** — `~` and `~/…` are resolved to the home directory, but the form naming another user needs the password database, and guessing would name the wrong file.
|
|
539
|
+
- **A file in an encoding neither reading recovers** — every run of text between NUL bytes is scanned, and UTF-16 is decoded first: by its byte-order mark, or by NULs falling on one side of each pair through the first sixteen kilobytes. Without a mark that verdict is a guess, so both readings are scanned and a wrong guess hides nothing. What is left out is an encoding that is neither: a file in Shift_JIS or GBK is read as the bytes it is, and a credential written in one of those character sets is not found. An ASCII credential inside such a file still is.
|
|
540
|
+
- **A shell construct that names the file only at run time** — `for f in secrets; do cat "$f"; done` and `find . -name secrets -exec cat {} +` both name the file in the command line, but the hook classifies the command it can see, and in these the reading command is `cat` reached through a loop or through `find`'s own argument list.
|
|
541
|
+
- **At most 64 MiB is read across one tool call** — the per-file cut bounds one file; this bounds the call. A glob naming three hundred large files took half a minute, which is long enough for the PreToolUse timeout to kill the hook, and a killed hook does not block. Files past the budget are not scanned, so naming enough large files before the one that matters is a way past the scan.
|
|
542
|
+
- **A relative path is resolved against the directory Claude Code reports** — and against a literal `cd` at the start of the same command. A `cd` later in the line, one inside a subshell, and one whose argument is a variable, a glob or `-` are all left alone, because where they land cannot be worked out here. A directory changed some other way is the same case.
|
|
543
|
+
- **`**` reaches one level, not every level** — a pattern crossing directories is expanded as a single `*`, because expanding it properly walked a whole tree until the hook was killed. `cat **/secrets` sees `*/secrets`.
|
|
544
|
+
- **One tool call stops reading after five seconds** — whatever it has read by then is what was scanned. A byte budget bounds the reading; this bounds the walking as well, and both are ways past the scan for anyone willing to name enough files first.
|
|
545
|
+
- **A glob is expanded by the hook, not by the shell** — `cat *.env` is expanded here to decide what to scan, a moment before the shell expands it and against the hook's own working directory. A file created in between is missed, and at most 256 matches of one pattern are scanned.
|
|
546
|
+
- **Paths held in shell variables** — a path is only scanned when it appears literally in the command. `f=.env; cat "$f"` resolves at run time, after the hook has already decided.
|
|
547
|
+
- **Paths arriving over a pipe** — `find . -name '.env' | xargs cat` names no file the hook can see.
|
|
548
|
+
- **Programs that read files themselves** — `python script.py` is not scanned, because running a script does not print its source; whatever the script opens at run time is beyond the hook's reach.
|
|
549
|
+
- **A flag's separate value is collected as a path** — on a printing command, only a few flags are known to take a value, so every other flag's value becomes a path candidate: the `5` in `head -n 5 f` and in `cut -c 5 f`. A pattern-first command is different — `grep -A 5 f` spends the `5` as the pattern instead, and only `f` is collected. Harmless in practice, since only paths that exist as regular files are read — a file named `5` in the working directory would be scanned, and nothing else is.
|
|
550
|
+
- **A command a wrapper hands off to may be mistaken for its argument, or the reverse** — the search past `sudo`, `timeout` and the others takes the first name it can classify, because a wrapper flag's value (`sudo -u root cat f`) cannot be told apart from a command name. So an unclassified command's arguments are searched too: `sudo mycmd cat f` resolves to `cat` and scans `f`. `echo`, `printf`, `true`, `false` and `:` are known to print their arguments rather than open them, and stop the search; any other unclassified name does not.
|
|
551
|
+
- **Inline program text is followed four levels deep** — each `-c` / `-e` script inside another costs one level, so a read buried five interpreters down is not reached. Nested command substitutions are not bounded this way.
|
|
552
|
+
- **Best effort only** — detection is not exhaustive. Arbitrary shell metacharacters, eval chains, and complex expansions may not be fully tracked.
|
|
553
|
+
|
|
342
554
|
---
|
|
343
555
|
|
|
344
556
|
## Allow Tags (detailed)
|
|
345
557
|
|
|
346
|
-
Allow tags filter the scan results — the scan still runs
|
|
558
|
+
Allow tags filter the scan results — the scan still runs, including for a `.env` file whose name guard a tag has lifted. Lifting the name guard is not the same as skipping the check: `[allow-secret]` on a `.env` holding an email address still blocks on the address.
|
|
347
559
|
|
|
348
560
|
### Mask tags
|
|
349
561
|
|
|
@@ -352,14 +564,14 @@ Allow tags filter the scan results — the scan still runs. The `.env`/`.env.*`
|
|
|
352
564
|
If you include a mask tag, sensitive-canary will explain this and list what was detected:
|
|
353
565
|
|
|
354
566
|
```
|
|
355
|
-
> [mask-secret] My key is
|
|
567
|
+
> [mask-secret] My deploy key is -----BEGIN RSA PRIVATE KEY----- , can you review this?
|
|
356
568
|
|
|
357
569
|
🐦 sensitive-canary: prompt masking is not supported
|
|
358
570
|
|
|
359
571
|
[mask-secret] cannot mask prompt content.
|
|
360
572
|
The following sensitive data was detected:
|
|
361
573
|
|
|
362
|
-
[Secret]
|
|
574
|
+
[Secret] PEM Private Key (private-key): ---****KEY
|
|
363
575
|
|
|
364
576
|
Please choose one of the following:
|
|
365
577
|
|
|
@@ -371,13 +583,19 @@ If you include a mask tag, sensitive-canary will explain this and list what was
|
|
|
371
583
|
|
|
372
584
|
### Allow + Mask tag priority
|
|
373
585
|
|
|
374
|
-
When
|
|
586
|
+
When more than one tag appears, **the last one wins**. It replaces the earlier ones entirely rather than combining with them, so changing your mind mid-message works the way it reads.
|
|
587
|
+
|
|
588
|
+
| Example | secret | pii |
|
|
589
|
+
|---------|--------|-----|
|
|
590
|
+
| `[allow-all] … [allow-secret]` | allow | blocked |
|
|
591
|
+
| `[allow-secret] … [allow-all]` | allow | allow |
|
|
592
|
+
| `[allow-secret] … [mask-secret]` | mask (unsupported) | blocked |
|
|
593
|
+
| `[mask-secret] … [allow-secret]` | allow | blocked |
|
|
594
|
+
| `[allow-secret] … [allow-pii]` | blocked | allow |
|
|
375
595
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
| `[mask-secret] [allow-secret] …` | masking not supported error |
|
|
380
|
-
| `[allow-secret] [mask-pii] …` | secret allowed, PII mask error |
|
|
596
|
+
The last line is the one to watch: two tags do not add up. Narrowing from `[allow-all]` to `[allow-secret]` really does put PII back under guard, which is the point — but so does writing `[allow-secret] [allow-pii]` and expecting both. **`[allow-all]` is how you ask for both.**
|
|
597
|
+
|
|
598
|
+
A tag counts wherever it appears in the message, mid-sentence included. What does not count is a tag inside a fenced code block, inside one of the elements Claude Code writes around command output, or in a line the runtime wrote rather than you — a compaction summary, a skill body, or a background task reporting back. Those are quoting, not asking.
|
|
381
599
|
|
|
382
600
|
---
|
|
383
601
|
|
|
@@ -394,6 +612,10 @@ src/
|
|
|
394
612
|
lib/
|
|
395
613
|
inspector.ts allow tag parsing, message scanning
|
|
396
614
|
rules.ts secret and PII detection rule definitions
|
|
615
|
+
default-config.json the rules themselves, as data
|
|
616
|
+
shell.ts shell syntax: tokens, quoting, heredocs, substitutions
|
|
617
|
+
bash-commands.ts what each command does with the files it is given
|
|
618
|
+
tool-inputs.ts which input fields of a tool name a file
|
|
397
619
|
```
|
|
398
620
|
|
|
399
621
|
---
|
|
@@ -401,12 +623,16 @@ src/
|
|
|
401
623
|
## Development
|
|
402
624
|
|
|
403
625
|
```bash
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
626
|
+
pnpm install # install dependencies
|
|
627
|
+
|
|
628
|
+
pnpm test # run tests
|
|
629
|
+
pnpm run test:watch # run tests in watch mode
|
|
630
|
+
pnpm run typecheck # type check (tsc)
|
|
631
|
+
pnpm run lint # lint with Biome (no changes)
|
|
632
|
+
pnpm run fix # lint + auto-fix with Biome
|
|
633
|
+
pnpm run ci # typecheck + lint + tests (for CI)
|
|
412
634
|
```
|
|
635
|
+
|
|
636
|
+
The lockfile is pnpm's, and every CI job installs with pnpm, so `npm install`
|
|
637
|
+
here ignores it, writes a second lockfile, and resolves a different tree from the
|
|
638
|
+
one that is tested. `CONTRIBUTING.md` has the rest of the workflow.
|