dsh-skill-harbor 0.2.0-rc.4 → 0.2.0-rc.5
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/dsh.plugin.json +1 -1
- package/lib/audit/normalize.js +77 -0
- package/lib/audit/rules.js +167 -0
- package/lib/budget/announcement.js +53 -0
- package/lib/client/api.js +79 -0
- package/lib/client/labels.js +6 -0
- package/lib/client/locales.js +688 -0
- package/lib/client/panel.js +284 -0
- package/lib/client.js +13 -4
- package/lib/config.js +42 -0
- package/lib/index.js +14 -6
- package/lib/market/install.js +169 -0
- package/lib/market/zip.js +201 -0
- package/lib/routes/index.js +385 -255
- package/package.json +1 -1
- package/src/audit/normalize.js +77 -0
- package/src/audit/rules.js +167 -0
- package/src/budget/announcement.js +53 -0
- package/src/client/api.js +79 -0
- package/src/client/labels.js +6 -0
- package/src/client/locales.js +688 -0
- package/src/client/panel.js +284 -0
- package/src/client.js +13 -4
- package/src/config.js +42 -0
- package/src/index.js +14 -6
- package/src/market/install.js +169 -0
- package/src/market/zip.js +201 -0
- package/src/routes/index.js +385 -255
package/dsh.plugin.json
CHANGED
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 字符预算 —— 渐进披露第一层到底占多少上下文。
|
|
3
|
+
*
|
|
4
|
+
* 只算 `name + description + whenToUse` 三层字段,**正文不计入**:
|
|
5
|
+
* 正文是第二层(按需加载),把它算进公告预算会系统性高估。
|
|
6
|
+
*/
|
|
7
|
+
import { charLength } from '../audit/normalize.js'
|
|
8
|
+
|
|
9
|
+
/** 单条技能的公告字符数。 */
|
|
10
|
+
export function announcementCharsOf(skill) {
|
|
11
|
+
if (skill === null || typeof skill !== 'object') return 0
|
|
12
|
+
return (
|
|
13
|
+
charLength(skill.name ?? '') +
|
|
14
|
+
charLength(skill.description ?? '') +
|
|
15
|
+
charLength(skill.whenToUse ?? '')
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 整个目录的字符账。
|
|
21
|
+
* 分开统计「全部」与「模型可见」—— 遮蔽掉的与 disable-model-invocation
|
|
22
|
+
* 关掉的都不进模型上下文,混在一起会误导用户。
|
|
23
|
+
*/
|
|
24
|
+
export function budgetReport(observations, options = {}) {
|
|
25
|
+
const { perSkillLimit = 4000 } = options
|
|
26
|
+
|
|
27
|
+
const rows = observations.map(observation => {
|
|
28
|
+
const chars = announcementCharsOf(observation)
|
|
29
|
+
return {
|
|
30
|
+
name: observation.name,
|
|
31
|
+
source: observation.source,
|
|
32
|
+
chars,
|
|
33
|
+
overLimit: chars > perSkillLimit,
|
|
34
|
+
modelInvocable: observation.modelInvocable === true,
|
|
35
|
+
shadowed: observation.shadowedBy !== undefined,
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const announced = rows.filter(row => !row.shadowed && row.modelInvocable)
|
|
40
|
+
const skipped = rows.filter(row => row.shadowed || !row.modelInvocable)
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
total: rows.reduce((sum, row) => sum + row.chars, 0),
|
|
44
|
+
announcedTotal: announced.reduce((sum, row) => sum + row.chars, 0),
|
|
45
|
+
skippedTotal: skipped.reduce((sum, row) => sum + row.chars, 0),
|
|
46
|
+
count: rows.length,
|
|
47
|
+
announcedCount: announced.length,
|
|
48
|
+
skippedCount: skipped.length,
|
|
49
|
+
perSkillLimit,
|
|
50
|
+
overLimit: announced.filter(row => row.overLimit),
|
|
51
|
+
rows,
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 客户端唯一 fetch 出口。
|
|
3
|
+
*
|
|
4
|
+
* 必须按 `document.baseURI` 解析 —— 反代带路径前缀(如 `/app/dsh/`)部署时,
|
|
5
|
+
* 写死 `/api/...` 会打到站点根而不是应用根,全部 404。
|
|
6
|
+
*/
|
|
7
|
+
import { LOCALES } from './locales.js'
|
|
8
|
+
|
|
9
|
+
export const API_PREFIX = '/dsh-skill-harbor/api'
|
|
10
|
+
export const CSRF_HEADER = 'x-dsh-skill-harbor'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 相对路径 → 绝对 URL,锚定到 document.baseURI。
|
|
14
|
+
* 非 `/` 开头或无 document 环境时原样返回。
|
|
15
|
+
*/
|
|
16
|
+
export function resolveApiUrl(path) {
|
|
17
|
+
if (typeof path !== 'string') return String(path)
|
|
18
|
+
try {
|
|
19
|
+
if (path.startsWith('/') && typeof document !== 'undefined' && document.baseURI) {
|
|
20
|
+
return new URL(path.slice(1), document.baseURI).href
|
|
21
|
+
}
|
|
22
|
+
} catch {
|
|
23
|
+
/* 落到原样返回 */
|
|
24
|
+
}
|
|
25
|
+
return path
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function request(path, options = {}) {
|
|
29
|
+
const url = resolveApiUrl(`${API_PREFIX}${path}`)
|
|
30
|
+
const method = options.method ?? 'GET'
|
|
31
|
+
|
|
32
|
+
const headers = { ...options.headers }
|
|
33
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
34
|
+
// 写方法必须带自定义头;跨站表单设不了这个头
|
|
35
|
+
headers[CSRF_HEADER] = '1'
|
|
36
|
+
if (options.body !== undefined) headers['content-type'] = 'application/json'
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const response = await fetch(url, {
|
|
40
|
+
method,
|
|
41
|
+
headers,
|
|
42
|
+
credentials: 'same-origin',
|
|
43
|
+
...options.body === undefined ? {} : { body: JSON.stringify(options.body) },
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
let payload = null
|
|
47
|
+
try {
|
|
48
|
+
payload = await response.json()
|
|
49
|
+
} catch {
|
|
50
|
+
payload = null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
const error = new Error(payload?.message ?? payload?.error ?? `HTTP ${response.status}`)
|
|
55
|
+
error.status = response.status
|
|
56
|
+
error.payload = payload
|
|
57
|
+
throw error
|
|
58
|
+
}
|
|
59
|
+
return payload
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const api = {
|
|
63
|
+
list: () => request('/list'),
|
|
64
|
+
read: path => request(`/read?path=${encodeURIComponent(path)}`),
|
|
65
|
+
audit: () => request('/audit'),
|
|
66
|
+
budget: () => request('/budget'),
|
|
67
|
+
setEnabled: (path, enabled) => request('/set-enabled', { method: 'POST', body: { path, enabled } }),
|
|
68
|
+
update: (path, content) => request('/update', { method: 'POST', body: { path, content } }),
|
|
69
|
+
remove: path => request('/delete', { method: 'POST', body: { path } }),
|
|
70
|
+
importArchive: (archive, targetSource) =>
|
|
71
|
+
request('/import', { method: 'POST', body: { archive, targetSource } }),
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 取某语言的字典,缺省回落 zh。 */
|
|
75
|
+
export function dict(lang) {
|
|
76
|
+
return LOCALES[lang] ?? LOCALES.zh
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { request }
|