dsh-privacy-gateway 0.1.3 → 0.1.4

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
@@ -125,7 +125,7 @@ With `vendorView` on (「保留发出内容,供对照查看」, Keep what was
125
125
 
126
126
  - The session log holds the real values: replies are restored before the log is written ([ADR-0003](docs/adr/0003-restore-before-session-log.md)). Session sync, backups and shares carry the real values.
127
127
  - 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)).
128
- - Compressed requests and replies are decompressed before they are masked or restored; a `content-encoding` with no decoder fails the request ([ADR-0025](docs/adr/0025-compression-and-upgrades.md)). Upgrades (the WebSocket handshake) are refused as well: Codex falls back to SSE the first time and takes SSE from then on in that session; a WebSocket another plugin opens through the global dispatcher is refused too, so put its host in `excludeHosts` if it needs one.
128
+ - Compressed requests and replies are decompressed before they are masked or restored; a `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`.
129
129
  - 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.
130
130
  - 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)).
131
131
  - 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`.
package/README.md CHANGED
@@ -123,7 +123,7 @@ someone@gmail.com → EMAIL
123
123
 
124
124
  - 会话日志里是真值:还原发生在日志落盘之前([ADR-0003](docs/adr/0003-restore-before-session-log.md))。会话同步、备份、分享带走的是真值。
125
125
  - 覆盖面只有走全局 `fetch()` 的流量;`node:http` 直连、worker 线程、独立进程不经过网关([ADR-0001](docs/adr/0001-intercept-at-undici-global-dispatcher.md))。
126
- - 压缩的请求与回复先解压再脱敏、还原;认不出的 `content-encoding` 不发([ADR-0025](docs/adr/0025-compression-and-upgrades.md))。带 `upgrade` 的握手(WebSocket)也不发:Codex 第一次被拒后回落到 SSE,之后同一会话直接走 SSE;其他插件经全局 dispatcher 打开的 WebSocket 同样被拒,需要时把它的 host 加进 `excludeHosts`。
126
+ - 压缩的请求与回复先解压再脱敏、还原;认不出的 `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`。
127
127
  - 值表只在内存里([ADR-0009](docs/adr/0009-value-table-and-restore-exfiltration.md))。重启后,规则命不中、只靠记忆兜住的值第一次出站会按原文发出。
128
128
  - 还原发生在回复里,包括工具调用参数:模型被诱导把占位符写进 `curl` 之类的命令时,工具执行时用的是真值。插件只记账(统计卡与 `/privacy status` 里的「工具参数」计数),不拦截([ADR-0009](docs/adr/0009-value-table-and-restore-exfiltration.md))。
129
129
  - 脱敏出错时请求不会发出,报错 `dsh-privacy-gateway: masking failed, request not sent`([ADR-0005](docs/adr/0005-fail-closed.md))。止血办法是 `/privacy off`,或在插件页停用本插件、把配置 `enabled` 设为 `false`。
package/lib/index.d.ts CHANGED
@@ -168,8 +168,11 @@ interface LayerInfo {
168
168
  * The gateway: an undici dispatcher chained in front of whatever dispatcher
169
169
  * was global before it (docs/adr/0001). A POST whose body is a JSON model
170
170
  * request goes on masked, its reply restored; everything else goes on
171
- * untouched. Masking that throws fails the request instead of sending it
172
- * (docs/adr/0005). The layers under it are ordered by role (docs/adr/0012).
171
+ * untouched. An upgrade a model call opens (the Codex WebSocket) is refused
172
+ * (docs/adr/0025); one a plugin opens outside a call — dsh-im-infoflow's
173
+ * inbound, say — is let through unmasked, with one warn per host
174
+ * (docs/adr/0032). Masking that throws fails the request instead of sending
175
+ * it (docs/adr/0005). The layers under it are ordered by role (docs/adr/0012).
173
176
  */
174
177
  declare class MaskingDispatcher extends Dispatcher {
175
178
  #private;
package/lib/index.js CHANGED
@@ -4463,27 +4463,46 @@ function elapsed(started) {
4463
4463
  const current = new AsyncLocalStorage();
4464
4464
  /**
4465
4465
  * The excluded provider whose stream the caller runs in, if any. A context
4466
- * that only carries the session (a masked provider's stream) names none: an
4467
- * empty or absent provider is never a bypass.
4466
+ * that is a model call names none: an empty or absent provider is never a
4467
+ * bypass.
4468
4468
  */
4469
4469
  function bypassedProvider() {
4470
- const provider = current.getStore()?.provider;
4471
- return typeof provider === "string" && provider !== "" ? provider : void 0;
4470
+ const store = current.getStore();
4471
+ return store?.kind === "excluded" && store.provider !== "" ? store.provider : void 0;
4472
4472
  }
4473
4473
  /** The session the request being started belongs to, when it is one the host named (docs/adr/0020). */
4474
4474
  function currentSession() {
4475
4475
  return current.getStore()?.session;
4476
4476
  }
4477
+ /**
4478
+ * Whether the current async context is a model call (docs/adr/0032). An
4479
+ * upgrade inside one is the provider's transport and is refused; one a plugin
4480
+ * opens outside any call is its own connection and was let through. It answers
4481
+ * "is this call", not "is masking on": a call an explicit bypass lets by is
4482
+ * still one, and the dispatcher asks the bypass first.
4483
+ */
4484
+ function inModelCall() {
4485
+ return current.getStore()?.kind === "masked";
4486
+ }
4477
4487
  /** `open`'s stream, with each step it takes (the request it starts, every chunk it reads) run in the context of `provider`. */
4478
4488
  function bypassing(provider, open$1, session) {
4479
4489
  return within((f) => current.run({
4490
+ kind: "excluded",
4480
4491
  provider,
4481
4492
  ...session ? { session } : {}
4482
4493
  }, f), open$1);
4483
4494
  }
4484
- /** `open`'s stream run outside any excluded provider: a nested call to a provider that is masked is masked. */
4485
- function unbypassing(open$1, session) {
4486
- return within((f) => current.run(session ? { session } : void 0, f), open$1);
4495
+ /**
4496
+ * `open`'s stream as a model call: it leaves any excluded provider's context
4497
+ * (a nested call to a provider that is masked is masked) and marks the stream
4498
+ * as a call, so the gateway can refuse an upgrade the call opens
4499
+ * (docs/adr/0032) and still name the session in its records.
4500
+ */
4501
+ function asModelCall(open$1, session) {
4502
+ return within((f) => current.run({
4503
+ kind: "masked",
4504
+ ...session ? { session } : {}
4505
+ }, f), open$1);
4487
4506
  }
4488
4507
  /**
4489
4508
  * What the `llm/stream` listener hands on for a call to `provider`: the stream
@@ -4493,7 +4512,7 @@ function unbypassing(open$1, session) {
4493
4512
  function routeStream(provider, excluded, next, sessionId) {
4494
4513
  const session = typeof sessionId === "string" && sessionId !== "" ? sessionId : void 0;
4495
4514
  if (isExcluded(provider, excluded)) return bypassing(provider, next, session);
4496
- return current.getStore() === void 0 && session === void 0 ? next() : unbypassing(next, session);
4515
+ return asModelCall(next, session);
4497
4516
  }
4498
4517
  function within(run, open$1) {
4499
4518
  return { [Symbol.asyncIterator]() {
@@ -5003,8 +5022,11 @@ const RANK = {
5003
5022
  * The gateway: an undici dispatcher chained in front of whatever dispatcher
5004
5023
  * was global before it (docs/adr/0001). A POST whose body is a JSON model
5005
5024
  * request goes on masked, its reply restored; everything else goes on
5006
- * untouched. Masking that throws fails the request instead of sending it
5007
- * (docs/adr/0005). The layers under it are ordered by role (docs/adr/0012).
5025
+ * untouched. An upgrade a model call opens (the Codex WebSocket) is refused
5026
+ * (docs/adr/0025); one a plugin opens outside a call — dsh-im-infoflow's
5027
+ * inbound, say — is let through unmasked, with one warn per host
5028
+ * (docs/adr/0032). Masking that throws fails the request instead of sending
5029
+ * it (docs/adr/0005). The layers under it are ordered by role (docs/adr/0012).
5008
5030
  */
5009
5031
  var MaskingDispatcher = class extends Dispatcher {
5010
5032
  #engine;
@@ -5014,6 +5036,8 @@ var MaskingDispatcher = class extends Dispatcher {
5014
5036
  #layers;
5015
5037
  #seq = 0;
5016
5038
  #order;
5039
+ /** Hosts a tunnel was let through to: each warned about once, however often it reconnects. */
5040
+ #warnedTunnels = /* @__PURE__ */ new Set();
5017
5041
  /** Set once the gateway is disposed: every request that still reaches it (through a layer that holds it) passes through. */
5018
5042
  bypass = false;
5019
5043
  [kGateway] = true;
@@ -5115,9 +5139,17 @@ var MaskingDispatcher = class extends Dispatcher {
5115
5139
  }
5116
5140
  if (!this.bypass) this.heal?.();
5117
5141
  if (!this.bypass && isTunnel(opts) && !this.#passes(opts)) {
5118
- this.#logger?.warn(`dsh-privacy-gateway: a ${tunnelName(opts)} connection to ${targetHost(opts)?.host ?? "an unknown host"} was refused: its messages cannot be masked`);
5119
- failRequest(wrapHandler(handler), new PrivacyGatewayError("a WebSocket or other upgraded connection cannot be masked, it was not opened", void 0, "DSH_PRIVACY_GATEWAY_UPGRADE_REFUSED"));
5120
- return true;
5142
+ const host = targetHost(opts)?.host ?? "an unknown host";
5143
+ if (inModelCall()) {
5144
+ this.#logger?.warn(`dsh-privacy-gateway: a ${tunnelName(opts)} connection to ${host} was refused: its messages cannot be masked`);
5145
+ failRequest(wrapHandler(handler), new PrivacyGatewayError("a WebSocket or other upgraded connection cannot be masked, it was not opened", void 0, "DSH_PRIVACY_GATEWAY_UPGRADE_REFUSED"));
5146
+ return true;
5147
+ }
5148
+ if (!this.#warnedTunnels.has(host)) {
5149
+ if (this.#warnedTunnels.size >= 32) this.#warnedTunnels.clear();
5150
+ this.#warnedTunnels.add(host);
5151
+ this.#logger?.warn(`dsh-privacy-gateway: a ${tunnelName(opts)} connection to ${host} was let through: its messages are not masked, and neither are those of further connections to it`);
5152
+ }
5121
5153
  }
5122
5154
  const next = this.downstream();
5123
5155
  if (this.bypass || !this.#candidate(opts)) return next.dispatch({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-privacy-gateway",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "description": "Process-wide HTTP redaction gateway for DSH: masks secrets in outgoing model requests and restores placeholders in the replies, for every provider.",
6
6
  "icon": "./assets/icon.svg",