dsh-privacy-gateway 0.3.0 → 0.3.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/README.en.md CHANGED
@@ -4,10 +4,15 @@
4
4
 
5
5
  ![The model only sees ⟦DSH_REDACTED:…⟧: secrets, credentials and personal data go out as placeholders and can come back as real values (off by default) — one interception point, every model request through the global fetch goes through it](docs/assets/screenshots/0-banner-en.png)
6
6
 
7
- A redaction gateway on the host process's undici global dispatcher. Secrets in outgoing model request bodies are replaced with placeholders; by default the placeholders in the replies go into the session log as they are, so no plaintext reaches it, and `restoreLog` puts them back with the real values on request. Adapters and providers don't notice it; by default the agent gets placeholders, and with `restoreLog` on it gets the real values as before. One interception point covers every provider that goes through `fetch()`, including adapters that plugins provide.
7
+ A redaction gateway on the host process's undici global dispatcher. Secrets in outgoing model request bodies are replaced with placeholders; by default the placeholders in the replies go into the session log as they are, so no real value from a reply reaches it, and `restoreLog` puts them back with the real values on request. Adapters and providers don't notice it; by default the agent gets placeholders, and with `restoreLog` on it gets the real values as before. One interception point covers every provider that goes through `fetch()`, including adapters that plugins provide.
8
8
 
9
9
  If you are using an **API relay**, or you are working with **sensitive information** or **production data**, please be sure to protect information security.
10
10
 
11
+ **[Capability boundaries](#capability-boundaries)**:
12
+
13
+ - This is a lightweight implementation: only the text in a request body and in a reply is masked. Files attached to the conversation (Word, Excel and PowerPoint documents), images and archives are neither parsed nor masked; they go as they are.
14
+ - If a model is led into writing a script that sends data out, the gateway does not stop it: with `restoreLog` on, `toolGuard` (off by default) covers only the one case of a restored value plus a non-local host, so a bare domain or writing a file and pushing it later cannot be caught.
15
+
11
16
  ## Install
12
17
 
13
18
  ```bash
@@ -15,7 +20,7 @@ If you are using an **API relay**, or you are working with **sensitive informati
15
20
  dsh plugin --profile web add dsh-privacy-gateway@latest
16
21
 
17
22
  # The same version as a GitHub Release asset (prereleases get one too)
18
- dsh plugin --profile web add https://github.com/xbzbing/dsh-privacy-gateway/releases/download/v0.3.0/dsh-privacy-gateway-0.3.0.tgz
23
+ dsh plugin --profile web add https://github.com/xbzbing/dsh-privacy-gateway/releases/download/v0.3.1/dsh-privacy-gateway-0.3.1.tgz
19
24
  ```
20
25
 
21
26
  A prerelease version has a `-` in it and goes to GitHub Releases only, never to npm. The repository does not commit `lib/`, so `dsh plugin add github:…` cannot load the plugin ([ADR-0006](docs/adr/0006-distribution-and-build-artifacts.md)).
@@ -26,7 +31,7 @@ In the default web profile the first install works at once, with no restart of t
26
31
 
27
32
  - **What is masked**: keys, tokens, passwords, private keys, cookies, email addresses, phone numbers, ID card and bank card numbers, and your own words — each with its own switch.
28
33
  - **Actions**: pick `mask` / `warn` / `block` per kind; a key can be sent as a made-up key of its own form (`shape`).
29
- - **No plaintext in the session log**: restore is off by default, so a reply's placeholders go into the session log as they are; turn `restoreLog` on to put the real values back, and the agent gets them too.
34
+ - **No real value written back from a reply**: restore is off by default, so a reply's placeholders go into the session log as they are; turn `restoreLog` on to put the real values back, and the agent gets them too.
30
35
  - **Records and statistics**: records written to disk by the day, a statistics card, and the session's 「隐私保护」 (Privacy) tab; a record holds placeholders, never a value.
31
36
  - **The `/privacy` command**: status, a temporary on / off, and `test` to preview how a text would go out.
32
37
  - **Live configuration**: everyday items are changed on the plugin page and apply from the next call; when masking fails, the request is not sent.
@@ -91,6 +96,50 @@ someone@gmail.com → EMAIL
91
96
  - The name of a cryptographic algorithm is no value: names like `SCRAM-SHA-512` and `PBKDF2WithHmacSHA256` are published in a spec for anyone to read, so masking one hides nothing; they go as they are ([ADR-0033](docs/adr/0033-algorithm-names.md)).
92
97
  - A key is masked the way a value is: the key in `{"metadata": {"sk-…": …}}` goes as a placeholder and comes back in the reply when `restoreLog` is on. The protocol's own keys (`model`, `role`, `type`, `name`, `id` and so on), the tool definitions and a tool call's argument names are never touched ([ADR-0029](docs/adr/0029-mask-json-keys.md)).
93
98
 
99
+ ## Capability boundaries
100
+
101
+ The gateway acts at one point: **the moment a model request leaves**. It does not judge meaning, does not change how the host behaves, and does not rewrite what is already on disk. Here is what it covers and what it does not.
102
+
103
+ ### What it covers
104
+
105
+ | Channel | Today |
106
+ | --- | --- |
107
+ | Model request body | POST JSON requests through the global `fetch()` (array `messages`, array or string `input`, `contents`, string `prompt`): matched values become placeholders, a compressed body is decompressed first ([ADR-0001](docs/adr/0001-intercept-at-undici-global-dispatcher.md), [ADR-0025](docs/adr/0025-compression-and-upgrades.md)) |
108
+ | Model reply | The response on the same connection: SSE streams and whole JSON, restored across chunks, compression and `\u27e6` escapes; written back into the log only with `restoreLog` on |
109
+ | Providers | Every adapter that goes through `fetch()`, ones a plugin provides included; `excludeHosts` / `excludeProviders` opt out |
110
+ | On failure | A masking error, an unknown `content-encoding`, or an `upgrade` / `CONNECT` on a model call is not sent ([ADR-0005](docs/adr/0005-fail-closed.md)) |
111
+ | Side channel | Records, statistics and Remote answers hold metadata only, never a value ([ADR-0008](docs/adr/0008-masking-records.md)) |
112
+
113
+ ### What it does not cover
114
+
115
+ In the order a reader is most likely to assume otherwise:
116
+
117
+ | Case | Today | Consequence |
118
+ | --- | --- | --- |
119
+ | Your own words | Written when you send them, before the gateway | **The session log holds what you typed**; it becomes placeholders only as it leaves ([ADR-0003](docs/adr/0003-restore-before-session-log.md)) |
120
+ | Tool results | Written when the tool returns | **The session log holds the tool's output**; masked only on the next request out |
121
+ | Images, files, base64 attachments | No rule recognises them; sent as they are | Screenshots, scans and attachments reach the model service; `multipart`, forms and non-JSON bodies are always sent untouched and leave no record |
122
+ | Direct `node:http`, worker threads, separate processes | Do not pass through the gateway | Those requests get no placeholder and leave no record ([ADR-0001](docs/adr/0001-intercept-at-undici-global-dispatcher.md)) |
123
+ | Headers, URL, method | Only the body is rewritten, with the length and coding headers that follow | `Authorization` carries the provider's own credential and would fail if changed; query parameters are untouched |
124
+ | Other egress | Not handled | A plugin sending its own messages, writing files or reaching its own service does not pass through it; a `fetch()` that is no model request goes as it is |
125
+ | What is already on disk | Not rewritten | Content logged before the upgrade stays as it is |
126
+
127
+ ### What it can recognise is limited
128
+
129
+ - A secret no rule matches is not masked: tokens of a custom shape, ciphertext, fragments split across places.
130
+ - Deliberately left alone: values shorter than 8 characters, values typed to stand for a key (`sk-…xxxx`, `changeme`), algorithm names (`SCRAM-SHA-512`), values that look like a file name ([ADR-0031](docs/adr/0031-no-short-or-template-values.md), [ADR-0033](docs/adr/0033-algorithm-names.md), [ADR-0026](docs/adr/0026-file-name-values.md)).
131
+ - There is no semantic judgement: rules and a word list only. Customer names, codenames and internal terms have to go into `words`; misses and false positives are both possible.
132
+ - A source constant whose field name contains a secret word may be judged a value when it is a name: a protocol-shaped slug (lower case, joined by `-`/`_`, each segment under 16 characters) under a field name ending in a container word (`header`, `id`, `file`, `name`…) is no longer mistaken for one ([ADR-0038](docs/adr/0038-container-field-slug-values.md)); put anything else confirmed not to be a secret into `allow`.
133
+ - Masking matches contiguous strings: hex and line-split encodings form no match and pass through by design, so the same file may read differently through a text tool and a hex tool.
134
+ - Images are not OCR'd; binary content goes out as it is.
135
+
136
+ ### What it is not responsible for
137
+
138
+ - It does not stop what a tool does with a real value (`restoreLog` on): a model led into writing a placeholder into a command such as `curl` runs the tool with the real value; the plugin only counts, and `toolGuard` can warn or block that one call ([ADR-0014](docs/adr/0014-tool-guard.md), [ADR-0009](docs/adr/0009-value-table-and-restore-exfiltration.md)).
139
+ - The value table is per process and cleared on restart; after a restart, a value no rule finds, masked only because it was remembered, goes as written the first time it is sent again ([ADR-0009](docs/adr/0009-value-table-and-restore-exfiltration.md)).
140
+ - It is not a compliance control and keeps no egress audit: a record shows that a substitution happened, not that nothing was missed.
141
+ - It does not protect data at rest: it protects the copy going to the model, not the session log, backups or exports on disk.
142
+
94
143
  ## Screens
95
144
 
96
145
  The screenshots show the Chinese interface.
@@ -156,11 +205,8 @@ With `vendorView` on (「保留发出内容,供对照查看」 on the config p
156
205
  ## Notes
157
206
 
158
207
  - Upgrading from 0.1.x: replies are no longer restored by default, so the agent gets placeholders. For the old behaviour (the agent and the log both hold the real values), turn `restoreLog` on in the config page.
159
- - By default the session log holds placeholders: with `restoreLog` off (the default), a reply's placeholders go into the log as they are, no plaintext reaches it, and the agent sees placeholders too; with it on, replies are restored before the log is written, so the log and the agent both hold the real values, and session sync, backups, shares and exports carry them too ([ADR-0003](docs/adr/0003-restore-before-session-log.md)).
160
- - Only traffic through the global `fetch()` is covered; direct `node:http`, worker threads and separate processes bypass the gateway ([ADR-0001](docs/adr/0001-intercept-at-undici-global-dispatcher.md)). Only JSON model requests it recognizes are rewritten (array `messages`, array or string `input`, `contents`, string `prompt`); multipart, forms and non-JSON bodies go as written and leave no record ([ADR-0001](docs/adr/0001-intercept-at-undici-global-dispatcher.md)).
161
- - Compressed requests are decompressed before they are masked; with `restoreLog` on, a compressed reply is decoded before it is restored, and with it off the reply passes through the gateway compressed, so an adapter reading it with `undici.request` must decompress it itself (`fetch` does it for the caller). A request's `content-encoding` with no decoder fails the request ([ADR-0025](docs/adr/0025-compression-and-upgrades.md)). An upgrade (the WebSocket handshake) or a `CONNECT` tunnel inside a model call is refused as well: Codex falls back to SSE the first time and takes SSE from then on in that session. One opened by a plugin outside a model call (dsh-im-infoflow's inbound, say) is let through, its messages unmasked, with one warning per host ([ADR-0032](docs/adr/0032-upgrade-scoped-to-masked-calls.md)) — a fail-closed to fail-open trade whose cost the ADR records; a host that needs a transparent link can still go into `excludeHosts`.
162
- - The value table is in memory only ([ADR-0009](docs/adr/0009-value-table-and-restore-exfiltration.md)). After a restart, a value no rule finds, masked only because it was remembered, goes as written the first time it is sent again.
163
- - With `restoreLog` on, replies are restored everywhere, tool call arguments included: a model led into writing a placeholder into a command such as `curl` runs the tool with the real value. The plugin counts these places (the tool-argument count on the card and in `/privacy status`) and does not block them ([ADR-0009](docs/adr/0009-value-table-and-restore-exfiltration.md)); with it off, no reply holds a real value and this path does not exist.
208
+ - By default the session log holds placeholders: with `restoreLog` off (the default), a reply's placeholders go into the log as they are, no real value from a reply reaches it, and the agent sees placeholders too; with it on, replies are restored before the log is written, so the log and the agent both hold the real values, and session sync, backups, shares and exports carry them too ([ADR-0003](docs/adr/0003-restore-before-session-log.md)).
209
+ - Compressed requests are decompressed before they are masked; with `restoreLog` on, a compressed reply is decoded before it is restored, and with it off the reply passes through the gateway compressed, so an adapter reading it with `undici.request` must decompress it itself (`fetch` does it for the caller). A request's `content-encoding` with no decoder fails the request ([ADR-0025](docs/adr/0025-compression-and-upgrades.md)). An upgrade (the WebSocket handshake) or a `CONNECT` tunnel inside a model call is refused as well: Codex falls back to SSE the first time and takes SSE from then on in that session. One opened by something other than a model call is let through, its messages unmasked, with one warning per host ([ADR-0032](docs/adr/0032-upgrade-scoped-to-masked-calls.md)) — a fail-closed to fail-open trade whose cost the ADR records; a host that needs a transparent link can still go into `excludeHosts`.
164
210
  - When masking fails, the request is not sent and fails with `dsh-privacy-gateway: masking failed, request not sent` ([ADR-0005](docs/adr/0005-fail-closed.md)). To recover, run `/privacy off`, or disable the plugin on the Plugins page or set `enabled` to `false`.
165
211
 
166
212
  ## Development
package/README.md CHANGED
@@ -4,10 +4,15 @@
4
4
 
5
5
  ![模型只看见 ⟦DSH_REDACTED:…⟧:密钥、凭据、个人信息发出前换成占位符,回复里的占位符可换回原值(默认不换),一处拦截,每个走全局 fetch 的模型请求都经过它](docs/assets/screenshots/0-banner.png)
6
6
 
7
- 在宿主进程的 undici 全局 dispatcher 上装一层脱敏网关。出站把模型请求体里的密钥换成占位符;入站默认让回流里的占位符原样进会话日志,明文不进日志,打开 `restoreLog` 才换回真值。对 adapter、provider 无感;agent 在默认关闭时拿到的是占位符,打开 `restoreLog` 后与以前一样拿到真值。一处拦截覆盖所有走 `fetch()` 的 provider,包括插件提供的 adapter。
7
+ 在宿主进程的 undici 全局 dispatcher 上装一层脱敏网关。出站把模型请求体里的密钥换成占位符;入站默认让回流里的占位符原样进会话日志,回复里的真值不进日志,打开 `restoreLog` 才换回真值。对 adapter、provider 无感;agent 在默认关闭时拿到的是占位符,打开 `restoreLog` 后与以前一样拿到真值。一处拦截覆盖所有走 `fetch()` 的 provider,包括插件提供的 adapter。
8
8
 
9
9
  如果你在用**中转站**或者在处理**敏感信息**、**线上数据**,请务必注意保护信息安全。
10
10
 
11
+ **[能力边界](#能力边界)**:
12
+
13
+ - 这是一个轻量级实现,只对请求体与回复里的文本做脱敏。对话附件里的文件(Word、Excel、PPT 这类文档)、图片、压缩包等都不解析、不脱敏,原样发出。
14
+ - 如果模型被诱导写出把数据外发的脚本,插件不拦:`restoreLog` 打开时 `toolGuard`(默认 `off`)也只覆盖「还原值 + 非本机 host」这一种外带,裸域名、先写文件再推送这些都拦不住。
15
+
11
16
  ## 安装
12
17
 
13
18
  ```bash
@@ -15,7 +20,7 @@
15
20
  dsh plugin --profile web add dsh-privacy-gateway@latest
16
21
 
17
22
  # 同一版本的 GitHub Release 附件(预发布也发在这里)
18
- dsh plugin --profile web add https://github.com/xbzbing/dsh-privacy-gateway/releases/download/v0.3.0/dsh-privacy-gateway-0.3.0.tgz
23
+ dsh plugin --profile web add https://github.com/xbzbing/dsh-privacy-gateway/releases/download/v0.3.1/dsh-privacy-gateway-0.3.1.tgz
19
24
  ```
20
25
 
21
26
  预发布版本号带 `-`,只发 GitHub Release、不发 npm。仓库不提交 `lib/`,`dsh plugin add github:…` 拿到的树加载不了([ADR-0006](docs/adr/0006-distribution-and-build-artifacts.md))。
@@ -26,7 +31,7 @@ dsh plugin --profile web add https://github.com/xbzbing/dsh-privacy-gateway/rele
26
31
 
27
32
  - **脱敏范围**:密钥、token、密码、私钥、cookie、邮箱、手机号、身份证、银行卡、自定义敏感词,各有开关。
28
33
  - **动作可选**:按 kind 选 `mask` / `warn` / `block`,密钥可换成同形的假 key(`shape`)。
29
- - **日志不写真值**:默认不还原,回复里的占位符原样进会话日志,明文不进日志;打开 `restoreLog` 才把真值写回,agent 同时拿到真值。
34
+ - **回复不写回真值**:默认不还原,回复里的占位符原样进会话日志,回复里的真值不进日志;打开 `restoreLog` 才把真值写回,agent 同时拿到真值。
30
35
  - **记录与统计**:按天落盘的脱敏记录、统计卡、会话「隐私保护」标签页;记录里只有占位符,没有真值。
31
36
  - **`/privacy` 命令**:查状态、临时开关、`test` 预览一段文本会怎样发出。
32
37
  - **配置热更新**:日常项在插件页上改,下一次调用即生效;脱敏出错时请求不发出。
@@ -91,6 +96,50 @@ someone@gmail.com → EMAIL
91
96
  - 加密算法的名字不算值:`SCRAM-SHA-512`、`PBKDF2WithHmacSHA256` 这类名字写在规范里、人人可读,遮住什么都不保护,原样发出([ADR-0033](docs/adr/0033-algorithm-names.md))。
92
97
  - 键名和值一样脱敏:`{"metadata": {"sk-…": …}}` 里的键名也换成占位符,`restoreLog` 打开时回复里再换回来;协议自身的键(`model`、`role`、`type`、`name`、`id` 等)、工具定义和工具调用的参数名不动([ADR-0029](docs/adr/0029-mask-json-keys.md))。
93
98
 
99
+ ## 能力边界
100
+
101
+ 网关只在一个位置动手:**模型请求出站的那一刻**。它不判断语义、不改宿主行为、不改已经落盘的内容。下面是它覆盖的与不覆盖的。
102
+
103
+ ### 能做的
104
+
105
+ | 通道 | 现在 |
106
+ | --- | --- |
107
+ | 模型请求体 | 走全局 `fetch()` 的 POST JSON 请求(数组 `messages`、数组或字符串 `input`、`contents`、字符串 `prompt`):命中规则的值换成占位符,压缩的先解压([ADR-0001](docs/adr/0001-intercept-at-undici-global-dispatcher.md)、[ADR-0025](docs/adr/0025-compression-and-upgrades.md)) |
108
+ | 模型回复 | 同一条连接的响应:SSE 流与整段 JSON,跨分片、压缩、`\u27e6` 转义都能还原;`restoreLog` 打开时才写回日志 |
109
+ | 覆盖的 provider | 所有走 `fetch()` 的 adapter,包括插件提供的;`excludeHosts` / `excludeProviders` 可以放行 |
110
+ | 出错时 | 脱敏出错、认不出的 `content-encoding`、模型调用上的 `upgrade` / `CONNECT` 一律不发([ADR-0005](docs/adr/0005-fail-closed.md)) |
111
+ | 旁路 | 记录、统计、Remote 返回值只有元信息,没有真值([ADR-0008](docs/adr/0008-masking-records.md)) |
112
+
113
+ ### 做不到的
114
+
115
+ 按最容易被误以为做了的顺序排:
116
+
117
+ | 场景 | 现在 | 后果 |
118
+ | --- | --- | --- |
119
+ | 你自己发的话 | 发送时就落盘,早于网关 | **会话日志里是你输入的原文**;只有出站那一刻才换成占位符([ADR-0003](docs/adr/0003-restore-before-session-log.md)) |
120
+ | 工具结果 | 工具返回后落盘 | **会话日志里是工具输出的原文**;下一次出站才脱敏 |
121
+ | 图片、文件、base64 附件 | 没有规则认它,原样发出 | 截图、扫描件、附件内容会到模型服务;`multipart`、表单、非 JSON body 更是一律原样发出,也不留记录 |
122
+ | `node:http` 直连、worker 线程、独立进程 | 不经过网关 | 这些路径上的请求没有占位符,也没有记录([ADR-0001](docs/adr/0001-intercept-at-undici-global-dispatcher.md)) |
123
+ | 请求头、URL、方法 | 只改 body,以及随之变化的长度与编码头 | `Authorization` 这类头里是 provider 自己的凭据,改了会认证失败;query 参数同样不动 |
124
+ | 别的出站通道 | 不管 | 别的插件自己发消息、写文件、连自己的服务,都不经过它;走 `fetch()` 但不是模型请求的,原样放行 |
125
+ | 已经落盘的历史 | 不改 | 升级前写进日志的内容保持原样 |
126
+
127
+ ### 认得出来的形态有限
128
+
129
+ - 规则没命中的密钥不脱敏:自定义格式的令牌、加密或编码后的密文、拆成多段的片段。
130
+ - 故意不脱敏的:短于 8 个字符的值、占位写法(`sk-…xxxx`、`changeme`)、算法名(`SCRAM-SHA-512`)、像文件名的值([ADR-0031](docs/adr/0031-no-short-or-template-values.md)、[ADR-0033](docs/adr/0033-algorithm-names.md)、[ADR-0026](docs/adr/0026-file-name-values.md))。
131
+ - 没有语义判断:只有规则和词表。客户名、项目代号、内部术语要自己加进 `words`;漏检和误伤都可能存在。
132
+ - 字段名里带 secret 词的源码常量可能被具名规则当成值脱敏(值其实是名字):末位是容器词(`header`、`id`、`file`、`name`…)的字段名下、小写且以 `-`/`_` 分段每段短于 16 字符的协议型 slug 已不再误判([ADR-0038](docs/adr/0038-container-field-slug-values.md)),其余的确认后放进 `allow`。
133
+ - 脱敏按连续字符串匹配:hex、分行的编码表示不形成连续匹配,天然旁路——同一份文件经文本工具和 hex 工具读到的内容可能不一致。
134
+ - 图片不做 OCR,二进制内容原样发出。
135
+
136
+ ### 不负责的事
137
+
138
+ - 不拦截工具拿到真值之后的动作(`restoreLog` 打开时):模型被诱导把占位符写进 `curl` 之类的命令,工具执行时用的就是真值;插件只记账,`toolGuard` 可选告警或拦那一次调用([ADR-0014](docs/adr/0014-tool-guard.md)、[ADR-0009](docs/adr/0009-value-table-and-restore-exfiltration.md))。
139
+ - 不跨进程共享值表:每个进程自己的值表,重启清空;重启后规则命不中、只靠记忆兜住的值第一次出站按原文发出([ADR-0009](docs/adr/0009-value-table-and-restore-exfiltration.md))。
140
+ - 不做合规认证、不做出口审计:记录只能说明「发生过替换」,不能说明「没有遗漏」。
141
+ - 不做静态数据保护:它保护的是发往模型的那一份,不是磁盘上的会话日志、备份或导出的文件。
142
+
94
143
  ## 界面
95
144
 
96
145
  | 插件配置 | 会话「隐私保护」 |
@@ -154,11 +203,8 @@ someone@gmail.com → EMAIL
154
203
  ## 须知
155
204
 
156
205
  - 从 0.1.x 升级上来:默认不再还原,agent 拿到的是占位符。需要旧行为(agent 与日志都拿到真值)时,在配置页打开 `restoreLog`(「会话日志写入真值」)。
157
- - 会话日志默认是占位符:`restoreLog` 关闭(默认)时,回复里的占位符原样进日志,明文不进日志,agent 拿到的也是占位符;打开后还原发生在日志落盘之前,日志与 agent 都是真值,会话同步、备份、分享、导出带走的也是真值([ADR-0003](docs/adr/0003-restore-before-session-log.md))。
158
- - 覆盖面只有走全局 `fetch()` 的流量;`node:http` 直连、worker 线程、独立进程不经过网关([ADR-0001](docs/adr/0001-intercept-at-undici-global-dispatcher.md))。只改写识别得出的 JSON 模型请求(数组 `messages`、数组或字符串 `input`、`contents`、字符串 `prompt`);multipart、表单、非 JSON body 原样发出,也不留记录([ADR-0001](docs/adr/0001-intercept-at-undici-global-dispatcher.md))。
159
- - 压缩的请求先解压再脱敏;`restoreLog` 打开时压缩的回复也先解码再还原,关闭时压缩的回复原样穿过网关、由适配器按 `content-encoding` 自己解压(用 `undici.request` 直连的 adapter 要自己解压,`fetch` 会自动解)。请求里认不出的 `content-encoding` 不发([ADR-0025](docs/adr/0025-compression-and-upgrades.md))。模型调用里的 `upgrade` 握手(WebSocket)与 `CONNECT` 隧道也不发:Codex 第一次被拒后回落到 SSE,之后同一会话直接走 SSE;模型调用之外的升级与隧道(别的插件自己的连接,如 dsh-im-infoflow 的入站)放行,消息不脱敏,每个 host 提示一次([ADR-0032](docs/adr/0032-upgrade-scoped-to-masked-calls.md))——这是 fail-closed 往 fail-open 的取舍,代价见 ADR;需要整条链路放行的 host 仍可加进 `excludeHosts`。
160
- - 值表只在内存里([ADR-0009](docs/adr/0009-value-table-and-restore-exfiltration.md))。重启后,规则命不中、只靠记忆兜住的值第一次出站会按原文发出。
161
- - `restoreLog` 打开时,还原发生在回复里,包括工具调用参数:模型被诱导把占位符写进 `curl` 之类的命令时,工具执行时用的是真值。插件只记账(统计卡与 `/privacy status` 里的「工具参数」计数),不拦截([ADR-0009](docs/adr/0009-value-table-and-restore-exfiltration.md));关闭时回复里没有真值,这条路径不存在。
206
+ - 会话日志默认是占位符:`restoreLog` 关闭(默认)时,回复里的占位符原样进日志,回复里的真值不进日志,agent 拿到的也是占位符;打开后还原发生在日志落盘之前,日志与 agent 都是真值,会话同步、备份、分享、导出带走的也是真值([ADR-0003](docs/adr/0003-restore-before-session-log.md))。
207
+ - 压缩的请求先解压再脱敏;`restoreLog` 打开时压缩的回复也先解码再还原,关闭时压缩的回复原样穿过网关、由适配器按 `content-encoding` 自己解压(用 `undici.request` 直连的 adapter 要自己解压,`fetch` 会自动解)。请求里认不出的 `content-encoding` 不发([ADR-0025](docs/adr/0025-compression-and-upgrades.md))。模型调用里的 `upgrade` 握手(WebSocket)与 `CONNECT` 隧道也不发:Codex 第一次被拒后回落到 SSE,之后同一会话直接走 SSE;模型调用之外的升级与隧道(别的组件自己的连接)放行,消息不脱敏,每个 host 提示一次([ADR-0032](docs/adr/0032-upgrade-scoped-to-masked-calls.md))——这是 fail-closed 往 fail-open 的取舍,代价见 ADR;需要整条链路放行的 host 仍可加进 `excludeHosts`。
162
208
  - 脱敏出错时请求不会发出,报错 `dsh-privacy-gateway: masking failed, request not sent`([ADR-0005](docs/adr/0005-fail-closed.md))。止血办法是 `/privacy off`,或在插件页停用本插件、把配置 `enabled` 设为 `false`。
163
209
 
164
210
  ## 开发
package/lib/index.js CHANGED
@@ -1037,6 +1037,19 @@ function secretValue(v) {
1037
1037
  return hasDigit(v) && hasLetter(v);
1038
1038
  }
1039
1039
  /**
1040
+ * A value shaped like a name rather than a key: all lower case, starting with
1041
+ * a letter, joined by - or _ into segments none long enough to be a run of
1042
+ * random characters (x-dsh-auth-gateway-…, a header's name, issue #4). A key
1043
+ * keeps a digit, a capital or one long run of characters, so it is not let
1044
+ * go. `notASlug` is the looser test the `sk-` rule needs, where a short word
1045
+ * counts as a slug too (docs/adr/0038).
1046
+ */
1047
+ function slugValue(v) {
1048
+ if (!/^[a-z][a-z_-]*$/.test(v)) return false;
1049
+ const parts = v.split(/[-_]/);
1050
+ return parts.length >= 2 && parts.every((w) => w.length > 0 && w.length < 16);
1051
+ }
1052
+ /**
1040
1053
  * A value of a field already named as a secret (`DB_PASSWORD=…`, `api_key: …`):
1041
1054
  * the name has done the proving, so a long passphrase of letters only and a
1042
1055
  * long number count too (ADR-0036). `secretValue` stays the test for a value
@@ -1161,6 +1174,10 @@ function algorithmValue(v) {
1161
1174
  const words = algorithmWords(v);
1162
1175
  return words.length > 0 && words.every((w) => ALGO_WORDS.has(w));
1163
1176
  }
1177
+ /**
1178
+ * The words a real secret is named by. Exported so the guard test can keep the
1179
+ * container word list disjoint from it (docs/adr/0038).
1180
+ */
1164
1181
  const SECRET_WORDS = new Set([
1165
1182
  "token",
1166
1183
  "tokens",
@@ -1213,13 +1230,28 @@ function fieldAt(s$1, a, b) {
1213
1230
  return !fileField(s$1, a) && !LINE_REF.test(v) && !pathValue(v) && !fileNameValue(v) && !namesASecret(v) && !skillName(s$1, a, b) && !versionValue(v);
1214
1231
  }
1215
1232
  /**
1233
+ * The words a field's name may end in for its value to be a name rather than
1234
+ * a secret: a container the secret goes in or is called by (issue #4). A
1235
+ * closed list, the way file names (ADR-0026) and algorithm names (ADR-0033)
1236
+ * are kept out: any other last word keeps masking, so a version suffix
1237
+ * (`SECRET_V2`), a glued name (`clientsecret`) or a hash (`DB_PASSWORD_HASH`)
1238
+ * never lets a value go by accident (docs/adr/0038). Exported so the guard
1239
+ * test pins every word: an addition that is not in the test fails it.
1240
+ */
1241
+ const CONTAINER_WORDS = new Set("header headers id ids uuid uuids guid guids file files path paths name names url urls uri uris label labels prefix prefixes suffix suffixes".split(" "));
1242
+ /**
1216
1243
  * Where a value of a field already named as a secret starts: `fieldAt`'s
1217
1244
  * places, plus a long run of digits. A bare number reads as a line reference
1218
1245
  * elsewhere (`token.go:L643`), but after `password=` it is the password
1219
1246
  * (ADR-0036).
1220
1247
  */
1221
1248
  function namedAt(s$1, a, b) {
1222
- if (fieldAt(s$1, a, b)) return true;
1249
+ if (fieldAt(s$1, a, b)) {
1250
+ const words = fieldWords(s$1, a);
1251
+ const last = words[words.length - 1];
1252
+ if (last !== void 0 && CONTAINER_WORDS.has(last) && slugValue(s$1.slice(a, b))) return false;
1253
+ return true;
1254
+ }
1223
1255
  return /^\d{10,}$/.test(s$1.slice(a, b)) && !fileField(s$1, a);
1224
1256
  }
1225
1257
  /** A field's name's words in lower case: next_page_token, nextPageToken and X-Next-Page-Token are next, page, token. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-privacy-gateway",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "Process-wide HTTP redaction gateway for DSH: masks secrets in outgoing model requests; replies keep placeholders in the session log by default, and restoreLog writes the real values back before it is written, for every provider.",
6
6
  "icon": "./assets/icon.svg",