dsh-sensitive-guard 0.4.0 → 0.5.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # 变更记录
2
2
 
3
+ ## 0.5.1
4
+
5
+ 无代码变更,只改文档与包元数据。
6
+
7
+ - README 首屏重排成 landing page:一句话钩子 → badges → 主截图 → 「不用它 / 用它」对比表 → 一行 `dsh plugin add` 安装命令。HMAC 原理与「往宿主注册了什么」下移到「工作原理」。
8
+ - 三张截图从 `raw.githubusercontent.com` 换到 jsdelivr CDN——raw 在国内经常裂图,而主要用户是中文用户。
9
+ - 新增英文版 `README.en.md`,README 顶部加中英切换。
10
+ - 新增 `screenshots.json`(3 张截图按顺序声明)与 `assets/icon-180.png`(180×180 位图)。插件市场的卡片图标只收位图,详情页截图优先读仓库里的 `screenshots.json` 而不是从 README 抽取。
11
+ - `package.json`:`description` 精简到 130 字符,`keywords` 7 → 18 项(补 `api-key` / `credential` / `secrets` / `password` / `token` / `gitleaks` 等),补 `icon` 字段。
12
+
13
+ ## 0.5.0
14
+
15
+ - 接入 [gitleaks](https://github.com/gitleaks/gitleaks) 官方规则库:从 `config/gitleaks.toml`(222 条,MIT)转换成 **221 条** JS 可用正则,随包发布 `rules/gitleaks.json` 与 `NOTICE`;`pkcs12-file` 是按文件名判定的,跳过并在规则页标注「不适用」。
16
+ - Go/RE2 → JS 语法降级:`(?i)`、`(?i:…)`、`(?-i:…)`、`(?P<…>`、`(?s:…)`,221 条全部编译通过。含 `(?-i:…)` 的 10 条标为「近似转换」、4 条宽松规则(含 `generic-api-key`)默认关,其余 207 条默认开。
17
+ - 每条规则带 keywords 预筛(不会 221 条正则全量扫文本)和独立 entropy 阈值(低熵命中直接丢);300k 字符 / 2000 密钥的性能基准仍通过。
18
+ - 设置页重做成两个标签:「自动检测规则」(搜索、全部/已开/已关筛选、每页 20 条共 13 页、默认「已开启优先」排序让 14 条默认关的集中在最后两页、逐条开关,开关写 `disabled` / `enabledRules`)、「敏感数据管理」(「添加敏感数据」弹窗:名称 / 明文 / 类型,类型可选「自定义分类…」自己命名;支持搜索与删除)。
19
+ - 新增 `GET/POST /guard/rules` 与 `POST /guard/entries`。手动录入的明文只进加密保管库(`pinned`,不参与 500 条淘汰),运行时解密到内存做字面量匹配,**不写 `rules.json`**。
20
+ - **设置页新增「关闭保护」**:与限时暂停分开的持久开关,点后弹确认框,确认写 `pause.json` 的 `off`,**重启也不自动恢复**;状态条变红并常驻提示,只有「开启保护」能恢复。`resume` 和限时暂停都不会误解除它,非本机来源同样 403。新增 `disable` / `enable` 两个 action 并记审计,自测里用新进程模拟重启做了回归。
21
+ - 自测 90 → 137 项,新增规则编译/分级、关键词预筛、逐条熵值、开关持久化、手动录入、持久关闭(含重启演练与来源门槛),以及两个设置页标签、添加弹窗、自定义分类、列表行与复制按钮、关闭确认弹窗的渲染冒烟测试。
22
+ - 列表可读性:表头与占位符从最暗的 `dimmed` 提到 `secondary`;去掉「入库时间」「命中」两列,第一列改为自适应宽度,修掉表头和明文被挤成竖排;新增「分类」列显示手动录入的自定义分类,名称放到主行;点锁后明文单行显示、悬停看全文,操作区新增「复制明文」按钮(浏览器直连 `/guard/reveal`,记审计,明文不进模型上下文,失败有提示)。
23
+
3
24
  ## 0.4.0
4
25
 
5
26
  - **补上 JSON / 结构化数据这个最大的漏口**:原来 `keyword-assign` 要求冒号紧跟字段名,`{"password": "…"}` 因为中间有个引号整条漏掉;`scrubDeep` 拿到 key 也只用来跳过 id,从不用字段名判断。现在字段名叫 `password`、`db_pass`、`credential`、`X-Auth-Credential`、`dsn`、`connection_string` 之类(含下划线/连字符/驼峰变体)时,值一律换代号,键名、层级、其它值全不动;已解析成对象的工具结果同样处理。只在 `agent/pre-step` 和 `tools/post-execute` 生效,模型自己写的示例 JSON 不会被打码。
package/NOTICE ADDED
@@ -0,0 +1,29 @@
1
+ dsh-sensitive-guard bundles a transformed subset of the gitleaks rule
2
+ configuration (config/gitleaks.toml) from https://github.com/gitleaks/gitleaks.
3
+
4
+ The upstream project is licensed under the MIT License:
5
+
6
+ Copyright (c) 2019 Zachary Rice
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
25
+
26
+ The regexes in rules/gitleaks.json are mechanically converted from Go/RE2
27
+ syntax to JavaScript RegExp by scripts/build-gitleaks.mjs. Rules are graded
28
+ (`core` / `approx` / `loose`) by this project; that grading is not part of the
29
+ upstream data.
package/README.en.md ADDED
@@ -0,0 +1,239 @@
1
+ # dsh-sensitive-guard
2
+
3
+ **Keeps your keys out of the model context.** API keys you paste into chat, the `.env` your agent reads, passwords buried in a `config.json` tool result — all swapped for `<SG_1e62c5fe7944>` placeholders before they reach the model. Plaintext stays encrypted on your own machine (AES-256-GCM); clicking the lock reveals it. The model only ever sees the code.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/dsh-sensitive-guard?color=3d8f66)](https://www.npmjs.com/package/dsh-sensitive-guard)
6
+ [![npm downloads](https://img.shields.io/npm/dm/dsh-sensitive-guard?color=3d8f66)](https://www.npmjs.com/package/dsh-sensitive-guard)
7
+ [![GitHub stars](https://img.shields.io/github/stars/wangxy496/dsh-sensitive-guard?color=3d8f66)](https://github.com/wangxy496/dsh-sensitive-guard/stargazers)
8
+ [![license](https://img.shields.io/npm/l/dsh-sensitive-guard?color=3d8f66)](./LICENSE)
9
+ [![dsh](https://img.shields.io/badge/dsh-%E2%89%A50.2.0-3d8f66)](https://github.com/deepseek-ai/deepseek-harness)
10
+
11
+ English · [中文](README.md)
12
+
13
+ <img src="https://cdn.jsdelivr.net/gh/wangxy496/dsh-sensitive-guard@main/docs/chat-masked.png" width="640" alt="A masked credential in the conversation: sk- followed by a lock pill">
14
+
15
+ *Paste `sk-proj-…` and you get this. Click the lock to reveal, it re-masks after 20 seconds.*
16
+
17
+ ## Why
18
+
19
+ One pasted `.env`, one `cat config.json`, and the secret is permanently in **the session log, the model context, and every screenshot you share**.
20
+
21
+ | | Model context | Session log on disk | Your screen |
22
+ | --- | --- | --- | --- |
23
+ | Without it | `sk-proj-abcdef…` in full | in full | in full |
24
+ | With it | `<SG_1e62c5fe7944>` | `v1:iv:tag:ciphertext` (AES-256-GCM) | a lock, plaintext only on demand |
25
+
26
+ It does not rewrite outbound calls, does not sandbox anything, and is no substitute for rotating keys. It guarantees exactly one thing: **a secret never becomes readable text in the model context just because you copied it once.**
27
+
28
+ ## Install
29
+
30
+ ```bash
31
+ dsh plugin add dsh-sensitive-guard
32
+ ```
33
+
34
+ Requires dsh 0.2.0+ and Node 18+. Tested on Windows 11 and Ubuntu 22.04; not tested on macOS.
35
+
36
+ A **Sensitive guard** section appears in Settings. To verify, send this in a session:
37
+
38
+ ```
39
+ my api key is sk-proj-abcdefghijklmnopqrstuvwx
40
+ ```
41
+
42
+ It should render as `my api key is sk-` plus a lock pill. Click it to reveal, it re-masks after 20 seconds. Then check **Settings → Sensitive guard**: the `inline reveal:` line at the bottom reads `ok` when healthy — any other value, see [Troubleshooting](#troubleshooting).
43
+
44
+ <details>
45
+ <summary>Without the CLI: edit package.json by hand</summary>
46
+
47
+ Edit `~/.dsh/profiles/<profile>/package.json`:
48
+
49
+ ```json
50
+ {
51
+ "dependencies": {
52
+ "dsh-sensitive-guard": "0.5.0"
53
+ },
54
+ "dsh": {
55
+ "profile": {
56
+ "bundles": ["dsh-sensitive-guard"]
57
+ }
58
+ }
59
+ }
60
+ ```
61
+
62
+ Keep the existing entries in `bundles` and append this one, then run `pnpm install` in the same directory and restart dsh.
63
+
64
+ </details>
65
+
66
+ <details>
67
+ <summary>Local development: link mode</summary>
68
+
69
+ Replace the version with `"link:/absolute/path/to/sensitive-guard"`. Client changes apply immediately; host-side changes still need a process restart.
70
+
71
+ </details>
72
+
73
+ ## Screenshots
74
+
75
+ Click the lock to reveal in place — it re-masks after 20 seconds:
76
+
77
+ <img src="https://cdn.jsdelivr.net/gh/wangxy496/dsh-sensitive-guard@main/docs/chat-revealed.png" width="640" alt="Revealed in place">
78
+
79
+ The Settings page lists every vault entry with the same lock, plus a **Copy plaintext** button that writes the value straight to your clipboard (local API call, audited, plaintext never enters the model context):
80
+
81
+ <img src="https://cdn.jsdelivr.net/gh/wangxy496/dsh-sensitive-guard@main/docs/settings.png" width="720" alt="Settings page">
82
+
83
+ ## What it catches
84
+
85
+ **Vendor rules.** 221 rules from [gitleaks](https://github.com/gitleaks/gitleaks) (MIT, see `NOTICE`) — 1Password, Adobe, Adafruit, Age, AWS, GCP, Azure, Stripe, Slack and the rest. Each carries a keyword prefilter, a regex and an optional entropy threshold, so a 221-regex sweep never runs over your text. 207 high-confidence rules are on by default; 10 "approximate conversion" rules and 4 loose ones ship off and can be enabled per rule in the Rules tab. Upstream's `pkcs12-file` matches on file name (`.p12`/`.pfx`) and is marked "not applicable" for text redaction.
86
+
87
+ Vendor prefixes: `sk-`, `sk_`, `ghp_`, `glpat-`, `npm_`, `hf_`, `AKIA`, `ASIA`, `xox*-`, `AIza`, `whsec_`, `SG.`. PEM private key blocks, JWTs, `Bearer` tokens.
88
+
89
+ Assignments and env vars: `password=`, `secret:`, `api_key:`, `SECRET_KEY=`, `DB_PASSWORD=`.
90
+
91
+ **JSON and structured data.** A tool that runs `cat config.json` returns one blob of JSON, and `{"password": "Abc!23456"}` has no vendor prefix — the value alone is unrecognizable. Any field named `password`, `passwd`, `pwd`, `token`, `credential`, `api_key`, `db_pass`, `dsn`, `connection_string` (including snake_case, kebab-case and camelCase variants) gets its value replaced. Key names, nesting and sibling values are untouched. Already-parsed object results are handled the same way.
92
+
93
+ Examples and placeholders stay visible: `your_password_here`, `${DB_PASS}`, `<your-token>`, `changeme`, and field names that merely describe something (`password_policy`, `token_count`).
94
+
95
+ **Chinese phrasings** too: `数据库密码是…`, `我的 token 是…`. Chinese-language passwords are caught from 6 characters (6-digit numeric passwords are common there); English and `key=value` forms start at 8, so `the secret is hidden` does not eat `hidden`. Trailing particles (`吗`/`呢`/`吧`) and punctuation stay outside the placeholder.
96
+
97
+ **Connection strings:** `mysql://user:pass@host`, `postgresql://…`, `password=` inside JDBC params, `X-Api-Key:`.
98
+
99
+ **Command lines:** `mysql -uroot -pabc12345`, `redis-cli -a xxx`. Only these clients are recognized, so `docker run -p 8080:80` is not a false positive.
100
+
101
+ **Keys broken across lines** are re-joined: pasting from a webpage or screenshot often injects `\r`/`\n` into `sk-proj-…`. The two halves are stitched back into one vault entry, with the identical placeholder you would get unbroken.
102
+
103
+ ## How the agent uses a credential
104
+
105
+ The model never gets plaintext. When it needs one — to connect to a database, to call an API — it exports:
106
+
107
+ ```
108
+ sensitive_guard_export(<SG_xxx>) # writes plaintext to a 0600 file, returns the path
109
+ $(cat /that/path) # in a command: plaintext never enters context
110
+ ```
111
+
112
+ There are only two tools. `sensitive_guard_status` reports status and entry metadata with no plaintext — not even an 8-character preview. `sensitive_guard_export` writes the file, which expires after 15 minutes.
113
+
114
+ The most common false bug report: the model reads `application.yml`, sees placeholders, and fails to connect. Either have it export first, or pause protection temporarily.
115
+
116
+ ## Data model
117
+
118
+ Three places, kept separate:
119
+
120
+ | | Content |
121
+ | --- | --- |
122
+ | Model context | `<SG_1e62c5fe7944>`, an HMAC prefix — not reversible offline |
123
+ | Disk | `v1:iv:tag:ciphertext`, AES-256-GCM, key in `vault.key` |
124
+ | Screen | The code plus a lock; plaintext only fetched from localhost on click |
125
+
126
+ The 12 hex digits are the first bytes of `HMAC-SHA256(vault.key, plaintext)`. The key never leaves the machine, so the code cannot be brute-forced back to a short password, while the same plaintext still yields the same code every time. Releases up to 0.3.4 used a `sha256(plaintext)` prefix, which is offline-guessable for low-entropy secrets; old codes are preserved in `vault.json` under `aliases` so old sessions still unlock, and every new capture uses the new algorithm.
127
+
128
+ Two interception points: user input through `agent/pre-step`, tool results through `tools/post-execute`. `llm/stream` additionally scrubs outbound streaming text as a fallback — only `text-delta` and `reasoning-delta`, never `tool-call-delta`, or tool arguments would turn into placeholders. Field-name matching only applies at the first two entry points, so example JSON the model writes itself is left alone.
129
+
130
+ ## Config
131
+
132
+ `<data dir>/rules.json`, applied within two seconds of saving, no restart. `%USERPROFILE%\.dsh\sensitive-guard\` on Windows, `~/.dsh/sensitive-guard/` on Linux.
133
+
134
+ ```json
135
+ {
136
+ "extraRules": [
137
+ { "id": "my-internal", "pattern": "X-WX-Token:\\s*([A-Za-z0-9]{20,})", "flags": "i", "group": 1 }
138
+ ],
139
+ "disabled": ["jwt"],
140
+ "enabledRules": [],
141
+ "outboundScrub": true,
142
+ "entropy": false,
143
+ "entropyThreshold": 4.5,
144
+ "allowCidrs": ["192.168.0.0/16"]
145
+ }
146
+ ```
147
+
148
+ `group` is the capture group to mask, 0 for the whole match. Your regex runs in a synchronous hook, so avoid `(a+)+` / `(.*)*` — a backtracking blowup stalls the host.
149
+
150
+ `disabled` turns off individual default-on rules by id; `enabledRules` turns on default-off ones. The Settings switches maintain both lists. `outboundScrub: false` disables the outbound fallback. `allowCidrs` is empty by default, meaning loopback only — see below.
151
+
152
+ ### Reaching it from another machine
153
+
154
+ Desktop users can skip this. All HTTP routes accept loopback only, which a browser on the same machine always satisfies.
155
+
156
+ One case reaches this: dsh started with `--host 0.0.0.0` or `--host 192.168.x.x` and opened from another machine's browser. Two options — open `127.0.0.1:3080` on the host machine itself, or allow the subnet in `rules.json`:
157
+
158
+ ```json
159
+ { "allowCidrs": ["192.168.0.0/16"] }
160
+ ```
161
+
162
+ The Settings page shows an orange banner when you arrive over the LAN. Unconfigured, the page degrades to read-only rather than blanking: entry counts, kinds, capture times and hit counts, no values and no 8-character preview, plus a card explaining the current address with a ready-to-copy config computed for your source address.
163
+
164
+ **Allowing a subnet means every device in it can read full plaintext, not just previews.** Use it on a trusted LAN only. There is no login because an auth layer would add two steps to every lock click; narrowing the subnet is the better trade.
165
+
166
+ ## HTTP routes
167
+
168
+ Five routes, loopback only by default.
169
+
170
+ | Method | Path | Purpose |
171
+ | --- | --- | --- |
172
+ | GET | `/guard/vault` | Panel data with 8-char previews and the current `access` level; unauthorized sources get a `restricted: true` read-only view without previews or audit |
173
+ | POST | `/guard/reveal` | `{placeholder}` → `{plaintext, body, prefix}`; 403 when unauthorized |
174
+ | GET | `/guard/settings` | `{off, paused, untilRestart, until, remainingMs}`; `remainingMs` is `null` when persistently off |
175
+ | POST | `/guard/settings` | `{action:"pause", minutes:30}`, `{action:"pause", untilRestart:true}`, `{action:"resume"}` clears a timed pause; `{action:"disable"}` persists off (survives restart), `{action:"enable"}` re-enables; 403 when unauthorized |
176
+ | GET | `/guard/rules` | Rule list (name, source, detector tag, tier, current state), totals, skipped items |
177
+ | POST | `/guard/rules` | `{id, enabled}` toggles one rule, writing `disabled` / `enabledRules` |
178
+ | POST | `/guard/entries` | `{action:"add", name, plaintext, kind}` or `{action:"delete", placeholder}`; plaintext goes only into the encrypted vault |
179
+
180
+ ## Known limits
181
+
182
+ 1. In-conversation pills cover user messages only (see above for why).
183
+ 2. No outbound rewriting. The platform's `tools/pre-execute` only exposes allow, deny, cancel and ask — there is no hook to modify arguments, so credentials must go through an export file.
184
+ 3. The vault defends against accidental reads and plaintext on disk, not against someone who already has your account. Encryption is AES-256-GCM and the key file is tightened with `icacls` on Windows.
185
+ 4. Historical sessions are unprotected. `pre-step` runs after the host has written the user text to the log, and `session/event` is a read-only observer, so the plugin has no chance to rewrite before disk. Plaintext in old sessions is still on disk; rotate those keys.
186
+ 5. `exports/` is the only place plaintext lands, cleaned on the next export after 15 minutes.
187
+ 6. The vault holds at most 500 entries, evicting by last hit.
188
+ 7. A corrupted vault fails open so the conversation continues, but counts the event — visible as `failOpen` in `sensitive_guard_status`.
189
+ 8. Each text is scanned up to 200,000 characters. The hook is synchronous, so this bounds custom-regex backtracking; the tail of very long text is unmasked.
190
+ 9. `allowCidrs` assumes the subnet is trustworthy. It blocks CSRF and DNS rebinding, not an active attacker inside the subnet. `0.0.0.0/0` is rejected and logged to `rules.error`.
191
+ 10. Unauthorized sources still read metadata (which entries exist, which rules, when captured, hit counts) so the Settings page can explain itself — the cost is that a LAN peer can infer "12 credentials, keys and passwords". Values stay hidden.
192
+ 11. The host is fail-fast: an exception at the top level of a client entry stops the whole app from booting. `apply` is fully guarded; that top-level risk cannot be removed.
193
+ 12. Codes issued by 0.3.4 and earlier use a `sha256(plaintext)` prefix and are offline-guessable for short passwords. New captures use HMAC. Old codes remain in `vault.json` under `aliases` so old sessions still unlock, but **codes already written into historical session logs cannot be changed** — rotate those.
194
+ 13. Rules come from gitleaks: 10 rules using Go's `(?-i:…)` scoping flag become slightly looser after conversion to JS and are marked "approximate conversion", off by default. `pkcs12-file` matches on file name and is not wired up. Rule data ships under the MIT license, declared in `NOTICE`.
195
+ 14. Manually added entries are pinned in the vault and never count against the 500-entry cap. Plaintext lives only in the encrypted `vault.json`, decrypted into memory for literal matching, and is **never written to `rules.json`** — but it still cannot stop someone with your local account.
196
+
197
+ ## Troubleshooting
198
+
199
+ Normal uninstall: edit `~/.dsh/profiles/<profile>/package.json`, remove `"dsh-sensitive-guard"` from `dependencies` and from the `dsh.profile.bundles` array, reopen the app.
200
+
201
+ If the app will not start, the log shows `web boot: N entry did not activate` with `dsh-sensitive-guard: failed`. Kill the leftover process first:
202
+
203
+ ```
204
+ taskkill /F /IM "DeepSeek Harness.exe"
205
+ ```
206
+
207
+ then remove the dependency as above. Crash logs live in `%APPDATA%\@deepseek-ai\dsh-desktop\logs\crash-*-web-boot.log`.
208
+
209
+ Values of `inline reveal:` — `ok` is healthy, `waiting` is still waiting for the `uiConversation` service, `no-service` cannot reach it, `matcher-failed` means the rule registration was rejected, `renderer-failed` means the renderer registration was rejected.
210
+
211
+ The data directory is `$DSH_PROFILE_DIR/sensitive-guard/`, holding `vault.json`, `vault.key`, `pause.json` (`until` for a timed pause plus `off` for persistent disable), `rules.json` and `exports/`. It falls back to `$DSH_HOME` when the variable is unset.
212
+
213
+ ## Development
214
+
215
+ ```bash
216
+ node test.mjs # offline self-test
217
+ node scripts/build-gitleaks.mjs # regenerate rule data from upstream
218
+ node scripts/build-gitleaks.mjs ./gitleaks.toml
219
+ ```
220
+
221
+ 137 offline tests using a temporary directory, never touching the real vault: the 0.3.x code-upgrade drill, both Settings tabs and the add dialog (including custom categories), list rows and the copy button, render smoke tests for the disable-confirmation dialog, compilation and tiering of all 221 rules, per-rule entropy/switch/manual-entry regressions, and a fresh-process restart proving persistent disable really survives.
222
+
223
+ | File | Role |
224
+ | --- | --- |
225
+ | `index.js` | Host: three hooks, vault, two tools, five routes |
226
+ | `client.js` | Client: both Settings tabs, rules page (search/filter/paging/toggles), add dialog, in-conversation pills |
227
+ | `rules/gitleaks.json` | 221 rules converted from the gitleaks TOML (MIT, see `NOTICE`) |
228
+ | `scripts/build-gitleaks.mjs` | Rule converter, Go/RE2 → JS downgrades and tiering |
229
+ | `test.mjs` | Self-test |
230
+ | `CHANGELOG.md` | Change log |
231
+ | `locale/` | Market metadata for the plugin marketplace |
232
+
233
+ GitHub Actions runs Node 18, 20 and 22 on ubuntu and windows. MIT licensed.
234
+
235
+ ---
236
+
237
+ If this ever kept a key out of a context it should not have entered, a star is the most direct way to help: [⭐ star](https://github.com/wangxy496/dsh-sensitive-guard/stargazers)
238
+
239
+ Found a credential format it misses? [Open an issue](https://github.com/wangxy496/dsh-sensitive-guard/issues) with a redacted sample — vendor rules can be added.
package/README.md CHANGED
@@ -1,51 +1,55 @@
1
1
  # dsh-sensitive-guard
2
2
 
3
- DeepSeek Harness(DSH)的插件,只做一件事:把密钥、令牌、密码挡在模型上下文之外。用户消息和工具结果里的凭证,在进入上下文之前就被换成 `<SG_xxxxxxxxxxxx>` 占位符,明文加密留在本机。模型拿到的是代号,你点一下能看到真值。
3
+ **把密钥挡在模型上下文之外。** 你贴进对话的 API Key、agent 读到的 `.env`、工具返回的 `config.json` 里的密码,在进入模型上下文之前就被换成 `<SG_1e62c5fe7944>` 这样的占位符。明文用 AES-256-GCM 加密留在本机,点一下小锁才看得见——模型拿到的永远只是代号。
4
4
 
5
- 代号里那 12 位十六进制是 `HMAC-SHA256(vault.key, 明文)` 的前缀。密钥只在本机,所以拿到代号的人没法靠穷举短密码反推;同一个密钥每次仍然得到同一个代号。0.3.4 及更早用的是 `sha256(明文)` 前缀,短密码可以离线猜出来,升级后旧代号仍保留可解锁,新捕获一律换成新算法。
5
+ [![npm version](https://img.shields.io/npm/v/dsh-sensitive-guard?color=3d8f66)](https://www.npmjs.com/package/dsh-sensitive-guard)
6
+ [![npm downloads](https://img.shields.io/npm/dm/dsh-sensitive-guard?color=3d8f66)](https://www.npmjs.com/package/dsh-sensitive-guard)
7
+ [![GitHub stars](https://img.shields.io/github/stars/wangxy496/dsh-sensitive-guard?color=3d8f66)](https://github.com/wangxy496/dsh-sensitive-guard/stargazers)
8
+ [![license](https://img.shields.io/npm/l/dsh-sensitive-guard?color=3d8f66)](./LICENSE)
9
+ [![dsh](https://img.shields.io/badge/dsh-%E2%89%A50.2.0-3d8f66)](https://github.com/deepseek-ai/deepseek-harness)
6
10
 
7
- ## 它往宿主里注册了什么
11
+ 中文 · [English](README.en.md)
8
12
 
9
- DSH 插件分 host 端和客户端两半,这个插件两半都占:host 端抓数据、管保管库、开接口;客户端加设置页和对话里的胶囊。
13
+ <img src="https://cdn.jsdelivr.net/gh/wangxy496/dsh-sensitive-guard@main/docs/chat-masked.png" width="640" alt="对话里被遮住的凭证:sk- 后面跟一个锁形胶囊">
10
14
 
11
- | 注册点 | 端 | 作用 |
12
- | --- | --- | --- |
13
- | `agent/pre-step` | host 钩子 | 用户输入进上下文前脱敏 |
14
- | `tools/post-execute` | host 钩子 | 工具结果进上下文前脱敏 |
15
- | `llm/stream` | host 钩子 | 出网的流式文本兜底清洗 |
16
- | `sensitive_guard_status`、`sensitive_guard_export` | host 工具 | 查状态;把明文导出成 0600 临时文件 |
17
- | `/guard/vault`、`/guard/reveal`、`/guard/settings` | HTTP 路由 | 设置页取数、揭示明文、暂停开关 |
18
- | `settings.section` | 客户端 slot | 设置页里的「敏感保护」 |
19
- | `conversation.user.text.node` | 客户端 slot | 对话里的锁形胶囊 |
15
+ *输入 `sk-proj-…` 就变成这样;点开锁看明文,20 秒后自动遮回。*
20
16
 
21
- 前提只要 `tools` 一个。`webServer` 不存在时脱敏照常工作,只是设置页和揭示用不了。
17
+ ## 为什么需要它
22
18
 
23
- ## 长什么样
19
+ 一段对话里只要贴过一次 `.env`、让 agent `cat config.json`,密钥就永久留在了**会话日志、模型上下文,以及你随手分享的截图**里。
24
20
 
25
- 遮住的时候,对话里是 `sk-` 后面跟一个锁:
21
+ | | 模型上下文 | 落盘的会话日志 | 你的屏幕 |
22
+ | --- | --- | --- | --- |
23
+ | 不用它 | `sk-proj-abcdef…` 完整明文 | 完整明文 | 完整明文 |
24
+ | 用它 | `<SG_1e62c5fe7944>` | `v1:iv:tag:密文`(AES-256-GCM) | 锁,点开才向本机要明文 |
26
25
 
27
- ![对话里被遮住的凭证](https://raw.githubusercontent.com/wangxy496/dsh-sensitive-guard/main/docs/chat-masked.png)
26
+ 它不做出口改写、不做沙箱、也不替代密钥轮换。它只保证一件事:**密钥不会因为你的一次复制粘贴,变成模型上下文里的一段可读文本。**
28
27
 
29
- 点一下锁,就地显示明文,20 秒后自动遮回:
28
+ ## 安装
30
29
 
31
- ![点开后显示明文](https://raw.githubusercontent.com/wangxy496/dsh-sensitive-guard/main/docs/chat-revealed.png)
30
+ ```bash
31
+ dsh plugin add dsh-sensitive-guard
32
+ ```
32
33
 
33
- 设置页列出保管库里全部条目,行尾的锁同样能揭示:
34
+ 要求 dsh 0.2.0 以上、Node 18 以上。Windows 11 和 Ubuntu 22.04 实测过,macOS 没测。
34
35
 
35
- ![设置页](https://raw.githubusercontent.com/wangxy496/dsh-sensitive-guard/main/docs/settings.png)
36
+ 装完在设置页会多出「敏感保护」一节。发一句:
36
37
 
37
- ## 安装
38
+ ```
39
+ 我的 api key 是 sk-proj-abcdefghijklmnopqrstuvwx
40
+ ```
38
41
 
39
- 要求 dsh 0.2.0 以上、Node 18 以上。Windows 11 和 Ubuntu 22.04 都实测过,macOS 没测。
42
+ 应该显示成 `我的 api key 是 sk-` 加一个锁形胶囊。点开能看到明文,20 秒后自动遮回。再看「设置 → 敏感保护」,最下面一行 `inline reveal:` 显示 `ok` 就正常,显示别的就翻到文末「卸载和故障」。
40
43
 
41
- 三种装法,按场景选。
44
+ <details>
45
+ <summary>不用 CLI:手改 package.json</summary>
42
46
 
43
- **从 npm 装**(给使用者)。编辑 `~/.dsh/profiles/<profile>/package.json`:
47
+ 编辑 `~/.dsh/profiles/<profile>/package.json`:
44
48
 
45
49
  ```json
46
50
  {
47
51
  "dependencies": {
48
- "dsh-sensitive-guard": "0.3.2"
52
+ "dsh-sensitive-guard": "0.5.0"
49
53
  },
50
54
  "dsh": {
51
55
  "profile": {
@@ -57,21 +61,24 @@ DSH 插件分 host 端和客户端两半,这个插件两半都占:host 端
57
61
 
58
62
  `bundles` 里保留原有条目,只追加这一项。然后在同目录跑 `pnpm install`,重启 dsh。
59
63
 
60
- **让 dsh 自己装**(不想手改文件的场合)。在会话里说「安装这个目录下的插件:/绝对/路径」,它走 `install_bundle`。
64
+ </details>
61
65
 
62
- **本地改代码用 link**。把版本号换成 `"link:/absolute/path/to/sensitive-guard"`,源码改动立刻生效,但 host 端的改动还是要重启进程。
66
+ <details>
67
+ <summary>本地改代码:link 模式</summary>
63
68
 
64
- ## 确认装好了
69
+ 把版本号换成 `"link:/absolute/path/to/sensitive-guard"`,源码改动立刻生效,但 host 端的改动还是要重启进程。
65
70
 
66
- 发一句:
71
+ </details>
67
72
 
68
- ```
69
- 我的 api key 是 sk-proj-abcdefghijklmnopqrstuvwx
70
- ```
73
+ ## 长什么样
74
+
75
+ 点一下锁,就地显示明文,20 秒后自动遮回:
76
+
77
+ <img src="https://cdn.jsdelivr.net/gh/wangxy496/dsh-sensitive-guard@main/docs/chat-revealed.png" width="640" alt="点开后显示明文">
71
78
 
72
- 应该显示成 `我的 api key 是 sk-` 加一个锁形胶囊。点开能看到明文,20 秒后自动遮回。
79
+ 设置页列出保管库里全部条目,行尾的锁同样能揭示;旁边的「复制明文」直接把值写进剪贴板(走本机接口,明文不进模型上下文):
73
80
 
74
- 再看「设置 → 敏感保护」,最下面一行 `inline reveal:` 显示 `ok` 就正常,显示别的就翻到文末「卸载和故障」。
81
+ <img src="https://cdn.jsdelivr.net/gh/wangxy496/dsh-sensitive-guard@main/docs/settings.png" width="720" alt="设置页">
75
82
 
76
83
  ## 日常怎么用
77
84
 
@@ -79,7 +86,11 @@ DSH 插件分 host 端和客户端两半,这个插件两半都占:host 端
79
86
 
80
87
  只有用户消息能点。AI 回复和工具结果走另一套节点渲染,宿主没留「包一层」的口子,要接就得整体替换助手的消息渲染,宿主升级会白屏,所以没做。那些位置到设置页看。
81
88
 
82
- **设置页。** 顶部是暂停开关,下面是保管库条目,每条都能点锁查看,揭示会记审计。暂停有三档:30 分钟、2 小时、到重启。连不上数据库、模型读不到密码的时候点一下就行,到期自动恢复。「到重启」不写盘,重启就恢复。
89
+ **设置页**分两个标签。「敏感数据管理」是保管库:列表只留名称/值、分类、规则三列,手动录入的名称和自定义分类直接显示;点小锁可临时查看明文(单行显示、悬停看全文),旁边的「复制明文」按钮走本机接口取完整明文写到剪贴板——浏览器直连、记审计、**明文不进模型上下文**;录错了可以删。顶部「添加敏感数据」是弹窗,填名称、明文、类型(选「自定义分类…」可以自己起分类名),明文直接加密进保管库、**不写 `rules.json`**,以后这段值出现就自动遮。「自动检测规则」列出 242 条规则(221 条上游 + 21 条内置),能搜名称/关键词、按全部/已开/已关筛选、每页 20 条翻页(共 13 页)、逐条开关;默认「已开启优先」排序,14 条默认关的集中在最后两页。
90
+
91
+ 顶部状态条分两类操作。**限时暂停**三档:30 分钟、2 小时、到重启,到期自动恢复,「到重启」不写盘、重启就恢复;连不上数据库、模型读不到密码时优先用这个。
92
+
93
+ **「关闭保护」是持久关闭**,跟暂停不同:点一下先弹确认框,确认后状态写进 `pause.json`,**重启也不会自动恢复**,只有再点「开启保护」才恢复。关闭期间所有脱敏停止,明文直接进入模型上下文与日志,状态条变红并常驻提示。`resume` 和限时暂停都不会误解除这个状态。
83
94
 
84
95
  **模型怎么用凭据。** 它拿不到明文。要连库或者调接口时让它导出:
85
96
 
@@ -92,6 +103,26 @@ sensitive_guard_export(<SG_xxx>) 明文写到 0600 文件,返回路径
92
103
 
93
104
  常被当成 bug 的一点:模型读 `application.yml` 时看到的是占位符,拿它去连库必然失败。要么让它先导出,要么临时点个暂停。
94
105
 
106
+ ## 工作原理
107
+
108
+ 代号里那 12 位十六进制是 `HMAC-SHA256(vault.key, 明文)` 的前缀。密钥只在本机,所以拿到代号的人没法靠穷举短密码反推;同一个密钥每次仍然得到同一个代号。0.3.4 及更早用的是 `sha256(明文)` 前缀,短密码可以离线猜出来,升级后旧代号仍保留可解锁,新捕获一律换成新算法。
109
+
110
+ ### 它往宿主里注册了什么
111
+
112
+ DSH 插件分 host 端和客户端两半,这个插件两半都占:host 端抓数据、管保管库、开接口;客户端加设置页和对话里的胶囊。
113
+
114
+ | 注册点 | 端 | 作用 |
115
+ | --- | --- | --- |
116
+ | `agent/pre-step` | host 钩子 | 用户输入进上下文前脱敏 |
117
+ | `tools/post-execute` | host 钩子 | 工具结果进上下文前脱敏 |
118
+ | `llm/stream` | host 钩子 | 出网的流式文本兜底清洗 |
119
+ | `sensitive_guard_status`、`sensitive_guard_export` | host 工具 | 查状态;把明文导出成 0600 临时文件 |
120
+ | `/guard/vault`、`/guard/reveal`、`/guard/settings`、`/guard/rules`、`/guard/entries` | HTTP 路由 | 设置页取数、揭示明文、暂停/关闭保护、规则清单与开关、手动录入敏感数据 |
121
+ | `settings.section` | 客户端 slot | 设置页里的「敏感保护」 |
122
+ | `conversation.user.text.node` | 客户端 slot | 对话里的锁形胶囊 |
123
+
124
+ 前提只要 `tools` 一个。`webServer` 不存在时脱敏照常工作,只是设置页和揭示用不了。
125
+
95
126
  ## 数据在哪
96
127
 
97
128
  三处是分开的。
@@ -106,6 +137,8 @@ sensitive_guard_export(<SG_xxx>) 明文写到 0600 文件,返回路径
106
137
 
107
138
  ## 认得出什么
108
139
 
140
+ **系统规则库。** 内置 221 条来自 [gitleaks](https://github.com/gitleaks/gitleaks) 的厂商规则(1Password、Adobe、Adafruit、Age、AWS、GCP、Azure、Stripe、Slack 等,MIT 许可,见 `NOTICE`)。每条都带关键词预筛、正则和可选熵值阈值,命中后才会去跑正则,不会 221 条正则全量扫一遍。默认开 207 条高置信规则;10 条「近似转换」和 4 条宽松规则默认关,在「自动检测规则」标签里自己开。上游还有 1 条 `pkcs12-file` 是按文件名 `.p12`/`.pfx` 判定的,文本脱敏用不上,规则页里标为「不适用」。规则数据由 `scripts/build-gitleaks.mjs` 从上游 TOML 转换生成,升级时重跑即可。
141
+
109
142
  厂商前缀:`sk-`、`sk_`、`ghp_`、`glpat-`、`npm_`、`hf_`、`AKIA`、`ASIA`、`xox*-`、`AIza`、`whsec_`、`SG.`。PEM 私钥块、JWT、`Bearer` 令牌。
110
143
 
111
144
  赋值和环境变量:`password=`、`secret:`、`api_key:`、`SECRET_KEY=`、`DB_PASSWORD=`。
@@ -124,7 +157,7 @@ sensitive_guard_export(<SG_xxx>) 明文写到 0600 文件,返回路径
124
157
 
125
158
  **可选的熵值检测,默认关闭。** `rules.json` 里加 `"entropy": true` 才会开。它抓的是没有前缀的随机串,但只对 base64 或混合字符集有用(熵约 4.8 到 5.0 bits/char)。纯 hex 串的上限就是 4.0,永远够不到 4.5 的默认阈值,所以 `8f3a9b2c…` 这种开了也抓不到;把阈值降到 3.9 又会误伤英文长句。另外 base64 图片、commit hash、UUID 都会被判成高熵。
126
159
 
127
- 剩下的边界:自研格式的凭据得自己往 `extraRules` 加正则;二进制和图片里的、被 base64 或 hex 二次编码搬运过的、JSON 里值写成数字而不是字符串的,仍然抓不到。
160
+ 剩下的边界:自研格式的凭据可以用设置页「添加敏感数据」按字面量记住(明文加密进保管库),或往 `extraRules` 加正则;二进制和图片里的、被 base64 或 hex 二次编码搬运过的、JSON 里值写成数字而不是字符串的,仍然抓不到。
128
161
 
129
162
  ## 配置
130
163
 
@@ -136,6 +169,7 @@ sensitive_guard_export(<SG_xxx>) 明文写到 0600 文件,返回路径
136
169
  { "id": "my-internal", "pattern": "X-WX-Token:\\s*([A-Za-z0-9]{20,})", "flags": "i", "group": 1 }
137
170
  ],
138
171
  "disabled": ["jwt"],
172
+ "enabledRules": [],
139
173
  "outboundScrub": true,
140
174
  "entropy": false,
141
175
  "entropyThreshold": 4.5,
@@ -145,7 +179,7 @@ sensitive_guard_export(<SG_xxx>) 明文写到 0600 文件,返回路径
145
179
 
146
180
  `group` 是要遮挡的捕获组序号,0 表示整个匹配。自定义正则的性能自己负责,别写 `(a+)+`、`(.*)*` 这类结构,它跑在同步钩子里,回溯爆炸会卡住宿主。
147
181
 
148
- `disabled` 里写规则 id 可以关掉单条内置规则。`outboundScrub: false` 会关掉出网补洗。
182
+ `disabled` 里写规则 id 可以关掉单条默认开的规则;`enabledRules` 里写 id 可以打开一条默认关的规则。设置页的开关就是维护这两个列表,不用手改文件。`outboundScrub: false` 会关掉出网补洗。
149
183
 
150
184
  `allowCidrs` 默认是空数组,意思是只认本机。它管的是 HTTP 接口,下一节细说。
151
185
 
@@ -173,14 +207,17 @@ sensitive_guard_export(<SG_xxx>) 明文写到 0600 文件,返回路径
173
207
 
174
208
  ## HTTP 接口
175
209
 
176
- 四个接口,默认只接受本机回环。
210
+ 五个路由,默认只接受本机回环。
177
211
 
178
212
  | 方法 | 路径 | 说明 |
179
213
  | --- | --- | --- |
180
214
  | GET | `/guard/vault` | 面板数据,含 8 位预览和 `access` 当前级别;未授权来源返回 `restricted: true` 的只读版,不含预览和审计 |
181
215
  | POST | `/guard/reveal` | `{placeholder}` 换 `{plaintext, body, prefix}`;未授权 403 |
182
- | GET | `/guard/settings` | `{paused, untilRestart, until, remainingMs}`;未授权 403 |
183
- | POST | `/guard/settings` | `{action:"pause", minutes:30}`、`{action:"pause", untilRestart:true}`、`{action:"resume"}`;未授权 403 |
216
+ | GET | `/guard/settings` | `{off, paused, untilRestart, until, remainingMs}`;`off` 为持久关闭时 `remainingMs` 为 `null`;未授权 403 |
217
+ | POST | `/guard/settings` | `{action:"pause", minutes:30}`、`{action:"pause", untilRestart:true}`、`{action:"resume"}` 清限时暂停;`{action:"disable"}` 持久关闭(写盘、重启不恢复)、`{action:"enable"}` 才重新开启;未授权 403 |
218
+ | GET | `/guard/rules` | 规则清单(名称、来源、检测器标签、分级、当前开关)、总数、跳过项;未授权 403 |
219
+ | POST | `/guard/rules` | `{id, enabled}` 切换单条规则;写 `disabled` / `enabledRules`;未授权 403 |
220
+ | POST | `/guard/entries` | `{action:"add", name, plaintext, kind}` 手动录入,或 `{action:"delete", placeholder}` 删除;明文只进加密保管库;未授权 403 |
184
221
 
185
222
  ## 已知限制
186
223
 
@@ -196,6 +233,8 @@ sensitive_guard_export(<SG_xxx>) 明文写到 0600 文件,返回路径
196
233
  10. 未授权来源仍然能读到元数据:有哪些条目、什么规则、什么时候入库、命中几次。这是为了让设置页能自我解释,代价是同网段的人能推断出「这里有 12 条凭据、分别是钥匙密码令牌」,但拿不到值。
197
234
  11. 宿主是 fail-fast 的,客户端 entry 在模块顶层抛错会让整个应用起不来。`apply` 已经整体兜底,顶层风险消不掉。
198
235
  12. 0.3.4 及更早的占位符是 `sha256(明文)` 前缀,短密码可以离线反推。升级后新捕获一律用 HMAC,旧代号为了兼容仍留在 `vault.json` 的 `aliases` 里,所以旧会话照样点得开;但**已经写进历史会话日志的旧代号改不了**,那批短密码该轮换还得轮换。
236
+ 13. 系统规则来自 gitleaks:10 条含 Go 专有 `(?-i:…)` 作用域标志的规则转成 JS 后会略微变宽松,标为「近似转换」并默认关;`pkcs12-file` 按文件名判定,文本脱敏用不上,未接入。规则数据按 MIT 许可随包分发,声明在 `NOTICE`。
237
+ 14. 手动录入的条目固定在保管库里(`pinned`),不参与 500 条上限淘汰。明文只在加密的 `vault.json`,运行时解密到内存做字面量匹配,**不写 `rules.json`**;但仍然防不住已经拿到本机账户权限的人。
199
238
 
200
239
  ## 卸载和故障
201
240
 
@@ -211,22 +250,32 @@ taskkill /F /IM "DeepSeek Harness.exe"
211
250
 
212
251
  `inline reveal:` 那行的取值:`ok` 正常,`waiting` 还在等 `uiConversation` 服务,`no-service` 拿不到服务,`matcher-failed` 规则注册被拒,`renderer-failed` 渲染器注册被拒。
213
252
 
214
- 数据目录在 `$DSH_PROFILE_DIR/sensitive-guard/`,包含 `vault.json`、`vault.key`、`pause.json`、`rules.json`、`exports/`。没设这个变量时回落到 `$DSH_HOME` 下。
253
+ 数据目录在 `$DSH_PROFILE_DIR/sensitive-guard/`,包含 `vault.json`、`vault.key`、`pause.json`(`until` 限时暂停 + `off` 持久关闭)、`rules.json`、`exports/`。没设这个变量时回落到 `$DSH_HOME` 下。
215
254
 
216
255
  ## 开发
217
256
 
218
257
  ```bash
219
- node test.mjs
258
+ node test.mjs # 离线自测
259
+ node scripts/build-gitleaks.mjs # 从上游重新生成规则数据
260
+ node scripts/build-gitleaks.mjs ./gitleaks.toml
220
261
  ```
221
262
 
222
- 90 项离线自测,用临时目录,不碰真实保管库。里面有一次真实的「从 0.3.x 升级」演练:把一条保管库记录改回旧代号,再用新进程加载,验证新旧代号都能解锁。
263
+ 137 项离线自测,用临时目录,不碰真实保管库。包含 0.3.x 旧代号升级演练、两个设置页标签与添加弹窗(含自定义分类)、列表行与复制按钮、关闭保护确认弹窗的渲染冒烟测试、221 条规则的编译与分级校验、逐条熵值/开关/手动录入回归,以及一次用新进程模拟重启验证持久关闭仍然生效。
223
264
 
224
265
  | 文件 | 作用 |
225
266
  | --- | --- |
226
- | `index.js` | host 端:三条钩子、保管库、两个工具、三条路由 |
227
- | `client.js` | 客户端:设置页、对话内胶囊、暂停开关 |
267
+ | `index.js` | host 端:三条钩子、保管库、两个工具、五条路由 |
268
+ | `client.js` | 客户端:两个设置页标签、规则页(搜索/筛选/分页/开关)、添加敏感数据弹窗(含自定义分类)、对话内胶囊 |
269
+ | `rules/gitleaks.json` | 从 gitleaks TOML 转换来的 221 条规则(MIT,见 `NOTICE`) |
270
+ | `scripts/build-gitleaks.mjs` | 规则转换脚本,负责 Go/RE2 → JS 语法降级与分级 |
228
271
  | `test.mjs` | 自测 |
229
272
  | `CHANGELOG.md` | 变更记录 |
230
273
  | `locale/` | 插件市场用的中英元数据 |
231
274
 
232
275
  GitHub Actions 在 ubuntu 和 windows 上跑 node 18、20、22 三个版本。许可证 MIT。
276
+
277
+ ---
278
+
279
+ 如果它帮你挡掉过一次不该进上下文的密钥,给个 star 是最直接的帮助:[⭐ star](https://github.com/wangxy496/dsh-sensitive-guard/stargazers)
280
+
281
+ 遇到不认的凭据格式,[提个 issue](https://github.com/wangxy496/dsh-sensitive-guard/issues),带上脱敏后的样例,规则可以按厂商补。
Binary file