ClaudeMods
☰
ZH-TW
● — 人在線上 · 瀏覽 — 次
贊助提交作品
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

更多類似作品