dsh-vigil 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +188 -0
- package/client.js +4652 -0
- package/cordis.patch.yml +7 -0
- package/docs/DETAIL.md +66 -0
- package/icon.png +0 -0
- package/illustrations/notice.png +0 -0
- package/illustrations/warning.png +0 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +75 -0
- package/rules/audit.yaml +130 -0
- package/rules/manifest.json +7 -0
- package/rules/sentinel.yaml +289 -0
- package/src/alert-route.js +194 -0
- package/src/approval.js +398 -0
- package/src/artwork-route.js +87 -0
- package/src/audit/ast.js +213 -0
- package/src/audit/install-scan.js +127 -0
- package/src/audit/lexer.js +307 -0
- package/src/audit/narrative.js +320 -0
- package/src/audit/rules.js +1440 -0
- package/src/audit/scan.js +880 -0
- package/src/audit-clear-route.js +89 -0
- package/src/audit-feed.js +265 -0
- package/src/audit-log.js +887 -0
- package/src/config-schema.js +194 -0
- package/src/config.js +580 -0
- package/src/delete/command-kinds.js +46 -0
- package/src/delete/lex.js +46 -0
- package/src/delete/parser-posix.js +1427 -0
- package/src/delete/parser-pwsh.js +1729 -0
- package/src/delete/parser.js +2082 -0
- package/src/delete/policy.js +680 -0
- package/src/delete/quarantine.js +417 -0
- package/src/index.js +1134 -0
- package/src/install-scan-route.js +58 -0
- package/src/install-watch.js +389 -0
- package/src/integrity.js +509 -0
- package/src/live-config.js +88 -0
- package/src/quarantine-route.js +74 -0
- package/src/quarantine-view.js +44 -0
- package/src/rules/pack.js +423 -0
- package/src/rules/update.js +341 -0
- package/src/rules/yaml.js +186 -0
- package/src/sage-action-route.js +63 -0
- package/src/sage-provision.js +716 -0
- package/src/sentinel/guard.js +392 -0
- package/src/sentinel/hook.js +546 -0
- package/src/sentinel/rules.js +488 -0
- package/src/sentinel/sage-load.js +79 -0
- package/src/sentinel/sage.js +550 -0
- package/src/tools.js +727 -0
- package/src/update-check.js +339 -0
- package/src/util.js +400 -0
- package/threats/README.md +25 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authenticated endpoint behind the settings page's "重新扫描" buttons:
|
|
3
|
+
*
|
|
4
|
+
* POST /api/dsh-vigil/install-scan { target: 'package-name' }
|
|
5
|
+
* POST /api/dsh-vigil/install-scan { all: true }
|
|
6
|
+
*
|
|
7
|
+
* The scan itself is session-independent: it walks the installed bundles under
|
|
8
|
+
* the profiles' `node_modules` and returns fresh audit entries. The scan is
|
|
9
|
+
* read-only; only the audit log receives a new `install-scan` record.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-vigil/install-scan-route
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** Authenticated browser Fetch path for manual plugin re-scans. */
|
|
15
|
+
export const INSTALL_SCAN_ROUTE_PATH = '/api/dsh-vigil/install-scan'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {{ getWatcher: () => any }} deps
|
|
19
|
+
* @returns {{ path: string, methods: Array<'POST'>, requestBody: 'buffered', fetch: (request: Request) => Promise<Response> }}
|
|
20
|
+
*/
|
|
21
|
+
export function createInstallScanRoute(deps) {
|
|
22
|
+
const headers = {
|
|
23
|
+
'content-type': 'application/json; charset=utf-8',
|
|
24
|
+
'cache-control': 'no-store',
|
|
25
|
+
'x-content-type-options': 'nosniff',
|
|
26
|
+
}
|
|
27
|
+
const reply = (status, payload) => new Response(JSON.stringify(payload), { status, headers })
|
|
28
|
+
return {
|
|
29
|
+
path: INSTALL_SCAN_ROUTE_PATH,
|
|
30
|
+
methods: ['POST'],
|
|
31
|
+
requestBody: 'buffered',
|
|
32
|
+
fetch: async request => {
|
|
33
|
+
let body
|
|
34
|
+
try {
|
|
35
|
+
body = await request.json()
|
|
36
|
+
} catch {
|
|
37
|
+
return reply(400, { ok: false, error: 'invalid-json' })
|
|
38
|
+
}
|
|
39
|
+
const watcher = deps.getWatcher?.()
|
|
40
|
+
if (!watcher || typeof watcher.rescan !== 'function') return reply(503, { ok: false, error: 'unavailable' })
|
|
41
|
+
const target = typeof body?.target === 'string' ? body.target.trim() : ''
|
|
42
|
+
const all = body?.all === true
|
|
43
|
+
if (!all && target === '') return reply(400, { ok: false, error: 'invalid-target' })
|
|
44
|
+
try {
|
|
45
|
+
const audits = await watcher.rescan({ target, all })
|
|
46
|
+
return reply(200, {
|
|
47
|
+
ok: true,
|
|
48
|
+
scanned: audits.length,
|
|
49
|
+
audits,
|
|
50
|
+
installed: typeof watcher.installed === 'function' ? watcher.installed() : [],
|
|
51
|
+
lastCheckAt: watcher.lastCheckAt ?? null,
|
|
52
|
+
})
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return reply(500, { ok: false, error: error instanceof Error ? error.message : String(error) })
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Install/update watcher: the "auto vigil_scan" half that works for every
|
|
3
|
+
* install surface (agent tool, GUI plugin manager, CLI, HTTP), because all of
|
|
4
|
+
* them end with the package present under a profile's `node_modules` and the
|
|
5
|
+
* profile's `package.json` (dependencies or installed version) updated.
|
|
6
|
+
*
|
|
7
|
+
* The watcher remembers `{ range, installed version }` for every dependency of
|
|
8
|
+
* every profile under `<DSH_HOME>/profiles/*`. On the first pass it only takes
|
|
9
|
+
* a baseline (already installed bundles are not re-audited on every restart).
|
|
10
|
+
* Afterwards:
|
|
11
|
+
* - a newly added dependency is scanned (`scanReason: 'install'`);
|
|
12
|
+
* - a changed version range or a changed installed version is scanned again
|
|
13
|
+
* (`scanReason: 'update'`).
|
|
14
|
+
* Results are recorded in the audit log (`event: 'install-scan'`), offered to
|
|
15
|
+
* the conversation feed (transient notice) and to the session-independent
|
|
16
|
+
* settings page, which also uses `rescan()` for the manual "重新扫描" buttons.
|
|
17
|
+
*
|
|
18
|
+
* @module dsh-vigil/install-watch
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs'
|
|
22
|
+
import { homedir } from 'node:os'
|
|
23
|
+
import { basename, join } from 'node:path'
|
|
24
|
+
import { scanPluginDirectory } from './audit/scan.js'
|
|
25
|
+
import { summarizeInstallScan } from './audit/install-scan.js'
|
|
26
|
+
import { sha256 } from './util.js'
|
|
27
|
+
|
|
28
|
+
const DEFAULT_INTERVAL_MS = 60_000
|
|
29
|
+
const MAX_AUDITS = 12
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {{ profileDir: string, name: string, range: string, version: string|null }} DependencyEntry
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {{
|
|
37
|
+
* record?: (entry: object) => void,
|
|
38
|
+
* logger?: { warn?: (message: string) => void },
|
|
39
|
+
* scan?: typeof scanPluginDirectory,
|
|
40
|
+
* dshHome?: string,
|
|
41
|
+
* intervalMs?: number,
|
|
42
|
+
* maxAudits?: number,
|
|
43
|
+
* }} [options]
|
|
44
|
+
*/
|
|
45
|
+
export function createInstallWatcher(options = {}) {
|
|
46
|
+
const record = options.record
|
|
47
|
+
const logger = options.logger
|
|
48
|
+
const scan = options.scan ?? scanPluginDirectory
|
|
49
|
+
const dshHome = options.dshHome ?? (process.env.DSH_HOME || join(homedir(), '.dsh'))
|
|
50
|
+
const profilesRoot = join(dshHome, 'profiles')
|
|
51
|
+
const intervalMs = Math.max(5000, Number(options.intervalMs) || DEFAULT_INTERVAL_MS)
|
|
52
|
+
const maxAudits = Math.max(1, Number(options.maxAudits) || MAX_AUDITS)
|
|
53
|
+
/**
|
|
54
|
+
* AST tuning handed to every background scan. A getter keeps the plugin's
|
|
55
|
+
* live Settings values authoritative: `config.scan.ast` inside the Host is a
|
|
56
|
+
* pair of volatile references, not plain booleans/strings.
|
|
57
|
+
*/
|
|
58
|
+
const getAst = typeof options.getAst === 'function'
|
|
59
|
+
? options.getAst
|
|
60
|
+
: () => options.ast
|
|
61
|
+
function currentAst() {
|
|
62
|
+
const value = getAst()
|
|
63
|
+
if (!value || typeof value !== 'object') return undefined
|
|
64
|
+
const ast = {}
|
|
65
|
+
if (typeof value.enabled === 'boolean') ast.enabled = value.enabled
|
|
66
|
+
if (typeof value.level === 'string') ast.level = value.level
|
|
67
|
+
return Object.keys(ast).length > 0 ? ast : undefined
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @type {Map<string, DependencyEntry>|null} */
|
|
71
|
+
let seen = null
|
|
72
|
+
/** @type {Array<object>} */
|
|
73
|
+
const audits = []
|
|
74
|
+
/** @type {any} */
|
|
75
|
+
let timer = null
|
|
76
|
+
/** @type {Promise<Array<object>>|null} */
|
|
77
|
+
let checking = null
|
|
78
|
+
/** @type {string|null} */
|
|
79
|
+
let lastCheckAt = null
|
|
80
|
+
/** @type {string|null} */
|
|
81
|
+
let lastError = null
|
|
82
|
+
/** Progress of the scan currently running (background scans must be visible). */
|
|
83
|
+
const scanState = { analyzing: null, scanned: 0, startedAt: null, finishedAt: null, packages: 0 }
|
|
84
|
+
/** Last dependency walk, reused while every profile manifest is unchanged. */
|
|
85
|
+
let collectCache = null
|
|
86
|
+
|
|
87
|
+
function profileDirs() {
|
|
88
|
+
try {
|
|
89
|
+
return readdirSync(profilesRoot, { withFileTypes: true })
|
|
90
|
+
.filter(entry => entry.isDirectory())
|
|
91
|
+
.map(entry => join(profilesRoot, entry.name))
|
|
92
|
+
} catch {
|
|
93
|
+
return []
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Content signature: profile list plus each `package.json`'s full text hash.
|
|
99
|
+
*
|
|
100
|
+
* The hash must be content-based, not `mtime`/`size`-based: NTFS timestamps
|
|
101
|
+
* come from the system clock tick, so a same-size manifest rewritten within
|
|
102
|
+
* one tick keeps both fields identical. A stat-only cache then reports "no
|
|
103
|
+
* change" for a very real update (and its install scan is never refreshed).
|
|
104
|
+
*/
|
|
105
|
+
function profilesSignature(dirs, texts) {
|
|
106
|
+
return dirs.map((dir, index) => {
|
|
107
|
+
const text = texts[index]
|
|
108
|
+
return `${basename(dir)}:${text === null ? 'missing' : sha256(text)}`
|
|
109
|
+
}).join('|')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @param {string} profileDir
|
|
114
|
+
* @param {string} name
|
|
115
|
+
* @returns {{ version: string|null, dir: string } | null}
|
|
116
|
+
*/
|
|
117
|
+
function locateInstalled(profileDir, name) {
|
|
118
|
+
const dir = join(profileDir, 'node_modules', ...name.split('/'))
|
|
119
|
+
try {
|
|
120
|
+
if (!existsSync(join(dir, 'package.json'))) return null
|
|
121
|
+
const manifest = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'))
|
|
122
|
+
return { version: typeof manifest?.version === 'string' ? manifest.version : null, dir }
|
|
123
|
+
} catch {
|
|
124
|
+
return null
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** @returns {Map<string, DependencyEntry>} */
|
|
129
|
+
function collectDependencies() {
|
|
130
|
+
const dirs = profileDirs()
|
|
131
|
+
// Read each profile manifest once per pass: the text feeds the signature and
|
|
132
|
+
// (on a real change) the dependency walk, so the cache saves the per-plugin
|
|
133
|
+
// `locateInstalled` reads rather than this single small read.
|
|
134
|
+
const texts = dirs.map(profileDir => {
|
|
135
|
+
try {
|
|
136
|
+
return readFileSync(join(profileDir, 'package.json'), 'utf8')
|
|
137
|
+
} catch {
|
|
138
|
+
return null
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
const signature = profilesSignature(dirs, texts)
|
|
142
|
+
if (collectCache && collectCache.signature === signature) {
|
|
143
|
+
return new Map(collectCache.entries)
|
|
144
|
+
}
|
|
145
|
+
const map = new Map()
|
|
146
|
+
for (let index = 0; index < dirs.length; index += 1) {
|
|
147
|
+
const profileDir = dirs[index]
|
|
148
|
+
const text = texts[index]
|
|
149
|
+
if (text === null) continue
|
|
150
|
+
let manifest
|
|
151
|
+
try {
|
|
152
|
+
manifest = JSON.parse(text)
|
|
153
|
+
} catch {
|
|
154
|
+
continue
|
|
155
|
+
}
|
|
156
|
+
const dependencies = {
|
|
157
|
+
...(manifest && typeof manifest === 'object' ? manifest.dependencies ?? {} : {}),
|
|
158
|
+
...(manifest && typeof manifest === 'object' ? manifest.devDependencies ?? {} : {}),
|
|
159
|
+
}
|
|
160
|
+
for (const [name, range] of Object.entries(dependencies)) {
|
|
161
|
+
if (typeof name !== 'string' || name.trim() === '') continue
|
|
162
|
+
const located = locateInstalled(profileDir, name)
|
|
163
|
+
if (located) map.set(`${profileDir}\u0000${name}`, { profileDir, name, range: String(range), version: located.version })
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
collectCache = { signature, entries: map }
|
|
167
|
+
return new Map(map)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Scan one installed dependency and publish the result.
|
|
172
|
+
* @param {DependencyEntry} entry
|
|
173
|
+
* @param {'install'|'update'|'manual'} scanReason
|
|
174
|
+
* @returns {Promise<object|null>}
|
|
175
|
+
*/
|
|
176
|
+
async function auditDependency(entry, scanReason) {
|
|
177
|
+
const located = locateInstalled(entry.profileDir, entry.name)
|
|
178
|
+
if (!located) return null
|
|
179
|
+
let report
|
|
180
|
+
scanState.analyzing = entry.name
|
|
181
|
+
scanState.scanned = 0
|
|
182
|
+
scanState.startedAt = new Date().toISOString()
|
|
183
|
+
scanState.finishedAt = null
|
|
184
|
+
scanState.packages = 0
|
|
185
|
+
try {
|
|
186
|
+
report = await scan(located.dir, {
|
|
187
|
+
includeInfo: false,
|
|
188
|
+
maxFiles: 400,
|
|
189
|
+
ast: currentAst(),
|
|
190
|
+
// Cooperative scanning: the scanner yields to the event loop every 25
|
|
191
|
+
// files, so a big package never freezes the Host while it is analysed.
|
|
192
|
+
onProgress: () => {
|
|
193
|
+
scanState.scanned += 1
|
|
194
|
+
},
|
|
195
|
+
})
|
|
196
|
+
} catch (error) {
|
|
197
|
+
lastError = `安装审计失败:${entry.name}: ${error instanceof Error ? error.message : String(error)}`
|
|
198
|
+
logger?.warn?.(`[dsh-vigil] ${lastError}`)
|
|
199
|
+
try {
|
|
200
|
+
record?.({
|
|
201
|
+
kind: 'event',
|
|
202
|
+
event: 'install-scan',
|
|
203
|
+
tool: 'plugin_manager',
|
|
204
|
+
decision: 'none',
|
|
205
|
+
ruleIds: ['VG-SCAN-02'],
|
|
206
|
+
reason: lastError,
|
|
207
|
+
})
|
|
208
|
+
} catch {
|
|
209
|
+
// an audit failure must not hide the scan failure itself
|
|
210
|
+
}
|
|
211
|
+
return null
|
|
212
|
+
} finally {
|
|
213
|
+
scanState.analyzing = null
|
|
214
|
+
scanState.finishedAt = new Date().toISOString()
|
|
215
|
+
scanState.packages += 1
|
|
216
|
+
}
|
|
217
|
+
const summary = summarizeInstallScan(report, { target: entry.name, dir: located.dir })
|
|
218
|
+
const auditEntry = {
|
|
219
|
+
name: entry.name,
|
|
220
|
+
version: located.version,
|
|
221
|
+
profile: basename(entry.profileDir),
|
|
222
|
+
at: new Date().toISOString(),
|
|
223
|
+
scanReason,
|
|
224
|
+
blockers: summary.blockers,
|
|
225
|
+
review: summary.review,
|
|
226
|
+
notice: summary.notice,
|
|
227
|
+
ruleIds: summary.ruleIds.slice(0, 12),
|
|
228
|
+
top: summary.top,
|
|
229
|
+
total: summary.total,
|
|
230
|
+
truncated: summary.truncated === true,
|
|
231
|
+
text: summary.text,
|
|
232
|
+
}
|
|
233
|
+
audits.push(auditEntry)
|
|
234
|
+
if (audits.length > maxAudits) audits.shift()
|
|
235
|
+
try {
|
|
236
|
+
record?.({
|
|
237
|
+
kind: 'event',
|
|
238
|
+
event: 'install-scan',
|
|
239
|
+
tool: 'plugin_manager',
|
|
240
|
+
// A post-install/manual scan is an observation, not a decision: nothing
|
|
241
|
+
// was asked, so recording `ask` would pollute the "询问" counters.
|
|
242
|
+
decision: 'none',
|
|
243
|
+
ruleIds: auditEntry.ruleIds,
|
|
244
|
+
reason: `${entry.name}${located.version ? `@${located.version}` : ''}${scanReason === 'update' ? '(更新)' : scanReason === 'manual' ? '(手动重扫)' : ''} ${summary.text}`,
|
|
245
|
+
extra: {
|
|
246
|
+
target: entry.name,
|
|
247
|
+
version: located.version,
|
|
248
|
+
profile: auditEntry.profile,
|
|
249
|
+
scanReason,
|
|
250
|
+
blockers: summary.blockers,
|
|
251
|
+
review: summary.review,
|
|
252
|
+
notices: summary.notice,
|
|
253
|
+
findings: summary.top,
|
|
254
|
+
totalFindings: summary.total,
|
|
255
|
+
truncated: summary.truncated === true,
|
|
256
|
+
},
|
|
257
|
+
})
|
|
258
|
+
} catch {
|
|
259
|
+
// audit failures never block the watcher
|
|
260
|
+
}
|
|
261
|
+
return auditEntry
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** One pass over every profile; returns the audits this pass produced. */
|
|
265
|
+
async function check() {
|
|
266
|
+
if (checking) return checking
|
|
267
|
+
checking = (async () => {
|
|
268
|
+
const next = collectDependencies()
|
|
269
|
+
lastCheckAt = new Date().toISOString()
|
|
270
|
+
lastError = null
|
|
271
|
+
if (seen === null) {
|
|
272
|
+
// Baseline: everything already installed predates this activation.
|
|
273
|
+
seen = next
|
|
274
|
+
return []
|
|
275
|
+
}
|
|
276
|
+
const previous = seen
|
|
277
|
+
const added = [...next.entries()].filter(([key]) => !previous.has(key))
|
|
278
|
+
const updated = [...next.entries()].filter(([key, entry]) => {
|
|
279
|
+
const before = previous.get(key)
|
|
280
|
+
return Boolean(before) && (before.range !== entry.range || before.version !== entry.version)
|
|
281
|
+
})
|
|
282
|
+
/** @type {Array<object>} */
|
|
283
|
+
const produced = []
|
|
284
|
+
/** Keys whose scan failed; they must stay eligible for a later retry. */
|
|
285
|
+
const failed = []
|
|
286
|
+
for (const [key, entry] of added) {
|
|
287
|
+
const result = await auditDependency(entry, 'install')
|
|
288
|
+
if (result) produced.push(result)
|
|
289
|
+
else failed.push(key)
|
|
290
|
+
}
|
|
291
|
+
for (const [key, entry] of updated) {
|
|
292
|
+
const result = await auditDependency(entry, 'update')
|
|
293
|
+
if (result) produced.push(result)
|
|
294
|
+
else failed.push(key)
|
|
295
|
+
}
|
|
296
|
+
for (const key of failed) {
|
|
297
|
+
const before = previous.get(key)
|
|
298
|
+
if (before) next.set(key, before)
|
|
299
|
+
else next.delete(key)
|
|
300
|
+
}
|
|
301
|
+
seen = next
|
|
302
|
+
return produced
|
|
303
|
+
})().finally(() => {
|
|
304
|
+
checking = null
|
|
305
|
+
})
|
|
306
|
+
return checking
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Manual re-scan from the settings page: one named package, or every installed
|
|
311
|
+
* dependency of every profile.
|
|
312
|
+
* @param {{ target?: string, all?: boolean }} [request]
|
|
313
|
+
* @returns {Promise<Array<object>>}
|
|
314
|
+
*/
|
|
315
|
+
async function rescan(request = {}) {
|
|
316
|
+
const all = request.all === true
|
|
317
|
+
const target = typeof request.target === 'string' ? request.target.trim() : ''
|
|
318
|
+
if (!all && target === '') return []
|
|
319
|
+
const entries = [...collectDependencies().values()].filter(entry => {
|
|
320
|
+
if (all) return true
|
|
321
|
+
return entry.name === target || `${entry.name}@${entry.version ?? ''}` === target
|
|
322
|
+
})
|
|
323
|
+
lastCheckAt = new Date().toISOString()
|
|
324
|
+
lastError = null
|
|
325
|
+
const produced = []
|
|
326
|
+
for (const entry of entries) {
|
|
327
|
+
const result = await auditDependency(entry, 'manual')
|
|
328
|
+
if (result) produced.push(result)
|
|
329
|
+
}
|
|
330
|
+
return produced
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Installed dependencies across profiles, for the settings page list. */
|
|
334
|
+
function installed() {
|
|
335
|
+
return [...collectDependencies().values()]
|
|
336
|
+
.map(entry => ({ name: entry.name, version: entry.version, profile: basename(entry.profileDir) }))
|
|
337
|
+
.sort((left, right) => left.name.localeCompare(right.name))
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function start() {
|
|
341
|
+
if (timer) return
|
|
342
|
+
void check().catch(error => {
|
|
343
|
+
lastError = error instanceof Error ? error.message : String(error)
|
|
344
|
+
})
|
|
345
|
+
timer = setInterval(() => {
|
|
346
|
+
void check().catch(error => {
|
|
347
|
+
lastError = error instanceof Error ? error.message : String(error)
|
|
348
|
+
})
|
|
349
|
+
}, intervalMs)
|
|
350
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function stop() {
|
|
354
|
+
if (timer) {
|
|
355
|
+
clearInterval(timer)
|
|
356
|
+
timer = null
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return {
|
|
361
|
+
start,
|
|
362
|
+
stop,
|
|
363
|
+
check,
|
|
364
|
+
rescan,
|
|
365
|
+
installed,
|
|
366
|
+
list: () => [...audits],
|
|
367
|
+
get lastCheckAt() {
|
|
368
|
+
return lastCheckAt
|
|
369
|
+
},
|
|
370
|
+
get lastError() {
|
|
371
|
+
return lastError
|
|
372
|
+
},
|
|
373
|
+
get baselineReady() {
|
|
374
|
+
return seen !== null
|
|
375
|
+
},
|
|
376
|
+
get state() {
|
|
377
|
+
return { ...scanState }
|
|
378
|
+
},
|
|
379
|
+
describe() {
|
|
380
|
+
if (scanState.analyzing) {
|
|
381
|
+
return `安装审计:正在扫描 ${scanState.analyzing}(已读 ${scanState.scanned} 个文件)`
|
|
382
|
+
}
|
|
383
|
+
const last = audits[audits.length - 1]
|
|
384
|
+
if (!last) return `安装审计:等待新插件(检查于 ${lastCheckAt ?? '尚未检查'})`
|
|
385
|
+
const reason = last.scanReason === 'update' ? '更新后' : last.scanReason === 'manual' ? '手动重扫' : '安装后'
|
|
386
|
+
return `安装审计:最近 ${last.name}${last.version ? `@${last.version}` : ''}(${reason}:${last.blockers} 高危 / ${last.review} 可疑),检查于 ${lastCheckAt ?? last.at}`
|
|
387
|
+
},
|
|
388
|
+
}
|
|
389
|
+
}
|