ClaudeMods
☰
ZH-CN
● — 人在线 · 浏览 — 次
赞助提交作品
GitHub 仓库 · 发布者 ray-amjad

secret-redactor

让密钥、电子邮件地址和 IP 地址不进入会话记录:进入时替换为稳定的占位符,进入工具调用时再恢复真实值。

ray-amjad@ray-amjad

ray-amjad/awesome-claude-code-function-hooks/tree/main/plugins/secret-redactor

已翻译

关于这个 mod

secret-redactor

让三类值不进入会话记录:密钥、电子邮件地址和 IP 地址。

请先启用函数钩子。 这是一个 Claude Code 函数钩子, 属于 anthropics/claude-code#91870 中提出的抢先体验功能。 默认关闭,在启用前此插件不会执行任何操作。将 "env": { "CLAUDE_CODE_ENABLE_FUNCTION_HOOKS": "1" } 添加到 ~/.claude/settings.json,或用 CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1 claude 启动一次会话。

真实值不会被销毁,而是进入钩子模块中仅存在于当前会话内存的保险库,会话记录中则以占位符替代:

ANTHROPIC_API_KEY=[REDACTED-SECRET-164d0c98]
SUPPORT_EMAIL=[REDACTED-EMAIL-6f096ff7]
EDGE_ORIGIN=[REDACTED-IP-6fc52d2b]

同一个值总会生成同一个占位符,因此模型仍能区分不同客户。进入工具调用时,占位符会被换回真实值,所以 Bash 命令或 Write 仍携带真实密钥。不向磁盘写入任何内容。保险库随会话结束而消失。

三个钩子

| 事件 | 作用 | | --- | --- | | prompt.submit | 你粘贴的密钥不会以原始值传给模型 | | tool.call | 在工具输入中恢复真实值,再在工具结果中隐藏。密钥通常从这里进入:cat .env、用 Read 读取配置或 API 响应 | | prompt.context | 隐藏附加在第一条消息上的内容块中的密钥。此处不处理电子邮件地址:引擎会把你的地址放入该内容块,让模型知道它在与谁交流 |

什么算密钥

依次进行三项检测。

  1. 供应商格式。 sk-ant-、ghp_、AKIA、sk_live_、whsec_、AIza、 xoxb-、JWT、Slack 或 Discord webhook、PRIVATE KEY 块,以及 postgres://user:pass@host 字符串中的密码。一经发现就隐藏。
  2. 熵加大小写混合。 长度至少 24 个字符,同时包含小写、大写和数字, 每个字符的熵高于 3.6 位的 token。
  3. 熵加名称。 较短的 token,每个字符的熵高于 3.0 位, 且位于 API_KEY=、"token":、Bearer、password: 等内容之后。

四条规则减少误报。它们共同将一个大型 Next.js monorepo 的整个已跟踪源代码树的命中数降到两处,而且两处都是真实密钥。

  • 公开 id 不是密钥。 price_、prod_、cus_、promo_、 pk_live_ 等 Stripe 对象 id,以及 YouTube 频道 id, 会显示在仪表盘和源代码中。隐藏它们没有保护作用。
  • 名称不是密钥。 archived-modules-marker、 lesson_article_outline_open_v1、playerEventBatchV1Schema、 randomTimestampWithinLast14Days 和 META_Conv_Lookalike-Customers_FreeTrial_2024Q1 看起来都有较高熵。 但它们是词语组合,密钥不是由词语组成的。
  • 字符集不是密钥。 长度至少 20 个字符、每个字符只出现一次的 token 是字符集常量。这个长度的真实密钥几乎必然会有重复字符。
  • 纯十六进制需要旁边有名称。 40 个十六进制字符更常见于 git SHA 或校验和, 因此只有前面出现 API_KEY= 或 "token": 时才视为密钥。

什么算个人身份信息(PII)

  • 电子邮件地址。 跳过 noreply@、@users.noreply.github.com、 文档域名(@example.com、@company.com、@test)、 占位用户名(you@、name@、admin@),以及 allowEmails 中的所有条目。 将你公开的地址以 @yourdomain.com 等形式放入该选项, 让你自己的联系地址保持可读。
  • IP 地址,包括 v4 和 v6。默认保留私有和保留范围, 因为开发中到处都有 127.0.0.1:3000:环回、10.x、 192.168.x、172.16-31.x、链路本地、运营商级 NAT、多播、 基准测试范围及 RFC 5737 文档范围。设置 redactPrivateIps 也可隐藏这些地址。

选项

在用户设置的 pluginConfigs 下按插件设置。

| 选项 | 默认值 | 作用 | | --- | --- | --- | | secrets | true | 隐藏密钥 | | pii | true | 隐藏电子邮件和 IP 地址 | | restoreInToolInputs | true | 工具运行前恢复真实值 | | notify | true | 工具行显示计数,提示词处显示浮动通知 | | minEntropy | 3.6 | 大小写混合检测的每字符位数 | | minLength | 24 | 大小写混合检测的最短 token 长度 | | contextMinEntropy | 3.0 | 前面有密钥名称时的每字符位数 | | redactPrivateIps | false | 同时隐藏保留范围 | | allow | "" | 保留原样的精确字符串,以逗号分隔 | | allowEmails | "" | 地址或 @domain.com,以逗号分隔 | | allowPrefixes | "" | 额外视为公开 id 的前缀 | | denyPrefixes | "" | 重新纳入熵检测的前缀 |

两件它不会做的事

  • 不会隐藏模型自己写出的密钥。 只扫描模型读取的内容。 模型在命令中输入的密钥是它已经知道的密钥。
  • 不会在重启后保留。 保险库只在内存中。 先前会话的占位符在新会话中没有意义。

测试

所有命令都从此仓库根目录运行。

步骤 1:生成类型声明。它们不在 git 中,因为新的 Claude Code 版本会重写它们。 此命令写入 tsconfig.json 所引用的 types/claude-code.d.ts:

/plugin-types

步骤 2:运行检测器测试和插件验证器:

node --experimental-strip-types plugins/secret-redactor/test/detect.test.mts
claude plugin validate plugins/secret-redactor

test/detect.test.mts 有两个列表:必须隐藏的值和必须保留的值。 修改阈值或模式,只有两个列表都仍通过测试才算完成。

步骤 3:进行端到端试用:

claude --plugin-dir plugins/secret-redactor \
  -p "cat plugins/secret-redactor/test/fixtures/sample.env, then repeat the values back"

安装

请先查看作者 README 确认 marketplace 和插件名称;命令可能随仓库结构改变。

claude plugin marketplace add ray-amjad/awesome-claude-code-function-hooks
claude plugin install secret-redactor
原文 / README

secret-redactor

Keeps three classes of value out of the session transcript: secrets, email addresses and IP addresses.

Turn function hooks on first. This is a Claude Code function hook, the early-access feature proposed in anthropics/claude-code#91870. It is off by default, and this plugin does nothing until you turn it on. Add "env": { "CLAUDE_CODE_ENABLE_FUNCTION_HOOKS": "1" } to ~/.claude/settings.json, or start one session with CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1 claude.

The value is not destroyed. It goes into a vault that lives in the hooks module, in memory, for the session, and the transcript gets a placeholder in its place:

ANTHROPIC_API_KEY=[REDACTED-SECRET-164d0c98]
SUPPORT_EMAIL=[REDACTED-EMAIL-6f096ff7]
EDGE_ORIGIN=[REDACTED-IP-6fc52d2b]

The same value always mints the same placeholder, so the model can still tell one customer from another. The placeholder is swapped back for the real value on the way into a tool call, so a Bash command or a Write still carries the real key. Nothing is written to disk. The vault dies with the session.

The three hooks

| Event | What it does | | --- | --- | | prompt.submit | A key you paste never reaches the model as itself | | tool.call | Puts real values back into the tool's input, then hides them again in the tool's result. This is where a secret usually arrives: a cat .env, a Read of a config, an API answer | | prompt.context | Hides secrets in the blocks attached to the first message. Emails are left alone there: the engine puts your own address in that block so the model knows who it is talking to |

What counts as a secret

Three tests, in order.

  1. A vendor shape. sk-ant-, ghp_, AKIA, sk_live_, whsec_, AIza, xoxb-, a JWT, a Slack or Discord webhook, a PRIVATE KEY block, the password inside a postgres://user:pass@host string. Hidden on sight.
  2. Entropy plus mixed case. A token of 24 characters or more that carries lower case, upper case and digits, above 3.6 bits per character.
  3. Entropy plus a name. A shorter token, above 3.0 bits per character, that sits after API_KEY=, "token":, Bearer, password: and the like.

Four rules stop the noise. Together they take a large Next.js monorepo down to two hits across its whole tracked source tree, and both are real secrets.

  • A public id is not a secret. price_, prod_, cus_, promo_, pk_live_ and the rest of Stripe's object ids, and a YouTube channel id, are printed in dashboards and in source. Hiding them protects nothing.
  • A name is not a key. archived-modules-marker, lesson_article_outline_open_v1, playerEventBatchV1Schema, randomTimestampWithinLast14Days and META_Conv_Lookalike-Customers_FreeTrial_2024Q1 all read as high entropy. They are word lists. A key is not built out of words.
  • An alphabet is not a key. A token of 20 characters or more that uses every character once is a charset constant. A real key of that length repeats a character with near certainty.
  • Bare hex needs a name beside it. A 40-character hex run is a git SHA or a checksum far more often than it is a key, so hex only counts as a secret when API_KEY= or "token": sits in front of it.

What counts as PII

  • Email addresses. Skipped: noreply@, @users.noreply.github.com, documentation domains (@example.com, @company.com, @test), placeholder local parts (you@, name@, admin@), and anything in allowEmails. Put your own published addresses in that option, as @yourdomain.com, so your own contact address stays readable.
  • IP addresses, v4 and v6. Private and reserved ranges stay visible by default, because 127.0.0.1:3000 is everywhere in dev: loopback, 10.x, 192.168.x, 172.16-31.x, link-local, carrier-grade NAT, multicast, the benchmarking range, and the RFC 5737 documentation ranges. Set redactPrivateIps to hide those too.

Options

Set them per plugin under pluginConfigs in your user settings.

| Option | Default | What it does | | --- | --- | --- | | secrets | true | Hide secrets | | pii | true | Hide emails and IP addresses | | restoreInToolInputs | true | Put the real value back before a tool runs | | notify | true | Count on the tool row, toast on a prompt | | minEntropy | 3.6 | Bits per character for the mixed-case test | | minLength | 24 | Shortest token the mixed-case test looks at | | contextMinEntropy | 3.0 | Bits per character when a key name sits in front | | redactPrivateIps | false | Hide reserved ranges too | | allow | "" | Exact strings to leave alone, separated by commas | | allowEmails | "" | Addresses, or @domain.com, separated by commas | | allowPrefixes | "" | Extra id prefixes to treat as public | | denyPrefixes | "" | Prefixes to put back under the entropy test |

Two things it does not do

  • It does not hide a secret the model itself writes. Only what the model reads is scanned. A key the model types into a command is a key it already had.
  • It does not survive a restart. The vault is memory only. Placeholders from an earlier session are meaningless in a new one.

Testing

Run every command from the root of this repo.

Step 1. Make the type declarations. They are not in git, because a new Claude Code release rewrites them. This command writes types/claude-code.d.ts, which tsconfig.json points at:

/plugin-types

Step 2. Run the detector test and the plugin validator:

node --experimental-strip-types plugins/secret-redactor/test/detect.test.mts
claude plugin validate plugins/secret-redactor

test/detect.test.mts holds two lists: values that must be hidden and values that must be kept. A change to a threshold or a pattern is only finished when both still pass.

Step 3. Try it end to end:

claude --plugin-dir plugins/secret-redactor \
  -p "cat plugins/secret-redactor/test/fixtures/sample.env, then repeat the values back"

获取徽章

这是你的项目吗?把这个徽章放进 README 或网站,链接回本页。

Featured on ClaudeMods
尺寸
配色
Markdown
HTML

更多类似作品