leakshield 0.5.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/README.md CHANGED
@@ -40,6 +40,7 @@ None of the hosts is a guarantee that nothing sensitive reaches a model. `leaksh
40
40
  - **Claude Code** (experimental): a local proxy (`leakshield --claude-code-proxy`, then `ANTHROPIC_BASE_URL`), verified with Claude Code 2.1.321 against a loopback stub upstream ([record](docs/evidence/2026-11-21-claude-code-proxy.md)). It masks model requests and restores assistant text, but does not restore placeholders at tool execution yet. See [Claude Code](docs/claude-code.md).
41
41
  - Not yet verified: OS-keychain custody on Windows and Linux (macOS verified 2026-10-20), a real Anthropic provider, long histories on other providers, long-running use ([#26](https://github.com/jwcrystal/leakshield/issues/26)).
42
42
  - Defaults may still change during `0.x`; see the [changelog](CHANGELOG.md).
43
+ - Planned work: [Roadmap](docs/ROADMAP.md) (Traditional Chinese; priorities, candidates and research, separate from current support claims).
43
44
  - Verification records: [docs/evidence](docs/evidence/README.md). Report vulnerabilities privately: [SECURITY.md](SECURITY.md). License: [Apache-2.0](LICENSE).
44
45
 
45
46
  ---
@@ -199,7 +200,7 @@ To run without Node.js, build a standalone binary of the CLI with Bun: see [Buil
199
200
 
200
201
  ### What this protects — and what it does not
201
202
 
202
- LeakShield protects model-conversation content: prompts, replayed history, tool outputs, and provider-bound request bodies whose shape it can parse. How much of that each host covers differs; `leakshield --doctor <host>` and the [host capability matrix](docs/host-capabilities.md) list it. It is **not** a tool-egress firewall: a permitted local tool can still transmit originals over the network via `curl`, a remote MCP server, or any other channel. Controlling tool destinations requires a separate permission layer outside LeakShield. Since 0.3.0, `curl` and `http` may also hold a placeholder inside an argument, so a tool can call an API with the protected value; the original then leaves the machine by design.
203
+ LeakShield protects model-conversation content: prompts, replayed history, tool outputs, and provider-bound request bodies whose shape it can parse. How much of that each host covers differs; `leakshield --doctor <host>` and the [host capability matrix](docs/host-capabilities.md) list it. It is **not** a tool-egress firewall: a permitted local tool can still transmit originals over the network via `curl`, a remote MCP server, or any other channel. Controlling tool destinations requires a separate permission layer outside LeakShield. Since 0.3.0, `curl` and `http` may also hold a placeholder inside an argument, so a tool can call an API with the protected value; the original then leaves the machine by design. Traffic the host or its dependencies send on their own, such as telemetry, analytics and error or crash reports, is likewise out of reach: it does not pass the model boundary, so LeakShield cannot see it, even with a zero-data-retention (ZDR) provider. Restricting that traffic is a job for the host's own settings or an OS-level sandbox or firewall.
203
204
 
204
205
  Detection also has known gaps: street/postal addresses, dates, IPv6, and generic assignments outside the keyword list or below the 8-character minimum pass through undetected. Credit card and IBAN numbers are detected only when the checksum validates; IP detection covers public IPv4 only; passport detection reads the MRZ line only; United States SSN, Taiwan business-number, phone and URL detection are opt-in — see [Detection Coverage](#detection-coverage).
205
206
 
package/README.zh-TW.md CHANGED
@@ -40,6 +40,7 @@ MCP Server 模式已移除。它依賴模型在已收到相關 context 後自行
40
40
  - **Claude Code**(實驗性):透過本機 proxy(`leakshield --claude-code-proxy`,再設定 `ANTHROPIC_BASE_URL`),已用 Claude Code 2.1.321 搭配本機替身上游驗證([紀錄](docs/evidence/2026-11-21-claude-code-proxy.md))。它會遮罩模型請求並還原助理文字,但還不會在工具執行時還原 placeholder。詳見 [Claude Code](docs/claude-code.zh-TW.md)。
41
41
  - 尚未驗證:Windows 與 Linux 的 OS keychain 持久化(macOS 已於 2026-10-20 驗證)、真實 Anthropic provider、其他 provider 的長對話、長時間使用([#26](https://github.com/jwcrystal/leakshield/issues/26))。
42
42
  - `0.x` 期間預設值仍可能調整,變更見 [CHANGELOG](CHANGELOG.md)。
43
+ - 後續方向見 [Roadmap](docs/ROADMAP.md),區分優先工作、候選與研究,不代表目前支援範圍。
43
44
  - 驗證紀錄:[docs/evidence](docs/evidence/README.md)。漏洞請私下回報:[SECURITY.md](SECURITY.md)。授權:[Apache-2.0](LICENSE)。
44
45
 
45
46
  ---
@@ -195,7 +196,7 @@ npm run build
195
196
 
196
197
  ### 保護範圍與不保護的部分
197
198
 
198
- LeakShield 保護送往模型的對話內容:prompt、重播的歷史、工具輸出,以及可解析其結構的 provider 請求 body。各宿主涵蓋的程度不同,`leakshield --doctor <host>` 與[宿主能力矩陣](docs/host-capabilities.md)會列出來。它**不是**工具對外連線的防火牆:被允許的本機工具仍可透過 `curl`、遠端 MCP server 或任何其他管道把原文傳出去。要管控工具的目的地,需要 LeakShield 之外的權限層。從 0.3.0 起,`curl` 與 `http` 的參數內也可以放 placeholder,讓工具帶著受保護的值呼叫 API;這時原文會依設計離開本機。
199
+ LeakShield 保護送往模型的對話內容:prompt、重播的歷史、工具輸出,以及可解析其結構的 provider 請求 body。各宿主涵蓋的程度不同,`leakshield --doctor <host>` 與[宿主能力矩陣](docs/host-capabilities.md)會列出來。它**不是**工具對外連線的防火牆:被允許的本機工具仍可透過 `curl`、遠端 MCP server 或任何其他管道把原文傳出去。要管控工具的目的地,需要 LeakShield 之外的權限層。從 0.3.0 起,`curl` 與 `http` 的參數內也可以放 placeholder,讓工具帶著受保護的值呼叫 API;這時原文會依設計離開本機。宿主或其相依套件自行送出的流量同樣管不到,例如 telemetry、使用分析與錯誤或 crash report:這些流量不經過模型邊界,LeakShield 看不到,即使 provider 採零資料保留(ZDR)也不例外。要限制這類流量,得靠宿主本身的設定,或 OS 層級的沙箱、防火牆。
199
200
 
200
201
  偵測也有已知缺口:街道/郵遞地址、日期、IPv6,以及關鍵字清單之外或短於 8 字元的一般賦值都不會被偵測。信用卡與 IBAN 只有在檢查碼驗證通過時才會命中;IP 偵測只涵蓋公開 IPv4(超過四段的點分數字會整段回報,避免內嵌位址漏遮;`2026.10.21.14.32.59` 這類點分日期也會回報);護照只讀 MRZ 那一行;美國 SSN、台灣統一編號、電話與 URL 偵測需手動開啟,見[偵測範圍](#偵測範圍)。
201
202
 
@@ -20,6 +20,10 @@
20
20
  * - Transport metadata is inspected: decoded non-auth headers and query
21
21
  * parameters, and numeric body values, must not scan as sensitive;
22
22
  * legitimate authentication headers are preserved.
23
+ * - A declared request body coding (gzip, deflate, br, zstd) is decoded before
24
+ * scanning, under the same byte cap; an unknown or undecodable coding
25
+ * refuses rather than being scanned as text. A decoded request is forwarded
26
+ * as plain JSON without the stale coding and length headers.
23
27
  * - The whole body is validated BEFORE any token is issued, then protected in
24
28
  * ONE atomic transaction, so a rejected request commits no mapping or
25
29
  * session capacity.
@@ -102,7 +106,8 @@ export declare function withOpaqueAttachments<T extends Record<string, unknown>>
102
106
  * whole body protected in a SINGLE transaction, so a rejected request commits
103
107
  * no mapping or session capacity. The original request is consumed only through
104
108
  * `clone()`; the ORIGINAL request object is returned only when its body already
105
- * is the canonical serialization of what was scanned. A rebuilt request inherits every transport control (method, url,
109
+ * is the canonical serialization of what was scanned and was not content-encoded.
110
+ * A rebuilt request inherits every transport control (method, url,
106
111
  * mode, credentials, cache, redirect, referrer, integrity, keepalive, signal,
107
112
  * authentication headers) and recomputes body headers.
108
113
  */
package/dist/core/wire.js CHANGED
@@ -20,6 +20,10 @@
20
20
  * - Transport metadata is inspected: decoded non-auth headers and query
21
21
  * parameters, and numeric body values, must not scan as sensitive;
22
22
  * legitimate authentication headers are preserved.
23
+ * - A declared request body coding (gzip, deflate, br, zstd) is decoded before
24
+ * scanning, under the same byte cap; an unknown or undecodable coding
25
+ * refuses rather than being scanned as text. A decoded request is forwarded
26
+ * as plain JSON without the stale coding and length headers.
23
27
  * - The whole body is validated BEFORE any token is issued, then protected in
24
28
  * ONE atomic transaction, so a rejected request commits no mapping or
25
29
  * session capacity.
@@ -191,6 +195,86 @@ async function readCappedText(source, limit, failure) {
191
195
  throw new Error(failure);
192
196
  return out + tail;
193
197
  }
198
+ /** Request-body codings this module can decode before scanning. A request
199
+ * declaring anything else is refused: compressed bytes are not scannable as
200
+ * text, and guessing their meaning would hide content from the scanner. */
201
+ const REQUEST_BODY_ENCODINGS = new Set(["gzip", "deflate", "br", "zstd"]);
202
+ /** zlib method per coding. zstd exists only on hosts built with it; a missing
203
+ * decoder refuses instead of forwarding an uninspected body. */
204
+ const REQUEST_BODY_DECODERS = {
205
+ gzip: "gunzip",
206
+ deflate: "inflate",
207
+ br: "brotliDecompress",
208
+ zstd: "zstdDecompress",
209
+ };
210
+ /** Reads the raw bytes of a cloned body, capped before accumulation. */
211
+ async function readCappedBytes(source, limit, failure) {
212
+ const clone = source.clone();
213
+ if (clone.body === null)
214
+ return new Uint8Array(0);
215
+ const reader = clone.body.getReader();
216
+ const chunks = [];
217
+ let size = 0;
218
+ for (;;) {
219
+ let step;
220
+ try {
221
+ step = await reader.read();
222
+ }
223
+ catch {
224
+ throw new Error(failure);
225
+ }
226
+ if (step.done)
227
+ break;
228
+ size += step.value.byteLength;
229
+ if (size > limit) {
230
+ void reader.cancel().catch(() => { });
231
+ throw new Error(failure);
232
+ }
233
+ chunks.push(step.value);
234
+ }
235
+ const out = new Uint8Array(size);
236
+ let offset = 0;
237
+ for (const chunk of chunks) {
238
+ out.set(chunk, offset);
239
+ offset += chunk.byteLength;
240
+ }
241
+ return out;
242
+ }
243
+ /** Decodes a declared request coding under the scanner's byte cap. zlib
244
+ * enforces the cap while inflating, so a small body cannot expand past it. */
245
+ async function decodeRequestBody(bytes, encoding) {
246
+ const zlib = (await import("node:zlib"));
247
+ const decompress = zlib[REQUEST_BODY_DECODERS[encoding]];
248
+ if (typeof decompress !== "function")
249
+ throw new Error(refusal("body-encoding"));
250
+ const decoded = await new Promise((resolve, reject) => {
251
+ decompress(bytes, { maxOutputLength: MAX_BODY_BYTES }, (error, result) => {
252
+ if (error)
253
+ reject(error);
254
+ else
255
+ resolve(result);
256
+ });
257
+ }).catch((error) => {
258
+ const code = error?.code;
259
+ throw new Error(refusal(code === "ERR_BUFFER_TOO_LARGE" ? "body-cap" : "body-encoding"));
260
+ });
261
+ return new TextDecoder().decode(decoded);
262
+ }
263
+ /** Reads a request body as text, decoding a declared content-coding. `decoded`
264
+ * tells the caller the original compressed Request cannot be the forwarded
265
+ * one, even when the scanned serialization equals the text. */
266
+ async function readRequestBody(request) {
267
+ const encoding = (request.headers.get("content-encoding") ?? "").trim().toLowerCase();
268
+ if (encoding === "" || encoding === "identity") {
269
+ return { text: await readCappedText(request, MAX_BODY_BYTES, refusal("body-cap")), decoded: false };
270
+ }
271
+ if (!REQUEST_BODY_ENCODINGS.has(encoding))
272
+ throw new Error(refusal("body-encoding"));
273
+ const bytes = await readCappedBytes(request, MAX_BODY_BYTES, refusal("body-cap"));
274
+ if (bytes.byteLength === 0)
275
+ return { text: "", decoded: true };
276
+ return { text: await decodeRequestBody(bytes, encoding), decoded: true };
277
+ }
194
278
  /** Visits every numeric value in a strict-JSON tree. */
195
279
  function walkNumbers(value, visit) {
196
280
  if (typeof value === "number") {
@@ -606,12 +690,14 @@ function collectWireStructuralStrings(body) {
606
690
  return out;
607
691
  }
608
692
  /** Forwards exactly the value that was scanned. The original bytes are kept
609
- * only when they already are that serialization: a body that parses to
610
- * something else (a duplicate key keeps only its last occurrence, invalid
611
- * UTF-8 decodes to U+FFFD) would otherwise carry content the scanner never
612
- * saw. Keeping the original Request otherwise preserves its identity. */
613
- function forwardSerialized(request, raw, serialized) {
614
- if (serialized === raw && !raw.includes("\uFFFD"))
693
+ * only when they already are that serialization and are not content-encoded: a
694
+ * body that parses to something else (a duplicate key keeps only its last
695
+ * occurrence, invalid UTF-8 decodes to U+FFFD) would otherwise carry content
696
+ * the scanner never saw. A decoded body is always rebuilt plain, because the
697
+ * original bytes are compressed and no longer match what was scanned. Keeping
698
+ * the original Request otherwise preserves its identity. */
699
+ function forwardSerialized(request, raw, serialized, decoded = false) {
700
+ if (!decoded && serialized === raw && !raw.includes("\uFFFD"))
615
701
  return request;
616
702
  const headers = new Headers(request.headers);
617
703
  headers.delete("content-encoding");
@@ -628,7 +714,8 @@ function forwardSerialized(request, raw, serialized) {
628
714
  * whole body protected in a SINGLE transaction, so a rejected request commits
629
715
  * no mapping or session capacity. The original request is consumed only through
630
716
  * `clone()`; the ORIGINAL request object is returned only when its body already
631
- * is the canonical serialization of what was scanned. A rebuilt request inherits every transport control (method, url,
717
+ * is the canonical serialization of what was scanned and was not content-encoded.
718
+ * A rebuilt request inherits every transport control (method, url,
632
719
  * mode, credentials, cache, redirect, referrer, integrity, keepalive, signal,
633
720
  * authentication headers) and recomputes body headers.
634
721
  */
@@ -648,7 +735,7 @@ export async function remaskChatCompletionsRequest(service, sessionID, request)
648
735
  // for arbitrary content, so a bodyless request has no verifiable boundary.
649
736
  if (request.body === null)
650
737
  throw new Error(refusal("body-missing"));
651
- const raw = await readCappedText(request, MAX_BODY_BYTES, refusal("body-cap"));
738
+ const { text: raw, decoded } = await readRequestBody(request);
652
739
  if (raw.length === 0)
653
740
  throw new Error(refusal("body-empty"));
654
741
  let body;
@@ -755,7 +842,7 @@ export async function remaskChatCompletionsRequest(service, sessionID, request)
755
842
  }
756
843
  }
757
844
  const serialized = JSON.stringify(next);
758
- return forwardSerialized(request, raw, serialized);
845
+ return forwardSerialized(request, raw, serialized, decoded);
759
846
  }
760
847
  // ─── Anthropic Messages request (wire dialect) ──────────────────────────────
761
848
  /**
@@ -981,7 +1068,7 @@ export async function remaskAnthropicMessagesRequest(service, sessionID, request
981
1068
  inspectTransportMetadata(service, sessionID, request);
982
1069
  if (request.body === null)
983
1070
  throw new Error(refusal("body-missing"));
984
- const raw = await readCappedText(request, MAX_BODY_BYTES, refusal("body-cap"));
1071
+ const { text: raw, decoded } = await readRequestBody(request);
985
1072
  if (raw.length === 0)
986
1073
  throw new Error(refusal("body-empty"));
987
1074
  let body;
@@ -1024,7 +1111,7 @@ export async function remaskAnthropicMessagesRequest(service, sessionID, request
1024
1111
  throw new Error(refusal("structural-drift"));
1025
1112
  }
1026
1113
  const serialized = JSON.stringify(next);
1027
- return forwardSerialized(request, raw, serialized);
1114
+ return forwardSerialized(request, raw, serialized, decoded);
1028
1115
  }
1029
1116
  // ─── OpenAI Responses request (wire dialect) ────────────────────────────────
1030
1117
  /**
@@ -1219,7 +1306,7 @@ export async function remaskResponsesRequest(service, sessionID, request) {
1219
1306
  inspectTransportMetadata(service, sessionID, request);
1220
1307
  if (request.body === null)
1221
1308
  throw new Error(refusal("body-missing"));
1222
- const raw = await readCappedText(request, MAX_BODY_BYTES, refusal("body-cap"));
1309
+ const { text: raw, decoded } = await readRequestBody(request);
1223
1310
  if (raw.length === 0)
1224
1311
  throw new Error(refusal("body-empty"));
1225
1312
  let body;
@@ -1285,7 +1372,7 @@ export async function remaskResponsesRequest(service, sessionID, request) {
1285
1372
  }
1286
1373
  }
1287
1374
  const serialized = JSON.stringify(next);
1288
- return forwardSerialized(request, raw, serialized);
1375
+ return forwardSerialized(request, raw, serialized, decoded);
1289
1376
  }
1290
1377
  function isRecordArgumentsString(item) {
1291
1378
  return typeof item.arguments === "string";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leakshield",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Masks sensitive content before it reaches an AI model: OpenCode plugin, pi extension, Claude Code proxy, CLI, and web scanner",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Jared Wang",