dsh-skill-harbor 0.5.4 → 0.6.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.
- package/LICENSE +21 -21
- package/README.md +44 -44
- package/cordis.patch.yml +4 -4
- package/dsh.plugin.json +1 -1
- package/package.json +2 -1
- package/src/audit/normalize.js +77 -77
- package/src/audit/rules.js +167 -167
- package/src/budget/announcement.js +53 -53
- package/src/client/api.js +79 -79
- package/src/client/labels.js +6 -6
- package/src/client/locales.js +688 -688
- package/src/collect/frontmatter.js +169 -169
- package/src/collect/observe.js +143 -143
- package/src/collect/ranks.js +56 -56
- package/src/collect/roots.js +92 -92
- package/src/collect/shadow.js +68 -68
- package/src/collect/watcher.js +86 -86
- package/src/config.js +42 -42
- package/src/market/fetch.js +94 -94
- package/src/market/install.js +169 -169
- package/src/market/source.js +90 -90
- package/src/market/store.js +66 -66
- package/src/market/zip.js +201 -201
- package/src/provider/lifecycle.js +74 -74
- package/src/routes/guard.js +156 -156
- package/src/routes/safe-path.js +96 -96
- package/src/routes/store.js +72 -72
- package/src/settings.js +83 -83
package/src/config.js
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 插件配置 schema(0.1.7+ 形态)。
|
|
3
|
-
*
|
|
4
|
-
* 按 Phase 0 实测:`installSection` 在 0.1.7 已移除,设置卡由 Config 字段
|
|
5
|
-
* 标 `.volatile()` 自动生成。这里只声明字段语义;具体 schema 对象由宿主
|
|
6
|
-
* 的 config schema 机制消费。
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/** 单技能公告字符上限的默认值。 */
|
|
10
|
-
export const DEFAULT_BUDGET_LIMIT = 4000
|
|
11
|
-
|
|
12
|
-
/** systemPrompt 公告的硬上限(checklist:开启时 ≤200 字)。 */
|
|
13
|
-
export const ANNOUNCEMENT_HARD_LIMIT = 200
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 配置的默认值与归一化。
|
|
17
|
-
* `managementOnly` 为 true 时**不注册 provider、不注册工具、不碰技能目录**。
|
|
18
|
-
*/
|
|
19
|
-
export function normalizeConfig(input = {}) {
|
|
20
|
-
return {
|
|
21
|
-
cwd: typeof input.cwd === 'string' ? input.cwd : process.cwd(),
|
|
22
|
-
dshHome: typeof input.dshHome === 'string' ? input.dshHome : undefined,
|
|
23
|
-
agentsHome: typeof input.agentsHome === 'string' ? input.agentsHome : undefined,
|
|
24
|
-
customSkillDirs: Array.isArray(input.customSkillDirs) ? input.customSkillDirs : [],
|
|
25
|
-
bundledSkillDir: typeof input.bundledSkillDir === 'string' ? input.bundledSkillDir : undefined,
|
|
26
|
-
|
|
27
|
-
budgetLimit: Number.isFinite(input.budgetLimit) && input.budgetLimit > 0
|
|
28
|
-
? input.budgetLimit
|
|
29
|
-
: DEFAULT_BUDGET_LIMIT,
|
|
30
|
-
|
|
31
|
-
// 默认**不**往 systemPrompt 写公告
|
|
32
|
-
announceToSystemPrompt: input.announceToSystemPrompt === true,
|
|
33
|
-
|
|
34
|
-
managementOnly: input.managementOnly === true,
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** 公告文本裁剪到硬上限(按码点,不切坏 emoji)。 */
|
|
39
|
-
export function clampAnnouncement(text) {
|
|
40
|
-
const chars = [...String(text ?? '')]
|
|
41
|
-
return chars.length <= ANNOUNCEMENT_HARD_LIMIT ? chars.join('') : chars.slice(0, ANNOUNCEMENT_HARD_LIMIT).join('')
|
|
42
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 插件配置 schema(0.1.7+ 形态)。
|
|
3
|
+
*
|
|
4
|
+
* 按 Phase 0 实测:`installSection` 在 0.1.7 已移除,设置卡由 Config 字段
|
|
5
|
+
* 标 `.volatile()` 自动生成。这里只声明字段语义;具体 schema 对象由宿主
|
|
6
|
+
* 的 config schema 机制消费。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** 单技能公告字符上限的默认值。 */
|
|
10
|
+
export const DEFAULT_BUDGET_LIMIT = 4000
|
|
11
|
+
|
|
12
|
+
/** systemPrompt 公告的硬上限(checklist:开启时 ≤200 字)。 */
|
|
13
|
+
export const ANNOUNCEMENT_HARD_LIMIT = 200
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 配置的默认值与归一化。
|
|
17
|
+
* `managementOnly` 为 true 时**不注册 provider、不注册工具、不碰技能目录**。
|
|
18
|
+
*/
|
|
19
|
+
export function normalizeConfig(input = {}) {
|
|
20
|
+
return {
|
|
21
|
+
cwd: typeof input.cwd === 'string' ? input.cwd : process.cwd(),
|
|
22
|
+
dshHome: typeof input.dshHome === 'string' ? input.dshHome : undefined,
|
|
23
|
+
agentsHome: typeof input.agentsHome === 'string' ? input.agentsHome : undefined,
|
|
24
|
+
customSkillDirs: Array.isArray(input.customSkillDirs) ? input.customSkillDirs : [],
|
|
25
|
+
bundledSkillDir: typeof input.bundledSkillDir === 'string' ? input.bundledSkillDir : undefined,
|
|
26
|
+
|
|
27
|
+
budgetLimit: Number.isFinite(input.budgetLimit) && input.budgetLimit > 0
|
|
28
|
+
? input.budgetLimit
|
|
29
|
+
: DEFAULT_BUDGET_LIMIT,
|
|
30
|
+
|
|
31
|
+
// 默认**不**往 systemPrompt 写公告
|
|
32
|
+
announceToSystemPrompt: input.announceToSystemPrompt === true,
|
|
33
|
+
|
|
34
|
+
managementOnly: input.managementOnly === true,
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 公告文本裁剪到硬上限(按码点,不切坏 emoji)。 */
|
|
39
|
+
export function clampAnnouncement(text) {
|
|
40
|
+
const chars = [...String(text ?? '')]
|
|
41
|
+
return chars.length <= ANNOUNCEMENT_HARD_LIMIT ? chars.join('') : chars.slice(0, ANNOUNCEMENT_HARD_LIMIT).join('')
|
|
42
|
+
}
|
package/src/market/fetch.js
CHANGED
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 仓库抓取与更新检查。
|
|
3
|
-
*
|
|
4
|
-
* 安全纪律:
|
|
5
|
-
* - 只接受 http(s),且只拼接受信主机(GitHub 系)——不跟随任意 URL
|
|
6
|
-
* - 归档下载后**不执行任何仓库代码**,只解包 + 审计
|
|
7
|
-
* - 更新检查只读 ref → sha 的映射,不改盘
|
|
8
|
-
*/
|
|
9
|
-
import { createHash } from 'node:crypto'
|
|
10
|
-
|
|
11
|
-
import { isCommitSha, makeSource, checkUpdate, applySync } from './source.js'
|
|
12
|
-
|
|
13
|
-
/** 允许的主机白名单(不跟随任意 URL)。 */
|
|
14
|
-
export const ALLOWED_HOSTS = new Set([
|
|
15
|
-
'github.com',
|
|
16
|
-
'api.github.com',
|
|
17
|
-
'codeload.github.com',
|
|
18
|
-
'raw.githubusercontent.com',
|
|
19
|
-
])
|
|
20
|
-
|
|
21
|
-
const FETCH_TIMEOUT_MS = 20_000
|
|
22
|
-
const MAX_ARCHIVE_BYTES = 8 * 1024 * 1024
|
|
23
|
-
|
|
24
|
-
/** 校验并构造一个 GitHub 归档 URL;非白名单主机一律拒绝。 */
|
|
25
|
-
export function archiveUrl({ owner, repo, sha }) {
|
|
26
|
-
if (!isCommitSha(sha)) return { ok: false, reason: 'invalid-sha' }
|
|
27
|
-
if (!/^[\w.-]+$/.test(owner) || !/^[\w.-]+$/.test(repo)) {
|
|
28
|
-
return { ok: false, reason: 'invalid-owner-or-repo' }
|
|
29
|
-
}
|
|
30
|
-
const host = 'codeload.github.com'
|
|
31
|
-
if (!ALLOWED_HOSTS.has(host)) return { ok: false, reason: 'host-not-allowed' }
|
|
32
|
-
return { ok: true, url: `https://${host}/${owner}/${repo}/zip/${sha}` }
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** 校验一个远端 URL 是否可信。 */
|
|
36
|
-
export function isAllowedUrl(raw) {
|
|
37
|
-
try {
|
|
38
|
-
const url = new URL(raw)
|
|
39
|
-
if (url.protocol !== 'https:') return false
|
|
40
|
-
return ALLOWED_HOSTS.has(url.hostname.toLowerCase())
|
|
41
|
-
} catch {
|
|
42
|
-
return false
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 下载归档到内存(带超时与体积上限)。
|
|
48
|
-
* @returns {{ ok: true, buffer: Buffer, sha256: string } | { ok: false, reason: string }}
|
|
49
|
-
*/
|
|
50
|
-
export async function fetchArchive(url, fetchImpl = globalThis.fetch) {
|
|
51
|
-
if (!isAllowedUrl(url)) return { ok: false, reason: 'url-not-allowed' }
|
|
52
|
-
if (typeof fetchImpl !== 'function') return { ok: false, reason: 'fetch-unavailable' }
|
|
53
|
-
|
|
54
|
-
const controller = new AbortController()
|
|
55
|
-
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS)
|
|
56
|
-
try {
|
|
57
|
-
const response = await fetchImpl(url, { signal: controller.signal, redirect: 'follow' })
|
|
58
|
-
if (!response.ok) return { ok: false, reason: `http-${response.status}` }
|
|
59
|
-
|
|
60
|
-
const declared = Number(response.headers?.get?.('content-length') ?? '0')
|
|
61
|
-
if (declared > MAX_ARCHIVE_BYTES) {
|
|
62
|
-
return { ok: false, reason: 'archive-too-large', declared }
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const buffer = Buffer.from(await response.arrayBuffer())
|
|
66
|
-
if (buffer.length > MAX_ARCHIVE_BYTES) {
|
|
67
|
-
return { ok: false, reason: 'archive-too-large', actual: buffer.length }
|
|
68
|
-
}
|
|
69
|
-
return { ok: true, buffer, sha256: createHash('sha256').update(buffer).digest('hex') }
|
|
70
|
-
} catch (error) {
|
|
71
|
-
const reason = error?.name === 'AbortError' ? 'timeout' : 'fetch-failed'
|
|
72
|
-
return { ok: false, reason, message: String(error?.message ?? error) }
|
|
73
|
-
} finally {
|
|
74
|
-
clearTimeout(timer)
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** 解析 GitHub commit API 的响应,取出完整 sha。 */
|
|
79
|
-
export function parseCommitResponse(payload) {
|
|
80
|
-
if (payload === null || typeof payload !== 'object') {
|
|
81
|
-
return { ok: false, reason: 'not-an-object' }
|
|
82
|
-
}
|
|
83
|
-
const sha = payload.sha
|
|
84
|
-
if (!isCommitSha(sha)) return { ok: false, reason: 'invalid-sha' }
|
|
85
|
-
return { ok: true, sha: String(sha).toLowerCase() }
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** 由一次成功的 ref 解析生成钉死的源记录。 */
|
|
89
|
-
export function sourceFromResolve({ owner, repo, ref, sha }) {
|
|
90
|
-
if (!isCommitSha(sha)) return { ok: false, reason: 'invalid-sha' }
|
|
91
|
-
return { ok: true, source: makeSource({ owner, repo, ref, commitSha: sha }) }
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export { checkUpdate, applySync }
|
|
1
|
+
/**
|
|
2
|
+
* 仓库抓取与更新检查。
|
|
3
|
+
*
|
|
4
|
+
* 安全纪律:
|
|
5
|
+
* - 只接受 http(s),且只拼接受信主机(GitHub 系)——不跟随任意 URL
|
|
6
|
+
* - 归档下载后**不执行任何仓库代码**,只解包 + 审计
|
|
7
|
+
* - 更新检查只读 ref → sha 的映射,不改盘
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from 'node:crypto'
|
|
10
|
+
|
|
11
|
+
import { isCommitSha, makeSource, checkUpdate, applySync } from './source.js'
|
|
12
|
+
|
|
13
|
+
/** 允许的主机白名单(不跟随任意 URL)。 */
|
|
14
|
+
export const ALLOWED_HOSTS = new Set([
|
|
15
|
+
'github.com',
|
|
16
|
+
'api.github.com',
|
|
17
|
+
'codeload.github.com',
|
|
18
|
+
'raw.githubusercontent.com',
|
|
19
|
+
])
|
|
20
|
+
|
|
21
|
+
const FETCH_TIMEOUT_MS = 20_000
|
|
22
|
+
const MAX_ARCHIVE_BYTES = 8 * 1024 * 1024
|
|
23
|
+
|
|
24
|
+
/** 校验并构造一个 GitHub 归档 URL;非白名单主机一律拒绝。 */
|
|
25
|
+
export function archiveUrl({ owner, repo, sha }) {
|
|
26
|
+
if (!isCommitSha(sha)) return { ok: false, reason: 'invalid-sha' }
|
|
27
|
+
if (!/^[\w.-]+$/.test(owner) || !/^[\w.-]+$/.test(repo)) {
|
|
28
|
+
return { ok: false, reason: 'invalid-owner-or-repo' }
|
|
29
|
+
}
|
|
30
|
+
const host = 'codeload.github.com'
|
|
31
|
+
if (!ALLOWED_HOSTS.has(host)) return { ok: false, reason: 'host-not-allowed' }
|
|
32
|
+
return { ok: true, url: `https://${host}/${owner}/${repo}/zip/${sha}` }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 校验一个远端 URL 是否可信。 */
|
|
36
|
+
export function isAllowedUrl(raw) {
|
|
37
|
+
try {
|
|
38
|
+
const url = new URL(raw)
|
|
39
|
+
if (url.protocol !== 'https:') return false
|
|
40
|
+
return ALLOWED_HOSTS.has(url.hostname.toLowerCase())
|
|
41
|
+
} catch {
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 下载归档到内存(带超时与体积上限)。
|
|
48
|
+
* @returns {{ ok: true, buffer: Buffer, sha256: string } | { ok: false, reason: string }}
|
|
49
|
+
*/
|
|
50
|
+
export async function fetchArchive(url, fetchImpl = globalThis.fetch) {
|
|
51
|
+
if (!isAllowedUrl(url)) return { ok: false, reason: 'url-not-allowed' }
|
|
52
|
+
if (typeof fetchImpl !== 'function') return { ok: false, reason: 'fetch-unavailable' }
|
|
53
|
+
|
|
54
|
+
const controller = new AbortController()
|
|
55
|
+
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS)
|
|
56
|
+
try {
|
|
57
|
+
const response = await fetchImpl(url, { signal: controller.signal, redirect: 'follow' })
|
|
58
|
+
if (!response.ok) return { ok: false, reason: `http-${response.status}` }
|
|
59
|
+
|
|
60
|
+
const declared = Number(response.headers?.get?.('content-length') ?? '0')
|
|
61
|
+
if (declared > MAX_ARCHIVE_BYTES) {
|
|
62
|
+
return { ok: false, reason: 'archive-too-large', declared }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const buffer = Buffer.from(await response.arrayBuffer())
|
|
66
|
+
if (buffer.length > MAX_ARCHIVE_BYTES) {
|
|
67
|
+
return { ok: false, reason: 'archive-too-large', actual: buffer.length }
|
|
68
|
+
}
|
|
69
|
+
return { ok: true, buffer, sha256: createHash('sha256').update(buffer).digest('hex') }
|
|
70
|
+
} catch (error) {
|
|
71
|
+
const reason = error?.name === 'AbortError' ? 'timeout' : 'fetch-failed'
|
|
72
|
+
return { ok: false, reason, message: String(error?.message ?? error) }
|
|
73
|
+
} finally {
|
|
74
|
+
clearTimeout(timer)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 解析 GitHub commit API 的响应,取出完整 sha。 */
|
|
79
|
+
export function parseCommitResponse(payload) {
|
|
80
|
+
if (payload === null || typeof payload !== 'object') {
|
|
81
|
+
return { ok: false, reason: 'not-an-object' }
|
|
82
|
+
}
|
|
83
|
+
const sha = payload.sha
|
|
84
|
+
if (!isCommitSha(sha)) return { ok: false, reason: 'invalid-sha' }
|
|
85
|
+
return { ok: true, sha: String(sha).toLowerCase() }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 由一次成功的 ref 解析生成钉死的源记录。 */
|
|
89
|
+
export function sourceFromResolve({ owner, repo, ref, sha }) {
|
|
90
|
+
if (!isCommitSha(sha)) return { ok: false, reason: 'invalid-sha' }
|
|
91
|
+
return { ok: true, source: makeSource({ owner, repo, ref, commitSha: sha }) }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { checkUpdate, applySync }
|
package/src/market/install.js
CHANGED
|
@@ -1,169 +1,169 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 导入安装器 —— 把归档里的技能落到正确的根,或在更弱的根上明确拒绝。
|
|
3
|
-
*
|
|
4
|
-
* 顺序(不可调换):
|
|
5
|
-
* 解包 → 审计四步 → 落点判定(rank 遮蔽) → 写
|
|
6
|
-
*
|
|
7
|
-
* 「审计先行」是关键:先落盘再审计等于把恶意文件写进用户磁盘,
|
|
8
|
-
* 哪怕随后删掉也留下过时间窗。
|
|
9
|
-
*/
|
|
10
|
-
import { mkdirSync, writeFileSync, existsSync } from 'node:fs'
|
|
11
|
-
import { basename, dirname, join, posix } from 'node:path'
|
|
12
|
-
|
|
13
|
-
import { extractTextFiles, stripCommonPrefix, IMPORT_LIMITS, ZipError } from './zip.js'
|
|
14
|
-
import { auditSkillText } from '../audit/rules.js'
|
|
15
|
-
import { safePath, MAX_SEGMENTS } from '../routes/safe-path.js'
|
|
16
|
-
import { rankOf } from '../collect/ranks.js'
|
|
17
|
-
|
|
18
|
-
/** 从一个 zip 里抽出所有技能(每个含 SKILL.md 的目录是一个技能)。 */
|
|
19
|
-
export function parseSkillsFromArchive(buffer) {
|
|
20
|
-
const { files, skippedSymlinks, totalBytes } = extractTextFiles(buffer, IMPORT_LIMITS)
|
|
21
|
-
|
|
22
|
-
const stripped = stripCommonPrefix(files.map(f => f.name))
|
|
23
|
-
const byDir = new Map()
|
|
24
|
-
|
|
25
|
-
for (let i = 0; i < files.length; i += 1) {
|
|
26
|
-
const path = stripped[i]
|
|
27
|
-
const content = files[i].content
|
|
28
|
-
if (!path.toLowerCase().endsWith('skill.md')) continue
|
|
29
|
-
|
|
30
|
-
const dir = posix.dirname(path)
|
|
31
|
-
const name = dir === '.' ? basename(dirname(path)) : posix.basename(dir)
|
|
32
|
-
const siblings = files
|
|
33
|
-
.map((f, index) => ({ path: stripped[index], content: f.content }))
|
|
34
|
-
.filter(item => posix.dirname(item.path) === dir)
|
|
35
|
-
|
|
36
|
-
byDir.set(dir === '.' ? '' : dir, { name, dir, content, siblings })
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (byDir.size === 0) {
|
|
40
|
-
throw new ZipError('no-skills', '归档里没有找到含 SKILL.md 的技能')
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return { skills: [...byDir.values()], skippedSymlinks, totalBytes }
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 判定落点:目标根必须存在,且不能让新技能被更强的根遮蔽。
|
|
48
|
-
*
|
|
49
|
-
* @param {object} options
|
|
50
|
-
* @param {string} options.skillName
|
|
51
|
-
* @param {{path:string, source:string, rank:number}} options.targetRoot 想写入的根
|
|
52
|
-
* @param {Array<{path:string, source:string, rank:number}>} options.allRoots 全部根
|
|
53
|
-
* @param {Array<object>} options.observations 当前扫描结果
|
|
54
|
-
*/
|
|
55
|
-
export function decidePlacement({ skillName, targetRoot, allRoots, observations }) {
|
|
56
|
-
const targetRank = rankOf(targetRoot.source)
|
|
57
|
-
|
|
58
|
-
// 已存在同名条目:只有 target 比所有现存的都**更强或相等**才允许覆盖,
|
|
59
|
-
// 否则写进去也是死的(会被遮蔽),报 shadowed 且不写。
|
|
60
|
-
const existing = observations.filter(
|
|
61
|
-
o => o.name.toLowerCase() === String(skillName).toLowerCase() && o.shadowedBy === undefined,
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
if (existing.length > 0) {
|
|
65
|
-
const winner = existing[0]
|
|
66
|
-
const winnerRank = rankOf(winner.source)
|
|
67
|
-
if (winnerRank < targetRank) {
|
|
68
|
-
return {
|
|
69
|
-
ok: false,
|
|
70
|
-
reason: 'shadowed',
|
|
71
|
-
message: `同名技能 "${winner.name}" 已存在于更强的根(${winner.source}, rank ${winnerRank}),写入 ${targetRoot.source}(rank ${targetRank})会被遮蔽,未写入`,
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
if (winnerRank === targetRank && winner.path !== undefined) {
|
|
75
|
-
return {
|
|
76
|
-
ok: false,
|
|
77
|
-
reason: 'already-exists',
|
|
78
|
-
message: `同名技能已存在于同一 rank 的根:${winner.path}`,
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// 目标目录的每一段都要过 safePath
|
|
84
|
-
const probe = join(targetRoot.path, skillName, 'SKILL.md')
|
|
85
|
-
const safe = safePath(probe)
|
|
86
|
-
if (!safe.ok) {
|
|
87
|
-
return { ok: false, reason: safe.reason, message: `目标路径不安全:${safe.reason}` }
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return { ok: true, targetDir: join(targetRoot.path, skillName) }
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* 安装一个技能到磁盘。审计未过则**不写任何文件**。
|
|
95
|
-
*
|
|
96
|
-
* @returns {{ installed: boolean, reason?: string, message?: string, files?: string[], verdict?: string }}
|
|
97
|
-
*/
|
|
98
|
-
export function installSkill({ skill, targetDir, dependencies = true }) {
|
|
99
|
-
// 1) 审计主体
|
|
100
|
-
const audit = auditSkillText(skill.content, { name: skill.name })
|
|
101
|
-
if (audit.verdict === 'fail') {
|
|
102
|
-
return {
|
|
103
|
-
installed: false,
|
|
104
|
-
reason: 'audit-failed',
|
|
105
|
-
verdict: audit.verdict,
|
|
106
|
-
message: `审计阻断:${audit.findings.map(f => `${f.rule} ${f.message}`).join('; ')}`,
|
|
107
|
-
findings: audit.findings,
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// 2) 目录安全
|
|
112
|
-
const dirSafe = safePath(targetDir)
|
|
113
|
-
if (!dirSafe.ok) {
|
|
114
|
-
return { installed: false, reason: dirSafe.reason, message: `目标目录不安全:${dirSafe.reason}` }
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// 3) 同目录其它文件也过审计(技能可以带脚本)
|
|
118
|
-
const sidecars = (skill.siblings ?? []).filter(item => !item.path.toLowerCase().endsWith('skill.md'))
|
|
119
|
-
for (const file of sidecars) {
|
|
120
|
-
const fileAudit = auditSkillText(file.content, { name: file.path })
|
|
121
|
-
if (fileAudit.verdict === 'fail') {
|
|
122
|
-
return {
|
|
123
|
-
installed: false,
|
|
124
|
-
reason: 'audit-failed',
|
|
125
|
-
verdict: fileAudit.verdict,
|
|
126
|
-
message: `附属文件 ${file.path} 审计阻断:${fileAudit.findings.map(f => `${f.rule} ${f.message}`).join('; ')}`,
|
|
127
|
-
findings: fileAudit.findings,
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
const relSafe = safePath(posix.normalize(file.path))
|
|
131
|
-
if (!relSafe.ok || posix.normalize(file.path).split('/').length > MAX_SEGMENTS) {
|
|
132
|
-
return { installed: false, reason: 'unsafe-sidecar-path', message: `附属文件路径不安全:${file.path}` }
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// 4) 落盘(只有到这里才开始写磁盘)
|
|
137
|
-
const written = []
|
|
138
|
-
try {
|
|
139
|
-
mkdirSync(targetDir, { recursive: true })
|
|
140
|
-
writeFileSync(join(targetDir, 'SKILL.md'), skill.content, 'utf8')
|
|
141
|
-
written.push(join(targetDir, 'SKILL.md'))
|
|
142
|
-
|
|
143
|
-
for (const file of sidecars) {
|
|
144
|
-
const relative = posix.normalize(file.path)
|
|
145
|
-
const dest = join(targetDir, relative)
|
|
146
|
-
// 再确认一次没跑出 targetDir(`..` 已被 safePath 挡,这是纵深防御)
|
|
147
|
-
if (!dest.startsWith(targetDir)) {
|
|
148
|
-
return { installed: false, reason: 'escaped-target', message: `附属文件越出目标目录:${file.path}` }
|
|
149
|
-
}
|
|
150
|
-
mkdirSync(dirname(dest), { recursive: true })
|
|
151
|
-
writeFileSync(dest, file.content, 'utf8')
|
|
152
|
-
written.push(dest)
|
|
153
|
-
}
|
|
154
|
-
} catch (error) {
|
|
155
|
-
return {
|
|
156
|
-
installed: false,
|
|
157
|
-
reason: 'write-failed',
|
|
158
|
-
message: `写入失败:${error?.message ?? error}`,
|
|
159
|
-
files: written,
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return {
|
|
164
|
-
installed: true,
|
|
165
|
-
verdict: audit.verdict,
|
|
166
|
-
findings: audit.findings,
|
|
167
|
-
files: written,
|
|
168
|
-
}
|
|
169
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 导入安装器 —— 把归档里的技能落到正确的根,或在更弱的根上明确拒绝。
|
|
3
|
+
*
|
|
4
|
+
* 顺序(不可调换):
|
|
5
|
+
* 解包 → 审计四步 → 落点判定(rank 遮蔽) → 写
|
|
6
|
+
*
|
|
7
|
+
* 「审计先行」是关键:先落盘再审计等于把恶意文件写进用户磁盘,
|
|
8
|
+
* 哪怕随后删掉也留下过时间窗。
|
|
9
|
+
*/
|
|
10
|
+
import { mkdirSync, writeFileSync, existsSync } from 'node:fs'
|
|
11
|
+
import { basename, dirname, join, posix } from 'node:path'
|
|
12
|
+
|
|
13
|
+
import { extractTextFiles, stripCommonPrefix, IMPORT_LIMITS, ZipError } from './zip.js'
|
|
14
|
+
import { auditSkillText } from '../audit/rules.js'
|
|
15
|
+
import { safePath, MAX_SEGMENTS } from '../routes/safe-path.js'
|
|
16
|
+
import { rankOf } from '../collect/ranks.js'
|
|
17
|
+
|
|
18
|
+
/** 从一个 zip 里抽出所有技能(每个含 SKILL.md 的目录是一个技能)。 */
|
|
19
|
+
export function parseSkillsFromArchive(buffer) {
|
|
20
|
+
const { files, skippedSymlinks, totalBytes } = extractTextFiles(buffer, IMPORT_LIMITS)
|
|
21
|
+
|
|
22
|
+
const stripped = stripCommonPrefix(files.map(f => f.name))
|
|
23
|
+
const byDir = new Map()
|
|
24
|
+
|
|
25
|
+
for (let i = 0; i < files.length; i += 1) {
|
|
26
|
+
const path = stripped[i]
|
|
27
|
+
const content = files[i].content
|
|
28
|
+
if (!path.toLowerCase().endsWith('skill.md')) continue
|
|
29
|
+
|
|
30
|
+
const dir = posix.dirname(path)
|
|
31
|
+
const name = dir === '.' ? basename(dirname(path)) : posix.basename(dir)
|
|
32
|
+
const siblings = files
|
|
33
|
+
.map((f, index) => ({ path: stripped[index], content: f.content }))
|
|
34
|
+
.filter(item => posix.dirname(item.path) === dir)
|
|
35
|
+
|
|
36
|
+
byDir.set(dir === '.' ? '' : dir, { name, dir, content, siblings })
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (byDir.size === 0) {
|
|
40
|
+
throw new ZipError('no-skills', '归档里没有找到含 SKILL.md 的技能')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { skills: [...byDir.values()], skippedSymlinks, totalBytes }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 判定落点:目标根必须存在,且不能让新技能被更强的根遮蔽。
|
|
48
|
+
*
|
|
49
|
+
* @param {object} options
|
|
50
|
+
* @param {string} options.skillName
|
|
51
|
+
* @param {{path:string, source:string, rank:number}} options.targetRoot 想写入的根
|
|
52
|
+
* @param {Array<{path:string, source:string, rank:number}>} options.allRoots 全部根
|
|
53
|
+
* @param {Array<object>} options.observations 当前扫描结果
|
|
54
|
+
*/
|
|
55
|
+
export function decidePlacement({ skillName, targetRoot, allRoots, observations }) {
|
|
56
|
+
const targetRank = rankOf(targetRoot.source)
|
|
57
|
+
|
|
58
|
+
// 已存在同名条目:只有 target 比所有现存的都**更强或相等**才允许覆盖,
|
|
59
|
+
// 否则写进去也是死的(会被遮蔽),报 shadowed 且不写。
|
|
60
|
+
const existing = observations.filter(
|
|
61
|
+
o => o.name.toLowerCase() === String(skillName).toLowerCase() && o.shadowedBy === undefined,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
if (existing.length > 0) {
|
|
65
|
+
const winner = existing[0]
|
|
66
|
+
const winnerRank = rankOf(winner.source)
|
|
67
|
+
if (winnerRank < targetRank) {
|
|
68
|
+
return {
|
|
69
|
+
ok: false,
|
|
70
|
+
reason: 'shadowed',
|
|
71
|
+
message: `同名技能 "${winner.name}" 已存在于更强的根(${winner.source}, rank ${winnerRank}),写入 ${targetRoot.source}(rank ${targetRank})会被遮蔽,未写入`,
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (winnerRank === targetRank && winner.path !== undefined) {
|
|
75
|
+
return {
|
|
76
|
+
ok: false,
|
|
77
|
+
reason: 'already-exists',
|
|
78
|
+
message: `同名技能已存在于同一 rank 的根:${winner.path}`,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 目标目录的每一段都要过 safePath
|
|
84
|
+
const probe = join(targetRoot.path, skillName, 'SKILL.md')
|
|
85
|
+
const safe = safePath(probe)
|
|
86
|
+
if (!safe.ok) {
|
|
87
|
+
return { ok: false, reason: safe.reason, message: `目标路径不安全:${safe.reason}` }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { ok: true, targetDir: join(targetRoot.path, skillName) }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 安装一个技能到磁盘。审计未过则**不写任何文件**。
|
|
95
|
+
*
|
|
96
|
+
* @returns {{ installed: boolean, reason?: string, message?: string, files?: string[], verdict?: string }}
|
|
97
|
+
*/
|
|
98
|
+
export function installSkill({ skill, targetDir, dependencies = true }) {
|
|
99
|
+
// 1) 审计主体
|
|
100
|
+
const audit = auditSkillText(skill.content, { name: skill.name })
|
|
101
|
+
if (audit.verdict === 'fail') {
|
|
102
|
+
return {
|
|
103
|
+
installed: false,
|
|
104
|
+
reason: 'audit-failed',
|
|
105
|
+
verdict: audit.verdict,
|
|
106
|
+
message: `审计阻断:${audit.findings.map(f => `${f.rule} ${f.message}`).join('; ')}`,
|
|
107
|
+
findings: audit.findings,
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 2) 目录安全
|
|
112
|
+
const dirSafe = safePath(targetDir)
|
|
113
|
+
if (!dirSafe.ok) {
|
|
114
|
+
return { installed: false, reason: dirSafe.reason, message: `目标目录不安全:${dirSafe.reason}` }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 3) 同目录其它文件也过审计(技能可以带脚本)
|
|
118
|
+
const sidecars = (skill.siblings ?? []).filter(item => !item.path.toLowerCase().endsWith('skill.md'))
|
|
119
|
+
for (const file of sidecars) {
|
|
120
|
+
const fileAudit = auditSkillText(file.content, { name: file.path })
|
|
121
|
+
if (fileAudit.verdict === 'fail') {
|
|
122
|
+
return {
|
|
123
|
+
installed: false,
|
|
124
|
+
reason: 'audit-failed',
|
|
125
|
+
verdict: fileAudit.verdict,
|
|
126
|
+
message: `附属文件 ${file.path} 审计阻断:${fileAudit.findings.map(f => `${f.rule} ${f.message}`).join('; ')}`,
|
|
127
|
+
findings: fileAudit.findings,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const relSafe = safePath(posix.normalize(file.path))
|
|
131
|
+
if (!relSafe.ok || posix.normalize(file.path).split('/').length > MAX_SEGMENTS) {
|
|
132
|
+
return { installed: false, reason: 'unsafe-sidecar-path', message: `附属文件路径不安全:${file.path}` }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 4) 落盘(只有到这里才开始写磁盘)
|
|
137
|
+
const written = []
|
|
138
|
+
try {
|
|
139
|
+
mkdirSync(targetDir, { recursive: true })
|
|
140
|
+
writeFileSync(join(targetDir, 'SKILL.md'), skill.content, 'utf8')
|
|
141
|
+
written.push(join(targetDir, 'SKILL.md'))
|
|
142
|
+
|
|
143
|
+
for (const file of sidecars) {
|
|
144
|
+
const relative = posix.normalize(file.path)
|
|
145
|
+
const dest = join(targetDir, relative)
|
|
146
|
+
// 再确认一次没跑出 targetDir(`..` 已被 safePath 挡,这是纵深防御)
|
|
147
|
+
if (!dest.startsWith(targetDir)) {
|
|
148
|
+
return { installed: false, reason: 'escaped-target', message: `附属文件越出目标目录:${file.path}` }
|
|
149
|
+
}
|
|
150
|
+
mkdirSync(dirname(dest), { recursive: true })
|
|
151
|
+
writeFileSync(dest, file.content, 'utf8')
|
|
152
|
+
written.push(dest)
|
|
153
|
+
}
|
|
154
|
+
} catch (error) {
|
|
155
|
+
return {
|
|
156
|
+
installed: false,
|
|
157
|
+
reason: 'write-failed',
|
|
158
|
+
message: `写入失败:${error?.message ?? error}`,
|
|
159
|
+
files: written,
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
installed: true,
|
|
165
|
+
verdict: audit.verdict,
|
|
166
|
+
findings: audit.findings,
|
|
167
|
+
files: written,
|
|
168
|
+
}
|
|
169
|
+
}
|