dsh-config-manager 0.1.0

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.
Files changed (253) hide show
  1. package/README.md +216 -0
  2. package/README.zh-CN.md +218 -0
  3. package/cordis.patch.yml +45 -0
  4. package/dsh.bundle.patch +21 -0
  5. package/dsh.client +39 -0
  6. package/lib/adapters/agent-presets.d.ts +13 -0
  7. package/lib/adapters/agent-presets.js +14 -0
  8. package/lib/adapters/agent-presets.js.map +1 -0
  9. package/lib/adapters/credentials.d.ts +36 -0
  10. package/lib/adapters/credentials.js +121 -0
  11. package/lib/adapters/credentials.js.map +1 -0
  12. package/lib/adapters/file-collection.d.ts +14 -0
  13. package/lib/adapters/file-collection.js +102 -0
  14. package/lib/adapters/file-collection.js.map +1 -0
  15. package/lib/adapters/index.d.ts +38 -0
  16. package/lib/adapters/index.js +46 -0
  17. package/lib/adapters/index.js.map +1 -0
  18. package/lib/adapters/mcp.d.ts +27 -0
  19. package/lib/adapters/mcp.js +175 -0
  20. package/lib/adapters/mcp.js.map +1 -0
  21. package/lib/adapters/plugin-files.d.ts +15 -0
  22. package/lib/adapters/plugin-files.js +91 -0
  23. package/lib/adapters/plugin-files.js.map +1 -0
  24. package/lib/adapters/plugins.d.ts +13 -0
  25. package/lib/adapters/plugins.js +177 -0
  26. package/lib/adapters/plugins.js.map +1 -0
  27. package/lib/adapters/prompts.d.ts +29 -0
  28. package/lib/adapters/prompts.js +228 -0
  29. package/lib/adapters/prompts.js.map +1 -0
  30. package/lib/adapters/providers.d.ts +33 -0
  31. package/lib/adapters/providers.js +143 -0
  32. package/lib/adapters/providers.js.map +1 -0
  33. package/lib/adapters/sessions.d.ts +14 -0
  34. package/lib/adapters/sessions.js +15 -0
  35. package/lib/adapters/sessions.js.map +1 -0
  36. package/lib/adapters/settings.d.ts +23 -0
  37. package/lib/adapters/settings.js +142 -0
  38. package/lib/adapters/settings.js.map +1 -0
  39. package/lib/adapters/skills.d.ts +13 -0
  40. package/lib/adapters/skills.js +14 -0
  41. package/lib/adapters/skills.js.map +1 -0
  42. package/lib/adapters/test-helpers.d.ts +99 -0
  43. package/lib/adapters/test-helpers.js +192 -0
  44. package/lib/adapters/test-helpers.js.map +1 -0
  45. package/lib/adapters/ui.d.ts +26 -0
  46. package/lib/adapters/ui.js +88 -0
  47. package/lib/adapters/ui.js.map +1 -0
  48. package/lib/adapters/workspaces.d.ts +12 -0
  49. package/lib/adapters/workspaces.js +92 -0
  50. package/lib/adapters/workspaces.js.map +1 -0
  51. package/lib/client.d.ts +377 -0
  52. package/lib/client.js +2598 -0
  53. package/lib/client.js.map +1 -0
  54. package/lib/core/analyzer.d.ts +35 -0
  55. package/lib/core/analyzer.js +563 -0
  56. package/lib/core/analyzer.js.map +1 -0
  57. package/lib/core/backup.d.ts +29 -0
  58. package/lib/core/backup.js +184 -0
  59. package/lib/core/backup.js.map +1 -0
  60. package/lib/core/exporter.d.ts +37 -0
  61. package/lib/core/exporter.js +222 -0
  62. package/lib/core/exporter.js.map +1 -0
  63. package/lib/core/importer.d.ts +42 -0
  64. package/lib/core/importer.js +25 -0
  65. package/lib/core/importer.js.map +1 -0
  66. package/lib/core/index.d.ts +12 -0
  67. package/lib/core/index.js +11 -0
  68. package/lib/core/index.js.map +1 -0
  69. package/lib/core/rollback.d.ts +9 -0
  70. package/lib/core/rollback.js +136 -0
  71. package/lib/core/rollback.js.map +1 -0
  72. package/lib/core/types.d.ts +331 -0
  73. package/lib/core/types.js +18 -0
  74. package/lib/core/types.js.map +1 -0
  75. package/lib/core/validator.d.ts +18 -0
  76. package/lib/core/validator.js +77 -0
  77. package/lib/core/validator.js.map +1 -0
  78. package/lib/index.d.ts +51 -0
  79. package/lib/index.js +875 -0
  80. package/lib/index.js.map +1 -0
  81. package/lib/migrations/index.d.ts +29 -0
  82. package/lib/migrations/index.js +56 -0
  83. package/lib/migrations/index.js.map +1 -0
  84. package/lib/migrations/v1-to-v2.d.ts +9 -0
  85. package/lib/migrations/v1-to-v2.js +10 -0
  86. package/lib/migrations/v1-to-v2.js.map +1 -0
  87. package/lib/profiles/index.d.ts +4 -0
  88. package/lib/profiles/index.js +5 -0
  89. package/lib/profiles/index.js.map +1 -0
  90. package/lib/profiles/profile-manager.d.ts +102 -0
  91. package/lib/profiles/profile-manager.js +522 -0
  92. package/lib/profiles/profile-manager.js.map +1 -0
  93. package/lib/schema/config.d.ts +28 -0
  94. package/lib/schema/config.js +136 -0
  95. package/lib/schema/config.js.map +1 -0
  96. package/lib/schema/manifest.d.ts +28 -0
  97. package/lib/schema/manifest.js +109 -0
  98. package/lib/schema/manifest.js.map +1 -0
  99. package/lib/schema/types.d.ts +169 -0
  100. package/lib/schema/types.js +8 -0
  101. package/lib/schema/types.js.map +1 -0
  102. package/lib/schema/versions.d.ts +27 -0
  103. package/lib/schema/versions.js +50 -0
  104. package/lib/schema/versions.js.map +1 -0
  105. package/lib/security/encryption.d.ts +41 -0
  106. package/lib/security/encryption.js +152 -0
  107. package/lib/security/encryption.js.map +1 -0
  108. package/lib/security/index.d.ts +16 -0
  109. package/lib/security/index.js +17 -0
  110. package/lib/security/index.js.map +1 -0
  111. package/lib/security/integrity.d.ts +30 -0
  112. package/lib/security/integrity.js +78 -0
  113. package/lib/security/integrity.js.map +1 -0
  114. package/lib/security/redaction.d.ts +26 -0
  115. package/lib/security/redaction.js +95 -0
  116. package/lib/security/redaction.js.map +1 -0
  117. package/lib/security/secret-scanner.d.ts +48 -0
  118. package/lib/security/secret-scanner.js +253 -0
  119. package/lib/security/secret-scanner.js.map +1 -0
  120. package/lib/security/zip-security.d.ts +27 -0
  121. package/lib/security/zip-security.js +154 -0
  122. package/lib/security/zip-security.js.map +1 -0
  123. package/lib/ui/conflict-view.d.ts +28 -0
  124. package/lib/ui/conflict-view.js +49 -0
  125. package/lib/ui/conflict-view.js.map +1 -0
  126. package/lib/ui/errors.d.ts +17 -0
  127. package/lib/ui/errors.js +76 -0
  128. package/lib/ui/errors.js.map +1 -0
  129. package/lib/ui/export-flow.d.ts +62 -0
  130. package/lib/ui/export-flow.js +79 -0
  131. package/lib/ui/export-flow.js.map +1 -0
  132. package/lib/ui/import-wizard.d.ts +67 -0
  133. package/lib/ui/import-wizard.js +169 -0
  134. package/lib/ui/import-wizard.js.map +1 -0
  135. package/lib/ui/path-mapping.d.ts +38 -0
  136. package/lib/ui/path-mapping.js +75 -0
  137. package/lib/ui/path-mapping.js.map +1 -0
  138. package/lib/ui/progress.d.ts +27 -0
  139. package/lib/ui/progress.js +76 -0
  140. package/lib/ui/progress.js.map +1 -0
  141. package/lib/ui/report.d.ts +21 -0
  142. package/lib/ui/report.js +146 -0
  143. package/lib/ui/report.js.map +1 -0
  144. package/lib/ui/test-helpers.d.ts +62 -0
  145. package/lib/ui/test-helpers.js +147 -0
  146. package/lib/ui/test-helpers.js.map +1 -0
  147. package/lib/ui/types.d.ts +129 -0
  148. package/lib/ui/types.js +21 -0
  149. package/lib/ui/types.js.map +1 -0
  150. package/lib/utils/hashing.d.ts +17 -0
  151. package/lib/utils/hashing.js +41 -0
  152. package/lib/utils/hashing.js.map +1 -0
  153. package/lib/utils/json.d.ts +24 -0
  154. package/lib/utils/json.js +76 -0
  155. package/lib/utils/json.js.map +1 -0
  156. package/lib/utils/logger.d.ts +39 -0
  157. package/lib/utils/logger.js +82 -0
  158. package/lib/utils/logger.js.map +1 -0
  159. package/lib/utils/paths.d.ts +31 -0
  160. package/lib/utils/paths.js +132 -0
  161. package/lib/utils/paths.js.map +1 -0
  162. package/lib/utils/zip.d.ts +54 -0
  163. package/lib/utils/zip.js +285 -0
  164. package/lib/utils/zip.js.map +1 -0
  165. package/package.json +109 -0
  166. package/src/adapters/agent-presets.ts +14 -0
  167. package/src/adapters/credentials.test.ts +90 -0
  168. package/src/adapters/credentials.ts +141 -0
  169. package/src/adapters/file-collection.ts +110 -0
  170. package/src/adapters/files.test.ts +113 -0
  171. package/src/adapters/index.ts +69 -0
  172. package/src/adapters/mcp.test.ts +96 -0
  173. package/src/adapters/mcp.ts +181 -0
  174. package/src/adapters/plugin-files.ts +96 -0
  175. package/src/adapters/plugins.test.ts +72 -0
  176. package/src/adapters/plugins.ts +176 -0
  177. package/src/adapters/prompts.test.ts +110 -0
  178. package/src/adapters/prompts.ts +226 -0
  179. package/src/adapters/providers.test.ts +125 -0
  180. package/src/adapters/providers.ts +161 -0
  181. package/src/adapters/roundtrip.test.ts +188 -0
  182. package/src/adapters/sessions.ts +15 -0
  183. package/src/adapters/settings.test.ts +93 -0
  184. package/src/adapters/settings.ts +165 -0
  185. package/src/adapters/skills.ts +14 -0
  186. package/src/adapters/test-helpers.ts +207 -0
  187. package/src/adapters/ui.test.ts +56 -0
  188. package/src/adapters/ui.ts +106 -0
  189. package/src/adapters/workspaces.test.ts +79 -0
  190. package/src/adapters/workspaces.ts +95 -0
  191. package/src/client/CLIENT_DEPENDENCIES.md +101 -0
  192. package/src/client/ConfigManagerSection.tsx +60 -0
  193. package/src/client/api.ts +270 -0
  194. package/src/client/client-types.ts +27 -0
  195. package/src/client/common/ErrorBanner.tsx +68 -0
  196. package/src/client/common/ProgressBar.tsx +49 -0
  197. package/src/client/common/ReportView.tsx +117 -0
  198. package/src/client/common/ui.tsx +165 -0
  199. package/src/client/config-manager.module.css +759 -0
  200. package/src/client/css-modules.d.ts +8 -0
  201. package/src/client/export/ExportView.tsx +222 -0
  202. package/src/client/import/ConflictList.tsx +72 -0
  203. package/src/client/import/ImportWizardView.tsx +406 -0
  204. package/src/client/import/PathMappingForm.tsx +77 -0
  205. package/src/client/index.ts +65 -0
  206. package/src/client/locales.ts +182 -0
  207. package/src/core/analyzer.ts +631 -0
  208. package/src/core/backup.ts +212 -0
  209. package/src/core/exporter.ts +246 -0
  210. package/src/core/importer.ts +66 -0
  211. package/src/core/index.ts +39 -0
  212. package/src/core/rollback.ts +140 -0
  213. package/src/core/smoke.test.ts +717 -0
  214. package/src/core/types.ts +343 -0
  215. package/src/core/validator.ts +84 -0
  216. package/src/index.ts +942 -0
  217. package/src/migrations/index.ts +71 -0
  218. package/src/migrations/v1-to-v2.ts +17 -0
  219. package/src/profiles/index.ts +8 -0
  220. package/src/profiles/profile-manager.test.ts +280 -0
  221. package/src/profiles/profile-manager.ts +606 -0
  222. package/src/schema/config.ts +137 -0
  223. package/src/schema/manifest.ts +124 -0
  224. package/src/schema/types.ts +179 -0
  225. package/src/schema/versions.ts +55 -0
  226. package/src/security/encryption.ts +180 -0
  227. package/src/security/index.ts +16 -0
  228. package/src/security/integrity.ts +102 -0
  229. package/src/security/redaction.ts +101 -0
  230. package/src/security/secret-scanner.ts +275 -0
  231. package/src/security/security.test.ts +796 -0
  232. package/src/security/zip-security.ts +180 -0
  233. package/src/ui/conflict-view.test.ts +62 -0
  234. package/src/ui/conflict-view.ts +65 -0
  235. package/src/ui/errors.test.ts +44 -0
  236. package/src/ui/errors.ts +95 -0
  237. package/src/ui/export-flow.test.ts +75 -0
  238. package/src/ui/export-flow.ts +128 -0
  239. package/src/ui/import-wizard.test.ts +128 -0
  240. package/src/ui/import-wizard.ts +208 -0
  241. package/src/ui/path-mapping.test.ts +67 -0
  242. package/src/ui/path-mapping.ts +103 -0
  243. package/src/ui/progress.test.ts +42 -0
  244. package/src/ui/progress.ts +90 -0
  245. package/src/ui/report.test.ts +93 -0
  246. package/src/ui/report.ts +140 -0
  247. package/src/ui/test-helpers.ts +177 -0
  248. package/src/ui/types.ts +193 -0
  249. package/src/utils/hashing.ts +53 -0
  250. package/src/utils/json.ts +77 -0
  251. package/src/utils/logger.ts +114 -0
  252. package/src/utils/paths.ts +122 -0
  253. package/src/utils/zip.ts +332 -0
@@ -0,0 +1,16 @@
1
+ /**
2
+ * 安全模块公共出口(m4-security):
3
+ * secret-scanner / encryption / integrity / zip-security / redaction。
4
+ *
5
+ * 与 core 的注入点对齐:
6
+ * - `createSecretScanner()` → ExporterOptions.scanner(SecretScanner 契约)
7
+ * - `createEncryptionProvider(pw)` → ExporterOptions.encryption(EncryptionProvider 契约)
8
+ * - `createHardenedZipParser()` → ImporterOptions.parseZipOverride((buf, limits?) => ZipArchive)
9
+ * - `safeExtractHardened()` → m5 导入文件类分区落盘通道
10
+ * - `verifyChecksumsJson()` → 完整性校验通道(core analyzer 已有内置校验,本模块供独立调用)
11
+ */
12
+ export * from './secret-scanner.ts';
13
+ export * from './encryption.ts';
14
+ export * from './integrity.ts';
15
+ export * from './zip-security.ts';
16
+ export * from './redaction.ts';
@@ -0,0 +1,102 @@
1
+ /**
2
+ * 配置完整性(规范 §18 / 设计 §9.7 / core utils/hashing.ts 的强化层)。
3
+ *
4
+ * checksums.json 实际格式由 core exporter 定死(`src/core/exporter.ts`):
5
+ * 扁平对象 `{ "<ZIP 内相对路径>": "<sha256 hex>" }`,覆盖除 manifest.json 与自身外的全部条目;
6
+ * core analyzer 导入时按此解析(`as Record<string, string>`)。本模块**对齐该格式**,不擅自改结构。
7
+ *
8
+ * 强化点(在 core hashing 之上):
9
+ * - 解析防线:结构校验(对象、值必须 64 位 hex)、条目数上限、拒绝 `__proto__`/`constructor`/`prototype`
10
+ * 危险键(防 prototype pollution);解析失败抛错 → 导入方归为「Backup integrity check failed」。
11
+ * - 一步到位 `verifyChecksumsJson`:解析 + 校验合并,供 m5/导入管线直接调用。
12
+ * - 校验语义:表内每个路径必须在条目集合中存在且 SHA-256 一致;缺失与不符均计入 mismatches。
13
+ */
14
+ import { parseJsonSafe } from '../utils/json.ts';
15
+ import {
16
+ buildChecksums as coreBuildChecksums,
17
+ verifyChecksums as coreVerifyChecksums,
18
+ } from '../utils/hashing.ts';
19
+ import type { ChecksumVerifyResult } from '../utils/hashing.ts';
20
+
21
+ export const CHECKSUMS_ALGORITHM = 'sha256' as const;
22
+ export const SHA256_HEX_RE = /^[0-9a-f]{64}$/i;
23
+ /** 与 core ZIP 条目数上限一致的默认表大小上限 */
24
+ export const DEFAULT_MAX_CHECKSUM_ENTRIES = 10_000;
25
+
26
+ /** 危险键(原型污染向量):解析时拒绝 */
27
+ const DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
28
+
29
+ export interface ChecksumsTableOptions {
30
+ maxEntries?: number;
31
+ }
32
+
33
+ /**
34
+ * 生成 checksums 表(对齐 core 扁平格式;内部复用 core buildChecksums)。
35
+ * entries.name 必须是 ZIP 内正斜杠相对路径。
36
+ */
37
+ export function buildChecksums(entries: { name: string; data: Uint8Array }[]): Record<string, string> {
38
+ return coreBuildChecksums(entries);
39
+ }
40
+
41
+ /**
42
+ * 解析并结构校验 checksums.json 原文。
43
+ * 非法结构 / 危险键 / 值非 SHA-256 hex / 条目数超限 → 抛错(不可信输入,宁可拒绝)。
44
+ */
45
+ export function parseChecksumsTable(
46
+ raw: string,
47
+ opts: ChecksumsTableOptions = {},
48
+ ): Record<string, string> {
49
+ const maxEntries = opts.maxEntries ?? DEFAULT_MAX_CHECKSUM_ENTRIES;
50
+ const parsed = parseJsonSafe(raw);
51
+ if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
52
+ throw new Error('checksums.json 必须是 JSON 对象');
53
+ }
54
+ const entries = Object.entries(parsed as Record<string, unknown>);
55
+ if (entries.length > maxEntries) {
56
+ throw new Error(`checksums.json 条目数 ${entries.length} 超过上限 ${maxEntries}`);
57
+ }
58
+ const table: Record<string, string> = {};
59
+ for (const [relPath, hash] of entries) {
60
+ if (DANGEROUS_KEYS.has(relPath)) {
61
+ throw new Error(`checksums.json 含危险键 "${relPath}",已拒绝`);
62
+ }
63
+ if (relPath === '' || relPath.includes('\\') || relPath.startsWith('/')) {
64
+ throw new Error(`checksums.json 含非法相对路径 "${relPath}"`);
65
+ }
66
+ if (typeof hash !== 'string' || !SHA256_HEX_RE.test(hash)) {
67
+ throw new Error(`checksums.json["${relPath}"] 不是合法的 SHA-256 hex`);
68
+ }
69
+ table[relPath] = hash;
70
+ }
71
+ return table;
72
+ }
73
+
74
+ /**
75
+ * 校验:表内每个路径必须在 entries 中存在且 SHA-256 一致;entries 多余文件(manifest/自身)不参与。
76
+ * 返回 { ok, mismatches, missing }(缺失与不符统一列入 mismatches 的展开视图,mismatches 只含不符)。
77
+ */
78
+ export function verifyChecksums(
79
+ entries: ReadonlyMap<string, Uint8Array>,
80
+ table: Record<string, string>,
81
+ ): ChecksumVerifyResult {
82
+ return coreVerifyChecksums(entries, table);
83
+ }
84
+
85
+ /** 一步到位:解析 checksums.json 原文并校验(解析失败抛错;不符返回 {ok:false,...}) */
86
+ export function verifyChecksumsJson(
87
+ entries: ReadonlyMap<string, Uint8Array>,
88
+ raw: string,
89
+ opts: ChecksumsTableOptions = {},
90
+ ): ChecksumVerifyResult {
91
+ const table = parseChecksumsTable(raw, opts);
92
+ return verifyChecksums(entries, table);
93
+ }
94
+
95
+ /** 人类可读的不匹配摘要(给「Backup integrity check failed」错误消息用) */
96
+ export function describeMismatches(result: ChecksumVerifyResult): string {
97
+ const parts = [
98
+ ...result.mismatches.map((m) => `"${m}" (hash 不符)`),
99
+ ...result.missing.map((m) => `"${m}" (缺失)`),
100
+ ];
101
+ return parts.join(', ');
102
+ }
@@ -0,0 +1,101 @@
1
+ /**
2
+ * 日志/文本脱敏(规范 §24 / 设计 §9.8 / core utils/logger.ts 的强化层)。
3
+ *
4
+ * 与 core logger.redact 的分工:core 版按固定字段名单做子串替换(不感知字段边界);
5
+ * 本模块为强化版,两模式:
6
+ * 1. **结构化字段形态**:JSON `"field": "value"` / `field=value` / `field: value`,
7
+ * 字段名命中敏感名单(复用 secret-scanner 的 `isSensitiveFieldName`,单一来源防漂移)→ 值替换。
8
+ * 2. **值形状模式**:与字段名无关,文本任意位置出现 sk- / JWT / AKIA / GitHub PAT /
9
+ * PEM 私钥 / Bearer / URL query 的敏感参数值 → 替换。
10
+ *
11
+ * 幂等性:替换产物 `***REDACTED***` 不匹配任何模式,重复 redact 结果不变;
12
+ * JSON 行的引号结构保留(输出仍是合法 JSON)。
13
+ */
14
+ import { DEFAULT_SECRET_FIELD_NAMES, isSensitiveFieldName } from './secret-scanner.ts';
15
+
16
+ export const REDACTED = '***REDACTED***';
17
+
18
+ /* ---------------- 结构化字段形态 ---------------- */
19
+
20
+ const JSON_FIELD_RE = /"([A-Za-z0-9_.\-]+)"\s*:\s*"([^"]*)"/g;
21
+ /** kv 值到空白/逗号/分号/&(& 截断:避免吞掉 URL query 的后续参数) */
22
+ const KV_FIELD_RE = /([A-Za-z0-9_.\-]+)\s*=\s*([^\s,;&]+)/g;
23
+ /** colon 值允许空格(如 `Authorization: Bearer xyz`),到逗号/分号/右大括号截断 */
24
+ const COLON_FIELD_RE = /([A-Za-z0-9_.\-]+)\s*:\s*([^,;}]+)/g;
25
+
26
+ /* ---------------- 值形状模式(包含式,字段名无关) ---------------- */
27
+
28
+ const VALUE_PATTERNS: { name: string; re: RegExp }[] = [
29
+ { name: 'openai-key', re: /sk-[A-Za-z0-9_-]{8,}/g },
30
+ { name: 'jwt', re: /eyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g },
31
+ { name: 'aws-access-key', re: /AKIA[0-9A-Z]{16}/g },
32
+ { name: 'github-token', re: /gh[pousr]_[A-Za-z0-9]{20,}/g },
33
+ { name: 'github-pat', re: /github_pat_[A-Za-z0-9_]{20,}/g },
34
+ { name: 'pem-private-key', re: /-----BEGIN [A-Za-z0-9 ]*PRIVATE KEY-----/g },
35
+ { name: 'bearer-token', re: /Bearer [A-Za-z0-9._~+/=-]{8,}/g },
36
+ ];
37
+
38
+ /** URL query 中的敏感参数值(保留参数名,只替换值) */
39
+ const URL_QUERY_RE = /([?&](?:token|api[_-]?key|key|secret|access[_-]?token|password|auth)=)([^&\s"']+)/gi;
40
+
41
+ /* ---------------- redact ---------------- */
42
+
43
+ function replaceSensitiveField(text: string, re: RegExp, blacklist: readonly string[]): string {
44
+ return text.replace(re, (match: string, field: string, value: string) => {
45
+ if (!isSensitiveFieldName(field, blacklist)) return match;
46
+ if (value === '') return match;
47
+ // 只替换值部分,保留原分隔符与引号形态(JSON `"f": "v"` / kv `f=v` / colon `f: v`)
48
+ const idx = match.lastIndexOf(value);
49
+ return match.slice(0, idx) + REDACTED + match.slice(idx + value.length);
50
+ });
51
+ }
52
+
53
+ /**
54
+ * 文本脱敏(幂等)。blacklist 为**附加**规范化字段名(小写无分隔符,如 'clientsecret')。
55
+ * 输出仍保留结构化形态(JSON 行合法)。
56
+ */
57
+ export function redact(text: string, blacklist: readonly string[] = []): string {
58
+ let out = text;
59
+ // 1. 结构化字段形态(顺序:JSON → kv → colon;JSON 键不会被后续形态二次命中)
60
+ out = replaceSensitiveField(out, JSON_FIELD_RE, blacklist);
61
+ out = replaceSensitiveField(out, KV_FIELD_RE, blacklist);
62
+ out = replaceSensitiveField(out, COLON_FIELD_RE, blacklist);
63
+ // 2. 值形状模式(字段名无关,包含式)
64
+ for (const { re } of VALUE_PATTERNS) {
65
+ re.lastIndex = 0;
66
+ out = out.replace(re, REDACTED);
67
+ }
68
+ out = out.replace(URL_QUERY_RE, `$1${REDACTED}`);
69
+ return out;
70
+ }
71
+
72
+ /** 对象级掩码:字段名命中敏感名单 → 整值替换(返回新对象,不改原对象;结构与类型保留) */
73
+ export function redactValue(value: unknown, blacklist: readonly string[] = []): unknown {
74
+ if (value === null || typeof value !== 'object') return value;
75
+ if (value instanceof Uint8Array) return value;
76
+ if (Array.isArray(value)) return value.map((v) => redactValue(v, blacklist));
77
+ const out: Record<string, unknown> = {};
78
+ for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
79
+ out[k] = isSensitiveFieldName(k, blacklist) ? REDACTED : redactValue(v, blacklist);
80
+ }
81
+ return out;
82
+ }
83
+
84
+ /** 预编译脱敏器(多次调用复用;blacklist 为附加规范化字段名) */
85
+ export function createRedactor(blacklist: readonly string[] = []): (text: string) => string {
86
+ const compiled = { json: JSON_FIELD_RE, kv: KV_FIELD_RE, colon: COLON_FIELD_RE };
87
+ return (text: string): string => {
88
+ let out = replaceSensitiveField(text, new RegExp(compiled.json.source, 'g'), blacklist);
89
+ out = replaceSensitiveField(out, new RegExp(compiled.kv.source, 'g'), blacklist);
90
+ out = replaceSensitiveField(out, new RegExp(compiled.colon.source, 'g'), blacklist);
91
+ for (const { re } of VALUE_PATTERNS) {
92
+ re.lastIndex = 0;
93
+ out = out.replace(re, REDACTED);
94
+ }
95
+ out = out.replace(URL_QUERY_RE, `$1${REDACTED}`);
96
+ return out;
97
+ };
98
+ }
99
+
100
+ /** 默认名单(文档用;redact 的默认附加名单为空,内置判定已含 DEFAULT_SECRET_FIELD_NAMES) */
101
+ export { DEFAULT_SECRET_FIELD_NAMES };
@@ -0,0 +1,275 @@
1
+ /**
2
+ * 敏感字段扫描器(规范 §6 / 设计 §7.2 / core `SecretScanner` 契约的强化实现)。
3
+ *
4
+ * 定位:`ctx.settings.describe({redactSecrets:true})` 已剥离 DSH 已知秘密(设计 §7.2),
5
+ * 本扫描器是**第二道防线**,兜底插件自定义字段、MCP headers、patch config、文件类分区文本。
6
+ *
7
+ * 判定策略(两级,避免误伤/漏报):
8
+ * - 字段名:规范化(小写、去 `_-. ` 分隔符)后做「精确命中 / 敏感后缀 / 敏感前缀」三级匹配;
9
+ * 只剥离**字符串叶值**(对象/数组整体递归,不整块剥离——保证 `credentials: [...]` 状态数组存活)。
10
+ * - 引用豁免:`apiKeyEnv` 等「只存引用名」字段、以及形如 `DEEPSEEK_API_KEY` 的全大写环境变量名
11
+ * (`isEnvVarName`)一律保留——值是名字不是秘密。
12
+ * - 值形状启发式(规范 §6「不能只根据固定字段判断」):字段名未命中但值长得像 secret
13
+ * (sk- / JWT / AKIA / GitHub PAT / PEM 私钥 / Bearer)也剥离;高熵 base64 默认关(误伤率高)。
14
+ *
15
+ * 实现不变量:返回 sanitized 为**全新对象**(不改原数据);迭代式显式栈(防爆栈)+ 深度上限;
16
+ * 命中记录只含路径与字段名,**值永不外泄**。
17
+ */
18
+ import { JsonDepthError } from '../utils/json.ts';
19
+ import type { SecretScanner, SensitiveHit } from '../core/types.ts';
20
+
21
+ /** 剥离占位(与 core defaultSecretScanner 语义一致:空串 = 需补录提示) */
22
+ export const REDACTED_PLACEHOLDER = '';
23
+
24
+ /**
25
+ * 敏感字段名单(规范化名:小写、无分隔符)。
26
+ * 覆盖规范 §6 原始清单 + 设计 §7.2 + core logger 名单,并保持克制以防误伤
27
+ * (不收录 `key`/`session` 等过宽子串——`sessionIds`/`monkey` 不得中招)。
28
+ */
29
+ export const DEFAULT_SECRET_FIELD_NAMES: readonly string[] = [
30
+ // 规范 §6 原文
31
+ 'password', 'passwd', 'token', 'accesstoken', 'refreshtoken',
32
+ 'apikey', 'secret', 'credential', 'authorization', 'cookie',
33
+ 'privatekey', 'clientsecret',
34
+ // 设计 §7.2 / core logger 扩充
35
+ 'pwd', 'passphrase', 'authtoken', 'sessionkey', 'apisecret',
36
+ 'authheader', 'bearer', 'webhooksecret',
37
+ ];
38
+
39
+ /** 引用字段(值只是「名字」而非秘密):规范化精确命中即豁免 */
40
+ export const DEFAULT_REFERENCE_FIELDS: readonly string[] = [
41
+ 'apikeyenv', 'apikeyname', 'apikeyref',
42
+ 'tokenenv', 'accesstokenenv', 'refreshtokenenv',
43
+ 'clientsecretenv', 'passwordenv', 'secretref', 'credentialref',
44
+ ];
45
+
46
+ /** 敏感后缀(规范化后以这些结尾 → 命中;`key` 故意不收,避免 monkey/whiskey 误伤) */
47
+ const SENSITIVE_SUFFIXES = ['token', 'secret', 'password', 'passwd', 'credential'] as const;
48
+
49
+ /** 敏感前缀(规范化后以这些开头 → 命中;token/secret 等过宽词不收入) */
50
+ const SENSITIVE_PREFIXES = [
51
+ 'apikey', 'accesstoken', 'refreshtoken', 'authtoken',
52
+ 'clientsecret', 'privatekey', 'authorization', 'authheader',
53
+ 'webhooksecret', 'apisecret',
54
+ ] as const;
55
+
56
+ /** 值形状强模式(默认开):一眼可辨的密钥/凭据形态(包含式:值内任意位置出现即命中) */
57
+ export interface ValuePattern { name: string; re: RegExp }
58
+ export const SECRET_VALUE_PATTERNS: readonly ValuePattern[] = [
59
+ { name: 'openai-style-key', re: /sk-[A-Za-z0-9_-]{8,}/ },
60
+ { name: 'jwt', re: /eyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/ },
61
+ { name: 'aws-access-key', re: /AKIA[0-9A-Z]{16}/ },
62
+ { name: 'github-token', re: /gh[pousr]_[A-Za-z0-9]{20,}/ },
63
+ { name: 'github-pat', re: /github_pat_[A-Za-z0-9_]{20,}/ },
64
+ { name: 'pem-private-key', re: /-----BEGIN (?:RSA |EC |OPENSSH |DSA |ENCRYPTED )?PRIVATE KEY-----/ },
65
+ { name: 'bearer-token', re: /Bearer [A-Za-z0-9._~+/=-]{8,}/ },
66
+ ];
67
+
68
+ export interface SecretScannerOptions {
69
+ /** 附加敏感字段名(任意原始形态,内部会规范化) */
70
+ extraFieldNames?: string[];
71
+ /** 附加引用字段名(规范化后精确命中即豁免) */
72
+ extraReferenceFields?: string[];
73
+ /** 值形状启发式开关(默认 true) */
74
+ valuePatterns?: boolean;
75
+ /** 高熵长串启发式(默认 false:误伤风险高,需显式开启) */
76
+ highEntropy?: boolean;
77
+ /** 最大递归深度(默认 64,与 JSON 深度上限一致) */
78
+ maxDepth?: number;
79
+ }
80
+
81
+ /** 规范化字段名:小写 + 去 `_-. ` 分隔符(用于名单匹配) */
82
+ export function normalizeFieldName(name: string): string {
83
+ return name.toLowerCase().replace(/[^a-z0-9]/g, '');
84
+ }
85
+
86
+ /** 是否为「环境变量名」形态(全大写下划线,如 DEEPSEEK_API_KEY)→ 视为引用名 */
87
+ export function isEnvVarName(value: string): boolean {
88
+ return /^[A-Z][A-Z0-9_]{2,}$/.test(value);
89
+ }
90
+
91
+ /** 字段名是否命中敏感名单(规范化后精确 / 敏感后缀 / 敏感前缀) */
92
+ export function isSensitiveFieldName(
93
+ field: string,
94
+ extra: readonly string[] = [],
95
+ ): boolean {
96
+ const norm = normalizeFieldName(field);
97
+ if (norm === '') return false;
98
+ if ([...DEFAULT_SECRET_FIELD_NAMES, ...extra].includes(norm)) return true;
99
+ if (SENSITIVE_SUFFIXES.some((s) => norm.endsWith(s) && norm.length > s.length)) return true;
100
+ return SENSITIVE_PREFIXES.some((p) => norm.startsWith(p));
101
+ }
102
+
103
+ /** 是否为引用字段(值只是名字,不剥离) */
104
+ export function isReferenceField(
105
+ field: string,
106
+ extra: readonly string[] = [],
107
+ ): boolean {
108
+ const norm = normalizeFieldName(field);
109
+ return [...DEFAULT_REFERENCE_FIELDS, ...extra].includes(norm);
110
+ }
111
+
112
+ /** 值是否命中强模式 secret 形状(返回命中模式名;未命中返回 null) */
113
+ export function matchSecretValuePattern(value: string): string | null {
114
+ for (const p of SECRET_VALUE_PATTERNS) {
115
+ if (p.re.test(value)) return p.name;
116
+ }
117
+ return null;
118
+ }
119
+
120
+ /** 高熵启发式:长 base64/hex 串且 Shannon 熵高(默认关) */
121
+ function isHighEntropySecret(value: string): boolean {
122
+ if (!/^[A-Za-z0-9+/=_-]{32,}$/.test(value)) return false;
123
+ if (/^[A-Za-z0-9_-]+$/.test(value) && !/[0-9]/.test(value)) return false; // 纯单词串(如文件名)
124
+ const counts = new Map<string, number>();
125
+ for (const ch of value) counts.set(ch, (counts.get(ch) ?? 0) + 1);
126
+ let entropy = 0;
127
+ for (const n of counts.values()) {
128
+ const p = n / value.length;
129
+ entropy -= p * Math.log2(p);
130
+ }
131
+ return entropy > 4.2;
132
+ }
133
+
134
+ /** 判定单个「字段名 + 字符串值」:strip | keep(含引用豁免与值形状)
135
+ * 顺序:值形状优先(AKIA 等全大写 secret 形状不能被 env 名豁免误放)→ 引用字段 → env 名 → 字段名。 */
136
+ function judgeFieldValue(field: string, value: string, opts: Required<Pick<SecretScannerOptions, 'extraFieldNames' | 'extraReferenceFields' | 'valuePatterns' | 'highEntropy'>>): boolean {
137
+ if (value === '') return false;
138
+ if (opts.valuePatterns && matchSecretValuePattern(value) !== null) return true;
139
+ if (isReferenceField(field, opts.extraReferenceFields)) return false;
140
+ if (isEnvVarName(value)) return false; // 值是 env 变量名 → 引用
141
+ if (isSensitiveFieldName(field, opts.extraFieldNames)) return true;
142
+ if (opts.highEntropy && isHighEntropySecret(value)) return true;
143
+ return false;
144
+ }
145
+
146
+ /** 递归扫描 + 剥离(纯函数,不改原数据;深度保护;返回清洗后数据与命中清单) */
147
+ export function scanAndRedact(data: unknown, opts: SecretScannerOptions = {}): { sanitized: unknown; hits: SensitiveHit[] } {
148
+ const maxDepth = opts.maxDepth ?? 64;
149
+ const cfg: Required<Pick<SecretScannerOptions, 'extraFieldNames' | 'extraReferenceFields' | 'valuePatterns' | 'highEntropy'>> = {
150
+ extraFieldNames: opts.extraFieldNames ?? [],
151
+ extraReferenceFields: opts.extraReferenceFields ?? [],
152
+ valuePatterns: opts.valuePatterns ?? true,
153
+ highEntropy: opts.highEntropy ?? false,
154
+ };
155
+ const hits: SensitiveHit[] = [];
156
+
157
+ if (data === null || typeof data !== 'object') return { sanitized: data, hits };
158
+ if (data instanceof Uint8Array) return { sanitized: data, hits };
159
+
160
+ const rootOut: unknown = Array.isArray(data) ? [] : {};
161
+ interface Frame { src: unknown; dst: unknown; path: string; depth: number }
162
+ const stack: Frame[] = [{ src: data, dst: rootOut, path: '', depth: 1 }];
163
+ const visited = new WeakSet<object>();
164
+ visited.add(data as object);
165
+
166
+ while (stack.length > 0) {
167
+ const frame = stack.pop()!;
168
+ const { src, dst, path, depth } = frame;
169
+ if (depth > maxDepth) throw new JsonDepthError(`扫描深度 ${depth} 超过上限 ${maxDepth}`);
170
+ if (src instanceof Uint8Array) continue;
171
+
172
+ if (Array.isArray(src)) {
173
+ const outArr = dst as unknown[];
174
+ for (let i = 0; i < src.length; i++) {
175
+ const v = src[i];
176
+ const childPath = `${path}[${i}]`;
177
+ if (v !== null && typeof v === 'object' && !(v instanceof Uint8Array)) {
178
+ if (visited.has(v)) throw new Error(`检测到循环引用,无法扫描(路径 ${childPath})`);
179
+ visited.add(v);
180
+ const childOut: unknown = Array.isArray(v) ? [] : {};
181
+ outArr.push(childOut);
182
+ stack.push({ src: v, dst: childOut, path: childPath, depth: depth + 1 });
183
+ } else {
184
+ outArr.push(v);
185
+ }
186
+ }
187
+ continue;
188
+ }
189
+
190
+ const outObj = dst as Record<string, unknown>;
191
+ for (const [k, v] of Object.entries(src as Record<string, unknown>)) {
192
+ const childPath = path === '' ? k : `${path}.${k}`;
193
+ if (typeof v === 'string') {
194
+ if (judgeFieldValue(k, v, cfg)) {
195
+ hits.push({ path: childPath, field: k });
196
+ outObj[k] = REDACTED_PLACEHOLDER;
197
+ } else {
198
+ outObj[k] = v;
199
+ }
200
+ continue;
201
+ }
202
+ if (v !== null && typeof v === 'object' && !(v instanceof Uint8Array)) {
203
+ if (visited.has(v)) throw new Error(`检测到循环引用,无法扫描(路径 ${childPath})`);
204
+ visited.add(v);
205
+ const childOut: unknown = Array.isArray(v) ? [] : {};
206
+ outObj[k] = childOut;
207
+ stack.push({ src: v, dst: childOut, path: childPath, depth: depth + 1 });
208
+ } else {
209
+ outObj[k] = v;
210
+ }
211
+ }
212
+ }
213
+
214
+ return { sanitized: rootOut, hits };
215
+ }
216
+
217
+ /* ---------------- 文本级扫描(scanText,文件类分区内容用) ---------------- */
218
+
219
+ const FIELD_VALUE_RE = [
220
+ // "field": "value"(JSON 形态,保留引号)
221
+ { re: /"([A-Za-z0-9_.\-]+)"\s*:\s*"([^"]*)"/g, json: true },
222
+ // field=value
223
+ { re: /([A-Za-z0-9_.\-]+)\s*=\s*([^\s,;]+)/g, json: false },
224
+ // field: value
225
+ { re: /([A-Za-z0-9_.\-]+)\s*:\s*([^\s,;}]+)/g, json: false },
226
+ ] as const;
227
+
228
+ /** 文本级扫描:逐行找「敏感字段名 = 值」与「值形状」命中(只报告,不修改文本) */
229
+ export function scanText(text: string, opts: SecretScannerOptions = {}): SensitiveHit[] {
230
+ const hits: SensitiveHit[] = [];
231
+ const extra = opts.extraFieldNames ?? [];
232
+ const valuePatterns = opts.valuePatterns ?? true;
233
+ const lines = text.split(/\r?\n/);
234
+
235
+ for (let i = 0; i < lines.length; i++) {
236
+ const line = lines[i]!;
237
+ const linePath = `line:${i + 1}`;
238
+ // 字段名形态
239
+ for (const { re, json } of FIELD_VALUE_RE) {
240
+ re.lastIndex = 0;
241
+ let m: RegExpExecArray | null;
242
+ while ((m = re.exec(line)) !== null) {
243
+ const field = m[1]!;
244
+ const value = m[2]!;
245
+ if (!isSensitiveFieldName(field, extra)) continue;
246
+ if (json) {
247
+ // JSON 形态下引号内值才报告(引用名豁免由调用方结合场景决定,这里保守报告)
248
+ hits.push({ path: linePath, field });
249
+ continue;
250
+ }
251
+ if (isEnvVarName(value)) continue; // 引用名不报告
252
+ hits.push({ path: linePath, field });
253
+ }
254
+ }
255
+ // 值形状(字段名无关)
256
+ if (valuePatterns) {
257
+ const trimmed = line.trim();
258
+ const name = matchSecretValuePattern(trimmed);
259
+ if (name !== null) hits.push({ path: linePath, field: `(${name})` });
260
+ }
261
+ }
262
+ return hits;
263
+ }
264
+
265
+ /** 强化版 SecretScanner(对齐 core SecretScanner 契约,可注入 Exporter.scanner) */
266
+ export function createSecretScanner(opts: SecretScannerOptions = {}): SecretScanner {
267
+ return {
268
+ scanAndRedact(data: unknown): { sanitized: unknown; hits: SensitiveHit[] } {
269
+ return scanAndRedact(data, opts);
270
+ },
271
+ scanText(text: string): SensitiveHit[] {
272
+ return scanText(text, opts);
273
+ },
274
+ };
275
+ }