dsh-skill-harbor 0.5.4 → 0.6.0-rc.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 drscrewdriver
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2026 drscrewdriver
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,44 +1,44 @@
1
- # dsh-skill-harbor(技能港湾)
2
-
3
- [![npm](https://img.shields.io/npm/v/dsh-skill-harbor)](https://www.npmjs.com/package/dsh-skill-harbor)
4
-
5
- DSH 技能管理面板插件:**技能发现 / 遮蔽判定 / 审计 / 市场管理**。
6
-
7
- > 当前版本 `0.0.1` = **基础骨架**(宿主半 + 客户端半 + 清单 + 构建脚本),业务逻辑按计划分阶段落地。
8
-
9
- ## 安装
10
-
11
- ```bash
12
- dsh plugin --profile <name> add dsh-skill-harbor@0.0.1
13
- ```
14
-
15
- ## 版本线
16
-
17
- | 版本 | 内容 | 状态 |
18
- |---|---|---|
19
- | `0.0.1` | 基础骨架 | ✅ 已发布 |
20
- | `0.1.0` | Phase 1 证据层(根枚举 / frontmatter / observe / shadow / provider) | 规划中 |
21
- | `0.2.0` | Phase 2 本地管理(路由闸 / 列表 / 写 / 导入) | 规划中 |
22
- | `0.3.0` | Phase 3 预算与审计 = **MVP** | 规划中 |
23
- | `0.4.0` | Phase 4 市场(不进 MVP) | 规划中 |
24
-
25
- 发布粒度:每 Phase 一个中版本,每 task 一次 rc。完整 26 个版本节点的拆解见 `docs/plans/dsh-skill-harbor/tasks.md`。
26
-
27
- ## 结构
28
-
29
- ```
30
- src/index.js 宿主半(apply(ctx, config))
31
- src/client.js 客户端半(__ModuleLoader__ 注册,id = 包名)
32
- scripts/build.mjs 零依赖构建 → lib/
33
- cordis.patch.yml bundle patch(dsh plugin add 自动挂载)
34
- dsh.plugin.json DSH 插件清单
35
- docs/plans/ 规划文档(spec / tasks / checklist / findings)
36
- ```
37
-
38
- ## 背景
39
-
40
- DSH 官方没有技能管理界面,且 `packages/bundle/web-app/cordis.patch.yml` 把 `skill-filesystem` / `tool-skill` 在 host 层设为 `disabled: true`,技能发现被下放到 preset(agent scope)层——host 面的 `ctx.skills` 读不到项目/用户技能。本插件要回答:**这个技能到底在哪、谁在遮蔽谁、模型收没收到。**
41
-
42
- ## License
43
-
44
- MIT
1
+ # dsh-skill-harbor(技能港湾)
2
+
3
+ [![npm](https://img.shields.io/npm/v/dsh-skill-harbor)](https://www.npmjs.com/package/dsh-skill-harbor)
4
+
5
+ DSH 技能管理面板插件:**技能发现 / 遮蔽判定 / 审计 / 市场管理**。
6
+
7
+ > 当前版本 `0.0.1` = **基础骨架**(宿主半 + 客户端半 + 清单 + 构建脚本),业务逻辑按计划分阶段落地。
8
+
9
+ ## 安装
10
+
11
+ ```bash
12
+ dsh plugin --profile <name> add dsh-skill-harbor@0.0.1
13
+ ```
14
+
15
+ ## 版本线
16
+
17
+ | 版本 | 内容 | 状态 |
18
+ |---|---|---|
19
+ | `0.0.1` | 基础骨架 | ✅ 已发布 |
20
+ | `0.1.0` | Phase 1 证据层(根枚举 / frontmatter / observe / shadow / provider) | 规划中 |
21
+ | `0.2.0` | Phase 2 本地管理(路由闸 / 列表 / 写 / 导入) | 规划中 |
22
+ | `0.3.0` | Phase 3 预算与审计 = **MVP** | 规划中 |
23
+ | `0.4.0` | Phase 4 市场(不进 MVP) | 规划中 |
24
+
25
+ 发布粒度:每 Phase 一个中版本,每 task 一次 rc。完整 26 个版本节点的拆解见 `docs/plans/dsh-skill-harbor/tasks.md`。
26
+
27
+ ## 结构
28
+
29
+ ```
30
+ src/index.js 宿主半(apply(ctx, config))
31
+ src/client.js 客户端半(__ModuleLoader__ 注册,id = 包名)
32
+ scripts/build.mjs 零依赖构建 → lib/
33
+ cordis.patch.yml bundle patch(dsh plugin add 自动挂载)
34
+ dsh.plugin.json DSH 插件清单
35
+ docs/plans/ 规划文档(spec / tasks / checklist / findings)
36
+ ```
37
+
38
+ ## 背景
39
+
40
+ DSH 官方没有技能管理界面,且 `packages/bundle/web-app/cordis.patch.yml` 把 `skill-filesystem` / `tool-skill` 在 host 层设为 `disabled: true`,技能发现被下放到 preset(agent scope)层——host 面的 `ctx.skills` 读不到项目/用户技能。本插件要回答:**这个技能到底在哪、谁在遮蔽谁、模型收没收到。**
41
+
42
+ ## License
43
+
44
+ MIT
package/cordis.patch.yml CHANGED
@@ -1,4 +1,4 @@
1
- # dsh-skill-harbor bundle patch — mounts the plugin via the official CLI.
2
- - insert:
3
- - id: dsh-skill-harbor
4
- name: 'dsh-skill-harbor'
1
+ # dsh-skill-harbor bundle patch — mounts the plugin via the official CLI.
2
+ - insert:
3
+ - id: dsh-skill-harbor
4
+ name: 'dsh-skill-harbor'
package/dsh.plugin.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "dsh-external/dsh-skill-harbor",
3
- "version": "0.5.4",
3
+ "version": "0.6.0-rc.1",
4
4
  "main": "./lib/index.js",
5
5
  "description": "技能港湾:技能发现、遮蔽判定、审计与市场管理(基础骨架)",
6
6
  "engines": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-skill-harbor",
3
- "version": "0.5.4",
3
+ "version": "0.6.0-rc.1",
4
4
  "description": "DSH 技能港湾:技能发现、遮蔽判定、审计与市场管理插件(基础骨架)",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -48,6 +48,7 @@
48
48
  "build:client": "node scripts/build-client.mjs",
49
49
  "prepack": "npm run build",
50
50
  "prepublishOnly": "npm run build",
51
+ "check-release": "node scripts/check-release.mjs",
51
52
  "test": "node --test \"tests/**/*.test.mjs\""
52
53
  }
53
54
  }
@@ -1,77 +1,77 @@
1
- /**
2
- * 唯一文本归一化函数 —— **所有审计入口共用这一份**。
3
- *
4
- * 为什么必须先归一化:恶意技能可以把 NUL/零宽字符插进 `curl` 之类的词里,
5
- * 让正则匹配不到,但 shell 执行时照样生效。剥掉伪装层再判定,才有意义。
6
- *
7
- * 保留:tab(0x09) LF(0x0A) CR(0x0D) ESC(0x1B)
8
- * 剥除:NUL(0x00)、其余 C0 控制字符、零宽字符、双向控制字符、替换字符 U+FFFD
9
- */
10
-
11
- /** 零宽与不可见格式字符。 */
12
- const INVISIBLE = /[\u200B-\u200F\u202A-\u202E\u2060-\u2064\u206A-\u206F\uFEFF\u00AD]/g
13
-
14
- /** Unicode tag 字符(U+E0000–E007F),常被用来夹带隐藏指令。 */
15
- const UNICODE_TAGS = /[\u{E0000}-\u{E007F}]/gu
16
-
17
- /** 替换字符。 */
18
- const REPLACEMENT = /\uFFFD/g
19
-
20
- /** C0 控制字符,保留 tab/LF/CR/ESC。 */
21
- const C0 = /[\u0000-\u0008\u000B\u000C\u000E-\u001A\u001C-\u001F]/g
22
-
23
- /** 归一化结果,同时报告发现了哪种伪装(供规则层直接用)。 */
24
- export function normalizeText(input) {
25
- if (typeof input !== 'string') return { text: '', findings: [] }
26
-
27
- const findings = []
28
-
29
- let text = input
30
- if (C0.test(text)) findings.push('c0-control')
31
- if (/\u0000/.test(text)) findings.push('nul-byte')
32
- if (INVISIBLE.test(text)) findings.push('invisible-format')
33
- if (UNICODE_TAGS.test(text)) findings.push('unicode-tag')
34
- if (REPLACEMENT.test(text)) findings.push('replacement-char')
35
- // 双向控制符单独标记:它能让显示顺序与实际执行顺序不一致
36
- if (/[\u202A-\u202E\u2066-\u2069]/.test(text)) findings.push('bidi-control')
37
-
38
- // 重置 lastIndex(这些正则带 g 或只用 test,安全起见重建)
39
- text = input
40
- .replace(/\u0000/g, '')
41
- .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001A\u001C-\u001F]/g, '')
42
- .replace(INVISIBLE, '')
43
- .replace(UNICODE_TAGS, '')
44
- .replace(/\uFFFD/g, '')
45
-
46
- // 统一换行,便于规则按行工作
47
- text = text.replace(/\r\n?/g, '\n')
48
-
49
- return { text, findings: [...new Set(findings)] }
50
- }
51
-
52
- /**
53
- * 编码伪装检测:UTF-16 / 换轮替代 / 大量 base64 块。
54
- * 返回命中的伪装类型列表。
55
- */
56
- export function detectEncodingDisguise(raw) {
57
- const hits = []
58
-
59
- // UTF-16 BOM
60
- if (/^\u00EF\u00BB\u00BF/.test(raw) === false && /^\u00FF\u00FE|^\u00FE\u00FF/.test(raw)) {
61
- hits.push('utf16-bom')
62
- }
63
- // 文本里夹着大量 NUL(UTF-16 被当 UTF-8 读的典型形态)
64
- const nulCount = (raw.match(/\u0000/g) ?? []).length
65
- if (nulCount > 8) hits.push('utf16-pattern')
66
-
67
- // 连续超长 base64 块(可能是被编码的载荷)
68
- const b64 = raw.match(/[A-Za-z0-9+/]{200,}={0,2}/g)
69
- if (b64 !== null) hits.push('base64-blob')
70
-
71
- return hits
72
- }
73
-
74
- /** 字符数(按码点,不按 UTF-16 code unit)。 */
75
- export function charLength(text) {
76
- return typeof text === 'string' ? [...text].length : 0
77
- }
1
+ /**
2
+ * 唯一文本归一化函数 —— **所有审计入口共用这一份**。
3
+ *
4
+ * 为什么必须先归一化:恶意技能可以把 NUL/零宽字符插进 `curl` 之类的词里,
5
+ * 让正则匹配不到,但 shell 执行时照样生效。剥掉伪装层再判定,才有意义。
6
+ *
7
+ * 保留:tab(0x09) LF(0x0A) CR(0x0D) ESC(0x1B)
8
+ * 剥除:NUL(0x00)、其余 C0 控制字符、零宽字符、双向控制字符、替换字符 U+FFFD
9
+ */
10
+
11
+ /** 零宽与不可见格式字符。 */
12
+ const INVISIBLE = /[\u200B-\u200F\u202A-\u202E\u2060-\u2064\u206A-\u206F\uFEFF\u00AD]/g
13
+
14
+ /** Unicode tag 字符(U+E0000–E007F),常被用来夹带隐藏指令。 */
15
+ const UNICODE_TAGS = /[\u{E0000}-\u{E007F}]/gu
16
+
17
+ /** 替换字符。 */
18
+ const REPLACEMENT = /\uFFFD/g
19
+
20
+ /** C0 控制字符,保留 tab/LF/CR/ESC。 */
21
+ const C0 = /[\u0000-\u0008\u000B\u000C\u000E-\u001A\u001C-\u001F]/g
22
+
23
+ /** 归一化结果,同时报告发现了哪种伪装(供规则层直接用)。 */
24
+ export function normalizeText(input) {
25
+ if (typeof input !== 'string') return { text: '', findings: [] }
26
+
27
+ const findings = []
28
+
29
+ let text = input
30
+ if (C0.test(text)) findings.push('c0-control')
31
+ if (/\u0000/.test(text)) findings.push('nul-byte')
32
+ if (INVISIBLE.test(text)) findings.push('invisible-format')
33
+ if (UNICODE_TAGS.test(text)) findings.push('unicode-tag')
34
+ if (REPLACEMENT.test(text)) findings.push('replacement-char')
35
+ // 双向控制符单独标记:它能让显示顺序与实际执行顺序不一致
36
+ if (/[\u202A-\u202E\u2066-\u2069]/.test(text)) findings.push('bidi-control')
37
+
38
+ // 重置 lastIndex(这些正则带 g 或只用 test,安全起见重建)
39
+ text = input
40
+ .replace(/\u0000/g, '')
41
+ .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001A\u001C-\u001F]/g, '')
42
+ .replace(INVISIBLE, '')
43
+ .replace(UNICODE_TAGS, '')
44
+ .replace(/\uFFFD/g, '')
45
+
46
+ // 统一换行,便于规则按行工作
47
+ text = text.replace(/\r\n?/g, '\n')
48
+
49
+ return { text, findings: [...new Set(findings)] }
50
+ }
51
+
52
+ /**
53
+ * 编码伪装检测:UTF-16 / 换轮替代 / 大量 base64 块。
54
+ * 返回命中的伪装类型列表。
55
+ */
56
+ export function detectEncodingDisguise(raw) {
57
+ const hits = []
58
+
59
+ // UTF-16 BOM
60
+ if (/^\u00EF\u00BB\u00BF/.test(raw) === false && /^\u00FF\u00FE|^\u00FE\u00FF/.test(raw)) {
61
+ hits.push('utf16-bom')
62
+ }
63
+ // 文本里夹着大量 NUL(UTF-16 被当 UTF-8 读的典型形态)
64
+ const nulCount = (raw.match(/\u0000/g) ?? []).length
65
+ if (nulCount > 8) hits.push('utf16-pattern')
66
+
67
+ // 连续超长 base64 块(可能是被编码的载荷)
68
+ const b64 = raw.match(/[A-Za-z0-9+/]{200,}={0,2}/g)
69
+ if (b64 !== null) hits.push('base64-blob')
70
+
71
+ return hits
72
+ }
73
+
74
+ /** 字符数(按码点,不按 UTF-16 code unit)。 */
75
+ export function charLength(text) {
76
+ return typeof text === 'string' ? [...text].length : 0
77
+ }
@@ -1,167 +1,167 @@
1
- /**
2
- * 审计规则表:阻断级 + 警告级。
3
- *
4
- * 判定语义四值(dsh-plugin-dev §6):`ok` / `warn` / `fail` / `skipped`。
5
- * 「无法判定」必须是 `skipped`,不能当 `warn` —— 否则全量假阳性。
6
- *
7
- * 设计纪律:
8
- * - 外传形状只看**结构**(`curl -d @file`),不枚举来源命令、不做域名判断
9
- * (域名判断必然失效且产生误报)
10
- * - 每条规则一个正例 + 一个反例(单测钉死)
11
- */
12
- import { normalizeText, detectEncodingDisguise, charLength } from './normalize.js'
13
-
14
- export const VERDICT = Object.freeze({
15
- OK: 'ok',
16
- WARN: 'warn',
17
- FAIL: 'fail',
18
- SKIPPED: 'skipped',
19
- })
20
-
21
- /** 体积/数量上限。 */
22
- export const LIMITS = Object.freeze({
23
- skillBytes: 256 * 1024,
24
- bodyChars: 32_000,
25
- lines: 4_000,
26
- })
27
-
28
- /** 凭据路径(出现在正文里 = 可能在读取用户机密)。 */
29
- const CREDENTIAL_PATHS = [
30
- /\.ssh\/(id_rsa|id_ed25519|id_ecdsa)/i,
31
- /\.aws\/credentials/i,
32
- /\.npmrc\b/i,
33
- /\.netrc\b/i,
34
- /\.git-credentials\b/i,
35
- /AppData[\\/]Roaming[\\/]npm[\\/]etc[\\/]npmrc/i,
36
- ]
37
-
38
- /** 管道执行:`curl … | sh` / `wget … | bash` / `eval $(curl …)`。 */
39
- const PIPE_EXEC = [
40
- /\b(curl|wget|fetch)\b[^\n|]*\|\s*(sudo\s+)?(ba|z|k)?sh\b/i,
41
- /\b(curl|wget)\b[^\n|]*\|\s*sudo\b/i,
42
- /\beval\s+\$\(\s*(curl|wget)\b/i,
43
- ]
44
-
45
- /**
46
- * 外传形状:只看「把文件内容塞进请求体」这个**结构**。
47
- * 不判断域名、不枚举来源命令。
48
- */
49
- const EXFIL_SHAPES = [
50
- // curl -d @file / --data @file / --data-binary @file
51
- /\bcurl\b[^\n]*(?:-d|--data(?:-binary|-raw)?)\s*@[\w./\\-]+/i,
52
- // curl -d @- (从 stdin 读,管道链的终点)
53
- /\bcurl\b[^\n]*(?:-d|--data(?:-binary|-raw)?)\s*@-/i,
54
- // 表单上传到变量/任意 host 的 --upload-file
55
- /\bcurl\b[^\n]*--upload-file\s+[\w./\\-]+/i,
56
- ]
57
-
58
- /** URL 里带长 base64 查询串 = 数据塞 URL 外传。 */
59
- const URL_PAYLOAD = /https?:\/\/[^\s"']+\?[^\s"']*=[A-Za-z0-9+/%]{120,}/
60
-
61
- function fail(rule, message) {
62
- return { rule, verdict: VERDICT.FAIL, message }
63
- }
64
- function warn(rule, message) {
65
- return { rule, verdict: VERDICT.WARN, message }
66
- }
67
-
68
- /**
69
- * 审计一个技能的文本。
70
- * @param {string} raw 原始文件内容
71
- * @param {{ name?: string }} [context]
72
- * @returns {{ verdict: string, findings: Array<{rule:string,verdict:string,message:string}>, stats: object }}
73
- */
74
- export function auditSkillText(raw, context = {}) {
75
- const findings = []
76
-
77
- if (typeof raw !== 'string') {
78
- return { verdict: VERDICT.SKIPPED, findings: [], stats: { reason: 'no-content', name: context.name } }
79
- }
80
-
81
- const { text, findings: disguiseHits } = normalizeText(raw)
82
- const encodingHits = detectEncodingDisguise(raw)
83
-
84
- // ---------- 阻断级 ----------
85
- if (disguiseHits.includes('nul-byte') || disguiseHits.includes('c0-control')) {
86
- findings.push(fail('SD001', `正文含控制字符:${disguiseHits.join(', ')}`))
87
- }
88
- if (disguiseHits.includes('invisible-format')) {
89
- findings.push(fail('SD002', '正文含零宽/不可见格式字符(可隐藏指令)'))
90
- }
91
- if (disguiseHits.includes('unicode-tag')) {
92
- findings.push(fail('SD003', '正文含 Unicode tag 字符(夹带隐藏文本)'))
93
- }
94
- if (disguiseHits.includes('bidi-control')) {
95
- findings.push(fail('SD004', '正文含双向控制字符(显示与执行不一致)'))
96
- }
97
- if (encodingHits.length > 0) {
98
- findings.push(fail('SD005', `编码伪装:${encodingHits.join(', ')}`))
99
- }
100
-
101
- const bytes = Buffer.byteLength(raw, 'utf8')
102
- if (bytes > LIMITS.skillBytes) {
103
- findings.push(fail('SD006', `体积 ${bytes}B 超过上限 ${LIMITS.skillBytes}B`))
104
- }
105
- const bodyChars = charLength(text)
106
- if (bodyChars > LIMITS.bodyChars) {
107
- findings.push(fail('SD007', `正文 ${bodyChars} 字符超过上限 ${LIMITS.bodyChars}`))
108
- }
109
- const lineCount = text.split('\n').length
110
- if (lineCount > LIMITS.lines) {
111
- findings.push(fail('SD008', `行数 ${lineCount} 超过上限 ${LIMITS.lines}`))
112
- }
113
-
114
- // ---------- 警告级 ----------
115
- for (const pattern of PIPE_EXEC) {
116
- if (pattern.test(text)) {
117
- findings.push(warn('SD010', '检测到管道执行(curl/wget → shell)'))
118
- break
119
- }
120
- }
121
-
122
- for (const pattern of CREDENTIAL_PATHS) {
123
- if (pattern.test(text)) {
124
- findings.push(warn('SD011', '正文引用了凭据路径'))
125
- break
126
- }
127
- }
128
-
129
- for (const pattern of EXFIL_SHAPES) {
130
- if (pattern.test(text)) {
131
- findings.push(warn('SD012', '检测到外传形状(请求体读文件)'))
132
- break
133
- }
134
- }
135
-
136
- if (URL_PAYLOAD.test(text)) {
137
- findings.push(warn('SD013', 'URL 查询串疑似携带编码载荷'))
138
- }
139
-
140
- return {
141
- verdict: aggregate(findings),
142
- findings,
143
- stats: {
144
- name: context.name,
145
- bytes,
146
- chars: bodyChars,
147
- lines: lineCount,
148
- disguise: disguiseHits,
149
- encoding: encodingHits,
150
- },
151
- }
152
- }
153
-
154
- /**
155
- * 聚合判定:任一 fail → fail;否则任一 warn → warn;否则 ok。
156
- * 空 findings 是 **ok**(不是 skipped)—— 我们确实检查了,没发现问题。
157
- */
158
- export function aggregate(findings) {
159
- if (findings.some(f => f.verdict === VERDICT.FAIL)) return VERDICT.FAIL
160
- if (findings.some(f => f.verdict === VERDICT.WARN)) return VERDICT.WARN
161
- return VERDICT.OK
162
- }
163
-
164
- /** 只有 warn / fail 可处置(共用一个谓词,防两处语义漂移)。 */
165
- export function isActionable(verdict) {
166
- return verdict === VERDICT.WARN || verdict === VERDICT.FAIL
167
- }
1
+ /**
2
+ * 审计规则表:阻断级 + 警告级。
3
+ *
4
+ * 判定语义四值(dsh-plugin-dev §6):`ok` / `warn` / `fail` / `skipped`。
5
+ * 「无法判定」必须是 `skipped`,不能当 `warn` —— 否则全量假阳性。
6
+ *
7
+ * 设计纪律:
8
+ * - 外传形状只看**结构**(`curl -d @file`),不枚举来源命令、不做域名判断
9
+ * (域名判断必然失效且产生误报)
10
+ * - 每条规则一个正例 + 一个反例(单测钉死)
11
+ */
12
+ import { normalizeText, detectEncodingDisguise, charLength } from './normalize.js'
13
+
14
+ export const VERDICT = Object.freeze({
15
+ OK: 'ok',
16
+ WARN: 'warn',
17
+ FAIL: 'fail',
18
+ SKIPPED: 'skipped',
19
+ })
20
+
21
+ /** 体积/数量上限。 */
22
+ export const LIMITS = Object.freeze({
23
+ skillBytes: 256 * 1024,
24
+ bodyChars: 32_000,
25
+ lines: 4_000,
26
+ })
27
+
28
+ /** 凭据路径(出现在正文里 = 可能在读取用户机密)。 */
29
+ const CREDENTIAL_PATHS = [
30
+ /\.ssh\/(id_rsa|id_ed25519|id_ecdsa)/i,
31
+ /\.aws\/credentials/i,
32
+ /\.npmrc\b/i,
33
+ /\.netrc\b/i,
34
+ /\.git-credentials\b/i,
35
+ /AppData[\\/]Roaming[\\/]npm[\\/]etc[\\/]npmrc/i,
36
+ ]
37
+
38
+ /** 管道执行:`curl … | sh` / `wget … | bash` / `eval $(curl …)`。 */
39
+ const PIPE_EXEC = [
40
+ /\b(curl|wget|fetch)\b[^\n|]*\|\s*(sudo\s+)?(ba|z|k)?sh\b/i,
41
+ /\b(curl|wget)\b[^\n|]*\|\s*sudo\b/i,
42
+ /\beval\s+\$\(\s*(curl|wget)\b/i,
43
+ ]
44
+
45
+ /**
46
+ * 外传形状:只看「把文件内容塞进请求体」这个**结构**。
47
+ * 不判断域名、不枚举来源命令。
48
+ */
49
+ const EXFIL_SHAPES = [
50
+ // curl -d @file / --data @file / --data-binary @file
51
+ /\bcurl\b[^\n]*(?:-d|--data(?:-binary|-raw)?)\s*@[\w./\\-]+/i,
52
+ // curl -d @- (从 stdin 读,管道链的终点)
53
+ /\bcurl\b[^\n]*(?:-d|--data(?:-binary|-raw)?)\s*@-/i,
54
+ // 表单上传到变量/任意 host 的 --upload-file
55
+ /\bcurl\b[^\n]*--upload-file\s+[\w./\\-]+/i,
56
+ ]
57
+
58
+ /** URL 里带长 base64 查询串 = 数据塞 URL 外传。 */
59
+ const URL_PAYLOAD = /https?:\/\/[^\s"']+\?[^\s"']*=[A-Za-z0-9+/%]{120,}/
60
+
61
+ function fail(rule, message) {
62
+ return { rule, verdict: VERDICT.FAIL, message }
63
+ }
64
+ function warn(rule, message) {
65
+ return { rule, verdict: VERDICT.WARN, message }
66
+ }
67
+
68
+ /**
69
+ * 审计一个技能的文本。
70
+ * @param {string} raw 原始文件内容
71
+ * @param {{ name?: string }} [context]
72
+ * @returns {{ verdict: string, findings: Array<{rule:string,verdict:string,message:string}>, stats: object }}
73
+ */
74
+ export function auditSkillText(raw, context = {}) {
75
+ const findings = []
76
+
77
+ if (typeof raw !== 'string') {
78
+ return { verdict: VERDICT.SKIPPED, findings: [], stats: { reason: 'no-content', name: context.name } }
79
+ }
80
+
81
+ const { text, findings: disguiseHits } = normalizeText(raw)
82
+ const encodingHits = detectEncodingDisguise(raw)
83
+
84
+ // ---------- 阻断级 ----------
85
+ if (disguiseHits.includes('nul-byte') || disguiseHits.includes('c0-control')) {
86
+ findings.push(fail('SD001', `正文含控制字符:${disguiseHits.join(', ')}`))
87
+ }
88
+ if (disguiseHits.includes('invisible-format')) {
89
+ findings.push(fail('SD002', '正文含零宽/不可见格式字符(可隐藏指令)'))
90
+ }
91
+ if (disguiseHits.includes('unicode-tag')) {
92
+ findings.push(fail('SD003', '正文含 Unicode tag 字符(夹带隐藏文本)'))
93
+ }
94
+ if (disguiseHits.includes('bidi-control')) {
95
+ findings.push(fail('SD004', '正文含双向控制字符(显示与执行不一致)'))
96
+ }
97
+ if (encodingHits.length > 0) {
98
+ findings.push(fail('SD005', `编码伪装:${encodingHits.join(', ')}`))
99
+ }
100
+
101
+ const bytes = Buffer.byteLength(raw, 'utf8')
102
+ if (bytes > LIMITS.skillBytes) {
103
+ findings.push(fail('SD006', `体积 ${bytes}B 超过上限 ${LIMITS.skillBytes}B`))
104
+ }
105
+ const bodyChars = charLength(text)
106
+ if (bodyChars > LIMITS.bodyChars) {
107
+ findings.push(fail('SD007', `正文 ${bodyChars} 字符超过上限 ${LIMITS.bodyChars}`))
108
+ }
109
+ const lineCount = text.split('\n').length
110
+ if (lineCount > LIMITS.lines) {
111
+ findings.push(fail('SD008', `行数 ${lineCount} 超过上限 ${LIMITS.lines}`))
112
+ }
113
+
114
+ // ---------- 警告级 ----------
115
+ for (const pattern of PIPE_EXEC) {
116
+ if (pattern.test(text)) {
117
+ findings.push(warn('SD010', '检测到管道执行(curl/wget → shell)'))
118
+ break
119
+ }
120
+ }
121
+
122
+ for (const pattern of CREDENTIAL_PATHS) {
123
+ if (pattern.test(text)) {
124
+ findings.push(warn('SD011', '正文引用了凭据路径'))
125
+ break
126
+ }
127
+ }
128
+
129
+ for (const pattern of EXFIL_SHAPES) {
130
+ if (pattern.test(text)) {
131
+ findings.push(warn('SD012', '检测到外传形状(请求体读文件)'))
132
+ break
133
+ }
134
+ }
135
+
136
+ if (URL_PAYLOAD.test(text)) {
137
+ findings.push(warn('SD013', 'URL 查询串疑似携带编码载荷'))
138
+ }
139
+
140
+ return {
141
+ verdict: aggregate(findings),
142
+ findings,
143
+ stats: {
144
+ name: context.name,
145
+ bytes,
146
+ chars: bodyChars,
147
+ lines: lineCount,
148
+ disguise: disguiseHits,
149
+ encoding: encodingHits,
150
+ },
151
+ }
152
+ }
153
+
154
+ /**
155
+ * 聚合判定:任一 fail → fail;否则任一 warn → warn;否则 ok。
156
+ * 空 findings 是 **ok**(不是 skipped)—— 我们确实检查了,没发现问题。
157
+ */
158
+ export function aggregate(findings) {
159
+ if (findings.some(f => f.verdict === VERDICT.FAIL)) return VERDICT.FAIL
160
+ if (findings.some(f => f.verdict === VERDICT.WARN)) return VERDICT.WARN
161
+ return VERDICT.OK
162
+ }
163
+
164
+ /** 只有 warn / fail 可处置(共用一个谓词,防两处语义漂移)。 */
165
+ export function isActionable(verdict) {
166
+ return verdict === VERDICT.WARN || verdict === VERDICT.FAIL
167
+ }