From 907fcea641490d6a443a46e4b2fce9e4934870f0 Mon Sep 17 00:00:00 2001 From: aftabkh4n Date: Sun, 8 Nov 2026 09:44:31 +0000 Subject: [PATCH] fix(log-mask): scrub JSON-quoted credentials and Groq/Hugging Face keys redactSecretsInText runs on every log line and, through redactForPublish, on the diagnostics blob that users paste into public GitHub issues. Its key=value rule required ":" or "=" right after the key name, so the JSON/dict shape that upstream error bodies and config dumps use - "api_key": "...", "password": "...", 'access_token': '...' - passed through verbatim unless the value happened to start with a known prefix such as sk-. The existing JSON test case only passed for that reason. Allow an optional quote between the key name and the separator, and mask gsk_ (Groq) and hf_ (Hugging Face) keys like sk- ones; both providers are on PUBLIC_HOST_SUFFIXES. Usage-count keys ("prompt_tokens", "tokens", "max_tokens") and short values stay verbatim, and a second pass over redacted text is still a no-op. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014TonJ1geSd97oAtSBZofUK --- CHANGELOG.md | 1 + src/log-mask.ts | 6 +++++- tests/log-mask.test.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c57a4af5e4..ae79e02012 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag - **系统架构文档体系落地(owner 指令 2026-11-08):`docs/architecture/` 成为实时维护的架构枢纽**:新增 `docs/architecture.md` / `docs/architecture.zh-CN.md` 总枢纽(整体架构图中英双版转正 + 8 模块总表),首个样板模块页 `docs/architecture/storage.{md,zh-CN.md}`(统一存储:职责/原理/工作机制/硬不变量,配模块图中英双版)。文件级地图 `reference/architecture.md` 补齐 125 个缺失条目(新存储层 / marker / fastpath / lanes 等全部新模块)并升级为 CI 强制全量覆盖。新增 CI 车道 `architecture-docs`(`tools/check-architecture-docs.mjs`):地图覆盖全 `src/` 文件、总表↔页面成对、图 svg 优先 + png 备选齐备、SVG 基础良构(未转义 `&` 曾让英文图线上破图)。AGENTS.md「Documentation Layout」新增实时维护规则:改结构 ⇒ 同 PR 更新页 + 图 + 总表。其余 7 个模块页(pipeline / lanes / kernel / identity / fastpaths / webui / update)同一 PR 全部补齐——总表 8 行全 ✅(每页 = 中英双版文档 + 中英双版图)。 ### Fixed +- **The log/publish secret scrubber now catches JSON-quoted credentials and Groq/Hugging Face keys.** `redactSecretsInText` (every log line, and via `redactForPublish` the diagnostics blob that lands in public GitHub issues) required `:`/`=` right after the key name, so the JSON/dict shape upstream error bodies and config dumps use — `"api_key": "…"`, `"password": "…"`, `'access_token': '…'` — slipped through unless the value happened to start with a known prefix such as `sk-`. An optional quote after the key name now matches it; `gsk_…` (Groq) and `hf_…` (Hugging Face) keys, both on the public-host list, are masked like `sk-…`. Usage-count keys (`"prompt_tokens"`, `"tokens"`, `"max_tokens"`) stay verbatim. Regression: `tests/log-mask.test.ts`. - **dsh 闸门:必然被上游拒绝的超窗请求不再裸发直连(#5613 安全网 ①②③)**:`none`/`threw` 归属车道(如经网关的 ds-harness-remote)上的超窗历史请求,旧行为二选一——要么按乐观的 bytes/4 估算判「不超窗」而直接裸发上游(实测计费偏差≈×1.14,估算系统性偏低;真实现场把 max_tokens 挤到 1、乃至上游 400 后整会话报废),要么即便超窗也没有任何持续留痕(只记第一行之后全静默)。现在双档计价:原有 bytes/4 下限继续标记「必然超窗」,另加 ×1.14 校准估算档(会话 ledger 实测偏差,常数 `CALIBRATED_TEXT_TOKEN_RATIO`,src/util.ts),过线即本地 422 拒绝而不是骑 #1117 去赌上游——小体量的合法直通语义(sidecar 短调用等)逐字节保留,未知尺寸流式体保持 fail-open。每次裸发 DIRECT 现在都带体积(`this body N B`)或「尺寸未知、超窗熔断未跑」明示,按 首次/状态变化/每第10次 落 durable 日志(bili.log,[dsh-client] 通道)——连续泄漏环路从此必有痕。tests/dsh-native.test.ts:#2924/#4068 双臂升级为双档断言+校准边界钉(3,508,768 B 直通 / 3,508,772 B 拒)、新增留痕节拍回归、#4111/#4912 谓词改名对齐。 - **全量压缩与 persona 文本漂移同轮发生时,主会话不再分叉到 `|sub:` 副车道(#5719)**:dsh 全量压缩 checkpoint(#3733 authored checkpoint,把整段历史头替换成一条 user 消息)与系统提示漂移(如 dsh 重启后 GUI 端口重分配写进 system prompt)落在同一轮时,既有的全部位置型 re-anchor 逃生结构性不可达——被记录的尾部按定义已被抹掉、incoming 比记录更短、压缩后首个模型步零 assistant——于是活着的会话确定性地分叉到 `|sub:`:父车道记录冻结、副车道从零起步(resume-inheritance miss)、事故当天又触发 3 次再压缩 + 1 次 REFUSED turn + 2 次 max-tokens 截断。根因=判定器要求一种全量压缩按定义抹掉的字节级续接证据,而唯一能存活压缩的自足证明——bili 自己铸造的 checkpoint 内嵌的 `conv=""` 自声明——从未被读取。现在 tagChanged 分支最前新增 E5 级:**第一条** user 消息携带我方 `\x3cbili-snapshot v1 …\x3e` 身份头且其 conv 与当前 id 逐字相等 → 直接 re-anchor 回主车道(record() 同步更新 tagHash,后续请求即普通 continue)。外部 persona 引用 checkpoint 的情形照旧分叉:要么根本到不了判定层(sideRequestLike 门,#4438),要么标记 conv 不符 / 不在第一条 user 消息。**旧行为→新行为**:该形状从「必然分叉」变为「带自声明时 re-anchor 回主车道」;无标记的形状逐字节不变(回归测试钉住每一既有 rung 的 reason 文案)。tests/issue5719-self-checkpoint-reanchor.test.ts(验收标准 1/2/3 + 四种 wire 形状 + foreign-quote 边界)。 diff --git a/src/log-mask.ts b/src/log-mask.ts index 6e41c3a6e6..efef437a85 100644 --- a/src/log-mask.ts +++ b/src/log-mask.ts @@ -354,7 +354,11 @@ export function redactSecretsInText(text: string): string { return text .replace(/\b(bearer|basic)\s+([A-Za-z0-9._~+\/=-]{8,})/gi, (_m, scheme: string, tok: string) => `${scheme} `) .replace(/(?") + // Groq (gsk_…) and Hugging Face (hf_…) keys, both on PUBLIC_HOST_SUFFIXES. + .replace(/(?") .replace(/(?") .replace(/eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}/g, "") - .replace(/(\b(?:api[_-]?key|access[_-]?token|auth(?:orization)?|secret|passwd|password|token)\b\s*[:=]\s*)(["']?)([A-Za-z0-9._~+\/=-]{8,})\2/gi, (_m, pre: string, q: string, val: string) => `${pre}${q}${q}`); + // The optional quote after the name covers JSON/dict keys ("api_key": "…"), the shape + // upstream error bodies and config dumps use; without it only key=value / key: value matched. + .replace(/(\b(?:api[_-]?key|access[_-]?token|auth(?:orization)?|secret|passwd|password|token)\b["']?\s*[:=]\s*)(["']?)([A-Za-z0-9._~+\/=-]{8,})\2/gi, (_m, pre: string, q: string, val: string) => `${pre}${q}${q}`); } diff --git a/tests/log-mask.test.ts b/tests/log-mask.test.ts index 8134537274..dd2b1c666b 100644 --- a/tests/log-mask.test.ts +++ b/tests/log-mask.test.ts @@ -702,6 +702,39 @@ test("redactSecretsInText: scrubs credential-shaped tokens from free-form text ( for (const s of negatives) assert.equal(redactSecretsInText(s), s, `must stay verbatim: ${s}`); }); +test("redactSecretsInText: JSON-quoted credential keys and Groq/Hugging Face keys are scrubbed", () => { + // A quote between the key name and the colon ("api_key": "…") used to stop the key=value + // rule, so a value without a known key prefix reached the log and the diagnostics blob. + assert.equal( + redactSecretsInText('{"error":{"message":"invalid","api_key":"0123456789abcdef0123"}}'), + '{"error":{"message":"invalid","api_key":""}}', + ); + assert.equal( + redactSecretsInText('{"user":"alice","password":"hunter2hunter2"}'), + '{"user":"alice","password":""}', + ); + assert.equal( + redactSecretsInText("{'access_token': 'ya29.a0AfH6SMBx3kL9ZqWv'}"), + "{'access_token': ''}", + ); + assert.equal( + redactSecretsInText("Invalid API Key: gsk_8fJ2kLmN4pQrS7tUvW9xYz1aBcDeFgHiJkLmNoPqRsTuVwXyZ012"), + "Invalid API Key: ", + ); + assert.equal(redactSecretsInText("auth failed for hf_AbCdEfGhIjKlMnOpQrStUvWxYz12345678"), "auth failed for "); + const negatives = [ + '{"prompt_tokens":1234,"completion_tokens":5678}', + '{"tokens": 1234}', + '{"token":"abc"}', + '{"max_tokens":4096}', + "hf_hub_download failed", + "gsk_short", + ]; + for (const s of negatives) assert.equal(redactSecretsInText(s), s, `must stay verbatim: ${s}`); + const once = redactSecretsInText('{"api_key":"0123456789abcdef0123"}'); + assert.equal(redactSecretsInText(once), once, "a second pass is a no-op"); +}); + test("maskIpsInText: non-loopback IP literals scrubbed from free-form text (#1718)", () => { assert.equal(maskIpsInText("connect ETIMEDOUT 203.0.113.5:8443"), "connect ETIMEDOUT :8443"); assert.equal(maskIpsInText("upstream refused 192.168.1.50:443 after 3 retries"), "upstream refused :443 after 3 retries");