dsh-skill-harbor 0.5.3 → 0.5.4
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/lib/client.js +1447 -23
- package/lib/index.js +34 -1
- package/package.json +7 -2
- 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/lang.js +20 -0
- package/src/client/locales.js +688 -688
- package/src/client/mount.js +31 -0
- package/src/client/panel.js +5 -1
- package/src/client/react.js +13 -0
- package/src/client.js +35 -11
- 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/index.js +34 -1
- 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/lib/client/api.js +0 -79
- package/lib/client/labels.js +0 -6
- package/lib/client/locales.js +0 -688
- package/lib/client/panel.js +0 -527
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
|
+
}
|
package/src/market/source.js
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 仓库源模型(findings:ref + commitSha 双存,id 由 hash 推导)。
|
|
3
|
-
*
|
|
4
|
-
* 关键设计:
|
|
5
|
-
* - `ref` 是用户可读的(`main` / `v1.2.0`),`commitSha` 是钉死的实际提交。
|
|
6
|
-
* 只存 ref 会在上游推进后悄悄改变内容(浮动 ref 事故),只存 sha 又没法更新。
|
|
7
|
-
* - `id` 由内容 hash 推导而非随机 UUID:状态文件被篡改时 id 重算不出来,
|
|
8
|
-
* 加载即失败 —— 这正是我们想要的(宁可拒绝,不要静默接受被改过的状态)。
|
|
9
|
-
*/
|
|
10
|
-
import { createHash } from 'node:crypto'
|
|
11
|
-
|
|
12
|
-
/** 一个仓库源的规范化形状。 */
|
|
13
|
-
export function makeSource({ owner, repo, ref, commitSha }) {
|
|
14
|
-
const normalized = {
|
|
15
|
-
owner: String(owner ?? '').trim(),
|
|
16
|
-
repo: String(repo ?? '').trim(),
|
|
17
|
-
ref: String(ref ?? 'HEAD').trim(),
|
|
18
|
-
commitSha: String(commitSha ?? '').trim().toLowerCase(),
|
|
19
|
-
}
|
|
20
|
-
return { ...normalized, id: deriveId(normalized) }
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** id = hash(owner/repo@ref#sha) 的前 16 位。改任一字段 id 就变。 */
|
|
24
|
-
export function deriveId({ owner, repo, ref, commitSha }) {
|
|
25
|
-
const material = `${owner}/${repo}@${ref}#${commitSha}`
|
|
26
|
-
return createHash('sha256').update(material).digest('hex').slice(0, 16)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** sha 形状校验:完整 40 位或短 7-40 位十六进制。 */
|
|
30
|
-
export function isCommitSha(value) {
|
|
31
|
-
return typeof value === 'string' && /^[0-9a-f]{7,40}$/i.test(value)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** 校验并解析一个持久化的源记录;id 不匹配即拒绝(篡改检测)。 */
|
|
35
|
-
export function loadSource(record) {
|
|
36
|
-
if (record === null || typeof record !== 'object') {
|
|
37
|
-
return { ok: false, reason: 'not-an-object' }
|
|
38
|
-
}
|
|
39
|
-
const { owner, repo, ref, commitSha, id } = record
|
|
40
|
-
if (typeof owner !== 'string' || owner === '') return { ok: false, reason: 'missing-owner' }
|
|
41
|
-
if (typeof repo !== 'string' || repo === '') return { ok: false, reason: 'missing-repo' }
|
|
42
|
-
if (!isCommitSha(commitSha)) return { ok: false, reason: 'invalid-sha' }
|
|
43
|
-
|
|
44
|
-
const expected = deriveId({ owner, repo, ref, commitSha })
|
|
45
|
-
if (id !== expected) {
|
|
46
|
-
return { ok: false, reason: 'id-mismatch', expected, actual: id }
|
|
47
|
-
}
|
|
48
|
-
return { ok: true, source: { owner, repo, ref, commitSha, id: expected } }
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** 序列化一组源(稳定排序,便于 diff)。 */
|
|
52
|
-
export function serializeSources(sources) {
|
|
53
|
-
return JSON.stringify(
|
|
54
|
-
[...sources].sort((a, b) => a.id.localeCompare(b.id)),
|
|
55
|
-
null,
|
|
56
|
-
2,
|
|
57
|
-
)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 比较「已安装的钉死 sha」与「远端最新 sha」。
|
|
62
|
-
* 两者不同即视为有更新 —— 但**不自动同步**,由用户决定。
|
|
63
|
-
*/
|
|
64
|
-
export function checkUpdate(installedSha, remoteSha) {
|
|
65
|
-
if (!isCommitSha(installedSha) || !isCommitSha(remoteSha)) {
|
|
66
|
-
return { ok: false, reason: 'invalid-sha', updateAvailable: null }
|
|
67
|
-
}
|
|
68
|
-
const installed = installedSha.toLowerCase()
|
|
69
|
-
const remote = remoteSha.toLowerCase()
|
|
70
|
-
return {
|
|
71
|
-
ok: true,
|
|
72
|
-
updateAvailable: installed !== remote,
|
|
73
|
-
installedSha: installed,
|
|
74
|
-
remoteSha: remote,
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** 同步:对齐到钉住的目标 sha,而不是跟随 ref 漂移。 */
|
|
79
|
-
export function applySync(source, remoteSha) {
|
|
80
|
-
if (!isCommitSha(remoteSha)) {
|
|
81
|
-
return { ok: false, reason: 'invalid-sha' }
|
|
82
|
-
}
|
|
83
|
-
const next = makeSource({
|
|
84
|
-
owner: source.owner,
|
|
85
|
-
repo: source.repo,
|
|
86
|
-
ref: source.ref,
|
|
87
|
-
commitSha: remoteSha,
|
|
88
|
-
})
|
|
89
|
-
return { ok: true, source: next, changed: next.id !== source.id }
|
|
90
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 仓库源模型(findings:ref + commitSha 双存,id 由 hash 推导)。
|
|
3
|
+
*
|
|
4
|
+
* 关键设计:
|
|
5
|
+
* - `ref` 是用户可读的(`main` / `v1.2.0`),`commitSha` 是钉死的实际提交。
|
|
6
|
+
* 只存 ref 会在上游推进后悄悄改变内容(浮动 ref 事故),只存 sha 又没法更新。
|
|
7
|
+
* - `id` 由内容 hash 推导而非随机 UUID:状态文件被篡改时 id 重算不出来,
|
|
8
|
+
* 加载即失败 —— 这正是我们想要的(宁可拒绝,不要静默接受被改过的状态)。
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from 'node:crypto'
|
|
11
|
+
|
|
12
|
+
/** 一个仓库源的规范化形状。 */
|
|
13
|
+
export function makeSource({ owner, repo, ref, commitSha }) {
|
|
14
|
+
const normalized = {
|
|
15
|
+
owner: String(owner ?? '').trim(),
|
|
16
|
+
repo: String(repo ?? '').trim(),
|
|
17
|
+
ref: String(ref ?? 'HEAD').trim(),
|
|
18
|
+
commitSha: String(commitSha ?? '').trim().toLowerCase(),
|
|
19
|
+
}
|
|
20
|
+
return { ...normalized, id: deriveId(normalized) }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** id = hash(owner/repo@ref#sha) 的前 16 位。改任一字段 id 就变。 */
|
|
24
|
+
export function deriveId({ owner, repo, ref, commitSha }) {
|
|
25
|
+
const material = `${owner}/${repo}@${ref}#${commitSha}`
|
|
26
|
+
return createHash('sha256').update(material).digest('hex').slice(0, 16)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** sha 形状校验:完整 40 位或短 7-40 位十六进制。 */
|
|
30
|
+
export function isCommitSha(value) {
|
|
31
|
+
return typeof value === 'string' && /^[0-9a-f]{7,40}$/i.test(value)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 校验并解析一个持久化的源记录;id 不匹配即拒绝(篡改检测)。 */
|
|
35
|
+
export function loadSource(record) {
|
|
36
|
+
if (record === null || typeof record !== 'object') {
|
|
37
|
+
return { ok: false, reason: 'not-an-object' }
|
|
38
|
+
}
|
|
39
|
+
const { owner, repo, ref, commitSha, id } = record
|
|
40
|
+
if (typeof owner !== 'string' || owner === '') return { ok: false, reason: 'missing-owner' }
|
|
41
|
+
if (typeof repo !== 'string' || repo === '') return { ok: false, reason: 'missing-repo' }
|
|
42
|
+
if (!isCommitSha(commitSha)) return { ok: false, reason: 'invalid-sha' }
|
|
43
|
+
|
|
44
|
+
const expected = deriveId({ owner, repo, ref, commitSha })
|
|
45
|
+
if (id !== expected) {
|
|
46
|
+
return { ok: false, reason: 'id-mismatch', expected, actual: id }
|
|
47
|
+
}
|
|
48
|
+
return { ok: true, source: { owner, repo, ref, commitSha, id: expected } }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 序列化一组源(稳定排序,便于 diff)。 */
|
|
52
|
+
export function serializeSources(sources) {
|
|
53
|
+
return JSON.stringify(
|
|
54
|
+
[...sources].sort((a, b) => a.id.localeCompare(b.id)),
|
|
55
|
+
null,
|
|
56
|
+
2,
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 比较「已安装的钉死 sha」与「远端最新 sha」。
|
|
62
|
+
* 两者不同即视为有更新 —— 但**不自动同步**,由用户决定。
|
|
63
|
+
*/
|
|
64
|
+
export function checkUpdate(installedSha, remoteSha) {
|
|
65
|
+
if (!isCommitSha(installedSha) || !isCommitSha(remoteSha)) {
|
|
66
|
+
return { ok: false, reason: 'invalid-sha', updateAvailable: null }
|
|
67
|
+
}
|
|
68
|
+
const installed = installedSha.toLowerCase()
|
|
69
|
+
const remote = remoteSha.toLowerCase()
|
|
70
|
+
return {
|
|
71
|
+
ok: true,
|
|
72
|
+
updateAvailable: installed !== remote,
|
|
73
|
+
installedSha: installed,
|
|
74
|
+
remoteSha: remote,
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 同步:对齐到钉住的目标 sha,而不是跟随 ref 漂移。 */
|
|
79
|
+
export function applySync(source, remoteSha) {
|
|
80
|
+
if (!isCommitSha(remoteSha)) {
|
|
81
|
+
return { ok: false, reason: 'invalid-sha' }
|
|
82
|
+
}
|
|
83
|
+
const next = makeSource({
|
|
84
|
+
owner: source.owner,
|
|
85
|
+
repo: source.repo,
|
|
86
|
+
ref: source.ref,
|
|
87
|
+
commitSha: remoteSha,
|
|
88
|
+
})
|
|
89
|
+
return { ok: true, source: next, changed: next.id !== source.id }
|
|
90
|
+
}
|
package/src/market/store.js
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 市场源持久化。
|
|
3
|
-
*
|
|
4
|
-
* 落在 `~/.dsh/skill-harbor/sources.json`(与前面所有路径同一套 base)。
|
|
5
|
-
* 读取时对每条记录做 `loadSource()` 校验:id 与内容 hash 不符即整条丢弃,
|
|
6
|
-
* 并回报丢弃原因 —— 宁可不显示,也不显示一条被篡改过的记录。
|
|
7
|
-
*/
|
|
8
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync } from 'node:fs'
|
|
9
|
-
import { dirname, join } from 'node:path'
|
|
10
|
-
|
|
11
|
-
import { loadSource, serializeSources } from '../market/source.js'
|
|
12
|
-
|
|
13
|
-
/** 状态文件路径。 */
|
|
14
|
-
export function sourcesFile(config) {
|
|
15
|
-
const base = config.dshHome ?? join(process.env.USERPROFILE ?? process.env.HOME ?? '.', '.dsh')
|
|
16
|
-
return join(base, 'skill-harbor', 'sources.json')
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 读取全部源。返回对象同时带 `rejected`,便于 UI 提示「有条目被丢弃」。
|
|
21
|
-
*/
|
|
22
|
-
export function readSources(config) {
|
|
23
|
-
const file = sourcesFile(config)
|
|
24
|
-
if (!existsSync(file)) return { sources: [], rejected: [] }
|
|
25
|
-
|
|
26
|
-
let parsed
|
|
27
|
-
try {
|
|
28
|
-
parsed = JSON.parse(readFileSync(file, 'utf8'))
|
|
29
|
-
} catch (error) {
|
|
30
|
-
return { sources: [], rejected: [{ reason: 'unreadable', message: String(error?.message ?? error) }] }
|
|
31
|
-
}
|
|
32
|
-
if (!Array.isArray(parsed)) {
|
|
33
|
-
return { sources: [], rejected: [{ reason: 'not-an-array' }] }
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const sources = []
|
|
37
|
-
const rejected = []
|
|
38
|
-
for (const record of parsed) {
|
|
39
|
-
const result = loadSource(record)
|
|
40
|
-
if (result.ok) sources.push(result.source)
|
|
41
|
-
else rejected.push({ reason: result.reason, record })
|
|
42
|
-
}
|
|
43
|
-
return { sources, rejected }
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** 写入(原子 rename,避免半截文件)。 */
|
|
47
|
-
export function writeSources(config, sources) {
|
|
48
|
-
const file = sourcesFile(config)
|
|
49
|
-
mkdirSync(dirname(file), { recursive: true })
|
|
50
|
-
const temp = `${file}.tmp-${process.pid}`
|
|
51
|
-
writeFileSync(temp, serializeSources(sources), 'utf8')
|
|
52
|
-
renameSync(temp, file)
|
|
53
|
-
return file
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 保存一个源:同 id 覆盖,否则追加。
|
|
58
|
-
* 若读取时发现有被丢弃的条目,**不写回**它们(丢弃是永久的,避免把坏数据带下去)。
|
|
59
|
-
*/
|
|
60
|
-
export function saveSource(config, source) {
|
|
61
|
-
const { sources } = readSources(config)
|
|
62
|
-
const next = sources.filter(item => item.id !== source.id)
|
|
63
|
-
next.push(source)
|
|
64
|
-
writeSources(config, next)
|
|
65
|
-
return source
|
|
66
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 市场源持久化。
|
|
3
|
+
*
|
|
4
|
+
* 落在 `~/.dsh/skill-harbor/sources.json`(与前面所有路径同一套 base)。
|
|
5
|
+
* 读取时对每条记录做 `loadSource()` 校验:id 与内容 hash 不符即整条丢弃,
|
|
6
|
+
* 并回报丢弃原因 —— 宁可不显示,也不显示一条被篡改过的记录。
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync } from 'node:fs'
|
|
9
|
+
import { dirname, join } from 'node:path'
|
|
10
|
+
|
|
11
|
+
import { loadSource, serializeSources } from '../market/source.js'
|
|
12
|
+
|
|
13
|
+
/** 状态文件路径。 */
|
|
14
|
+
export function sourcesFile(config) {
|
|
15
|
+
const base = config.dshHome ?? join(process.env.USERPROFILE ?? process.env.HOME ?? '.', '.dsh')
|
|
16
|
+
return join(base, 'skill-harbor', 'sources.json')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 读取全部源。返回对象同时带 `rejected`,便于 UI 提示「有条目被丢弃」。
|
|
21
|
+
*/
|
|
22
|
+
export function readSources(config) {
|
|
23
|
+
const file = sourcesFile(config)
|
|
24
|
+
if (!existsSync(file)) return { sources: [], rejected: [] }
|
|
25
|
+
|
|
26
|
+
let parsed
|
|
27
|
+
try {
|
|
28
|
+
parsed = JSON.parse(readFileSync(file, 'utf8'))
|
|
29
|
+
} catch (error) {
|
|
30
|
+
return { sources: [], rejected: [{ reason: 'unreadable', message: String(error?.message ?? error) }] }
|
|
31
|
+
}
|
|
32
|
+
if (!Array.isArray(parsed)) {
|
|
33
|
+
return { sources: [], rejected: [{ reason: 'not-an-array' }] }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const sources = []
|
|
37
|
+
const rejected = []
|
|
38
|
+
for (const record of parsed) {
|
|
39
|
+
const result = loadSource(record)
|
|
40
|
+
if (result.ok) sources.push(result.source)
|
|
41
|
+
else rejected.push({ reason: result.reason, record })
|
|
42
|
+
}
|
|
43
|
+
return { sources, rejected }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 写入(原子 rename,避免半截文件)。 */
|
|
47
|
+
export function writeSources(config, sources) {
|
|
48
|
+
const file = sourcesFile(config)
|
|
49
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
50
|
+
const temp = `${file}.tmp-${process.pid}`
|
|
51
|
+
writeFileSync(temp, serializeSources(sources), 'utf8')
|
|
52
|
+
renameSync(temp, file)
|
|
53
|
+
return file
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 保存一个源:同 id 覆盖,否则追加。
|
|
58
|
+
* 若读取时发现有被丢弃的条目,**不写回**它们(丢弃是永久的,避免把坏数据带下去)。
|
|
59
|
+
*/
|
|
60
|
+
export function saveSource(config, source) {
|
|
61
|
+
const { sources } = readSources(config)
|
|
62
|
+
const next = sources.filter(item => item.id !== source.id)
|
|
63
|
+
next.push(source)
|
|
64
|
+
writeSources(config, next)
|
|
65
|
+
return source
|
|
66
|
+
}
|