dsh-modpack-kit 0.0.0-stage → 0.1.3
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 +75 -0
- package/README.md +224 -2
- package/assets/icon-180.png +0 -0
- package/assets/install-stub.cmd +15 -0
- package/assets/install-stub.mjs +259 -0
- package/assets/install-stub.sh +9 -0
- package/client.js +1275 -0
- package/cordis.patch.yml +3 -0
- package/docs/DETAIL.md +97 -0
- package/icon.svg +14 -0
- package/index.js +820 -0
- package/lib/classify.mjs +105 -0
- package/lib/compat.mjs +112 -0
- package/lib/install.mjs +349 -0
- package/lib/manifest.mjs +197 -0
- package/lib/market-catalog.mjs +419 -0
- package/lib/pack.mjs +460 -0
- package/lib/profile.mjs +274 -0
- package/lib/registry.mjs +223 -0
- package/lib/scan.mjs +325 -0
- package/lib/semver.mjs +143 -0
- package/lib/store.mjs +183 -0
- package/lib/tar.mjs +107 -0
- package/lib/yamlish.mjs +385 -0
- package/lib/zip.mjs +232 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +54 -3
package/lib/scan.mjs
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-modpack —— 安全检测:敏感信息与恶意代码痕迹。
|
|
3
|
+
*
|
|
4
|
+
* 三条硬规矩(也写进 README 给使用者看):
|
|
5
|
+
* 1. 检测在内存里完成,命中的原文**不写盘、不回传**;报告里只有规则名、
|
|
6
|
+
* 文件、行号和打码片段。
|
|
7
|
+
* 2. 只做静态特征匹配,不是杀毒引擎;结论是「发现可疑」而不是「保证安全」。
|
|
8
|
+
* 3. 高风险默认阻断打包/安装,必须使用者显式确认才继续,确认会记进报告。
|
|
9
|
+
*
|
|
10
|
+
* 规则表是数据,扩展接口 `registerScanner` 可以往里加规则,不用改这个文件。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const TEXT_EXT = new Set([
|
|
14
|
+
'.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.json', '.yml', '.yaml', '.toml', '.ini', '.cfg',
|
|
15
|
+
'.md', '.txt', '.sh', '.bash', '.ps1', '.bat', '.cmd', '.py', '.html', '.css', '.xml', '.env',
|
|
16
|
+
'.lock', '.gitignore', '.npmrc', '.vue', '.svelte',
|
|
17
|
+
])
|
|
18
|
+
|
|
19
|
+
const BINARY_EXT = new Set(['.exe', '.dll', '.scr', '.msi', '.com', '.sys', '.drv', '.so', '.dylib', '.node'])
|
|
20
|
+
const IMAGE_EXT = new Set(['.png', '.jpg', '.jpeg', '.gif', '.webp', '.ico', '.bmp', '.avif'])
|
|
21
|
+
const SCRIPT_EXT = new Set(['.ps1', '.bat', '.cmd', '.vbs', '.hta', '.jar'])
|
|
22
|
+
|
|
23
|
+
const MAX_SCAN_BYTES = 2 * 1024 * 1024
|
|
24
|
+
|
|
25
|
+
/** @typedef {'high'|'medium'|'low'} Severity */
|
|
26
|
+
|
|
27
|
+
const SECRET_RULES = [
|
|
28
|
+
{ id: 'secret.private-key', severity: 'high', category: 'secret', label: '私钥文件内容', re: /-----BEGIN (?:RSA |EC |DSA |OPENSSH |PGP )?PRIVATE KEY-----/gu },
|
|
29
|
+
{ id: 'secret.aws-key', severity: 'high', category: 'secret', label: 'AWS 访问密钥', re: /\bAKIA[0-9A-Z]{16}\b/gu },
|
|
30
|
+
{ id: 'secret.github-token', severity: 'high', category: 'secret', label: 'GitHub 令牌', re: /\bgh[pousr]_[A-Za-z0-9]{36,}\b/gu },
|
|
31
|
+
{ id: 'secret.github-pat', severity: 'high', category: 'secret', label: 'GitHub 细粒度令牌', re: /\bgithub_pat_[A-Za-z0-9_]{20,}\b/gu },
|
|
32
|
+
{ id: 'secret.slack-token', severity: 'high', category: 'secret', label: 'Slack 令牌', re: /\bxox[baprs]-[A-Za-z0-9-]{10,}\b/gu },
|
|
33
|
+
{ id: 'secret.api-key', severity: 'high', category: 'secret', label: 'API Key(sk- 形式)', re: /\bsk-[A-Za-z0-9_-]{20,}\b/gu },
|
|
34
|
+
{ id: 'secret.jwt', severity: 'high', category: 'secret', label: 'JWT', re: /\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/gu },
|
|
35
|
+
// 只认“字段 = 真实值”:代码里的属性访问(cookie.bili_jct、raw.SESSDATA)不算,避免把正常插件误判成夹带 Cookie
|
|
36
|
+
{ id: 'secret.bili-cookie', severity: 'high', category: 'secret', label: 'B站登录 Cookie', re: /\b(?:SESSDATA|bili_jct|DedeUserID|buvid3|buvid4|ac_time_value)\b\s*[=:]\s*(?:["'][^"'\n]{8,}["']|[A-Za-z0-9%_-]{8,})/gu },
|
|
37
|
+
{ id: 'secret.cookie-header', severity: 'high', category: 'secret', label: 'Cookie 请求头', re: /\bcookie\s*[:=]\s*["']?[A-Za-z0-9_%-]{6,}=[^\s"']{8,}/giu },
|
|
38
|
+
{ id: 'secret.generic-assign', severity: 'medium', category: 'secret', label: '疑似密钥赋值', re: /\b(?:api[_-]?key|secret|password|passwd|access[_-]?token|auth[_-]?token)\b\s*[:=]\s*["'][^"'\n]{8,}["']/giu },
|
|
39
|
+
{ id: 'secret.private-path', severity: 'medium', category: 'privacy', label: '本机用户目录路径', re: /[A-Za-z]:\\Users\\[^\\\s"']+/gu },
|
|
40
|
+
{ id: 'privacy.email', severity: 'medium', category: 'privacy', label: '电子邮箱', re: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/gu },
|
|
41
|
+
{ id: 'privacy.cn-phone', severity: 'medium', category: 'privacy', label: '手机号', re: /(?<!\d)1[3-9]\d{9}(?!\d)/gu },
|
|
42
|
+
{ id: 'privacy.cn-id', severity: 'high', category: 'privacy', label: '身份证号', re: /(?<!\d)[1-9]\d{5}(?:19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dXx](?!\d)/gu },
|
|
43
|
+
{ id: 'privacy.private-ip', severity: 'low', category: 'privacy', label: '内网地址', re: /\b(?:10\.\d{1,3}|192\.168|172\.(?:1[6-9]|2\d|3[01]))\.\d{1,3}\.\d{1,3}\b/gu },
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
const MALWARE_RULES = [
|
|
47
|
+
{ id: 'malware.remote-pipe', severity: 'high', label: '远程脚本直接执行', re: /\b(?:curl|wget)\b[^\n|]{0,200}\|\s*(?:sudo\s+)?(?:ba|z)?sh\b/gu },
|
|
48
|
+
{ id: 'malware.powershell-iex', severity: 'high', label: '下载后 iex 执行', re: /\b(?:Invoke-Expression|iex)\b[^\n]{0,120}(?:DownloadString|Invoke-WebRequest|iwr|curl)/giu },
|
|
49
|
+
{ id: 'malware.powershell-encoded', severity: 'high', label: 'PowerShell 编码命令', re: /(?:-enc(?:odedcommand)?\s+)[A-Za-z0-9+/=]{40,}/giu },
|
|
50
|
+
{ id: 'malware.eval', severity: 'medium', label: '动态执行代码', re: /\b(?:eval|new\s+Function)\s*\(/gu },
|
|
51
|
+
{ id: 'malware.child-process', severity: 'medium', label: '调用子进程', re: /\b(?:child_process|execSync|spawnSync|execFileSync|exec)\s*[(`]/gu },
|
|
52
|
+
{ id: 'malware.obfuscation', severity: 'medium', label: '疑似混淆(长 base64)', re: /["'][A-Za-z0-9+/]{600,}={0,2}["']/gu },
|
|
53
|
+
{ id: 'malware.hex-escape', severity: 'low', label: '大量十六进制转义', re: /(?:\\x[0-9a-fA-F]{2}){20,}/gu },
|
|
54
|
+
{ id: 'malware.disabled-tls', severity: 'medium', label: '关闭 TLS 校验', re: /rejectUnauthorized\s*:\s*false|NODE_TLS_REJECT_UNAUTHORIZED\s*=\s*["']?0/gu },
|
|
55
|
+
{ id: 'malware.process-env-dump', severity: 'low', label: '读取全部环境变量', re: /JSON\.stringify\s*\(\s*process\.env\s*\)/gu },
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
const FILE_NAME_RULES = [
|
|
59
|
+
{ id: 'file.private-key', severity: 'high', category: 'secret', label: '私钥/证书文件', re: /(?:^|[\\/])(?:id_(?:rsa|ed25519|ecdsa)|[^\\/]+\.(?:pem|key|p12|pfx|jks|keystore))$/iu },
|
|
60
|
+
{ id: 'file.env', severity: 'high', category: 'secret', label: '.env 环境文件', re: /(?:^|[\\/])\.env(?:\.local|\.production|\.development)?$/iu },
|
|
61
|
+
{ id: 'file.credential', severity: 'high', category: 'secret', label: '凭证/登录态文件', re: /(?:^|[\\/])[^\\/]*(?:credential|cookie|token|session|auth)[^\\/]*\.(?:json|txt|dat)$/iu },
|
|
62
|
+
]
|
|
63
|
+
|
|
64
|
+
/** 名字里的命中不看内容,避免把「凭证文件」本身漏掉。 */
|
|
65
|
+
export function scanFileName(name) {
|
|
66
|
+
const hits = []
|
|
67
|
+
for (const rule of FILE_NAME_RULES) {
|
|
68
|
+
rule.re.lastIndex = 0
|
|
69
|
+
if (rule.re.test(name)) hits.push(findingOf(rule, name, 0, ''))
|
|
70
|
+
}
|
|
71
|
+
return hits
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function maskSecret(text) {
|
|
75
|
+
const s = String(text)
|
|
76
|
+
if (s.length <= 4) return '****'
|
|
77
|
+
return s.slice(0, 2) + '****' + `(${s.length} 字符已隐藏)`
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function maskAll(text) {
|
|
81
|
+
const s = String(text)
|
|
82
|
+
return s.length > 0 ? '***' : ''
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function findingOf(rule, file, line, masked) {
|
|
86
|
+
return {
|
|
87
|
+
rule: rule.id,
|
|
88
|
+
severity: rule.severity,
|
|
89
|
+
category: rule.category || 'malware',
|
|
90
|
+
label: rule.label,
|
|
91
|
+
file,
|
|
92
|
+
line,
|
|
93
|
+
masked,
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function looksBinary(buf) {
|
|
98
|
+
const n = Math.min(buf.length, 4096)
|
|
99
|
+
for (let i = 0; i < n; i += 1) if (buf[i] === 0) return true
|
|
100
|
+
return false
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function extOf(name) {
|
|
104
|
+
const base = name.split('/').pop() || ''
|
|
105
|
+
const dot = base.lastIndexOf('.')
|
|
106
|
+
return dot <= 0 ? '' : base.slice(dot).toLowerCase()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function lineAt(text, index) {
|
|
110
|
+
let line = 1
|
|
111
|
+
for (let i = 0; i < index && i < text.length; i += 1) if (text.charCodeAt(i) === 10) line += 1
|
|
112
|
+
return line
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 扫一个文件。
|
|
117
|
+
* @param {string} name 相对路径
|
|
118
|
+
* @param {Buffer} data
|
|
119
|
+
* @returns {{findings: object[], skipped?: string}}
|
|
120
|
+
*/
|
|
121
|
+
export function scanFile(name, data) {
|
|
122
|
+
const findings = scanFileName(name)
|
|
123
|
+
const ext = extOf(name)
|
|
124
|
+
if (BINARY_EXT.has(ext)) {
|
|
125
|
+
findings.push({ rule: 'file.binary-executable', severity: 'high', category: 'malware', label: '可执行二进制', file: name, line: 0, masked: '' })
|
|
126
|
+
return { findings }
|
|
127
|
+
}
|
|
128
|
+
if (SCRIPT_EXT.has(ext) && !data.toString('utf8', 0, 64).startsWith('#!/usr/bin/env node')) {
|
|
129
|
+
findings.push({ rule: 'file.script', severity: 'low', category: 'malware', label: '脚本文件(请人工确认)', file: name, line: 0, masked: '' })
|
|
130
|
+
}
|
|
131
|
+
if (data.length > MAX_SCAN_BYTES) return { findings, skipped: '文件超过 2MB,仅按文件名检测' }
|
|
132
|
+
if (IMAGE_EXT.has(ext)) return { findings }
|
|
133
|
+
if (looksBinary(data)) {
|
|
134
|
+
findings.push({ rule: 'file.binary', severity: 'medium', category: 'malware', label: '二进制内容', file: name, line: 0, masked: '' })
|
|
135
|
+
return { findings }
|
|
136
|
+
}
|
|
137
|
+
if (!TEXT_EXT.has(ext) && ext !== '') return { findings, skipped: '非文本扩展名,仅按文件名检测' }
|
|
138
|
+
|
|
139
|
+
const text = data.toString('utf8')
|
|
140
|
+
const seen = new Set()
|
|
141
|
+
for (const rule of [...SECRET_RULES, ...MALWARE_RULES]) {
|
|
142
|
+
rule.re.lastIndex = 0
|
|
143
|
+
let match
|
|
144
|
+
let guard = 0
|
|
145
|
+
while ((match = rule.re.exec(text)) !== null && guard < 50) {
|
|
146
|
+
guard += 1
|
|
147
|
+
const category = rule.category || 'malware'
|
|
148
|
+
const masked = category === 'secret' || category === 'privacy' ? maskSecret(match[0]) : maskAll(match[0])
|
|
149
|
+
const line = lineAt(text, match.index)
|
|
150
|
+
// 同一行同一规则只记一次:一个文件里几十处 require('child_process') 不该刷出几十条
|
|
151
|
+
const key = rule.id + ':' + line
|
|
152
|
+
if (!seen.has(key)) {
|
|
153
|
+
seen.add(key)
|
|
154
|
+
findings.push(findingOf(rule, name, line, masked))
|
|
155
|
+
}
|
|
156
|
+
if (match[0].length === 0) rule.re.lastIndex += 1
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (/(?:^|\/)(?:package|plugin)\.json$/u.test(name)) {
|
|
161
|
+
try {
|
|
162
|
+
const pkg = JSON.parse(text)
|
|
163
|
+
const scripts = pkg && typeof pkg === 'object' ? pkg.scripts : null
|
|
164
|
+
if (scripts && typeof scripts === 'object') {
|
|
165
|
+
for (const key of Object.keys(scripts)) {
|
|
166
|
+
if (/^(?:pre|post)?install$/u.test(key)) {
|
|
167
|
+
findings.push({
|
|
168
|
+
rule: 'json.install-script', severity: 'high', category: 'malware',
|
|
169
|
+
label: '安装时执行的脚本(' + key + ')', file: name, line: 0, masked: maskAll(scripts[key]),
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const bin = pkg && pkg.bin
|
|
175
|
+
if (bin && typeof bin === 'object' && Object.keys(bin).length > 0) {
|
|
176
|
+
findings.push({ rule: 'json.bin', severity: 'low', category: 'malware', label: '声明了命令行入口', file: name, line: 0, masked: '' })
|
|
177
|
+
}
|
|
178
|
+
} catch {
|
|
179
|
+
// 包清单坏掉会在后面校验里报,不在扫描里重复报
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return { findings }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const SEVERITY_ORDER = { high: 0, medium: 1, low: 2 }
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 把命中按来源分组:插件本体 / 该插件的配置快照 / 数据快照 / 整合包自身文件。
|
|
189
|
+
* @param {object[]} findings
|
|
190
|
+
* @param {{name: string, dirName?: string, source?: string}[]} plugins
|
|
191
|
+
*/
|
|
192
|
+
export function groupFindings(findings, plugins = []) {
|
|
193
|
+
const byDir = new Map()
|
|
194
|
+
for (const p of plugins) {
|
|
195
|
+
if (!p || !p.name) continue
|
|
196
|
+
byDir.set(p.dirName || p.name, p)
|
|
197
|
+
}
|
|
198
|
+
const groups = new Map()
|
|
199
|
+
const originOf = (p) => (p.source === 'npm' ? 'market' : 'local')
|
|
200
|
+
const slot = (key, label, origin, pluginName) => {
|
|
201
|
+
if (!groups.has(key)) groups.set(key, { key, label, origin, plugin: pluginName || '', counts: { high: 0, medium: 0, low: 0 }, findings: [] })
|
|
202
|
+
return groups.get(key)
|
|
203
|
+
}
|
|
204
|
+
for (const finding of findings) {
|
|
205
|
+
const file = String(finding.file || '')
|
|
206
|
+
let hit
|
|
207
|
+
if ((hit = /^plugins\/([^/]+)\//u.exec(file))) {
|
|
208
|
+
const plugin = byDir.get(hit[1])
|
|
209
|
+
const group = slot('plugin:' + hit[1], (plugin && plugin.name) || hit[1], plugin ? originOf(plugin) : 'local', (plugin && plugin.name) || hit[1])
|
|
210
|
+
group.findings.push(finding)
|
|
211
|
+
group.counts[finding.severity] = (group.counts[finding.severity] || 0) + 1
|
|
212
|
+
} else if ((hit = /^configs\/([^/]+)\.json$/u.exec(file))) {
|
|
213
|
+
const plugin = byDir.get(hit[1])
|
|
214
|
+
const group = slot('config:' + hit[1], ((plugin && plugin.name) || hit[1]) + ' 的配置快照', 'config', (plugin && plugin.name) || hit[1])
|
|
215
|
+
group.findings.push(finding)
|
|
216
|
+
group.counts[finding.severity] = (group.counts[finding.severity] || 0) + 1
|
|
217
|
+
} else if ((hit = /^data\/([^/]+)\//u.exec(file))) {
|
|
218
|
+
const plugin = byDir.get(hit[1])
|
|
219
|
+
const group = slot('data:' + hit[1], ((plugin && plugin.name) || hit[1]) + ' 的数据快照', 'data', (plugin && plugin.name) || hit[1])
|
|
220
|
+
group.findings.push(finding)
|
|
221
|
+
group.counts[finding.severity] = (group.counts[finding.severity] || 0) + 1
|
|
222
|
+
} else {
|
|
223
|
+
const group = slot('pack', '整合包自身的清单与安装器', 'pack', '')
|
|
224
|
+
group.findings.push(finding)
|
|
225
|
+
group.counts[finding.severity] = (group.counts[finding.severity] || 0) + 1
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const weight = (g) => (g.counts.high > 0 ? 0 : g.counts.medium > 0 ? 1 : 2)
|
|
229
|
+
return [...groups.values()]
|
|
230
|
+
.map((g) => ({ ...g, findings: g.findings.sort((a, b) => severityRank(a.severity) - severityRank(b.severity) || a.line - b.line) }))
|
|
231
|
+
.sort((a, b) => weight(a) - weight(b) || b.findings.length - a.findings.length || a.label.localeCompare(b.label))
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function severityRank(severity) {
|
|
235
|
+
return SEVERITY_ORDER[severity] ?? 3
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 扫一批文件(内存内)。
|
|
240
|
+
* @param {{name: string, data: Buffer}[]} entries
|
|
241
|
+
* @param {{extraRules?: object[]}} [options]
|
|
242
|
+
*/
|
|
243
|
+
export function scanEntries(entries, options = {}) {
|
|
244
|
+
const findings = []
|
|
245
|
+
let scannedBytes = 0
|
|
246
|
+
const skipped = []
|
|
247
|
+
const rules = Array.isArray(options.extraRules) ? options.extraRules : []
|
|
248
|
+
const extra = rules
|
|
249
|
+
.filter((r) => r && typeof r.id === 'string' && r.re instanceof RegExp)
|
|
250
|
+
.map((r) => ({ id: 'ext.' + r.id, severity: r.severity || 'medium', category: r.category || 'malware', label: r.label || r.id, re: new RegExp(r.re.source, r.re.flags.includes('g') ? r.re.flags : r.re.flags + 'g') }))
|
|
251
|
+
|
|
252
|
+
for (const entry of entries) {
|
|
253
|
+
scannedBytes += entry.data.length
|
|
254
|
+
const result = scanFile(entry.name, entry.data)
|
|
255
|
+
findings.push(...result.findings)
|
|
256
|
+
if (result.skipped) skipped.push({ file: entry.name, reason: result.skipped })
|
|
257
|
+
}
|
|
258
|
+
for (const rule of extra) {
|
|
259
|
+
for (const entry of entries) {
|
|
260
|
+
if (looksBinary(entry.data) || entry.data.length > MAX_SCAN_BYTES) continue
|
|
261
|
+
const text = entry.data.toString('utf8')
|
|
262
|
+
rule.re.lastIndex = 0
|
|
263
|
+
let match
|
|
264
|
+
let guard = 0
|
|
265
|
+
while ((match = rule.re.exec(text)) !== null && guard < 50) {
|
|
266
|
+
guard += 1
|
|
267
|
+
findings.push({ rule: rule.id, severity: rule.severity, category: rule.category, label: rule.label, file: entry.name, line: lineAt(text, match.index), masked: maskAll(match[0]) })
|
|
268
|
+
if (match[0].length === 0) rule.re.lastIndex += 1
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
findings.sort((a, b) => severityRank(a.severity) - severityRank(b.severity) || a.file.localeCompare(b.file) || a.line - b.line)
|
|
274
|
+
const counts = { high: 0, medium: 0, low: 0 }
|
|
275
|
+
for (const f of findings) counts[f.severity] = (counts[f.severity] || 0) + 1
|
|
276
|
+
return {
|
|
277
|
+
findings,
|
|
278
|
+
counts,
|
|
279
|
+
scannedFiles: entries.length,
|
|
280
|
+
scannedBytes,
|
|
281
|
+
skipped,
|
|
282
|
+
redacted: true,
|
|
283
|
+
note: '命中原文只在内存里用于判定,未写入任何文件;报告中的片段已打码。',
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 生成给人看的扫描报告(纯文本)。只使用已打码的片段。
|
|
289
|
+
*/
|
|
290
|
+
export function renderScanReport(title, scan, options = {}) {
|
|
291
|
+
const lines = []
|
|
292
|
+
lines.push('安全检测报告 —— ' + title)
|
|
293
|
+
lines.push('生成时间:' + new Date().toISOString())
|
|
294
|
+
lines.push('扫描文件:' + scan.scannedFiles + ',字节:' + scan.scannedBytes)
|
|
295
|
+
lines.push('结果:高风险 ' + scan.counts.high + ' / 中风险 ' + scan.counts.medium + ' / 低风险 ' + scan.counts.low)
|
|
296
|
+
lines.push('')
|
|
297
|
+
lines.push('说明:检测在本机内存中完成,命中的原文没有落盘,也无法从本报告还原。')
|
|
298
|
+
lines.push('检测是静态特征匹配,不是杀毒引擎;「未发现」不等于「保证安全」。')
|
|
299
|
+
if (options.approvedRisk) {
|
|
300
|
+
lines.push('')
|
|
301
|
+
lines.push('⚠ 打包者/安装者在存在高风险项的情况下明确选择了继续,风险由使用者承担。')
|
|
302
|
+
}
|
|
303
|
+
lines.push('')
|
|
304
|
+
if (scan.findings.length === 0) {
|
|
305
|
+
lines.push('未发现敏感信息或恶意代码特征。')
|
|
306
|
+
} else {
|
|
307
|
+
const byFile = new Map()
|
|
308
|
+
for (const f of scan.findings) {
|
|
309
|
+
if (!byFile.has(f.file)) byFile.set(f.file, [])
|
|
310
|
+
byFile.get(f.file).push(f)
|
|
311
|
+
}
|
|
312
|
+
for (const [file, items] of byFile) {
|
|
313
|
+
lines.push('· ' + file)
|
|
314
|
+
for (const f of items) {
|
|
315
|
+
lines.push(' [' + f.severity + '] ' + f.label + (f.line ? ' 第 ' + f.line + ' 行' : '') + (f.masked ? ' ' + f.masked : ''))
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (scan.skipped && scan.skipped.length > 0) {
|
|
320
|
+
lines.push('')
|
|
321
|
+
lines.push('未完整扫描:')
|
|
322
|
+
for (const s of scan.skipped.slice(0, 50)) lines.push(' · ' + s.file + '(' + s.reason + ')')
|
|
323
|
+
}
|
|
324
|
+
return lines.join('\n')
|
|
325
|
+
}
|
package/lib/semver.mjs
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-modpack —— 极小 semver(够判兼容性,不做完整实现)。
|
|
3
|
+
*
|
|
4
|
+
* 支持:`1.2.3`、`^`、`~`、`>= <= > <`、`*`/`x`、逗号或空格分隔的与、`||` 分隔的或。
|
|
5
|
+
* 不支持:hyphen range、预发布标签的优先级细节。判断插件 peer 兼容够用,
|
|
6
|
+
* 遇到看不懂的范围一律返回 `unknown`,不假装通过。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const VERSION_RE = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$/u
|
|
10
|
+
|
|
11
|
+
export function parseVersion(input) {
|
|
12
|
+
if (input === null || input === undefined) return null
|
|
13
|
+
const m = VERSION_RE.exec(String(input).trim())
|
|
14
|
+
if (!m) return null
|
|
15
|
+
return { major: Number(m[1]), minor: Number(m[2]), patch: Number(m[3]), pre: m[4] || '' }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function compareVersions(a, b) {
|
|
19
|
+
const pa = parseVersion(a)
|
|
20
|
+
const pb = parseVersion(b)
|
|
21
|
+
if (!pa || !pb) return null
|
|
22
|
+
for (const key of ['major', 'minor', 'patch']) {
|
|
23
|
+
if (pa[key] !== pb[key]) return pa[key] < pb[key] ? -1 : 1
|
|
24
|
+
}
|
|
25
|
+
if (pa.pre === pb.pre) return 0
|
|
26
|
+
if (pa.pre === '') return 1
|
|
27
|
+
if (pb.pre === '') return -1
|
|
28
|
+
return pa.pre < pb.pre ? -1 : 1
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const isWild = (part) => part === undefined || part === '' || part === '*' || part === 'x' || part === 'X'
|
|
32
|
+
|
|
33
|
+
function bound(major, minor, patch, pre, op) {
|
|
34
|
+
return { major, minor, patch, pre: pre || '', op }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 把一个比较符句子(如 `^1.2.3`、`>=1.0.0 <2.0.0`)展开成下界/上界。 */
|
|
38
|
+
function parseComparatorSet(text) {
|
|
39
|
+
const parts = String(text).trim().split(/\s+/u).filter(Boolean)
|
|
40
|
+
if (parts.length === 0) return { any: true }
|
|
41
|
+
const bounds = []
|
|
42
|
+
for (const part of parts) {
|
|
43
|
+
const m = /^(\^|~|>=|<=|>|<|=)?\s*(.*)$/u.exec(part)
|
|
44
|
+
const op = m[1] || ''
|
|
45
|
+
const rest = m[2]
|
|
46
|
+
if (rest === '' || rest === '*' || rest === 'x') {
|
|
47
|
+
if (op === '' || op === '=' || op === '>=') continue
|
|
48
|
+
if (op === '<' || op === '<=') return { impossible: true }
|
|
49
|
+
continue
|
|
50
|
+
}
|
|
51
|
+
const nums = rest.split('.').map((s) => s.trim())
|
|
52
|
+
const majorRaw = nums[0]
|
|
53
|
+
const minorRaw = nums[1]
|
|
54
|
+
const patchRaw = nums[2]
|
|
55
|
+
if (isWild(majorRaw)) {
|
|
56
|
+
if (op === '' || op === '=' || op === '>=' || op === '^' || op === '~') continue
|
|
57
|
+
if (op === '<' || op === '<=') return { impossible: true }
|
|
58
|
+
continue
|
|
59
|
+
}
|
|
60
|
+
if (!/^\d+$/u.test(majorRaw)) return null
|
|
61
|
+
const major = Number(majorRaw)
|
|
62
|
+
const minorWild = isWild(minorRaw)
|
|
63
|
+
const patchWild = isWild((patchRaw || '').split('-')[0])
|
|
64
|
+
const minor = minorWild ? 0 : Number(minorRaw)
|
|
65
|
+
const patchPart = (patchRaw || '0').split('-')
|
|
66
|
+
const patch = patchWild ? 0 : Number(patchPart[0])
|
|
67
|
+
const pre = patchPart.slice(1).join('-')
|
|
68
|
+
if (Number.isNaN(minor) || Number.isNaN(patch)) return null
|
|
69
|
+
|
|
70
|
+
if (op === '^') {
|
|
71
|
+
bounds.push(bound(major, minor, patch, pre, '>='))
|
|
72
|
+
if (major > 0) bounds.push(bound(major + 1, 0, 0, '', '<'))
|
|
73
|
+
else if (minorWild) bounds.push(bound(1, 0, 0, '', '<'))
|
|
74
|
+
else if (patchWild) bounds.push(bound(0, minor + 1, 0, '', '<'))
|
|
75
|
+
// ^0.0.3 只允许 0.0.3 本身(npm 语义:上界 0.0.4),^0.x 上界 0.(x+1).0
|
|
76
|
+
else if (minor === 0) bounds.push(bound(0, 0, patch + 1, '', '<'))
|
|
77
|
+
else bounds.push(bound(0, minor + 1, 0, '', '<'))
|
|
78
|
+
continue
|
|
79
|
+
}
|
|
80
|
+
if (op === '~') {
|
|
81
|
+
bounds.push(bound(major, minor, patch, pre, '>='))
|
|
82
|
+
bounds.push(minorWild ? bound(major + 1, 0, 0, '', '<') : bound(major, minor + 1, 0, '', '<'))
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
if (op === '>' || op === '>=' || op === '<' || op === '<=') {
|
|
86
|
+
bounds.push(bound(major, minor, patch, pre, op))
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
// 裸版本 / `=`:x.y.z 精确;x.y 视为 ~x.y;x 视为 ^x
|
|
90
|
+
if (minorWild) {
|
|
91
|
+
bounds.push(bound(major, 0, 0, '', '>='))
|
|
92
|
+
bounds.push(bound(major + 1, 0, 0, '', '<'))
|
|
93
|
+
} else if (patchWild) {
|
|
94
|
+
bounds.push(bound(major, minor, 0, '', '>='))
|
|
95
|
+
bounds.push(bound(major, minor + 1, 0, '', '<'))
|
|
96
|
+
} else {
|
|
97
|
+
bounds.push(bound(major, minor, patch, pre, '='))
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return { bounds }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function testBounds(version, bounds) {
|
|
104
|
+
for (const b of bounds) {
|
|
105
|
+
const cmp = compareVersions(version, `${b.major}.${b.minor}.${b.patch}${b.pre ? '-' + b.pre : ''}`)
|
|
106
|
+
if (cmp === null) return null
|
|
107
|
+
if (b.op === '=' && cmp !== 0) return false
|
|
108
|
+
if (b.op === '>' && cmp <= 0) return false
|
|
109
|
+
if (b.op === '>=' && cmp < 0) return false
|
|
110
|
+
if (b.op === '<' && cmp >= 0) return false
|
|
111
|
+
if (b.op === '<=' && cmp > 0) return false
|
|
112
|
+
}
|
|
113
|
+
return true
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @returns {boolean|null} null = 范围看不懂 / 版本非法,不要当成通过。
|
|
118
|
+
*/
|
|
119
|
+
export function satisfies(version, range) {
|
|
120
|
+
if (!parseVersion(version)) return null
|
|
121
|
+
if (range === undefined || range === null) return true
|
|
122
|
+
const text = String(range).trim()
|
|
123
|
+
if (text === '' || text === '*' || text === 'x') return true
|
|
124
|
+
const sets = text.split('||')
|
|
125
|
+
let sawUnknown = false
|
|
126
|
+
for (const set of sets) {
|
|
127
|
+
const parsed = parseComparatorSet(set)
|
|
128
|
+
if (!parsed) { sawUnknown = true; continue }
|
|
129
|
+
if (parsed.impossible) continue
|
|
130
|
+
if (parsed.any) return true
|
|
131
|
+
const ok = testBounds(version, parsed.bounds)
|
|
132
|
+
if (ok === true) return true
|
|
133
|
+
if (ok === null) sawUnknown = true
|
|
134
|
+
}
|
|
135
|
+
return sawUnknown ? null : false
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 从依赖表里挑一个字符串范围。 */
|
|
139
|
+
export function rangeOf(range) {
|
|
140
|
+
if (typeof range === 'string') return range
|
|
141
|
+
if (range && typeof range === 'object' && typeof range.version === 'string') return range.version
|
|
142
|
+
return '*'
|
|
143
|
+
}
|
package/lib/store.mjs
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-modpack —— 本地状态:设置、历史、目录解析。
|
|
3
|
+
*
|
|
4
|
+
* 只写 `$DSH_HOME/dsh-modpack/` 下面:settings.json、history.json、
|
|
5
|
+
* packs/(产出的整合包)、vendor/(从整合包解出来的插件源码)。
|
|
6
|
+
* 不碰 profile 里别人的文件;唯一例外在 install.mjs 里,且带备份。
|
|
7
|
+
*/
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import os from 'node:os'
|
|
10
|
+
import path from 'node:path'
|
|
11
|
+
import { randomBytes } from 'node:crypto'
|
|
12
|
+
|
|
13
|
+
export function dshHome() {
|
|
14
|
+
return process.env.DSH_HOME || path.join(os.homedir(), '.dsh')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function pluginHome() {
|
|
18
|
+
return path.join(dshHome(), 'dsh-modpack')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function profileName(config) {
|
|
22
|
+
const fromEnv = process.env.DSH_PROFILE || process.env.DSH_PROFILE_NAME || ''
|
|
23
|
+
const name = (config && config.profile) || fromEnv || 'desktop'
|
|
24
|
+
return /^[A-Za-z0-9_-]{1,64}$/u.test(name) ? name : 'desktop'
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function profileDir(config) {
|
|
28
|
+
return path.join(dshHome(), 'profiles', profileName(config))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const HOSTS_VERSION = 2
|
|
32
|
+
|
|
33
|
+
export const DEFAULT_CONFIG = {
|
|
34
|
+
profile: '',
|
|
35
|
+
packDir: '',
|
|
36
|
+
vendorDir: '',
|
|
37
|
+
includePayload: true,
|
|
38
|
+
includeConfig: true,
|
|
39
|
+
includeLocalEdits: true,
|
|
40
|
+
keepLocal: true,
|
|
41
|
+
scanBeforePack: true,
|
|
42
|
+
scanBeforeInstall: true,
|
|
43
|
+
blockHighRisk: true,
|
|
44
|
+
applyPackedConfig: true,
|
|
45
|
+
licenseAllow: [
|
|
46
|
+
'MIT', 'ISC', 'Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', '0BSD', 'Unlicense', 'CC0-1.0',
|
|
47
|
+
'MPL-2.0', 'LGPL-2.1', 'LGPL-3.0', 'GPL-2.0', 'GPL-3.0', 'AGPL-3.0',
|
|
48
|
+
'PolyForm-Noncommercial-1.0.0', 'PolyForm-Shield-1.0.0',
|
|
49
|
+
],
|
|
50
|
+
registries: ['https://registry.npmjs.org', 'https://registry.npmmirror.com'],
|
|
51
|
+
allowHosts: [
|
|
52
|
+
'registry.npmjs.org', 'registry.npmmirror.com', 'awesome-dsh-plugin.com', 'dshmarket.com',
|
|
53
|
+
'cdn.jsdelivr.net', 'fastly.jsdelivr.net', 'gh-proxy.com',
|
|
54
|
+
'github.com', 'api.github.com', 'codeload.github.com', 'objects.githubusercontent.com', 'raw.githubusercontent.com',
|
|
55
|
+
],
|
|
56
|
+
maxDownloadBytes: 256 * 1024 * 1024,
|
|
57
|
+
maxPackBytes: 512 * 1024 * 1024,
|
|
58
|
+
maxFileBytes: 16 * 1024 * 1024,
|
|
59
|
+
// 二次开发用:额外扫描规则(形状同 lib/scan.mjs 的 SECRET_RULES)
|
|
60
|
+
extraScanRules: [],
|
|
61
|
+
historyLimit: 50,
|
|
62
|
+
// 插件目录(分类与词条照插件市场):每页多少条、隔多久自己去更新一次
|
|
63
|
+
catalogPageSize: 20,
|
|
64
|
+
catalogRefreshHours: 24,
|
|
65
|
+
// 三处显示位,各自独立;关闭只是不挂载,不影响功能与 API
|
|
66
|
+
showInSettings: true,
|
|
67
|
+
showInSidebar: true,
|
|
68
|
+
showInPluginsTab: false,
|
|
69
|
+
// 安装后是否直接登记到 bundles(默认关:只装文件,重启也不加载)
|
|
70
|
+
enableOnInstall: false,
|
|
71
|
+
// 白名单迁移标记:跑过一次就别再往里并默认域名,尊重用户自己的增删
|
|
72
|
+
hostsVersion: HOSTS_VERSION,
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function pathsFor(config) {
|
|
76
|
+
const home = pluginHome()
|
|
77
|
+
return {
|
|
78
|
+
home,
|
|
79
|
+
settings: path.join(home, 'settings.json'),
|
|
80
|
+
history: path.join(home, 'history.json'),
|
|
81
|
+
log: path.join(pluginHome(), 'modpack.log'),
|
|
82
|
+
packs: (config && config.packDir) || path.join(home, 'packs'),
|
|
83
|
+
vendor: (config && config.vendorDir) || path.join(home, 'vendor'),
|
|
84
|
+
profile: profileDir(config || {}),
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function clampConfig(raw) {
|
|
89
|
+
const cfg = { ...DEFAULT_CONFIG }
|
|
90
|
+
if (!raw || typeof raw !== 'object') return cfg
|
|
91
|
+
for (const key of Object.keys(DEFAULT_CONFIG)) {
|
|
92
|
+
const value = raw[key]
|
|
93
|
+
if (value === undefined) continue
|
|
94
|
+
const def = DEFAULT_CONFIG[key]
|
|
95
|
+
if (Array.isArray(def)) {
|
|
96
|
+
if (Array.isArray(value)) cfg[key] = value.filter((v) => typeof v === 'string').slice(0, 200)
|
|
97
|
+
} else if (typeof def === 'boolean') {
|
|
98
|
+
if (typeof value === 'boolean') cfg[key] = value
|
|
99
|
+
} else if (typeof def === 'number') {
|
|
100
|
+
if (typeof value === 'number' && Number.isFinite(value) && value > 0) cfg[key] = Math.floor(value)
|
|
101
|
+
} else if (typeof value === 'string') {
|
|
102
|
+
cfg[key] = value.slice(0, 512)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// 分页大小与刷新周期:给了边界,避免有人把 10000 写进设置把页面拖死
|
|
106
|
+
cfg.catalogPageSize = Math.min(100, Math.max(5, Number(cfg.catalogPageSize) || DEFAULT_CONFIG.catalogPageSize))
|
|
107
|
+
cfg.catalogRefreshHours = Math.min(168, Math.max(1, Number(cfg.catalogRefreshHours) || DEFAULT_CONFIG.catalogRefreshHours))
|
|
108
|
+
// 进度窗位置不在这里管:它由界面自己拖动并存在浏览器本地,服务端不需要知道
|
|
109
|
+
// 打包前安全检测是强制的:设置里怎么写都不认,打包一律扫。
|
|
110
|
+
cfg.scanBeforePack = true
|
|
111
|
+
return cfg
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function loadConfig() {
|
|
115
|
+
let raw = null
|
|
116
|
+
try { raw = JSON.parse(fs.readFileSync(pathsFor({}).settings, 'utf8')) } catch { raw = null }
|
|
117
|
+
const cfg = clampConfig(raw)
|
|
118
|
+
// 老版本存下的白名单里没有后来新增的域名(例如目录用的 CDN):并一次,之后尊重用户自己的增删
|
|
119
|
+
const known = Number((raw && raw.hostsVersion) || 0)
|
|
120
|
+
if (known < HOSTS_VERSION) {
|
|
121
|
+
const merged = new Set(Array.isArray(cfg.allowHosts) ? cfg.allowHosts : [])
|
|
122
|
+
for (const host of DEFAULT_CONFIG.allowHosts) merged.add(host)
|
|
123
|
+
cfg.allowHosts = [...merged]
|
|
124
|
+
cfg.hostsVersion = HOSTS_VERSION
|
|
125
|
+
if (raw) { try { writeJsonAtomic(pathsFor({}).settings, cfg) } catch {} }
|
|
126
|
+
}
|
|
127
|
+
return cfg
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function writeJsonAtomic(file, value) {
|
|
131
|
+
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
132
|
+
const tmp = file + '.' + randomBytes(4).toString('hex') + '.tmp'
|
|
133
|
+
fs.writeFileSync(tmp, JSON.stringify(value, null, 2), 'utf8')
|
|
134
|
+
fs.renameSync(tmp, file)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function saveConfig(config) {
|
|
138
|
+
const cfg = clampConfig(config)
|
|
139
|
+
writeJsonAtomic(pathsFor(cfg).settings, cfg)
|
|
140
|
+
return cfg
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function readHistory() {
|
|
144
|
+
try {
|
|
145
|
+
const raw = JSON.parse(fs.readFileSync(pathsFor({}).history, 'utf8'))
|
|
146
|
+
return Array.isArray(raw) ? raw : []
|
|
147
|
+
} catch {
|
|
148
|
+
return []
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function appendHistory(entry, limit = DEFAULT_CONFIG.historyLimit) {
|
|
153
|
+
const list = readHistory()
|
|
154
|
+
list.unshift({ ...entry, at: new Date().toISOString() })
|
|
155
|
+
writeJsonAtomic(pathsFor({}).history, list.slice(0, Math.max(1, limit)))
|
|
156
|
+
return list
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function appendLog(line) {
|
|
160
|
+
try {
|
|
161
|
+
fs.mkdirSync(pluginHome(), { recursive: true })
|
|
162
|
+
fs.appendFileSync(pathsFor({}).log, new Date().toISOString() + ' ' + line + '\n')
|
|
163
|
+
} catch {
|
|
164
|
+
// 日志写不进去不该影响主流程
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 文件名安全的 slug:保留中文,去掉路径分隔符与控制字符。 */
|
|
169
|
+
export function slugify(text, fallback = 'modpack') {
|
|
170
|
+
const cleaned = String(text ?? '')
|
|
171
|
+
.replace(/[\u0000-\u001f<>:"/\\|?*]+/gu, '-')
|
|
172
|
+
.replace(/\s+/gu, '-')
|
|
173
|
+
.replace(/-+/gu, '-')
|
|
174
|
+
.replace(/^[-.]+|[-.]+$/gu, '')
|
|
175
|
+
.slice(0, 60)
|
|
176
|
+
return cleaned || fallback
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 同步建目录:调用点全是同步写盘,异步版本会有竞态。 */
|
|
180
|
+
export function ensureDir(dir) {
|
|
181
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
182
|
+
return dir
|
|
183
|
+
}
|