dsh-mask 0.2.20 → 0.2.23

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.
@@ -0,0 +1,111 @@
1
+ // lib/deliver.mjs — 还原结果的受控明文交付(唯一的非日志用户可见通道,零 DSH 依赖)。
2
+ //
3
+ // 为什么需要这个模块:宿主把命令结果**原样**写进持久会话日志,并且用户可见的命令
4
+ // 输出正是从那条日志事件渲染出来的(两条通道同源,无法分离):
5
+ // - 落盘:dsh-commands `execute()` → `appendLifecycle(session, 'command/done', {text: result.text})`
6
+ // (`types.ts` 的 `command/done` 声明明说 text 是处理器结果的 verbatim 文本)。
7
+ // - 渲染:client `ui-chat` 的 command 节点由 `command/done` 事件构造,`GenericCommandCard`
8
+ // 直接渲染 `node.outcome.text`(即同一条事件的 text)。
9
+ // 所以:命令结果里放什么,就等于往会话日志里放什么,也就等于往用户的转录里放什么;
10
+ // 宿主没有任何「记日志但不渲染」或「渲染但不记日志」的开关(`recordInput` 只管输入侧)。
11
+ // 结论:还原出的明文**绝不可**放进命令结果,只能走这条受控文件通道,把路径回给用户。
12
+ //
13
+ // 落点与收敛:每个会话固定一个文件(文件名 = 会话 id 的 sha256 前 16 位十六进制,
14
+ // 不含会话 id 本身、更不含原文),下一次还原覆盖同一个文件 ⇒ 磁盘上的明文副本数量
15
+ // 有界(每会话至多一份),不会随还原次数累积。
16
+ //
17
+ // 权限与失败关闭:目录 0700、文件 0600(POSIX;Windows 上 chmod 只影响只读位,无害)。
18
+ // 目录与目标文件都先 lstat 并拒绝符号链接,避免把 PII 写进他人预置的软链指向处;
19
+ // 目录属主不是当前用户时拒绝写入。任何一步失败都抛错,由调用方转成不泄露原文的错误
20
+ // 结果——绝不回退到「把明文放进命令结果」。
21
+
22
+ import { createHash } from 'node:crypto'
23
+ import { chmod, lstat, mkdir, writeFile } from 'node:fs/promises'
24
+ import os from 'node:os'
25
+ import path from 'node:path'
26
+
27
+ /** 默认交付目录名(`restoreDir` 配空时的落点,位于系统临时目录下)。 */
28
+ export const RESTORE_DIR_NAME = 'dsh-mask-restore'
29
+
30
+ /** 交付文件名前缀(真正写入前先 lstat 拒绝符号链接)。 */
31
+ export const RESTORE_FILE_PREFIX = 'restore-'
32
+
33
+ /** 0644 会让同机其它用户读到 PII;目录与文件都取最小可读面。 */
34
+ const DIR_MODE = 0o700
35
+ const FILE_MODE = 0o600
36
+
37
+ /**
38
+ * 解析交付目录:配置为空串(默认)时落在 `os.tmpdir()/dsh-mask-restore`,
39
+ * 否则按配置解析为绝对路径(相对路径相对宿主进程 cwd)。
40
+ * 纯函数(除 `os.tmpdir()`/`path.resolve` 的宿主事实外无副作用),便于测试。
41
+ * @param {unknown} configured - `restoreDir` 配置值。
42
+ * @returns {string} 绝对目录路径。
43
+ */
44
+ export function resolveRestoreDir(configured) {
45
+ const raw = typeof configured === 'string' ? configured.trim() : ''
46
+ return raw === '' ? path.join(os.tmpdir(), RESTORE_DIR_NAME) : path.resolve(raw)
47
+ }
48
+
49
+ /**
50
+ * 会话的交付文件路径:`<dir>/restore-<sha256(sessionId)[0:16]>.txt`。
51
+ * 纯函数;同一会话恒定同一个路径(覆盖语义),不同会话互不覆盖。
52
+ * @param {string} dir - 交付目录(`resolveRestoreDir` 的结果)。
53
+ * @param {unknown} sessionId - 会话 id。
54
+ * @returns {string} 交付文件绝对路径。
55
+ */
56
+ export function restoreFilePath(dir, sessionId) {
57
+ const digest = createHash('sha256').update(String(sessionId ?? '')).digest('hex').slice(0, 16)
58
+ return path.join(dir, `${RESTORE_FILE_PREFIX}${digest}.txt`)
59
+ }
60
+
61
+ /**
62
+ * 目标路径必须不是符号链接:否则写入会跟随软链落到他人指定的位置。
63
+ * @param {string} target - 待检查路径。
64
+ * @param {string} what - 错误消息里的对象名。
65
+ * @returns {Promise<import('node:fs').Stats|undefined>} 存在时的 lstat 结果。
66
+ */
67
+ async function assertNotSymlink(target, what) {
68
+ try {
69
+ const stats = await lstat(target)
70
+ if (stats.isSymbolicLink()) {
71
+ throw new Error(`${what} is a symbolic link: refusing to write plaintext through it`)
72
+ }
73
+ return stats
74
+ } catch (error) {
75
+ if (/** @type {NodeJS.ErrnoException} */ (error).code === 'ENOENT') return undefined
76
+ throw error
77
+ }
78
+ }
79
+
80
+ /**
81
+ * 把还原出的明文写进受控文件(覆盖本会话上一份)。
82
+ * 绝不返回、也绝不记录明文本身——只返回落点与字节数供结果文本使用。
83
+ * @param {object} options - {dir, sessionId, text}。
84
+ * @param {string} options.dir - 交付目录。
85
+ * @param {unknown} options.sessionId - 会话 id。
86
+ * @param {string} options.text - 还原后的明文(只写入文件,不进任何日志/结果)。
87
+ * @returns {Promise<{path: string, bytes: number}>} 落点与 UTF-8 字节数。
88
+ */
89
+ export async function deliverPlaintext({ dir, sessionId, text }) {
90
+ const target = restoreFilePath(dir, sessionId)
91
+ // Check the existing entry before mkdir: a dangling symlink makes mkdir throw
92
+ // ENOENT, which would mask the real refusal reason and fail the wrong way.
93
+ await assertNotSymlink(dir, 'restoreDir')
94
+ await mkdir(dir, { recursive: true, mode: DIR_MODE })
95
+ const dirStats = await assertNotSymlink(dir, 'restoreDir')
96
+ if (dirStats === undefined || !dirStats.isDirectory()) {
97
+ throw new Error('restoreDir is not a directory')
98
+ }
99
+ const uid = typeof process.getuid === 'function' ? process.getuid() : undefined
100
+ if (uid !== undefined && dirStats.uid !== uid) {
101
+ throw new Error('restoreDir is owned by another user: refusing to write plaintext there')
102
+ }
103
+ // 目录可能早已存在(预置或上次运行),mkdir 的 mode 不会收紧既有权限。
104
+ await chmod(dir, DIR_MODE)
105
+ await assertNotSymlink(target, 'restore file')
106
+ const body = typeof text === 'string' ? text : String(text)
107
+ await writeFile(target, body, { encoding: 'utf8', mode: FILE_MODE, flag: 'w' })
108
+ // 覆盖写不会改既有文件的 mode,显式收紧一次。
109
+ await chmod(target, FILE_MODE)
110
+ return { path: target, bytes: Buffer.byteLength(body, 'utf8') }
111
+ }
package/lib/store.mjs CHANGED
@@ -68,12 +68,36 @@ export class RestoreStore {
68
68
  * @returns {Promise<string>} 还原后的文本。
69
69
  */
70
70
  async restore(sessionId, text) {
71
+ const stripper = await this._stripperForRestore(sessionId)
72
+ if (stripper === undefined) return text
73
+ return stripper.restore(text)
74
+ }
75
+
76
+ /**
77
+ * 还原并报告本次涉及的占位符(供命令结果只写占位符、不写明文)。
78
+ * 与 {@link restore} 同源同语义:命中集合与替换用的映射完全一致。
79
+ * @param {string} sessionId - 会话 id。
80
+ * @param {string} text - 含占位符文本。
81
+ * @returns {Promise<{text: string, placeholders: string[]}>} 还原文本 + 命中的占位符(长度降序)。
82
+ */
83
+ async restoreDetailed(sessionId, text) {
84
+ const stripper = await this._stripperForRestore(sessionId)
85
+ if (stripper === undefined) return { text, placeholders: [] }
86
+ return { text: stripper.restore(text), placeholders: stripper.placeholdersIn(text) }
87
+ }
88
+
89
+ /**
90
+ * 取会话脱敏器,内存未命中且 persist 开启时从领域回载(restore 与
91
+ * restoreDetailed 的唯一差异只是返回值,故共用这一段)。
92
+ * @param {string} sessionId - 会话 id。
93
+ * @returns {Promise<import('./strip.mjs').Stripper|undefined>} 脱敏器或 undefined。
94
+ */
95
+ async _stripperForRestore(sessionId) {
71
96
  let stripper = this.strippers.get(sessionId)
72
97
  if (stripper === undefined && this.persistEnabled && this.domainPromise !== null) {
73
98
  stripper = await this._load(sessionId)
74
99
  }
75
- if (stripper === undefined) return text
76
- return stripper.restore(text)
100
+ return stripper
77
101
  }
78
102
 
79
103
  /**
package/lib/strip.mjs CHANGED
@@ -132,6 +132,20 @@ export class Stripper {
132
132
  return result
133
133
  }
134
134
 
135
+ /**
136
+ * 文本中出现的、本会话已知的占位符(按 `restore()` 的替换顺序:长度降序)。
137
+ * 只读、不改文本、绝不返回原文——用于把「这次还原涉及哪些占位符」安全地
138
+ * 写进用户可见结果(占位符本身已按设计存在于模型可见的日志里)。
139
+ * @param {unknown} text - 含占位符的文本。
140
+ * @returns {string[]} 命中的占位符(去重,长度降序)。
141
+ */
142
+ placeholdersIn(text) {
143
+ if (typeof text !== 'string' || text.length === 0) return []
144
+ return [...this.placeholderMap.keys()]
145
+ .sort((a, b) => b.length - a.length)
146
+ .filter(placeholder => text.includes(placeholder))
147
+ }
148
+
135
149
  /** @returns {Record<string, string>} {占位符: 原文} 只读副本。 */
136
150
  mapping() {
137
151
  return Object.fromEntries(this.placeholderMap)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-mask",
3
3
  "description": "PII masking middleware for DeepSeek Harness: regex-detect and replace phones, emails, ID cards, bank cards, keys, and (opt-in) IPs with placeholders before they reach the model, keep the restore table host-side (memory plus a controlled storage domain, never the session log) with the /mask restore command and the exported RestoreStore seam, and expose /mask status and the mask_test tool. Name and address recognition needs an external NER recognizer, which this pure-host form does not bundle: mode \"regex+ner\" and the person/address entities fail loudly at load. The phone and ID-card detectors match mainland-China formats only.",
4
- "version": "0.2.20",
4
+ "version": "0.2.23",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/PerryLink/dsh-mask.git"
@@ -63,6 +63,7 @@
63
63
  },
64
64
  "permissions": [
65
65
  "filesystem:read",
66
+ "filesystem:write",
66
67
  "storage-domain:read",
67
68
  "storage-domain:write",
68
69
  "session-log:read",
@@ -70,14 +71,21 @@
70
71
  "network:none"
71
72
  ],
72
73
  "compatibility": {
73
- "dshVersions": ["0.1.2-rc.1","0.1.5-rc.2","0.1.7-alpha.2","0.1.7-rc.1", "0.1.7-rc.2",
74
- "0.2.1-alpha.1"]
74
+ "dshVersions": [
75
+ "0.1.2-rc.1",
76
+ "0.1.5-rc.2",
77
+ "0.1.7-alpha.2",
78
+ "0.1.7-rc.1",
79
+ "0.1.7-rc.2",
80
+ "0.2.1-alpha.1",
81
+ "0.2.1-rc.2"
82
+ ]
75
83
  },
76
84
  "capability": {
77
85
  "id": "mask",
78
86
  "kind": "command",
79
87
  "invocation": "/mask status",
80
- "expected": "the /mask command is registered on the commands service: status reports masked counts and type distribution, on/off toggles masking at runtime, restore <text> unmaps placeholders back to the values stored for this session; mask_test tool masks a snippet and reports the placeholder result"
88
+ "expected": "the /mask command is registered on the commands service: status reports masked counts and type distribution, on/off toggles masking at runtime, restore <text> unmaps placeholders back to the values stored for this session and delivers the plaintext as a controlled file whose path (never the plaintext) is what the command result and the session log carry; mask_test tool masks a snippet and reports the placeholder result"
81
89
  },
82
90
  "evidence": {
83
91
  "install": null,
@@ -87,35 +95,36 @@
87
95
  }
88
96
  },
89
97
  "peerDependencies": {
90
- "@deepseek-ai/cordis": "^4.0.5-alpha.1",
98
+ "@deepseek-ai/cordis": "^4.0.5",
91
99
  "@deepseek-ai/dsh-commands": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0 || >=0.2.0-0 <0.3.0 || >=0.2.1-0 <0.3.0",
92
100
  "@deepseek-ai/dsh-session": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0 || >=0.2.0-0 <0.3.0 || >=0.2.1-0 <0.3.0",
93
101
  "@deepseek-ai/dsh-storage": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0 || >=0.2.0-0 <0.3.0 || >=0.2.1-0 <0.3.0",
94
102
  "@deepseek-ai/dsh-storage-domain": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0 || >=0.2.0-0 <0.3.0 || >=0.2.1-0 <0.3.0",
95
103
  "@deepseek-ai/dsh-storage-json": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0 || >=0.2.0-0 <0.3.0 || >=0.2.1-0 <0.3.0",
96
104
  "@deepseek-ai/dsh-tools": ">=0.1.2-rc.1 <0.2.0 || >=0.1.5-alpha.1 <0.2.0 || >=0.1.6-0 <0.2.0 || >=0.1.7-0 <0.2.0 || >=0.2.0-0 <0.3.0 || >=0.2.1-0 <0.3.0",
97
- "@deepseek-ai/schemastery": "^3.18.5-alpha.1"
105
+ "@deepseek-ai/schemastery": "^3.18.5-alpha.2"
98
106
  },
99
107
  "dependencies": {
100
- "zod": "^4.4.3"
108
+ "zod": "4.4.3"
101
109
  },
102
110
  "devDependencies": {
103
- "@deepseek-ai/dsh-attachment": "0.2.1-alpha.1",
104
- "@deepseek-ai/cordis": "^4.0.5-alpha.1",
105
- "@deepseek-ai/cordis-plugin-include": "^1.0.10-alpha.1",
106
- "@deepseek-ai/cordis-plugin-loader": "^1.0.6-alpha.1",
107
- "@deepseek-ai/dsh-agent": "0.2.1-alpha.1",
108
- "@deepseek-ai/dsh-commands": "0.2.1-alpha.1",
109
- "@deepseek-ai/dsh-session": "0.2.1-alpha.1",
110
- "@deepseek-ai/dsh-storage": "0.2.1-alpha.1",
111
- "@deepseek-ai/dsh-storage-domain": "0.2.1-alpha.1",
112
- "@deepseek-ai/dsh-storage-json": "0.2.1-alpha.1",
113
- "@deepseek-ai/dsh-system-prompt": "0.2.1-alpha.1",
114
- "@deepseek-ai/dsh-tools": "0.2.1-alpha.1",
115
- "@deepseek-ai/schemastery": "^3.18.5-alpha.1",
111
+ "@deepseek-ai/dsh-attachment": "0.2.1-rc.2",
112
+ "@deepseek-ai/cordis": "^4.0.5",
113
+ "@deepseek-ai/cordis-plugin-include": "^1.0.10-alpha.2",
114
+ "@deepseek-ai/cordis-plugin-loader": "^1.0.6",
115
+ "@deepseek-ai/dsh-agent": "0.2.1-rc.2",
116
+ "@deepseek-ai/dsh-commands": "0.2.1-rc.2",
117
+ "@deepseek-ai/dsh-session": "0.2.1-rc.2",
118
+ "@deepseek-ai/dsh-storage": "0.2.1-rc.2",
119
+ "@deepseek-ai/dsh-storage-domain": "0.2.1-rc.2",
120
+ "@deepseek-ai/dsh-storage-json": "0.2.1-rc.2",
121
+ "@deepseek-ai/dsh-system-prompt": "0.2.1-rc.2",
122
+ "@deepseek-ai/dsh-tools": "0.2.1-rc.2",
123
+ "@deepseek-ai/schemastery": "^3.18.5-alpha.2",
116
124
  "@types/node": "^22.19.0",
117
125
  "oxlint": "0.18.1",
118
- "typescript": "^5.9.0"
126
+ "typescript": "^5.9.0",
127
+ "@deepseek-ai/dsh-working-directory": "0.2.1-rc.2"
119
128
  },
120
129
  "scripts": {
121
130
  "typecheck": "tsc -p tsconfig.check.json",
package/types.d.ts CHANGED
@@ -42,6 +42,13 @@ export interface Config {
42
42
  maxSessions?: number
43
43
  /** 浏览器半自动还原气泡开关(防御性 client slot;默认 false,未启用前对 UI 无副作用)。 */
44
44
  maskClientEnabled?: boolean
45
+ /**
46
+ * `/mask restore` 明文交付目录(默认空串 = `os.tmpdir()/dsh-mask-restore`)。
47
+ * 宿主把命令结果原样写进会话日志、客户端又从同一条事件渲染用户可见输出,
48
+ * `restore` 的明文因此绝不放进结果文本,只写进该目录下的受控文件
49
+ * (每会话一个,mode 0600,下次还原覆盖),结果文本只回路径与占位符名。
50
+ */
51
+ restoreDir?: string
45
52
  }
46
53
 
47
54
  /** mask_test 工具规范结果。 */