dsh-vigil 0.0.1 → 0.0.2
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/client.js +4333 -4652
- package/package.json +1 -1
- package/rules/audit.yaml +130 -130
- package/rules/manifest.json +7 -7
- package/rules/sentinel.yaml +289 -289
- package/src/alert-route.js +194 -194
- package/src/artwork-route.js +87 -87
- package/src/audit/ast.js +213 -213
- package/src/audit/install-scan.js +127 -127
- package/src/audit/narrative.js +320 -320
- package/src/audit/rules.js +1440 -1440
- package/src/audit/scan.js +880 -880
- package/src/audit-feed.js +268 -265
- package/src/audit-log.js +887 -887
- package/src/config-schema.js +194 -194
- package/src/config.js +580 -580
- package/src/delete/parser-posix.js +1427 -1427
- package/src/delete/parser-pwsh.js +1729 -1729
- package/src/delete/parser.js +2082 -2082
- package/src/delete/policy.js +680 -680
- package/src/delete/quarantine.js +417 -417
- package/src/index.js +1128 -1134
- package/src/install-watch.js +389 -389
- package/src/integrity.js +509 -509
- package/src/rules/pack.js +423 -423
- package/src/rules/update.js +341 -341
- package/src/rules/yaml.js +186 -186
- package/src/sentinel/guard.js +392 -392
- package/src/sentinel/hook.js +546 -546
- package/src/sentinel/rules.js +488 -488
- package/src/tools.js +727 -727
- package/src/util.js +400 -400
- package/src/update-check.js +0 -339
package/src/audit-feed.js
CHANGED
|
@@ -1,265 +1,268 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Conversation-facing audit feed.
|
|
3
|
-
*
|
|
4
|
-
* The Chat audit drawer reads the Host's hash-chained log through the Harness
|
|
5
|
-
* browser-native Fetch channel:
|
|
6
|
-
*
|
|
7
|
-
* GET /api/dsh-vigil/audit?limit=40
|
|
8
|
-
*
|
|
9
|
-
* `ctx.connection.fetch.register()` routes are mounted inside the authenticated
|
|
10
|
-
* `/api` channel, so the browser's same-origin `fetch` carries the normal signed
|
|
11
|
-
* credential and the carrier rejects unauthenticated peers before this handler
|
|
12
|
-
* runs. The route is therefore read-only and does not implement its own trust
|
|
13
|
-
* fence; it must never be mounted on the raw Web server instead.
|
|
14
|
-
*
|
|
15
|
-
* The payload contains only the already-scrubbed audit summary (tool name,
|
|
16
|
-
* decision, reason, rule ids, Sage attribution). Raw tool arguments never leave
|
|
17
|
-
* the Host through this route.
|
|
18
|
-
*
|
|
19
|
-
* @module dsh-vigil/audit-feed
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
import { nowIso, sha256 } from './util.js'
|
|
23
|
-
import { quarantineView } from './quarantine-view.js'
|
|
24
|
-
import { probeAlertWindow } from './alert-route.js'
|
|
25
|
-
|
|
26
|
-
/** Authenticated browser fetch path for the audit drawer. */
|
|
27
|
-
export const AUDIT_FEED_PATH = '/api/dsh-vigil/audit'
|
|
28
|
-
|
|
29
|
-
/** `installed()` walks every profile; cache it until the watcher checks again. */
|
|
30
|
-
const installedCache = new WeakMap()
|
|
31
|
-
/** Stable `generatedAt` while the feed content is unchanged (enables 204s). */
|
|
32
|
-
const feedCache = new WeakMap()
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @param {any} watcher
|
|
36
|
-
* @returns {Array<object>}
|
|
37
|
-
*/
|
|
38
|
-
function installedFor(watcher) {
|
|
39
|
-
if (!watcher || typeof watcher.installed !== 'function') return []
|
|
40
|
-
const key = `${watcher.lastCheckAt ?? ''}\u0000${watcher.lastError ?? ''}`
|
|
41
|
-
const cached = installedCache.get(watcher)
|
|
42
|
-
if (cached && cached.key === key) return cached.value
|
|
43
|
-
const value = watcher.installed()
|
|
44
|
-
const list = Array.isArray(value) ? value : []
|
|
45
|
-
installedCache.set(watcher, { key, value: list })
|
|
46
|
-
return list
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Project the current runtime's audit entries into the feed payload.
|
|
51
|
-
* @param {any} runtime current runtime generation
|
|
52
|
-
* @param {{ isDisabled?: () => boolean }} deps
|
|
53
|
-
* @param {{ limit?: number }} [options]
|
|
54
|
-
* @returns {object}
|
|
55
|
-
*/
|
|
56
|
-
export function buildAuditFeed(runtime, deps, options = {}) {
|
|
57
|
-
// The client projects this feed to one session. A window that is too small
|
|
58
|
-
// lets other sessions (or a busy subagent tree) push the current session's
|
|
59
|
-
// own blocks out, which read as "本会话还没有拦截记录". Keep a much wider
|
|
60
|
-
// window and let the per-session projection do the trimming.
|
|
61
|
-
const limit = Math.max(1, Math.min(Number(options.limit ?? 300) || 300, 500))
|
|
62
|
-
const windowSize = Math.max(limit * 2, 600)
|
|
63
|
-
const rawRecords = typeof runtime?.audit?.read === 'function'
|
|
64
|
-
? runtime.audit.read({ limit: windowSize })
|
|
65
|
-
: []
|
|
66
|
-
const records = Array.isArray(rawRecords) ? rawRecords : []
|
|
67
|
-
/** @type {Array<{ ts: string, tool: string|null, decision: string, wouldBe: string|null, ruleIds: string[], reason: string, sage: object|null, session: string|null }>} */
|
|
68
|
-
const decisions = []
|
|
69
|
-
for (const record of records) {
|
|
70
|
-
if (record?.kind !== 'decision') continue
|
|
71
|
-
if (record.event !== 'pre-execute' && record.event !== 'pre-execute-error') continue
|
|
72
|
-
decisions.push({
|
|
73
|
-
ts: typeof record.ts === 'string' ? record.ts : '',
|
|
74
|
-
tool: typeof record.tool === 'string' ? record.tool : null,
|
|
75
|
-
decision: typeof record.decision === 'string' ? record.decision : 'none',
|
|
76
|
-
wouldBe: typeof record.wouldBe === 'string' ? record.wouldBe : null,
|
|
77
|
-
ruleIds: Array.isArray(record.ruleIds) ? record.ruleIds.filter(item => typeof item === 'string') : [],
|
|
78
|
-
reason: typeof record.reason === 'string' ? record.reason : '',
|
|
79
|
-
sage: record.sage && typeof record.sage === 'object'
|
|
80
|
-
? {
|
|
81
|
-
threatId: typeof record.sage.threatId === 'string' ? record.sage.threatId : null,
|
|
82
|
-
category: typeof record.sage.category === 'string' ? record.sage.category : null,
|
|
83
|
-
severity: typeof record.sage.severity === 'string' ? record.sage.severity : null,
|
|
84
|
-
attribution: typeof record.sage.attribution === 'string' ? record.sage.attribution : 'Sage (@gendigital/sage-core, Apache-2.0)',
|
|
85
|
-
}
|
|
86
|
-
: null,
|
|
87
|
-
session: typeof record.session === 'string' ? record.session : null,
|
|
88
|
-
approval: record.approval && typeof record.approval === 'object'
|
|
89
|
-
? {
|
|
90
|
-
action: typeof record.approval.action === 'string' ? record.approval.action : null,
|
|
91
|
-
scope: typeof record.approval.scope === 'string' ? record.approval.scope : null,
|
|
92
|
-
remembered: record.approval.remembered === true,
|
|
93
|
-
}
|
|
94
|
-
: null,
|
|
95
|
-
})
|
|
96
|
-
}
|
|
97
|
-
const recent = decisions.slice(-limit)
|
|
98
|
-
const counts = { allow: 0, ask: 0, deny: 0 }
|
|
99
|
-
for (const decision of recent) {
|
|
100
|
-
if (decision.decision === 'allow') counts.allow += 1
|
|
101
|
-
else if (decision.decision === 'ask') counts.ask += 1
|
|
102
|
-
else if (decision.decision === 'deny') counts.deny += 1
|
|
103
|
-
}
|
|
104
|
-
const sage = runtime?.sage?.status ?? {}
|
|
105
|
-
const feed = {
|
|
106
|
-
schema: 1,
|
|
107
|
-
generatedAt: nowIso(),
|
|
108
|
-
mode: runtime?.config?.mode ?? 'enforce',
|
|
109
|
-
/**
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
return []
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return []
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
return []
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
//
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
feed.generatedAt =
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
*
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
'
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Conversation-facing audit feed.
|
|
3
|
+
*
|
|
4
|
+
* The Chat audit drawer reads the Host's hash-chained log through the Harness
|
|
5
|
+
* browser-native Fetch channel:
|
|
6
|
+
*
|
|
7
|
+
* GET /api/dsh-vigil/audit?limit=40
|
|
8
|
+
*
|
|
9
|
+
* `ctx.connection.fetch.register()` routes are mounted inside the authenticated
|
|
10
|
+
* `/api` channel, so the browser's same-origin `fetch` carries the normal signed
|
|
11
|
+
* credential and the carrier rejects unauthenticated peers before this handler
|
|
12
|
+
* runs. The route is therefore read-only and does not implement its own trust
|
|
13
|
+
* fence; it must never be mounted on the raw Web server instead.
|
|
14
|
+
*
|
|
15
|
+
* The payload contains only the already-scrubbed audit summary (tool name,
|
|
16
|
+
* decision, reason, rule ids, Sage attribution). Raw tool arguments never leave
|
|
17
|
+
* the Host through this route.
|
|
18
|
+
*
|
|
19
|
+
* @module dsh-vigil/audit-feed
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { nowIso, sha256 } from './util.js'
|
|
23
|
+
import { quarantineView } from './quarantine-view.js'
|
|
24
|
+
import { probeAlertWindow } from './alert-route.js'
|
|
25
|
+
|
|
26
|
+
/** Authenticated browser fetch path for the audit drawer. */
|
|
27
|
+
export const AUDIT_FEED_PATH = '/api/dsh-vigil/audit'
|
|
28
|
+
|
|
29
|
+
/** `installed()` walks every profile; cache it until the watcher checks again. */
|
|
30
|
+
const installedCache = new WeakMap()
|
|
31
|
+
/** Stable `generatedAt` while the feed content is unchanged (enables 204s). */
|
|
32
|
+
const feedCache = new WeakMap()
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {any} watcher
|
|
36
|
+
* @returns {Array<object>}
|
|
37
|
+
*/
|
|
38
|
+
function installedFor(watcher) {
|
|
39
|
+
if (!watcher || typeof watcher.installed !== 'function') return []
|
|
40
|
+
const key = `${watcher.lastCheckAt ?? ''}\u0000${watcher.lastError ?? ''}`
|
|
41
|
+
const cached = installedCache.get(watcher)
|
|
42
|
+
if (cached && cached.key === key) return cached.value
|
|
43
|
+
const value = watcher.installed()
|
|
44
|
+
const list = Array.isArray(value) ? value : []
|
|
45
|
+
installedCache.set(watcher, { key, value: list })
|
|
46
|
+
return list
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Project the current runtime's audit entries into the feed payload.
|
|
51
|
+
* @param {any} runtime current runtime generation
|
|
52
|
+
* @param {{ isDisabled?: () => boolean }} deps
|
|
53
|
+
* @param {{ limit?: number }} [options]
|
|
54
|
+
* @returns {object}
|
|
55
|
+
*/
|
|
56
|
+
export function buildAuditFeed(runtime, deps, options = {}) {
|
|
57
|
+
// The client projects this feed to one session. A window that is too small
|
|
58
|
+
// lets other sessions (or a busy subagent tree) push the current session's
|
|
59
|
+
// own blocks out, which read as "本会话还没有拦截记录". Keep a much wider
|
|
60
|
+
// window and let the per-session projection do the trimming.
|
|
61
|
+
const limit = Math.max(1, Math.min(Number(options.limit ?? 300) || 300, 500))
|
|
62
|
+
const windowSize = Math.max(limit * 2, 600)
|
|
63
|
+
const rawRecords = typeof runtime?.audit?.read === 'function'
|
|
64
|
+
? runtime.audit.read({ limit: windowSize })
|
|
65
|
+
: []
|
|
66
|
+
const records = Array.isArray(rawRecords) ? rawRecords : []
|
|
67
|
+
/** @type {Array<{ ts: string, tool: string|null, decision: string, wouldBe: string|null, ruleIds: string[], reason: string, sage: object|null, session: string|null }>} */
|
|
68
|
+
const decisions = []
|
|
69
|
+
for (const record of records) {
|
|
70
|
+
if (record?.kind !== 'decision') continue
|
|
71
|
+
if (record.event !== 'pre-execute' && record.event !== 'pre-execute-error') continue
|
|
72
|
+
decisions.push({
|
|
73
|
+
ts: typeof record.ts === 'string' ? record.ts : '',
|
|
74
|
+
tool: typeof record.tool === 'string' ? record.tool : null,
|
|
75
|
+
decision: typeof record.decision === 'string' ? record.decision : 'none',
|
|
76
|
+
wouldBe: typeof record.wouldBe === 'string' ? record.wouldBe : null,
|
|
77
|
+
ruleIds: Array.isArray(record.ruleIds) ? record.ruleIds.filter(item => typeof item === 'string') : [],
|
|
78
|
+
reason: typeof record.reason === 'string' ? record.reason : '',
|
|
79
|
+
sage: record.sage && typeof record.sage === 'object'
|
|
80
|
+
? {
|
|
81
|
+
threatId: typeof record.sage.threatId === 'string' ? record.sage.threatId : null,
|
|
82
|
+
category: typeof record.sage.category === 'string' ? record.sage.category : null,
|
|
83
|
+
severity: typeof record.sage.severity === 'string' ? record.sage.severity : null,
|
|
84
|
+
attribution: typeof record.sage.attribution === 'string' ? record.sage.attribution : 'Sage (@gendigital/sage-core, Apache-2.0)',
|
|
85
|
+
}
|
|
86
|
+
: null,
|
|
87
|
+
session: typeof record.session === 'string' ? record.session : null,
|
|
88
|
+
approval: record.approval && typeof record.approval === 'object'
|
|
89
|
+
? {
|
|
90
|
+
action: typeof record.approval.action === 'string' ? record.approval.action : null,
|
|
91
|
+
scope: typeof record.approval.scope === 'string' ? record.approval.scope : null,
|
|
92
|
+
remembered: record.approval.remembered === true,
|
|
93
|
+
}
|
|
94
|
+
: null,
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
const recent = decisions.slice(-limit)
|
|
98
|
+
const counts = { allow: 0, ask: 0, deny: 0 }
|
|
99
|
+
for (const decision of recent) {
|
|
100
|
+
if (decision.decision === 'allow') counts.allow += 1
|
|
101
|
+
else if (decision.decision === 'ask') counts.ask += 1
|
|
102
|
+
else if (decision.decision === 'deny') counts.deny += 1
|
|
103
|
+
}
|
|
104
|
+
const sage = runtime?.sage?.status ?? {}
|
|
105
|
+
const feed = {
|
|
106
|
+
schema: 1,
|
|
107
|
+
generatedAt: nowIso(),
|
|
108
|
+
mode: runtime?.config?.mode ?? 'enforce',
|
|
109
|
+
/** Installed plugin version, read from the manifest; never fetched remotely. */
|
|
110
|
+
version: typeof options.version === 'string' ? options.version : null,
|
|
111
|
+
/** Host locale hint for reasons that are still generated in Chinese. */
|
|
112
|
+
locale: typeof runtime?.config?.locale === 'string' ? runtime.config.locale : 'auto',
|
|
113
|
+
killSwitch: deps?.isDisabled?.() === true,
|
|
114
|
+
killSwitchPath: typeof runtime?.config?.killSwitch?.path === 'string' ? runtime.config.killSwitch.path : '',
|
|
115
|
+
killSwitchEnvVar: typeof runtime?.config?.killSwitch?.envVar === 'string' && runtime.config.killSwitch.envVar !== ''
|
|
116
|
+
? runtime.config.killSwitch.envVar
|
|
117
|
+
: 'DSH_VIGIL_DISABLED',
|
|
118
|
+
notify: {
|
|
119
|
+
enabled: runtime?.config?.notify?.enabled !== false,
|
|
120
|
+
includeAsk: runtime?.config?.notify?.includeAsk !== false,
|
|
121
|
+
},
|
|
122
|
+
approval: {
|
|
123
|
+
/** The page can answer `ask` decisions itself instead of waiting on the host dialog. */
|
|
124
|
+
enabled: runtime?.config?.notify?.inlineApproval !== false,
|
|
125
|
+
timeoutMs: Number(runtime?.config?.notify?.approvalTimeoutMs) || 120000,
|
|
126
|
+
/** Per-session remembered allow state (blanket + per-finding). */
|
|
127
|
+
sessions: (() => {
|
|
128
|
+
try {
|
|
129
|
+
return Array.isArray(options.approval?.sessions?.()) ? options.approval.sessions() : []
|
|
130
|
+
} catch {
|
|
131
|
+
return []
|
|
132
|
+
}
|
|
133
|
+
})(),
|
|
134
|
+
},
|
|
135
|
+
/** Calls currently parked waiting for the page's 允许 / 拒绝 answer. */
|
|
136
|
+
pending: (() => {
|
|
137
|
+
try {
|
|
138
|
+
return Array.isArray(options.approval?.list?.()) ? options.approval.list() : []
|
|
139
|
+
} catch {
|
|
140
|
+
return []
|
|
141
|
+
}
|
|
142
|
+
})(),
|
|
143
|
+
decisions: recent,
|
|
144
|
+
counts,
|
|
145
|
+
/** Read-only status lines for the Settings page card. */
|
|
146
|
+
integrity: {
|
|
147
|
+
describe: typeof runtime?.integrity?.describe === 'function' ? runtime.integrity.describe() : '',
|
|
148
|
+
baseline: runtime?.integrity?.state?.baseline?.state ?? null,
|
|
149
|
+
},
|
|
150
|
+
rules: {
|
|
151
|
+
sources: Array.isArray(runtime?.rulePack?.sources) ? runtime.rulePack.sources.length : 0,
|
|
152
|
+
warnings: Array.isArray(runtime?.rulePack?.warnings) ? runtime.rulePack.warnings : [],
|
|
153
|
+
},
|
|
154
|
+
sage: {
|
|
155
|
+
state: typeof sage.state === 'string' ? sage.state : 'unknown',
|
|
156
|
+
version: typeof sage.version === 'string' ? sage.version : null,
|
|
157
|
+
rules: Number(sage.rules) || 0,
|
|
158
|
+
describe: typeof runtime?.sage?.describe === 'function' ? runtime.sage.describe() : '',
|
|
159
|
+
},
|
|
160
|
+
quarantine: quarantineView(runtime?.quarantine),
|
|
161
|
+
installAudits: (() => {
|
|
162
|
+
try {
|
|
163
|
+
const list = options.installWatcher?.list?.()
|
|
164
|
+
return Array.isArray(list) ? list : []
|
|
165
|
+
} catch {
|
|
166
|
+
return []
|
|
167
|
+
}
|
|
168
|
+
})(),
|
|
169
|
+
installScan: (() => {
|
|
170
|
+
try {
|
|
171
|
+
const watcher = options.installWatcher
|
|
172
|
+
return {
|
|
173
|
+
lastCheckAt: typeof watcher?.lastCheckAt === 'string' ? watcher.lastCheckAt : null,
|
|
174
|
+
lastError: typeof watcher?.lastError === 'string' ? watcher.lastError : null,
|
|
175
|
+
description: typeof watcher?.describe === 'function' ? watcher.describe() : '安装审计:未启用',
|
|
176
|
+
state: watcher?.state && typeof watcher.state === 'object' ? watcher.state : null,
|
|
177
|
+
installed: installedFor(watcher),
|
|
178
|
+
}
|
|
179
|
+
} catch {
|
|
180
|
+
return { lastCheckAt: null, lastError: null, description: '安装审计:不可用' }
|
|
181
|
+
}
|
|
182
|
+
})(),
|
|
183
|
+
audit: {
|
|
184
|
+
count: Number(runtime?.audit?.stats?.()?.total) || 0,
|
|
185
|
+
dir: typeof runtime?.audit?.dir === 'string' ? runtime.audit.dir : '',
|
|
186
|
+
},
|
|
187
|
+
alert: (() => {
|
|
188
|
+
const alert = runtime?.config?.notify?.alert
|
|
189
|
+
if (!alert || typeof alert !== 'object') {
|
|
190
|
+
return { enabled: false, sound: false, windowsNotification: false, raiseWindow: false }
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
enabled: alert.enabled !== false,
|
|
194
|
+
sound: alert.sound !== false,
|
|
195
|
+
windowsNotification: alert.windowsNotification !== false,
|
|
196
|
+
raiseWindow: alert.raiseWindow !== false,
|
|
197
|
+
}
|
|
198
|
+
})(),
|
|
199
|
+
alertWindow: probeAlertWindow(),
|
|
200
|
+
}
|
|
201
|
+
// Keep `generatedAt` stable while the observable content is unchanged: the
|
|
202
|
+
// route hashes the body and can answer 204 instead of resending it on every
|
|
203
|
+
// 2.5s poll. Only a real change refreshes the timestamp.
|
|
204
|
+
const signature = JSON.stringify({ ...feed, generatedAt: null })
|
|
205
|
+
const cached = feedCache.get(runtime)
|
|
206
|
+
if (cached && cached.signature === signature) {
|
|
207
|
+
feed.generatedAt = cached.generatedAt
|
|
208
|
+
} else {
|
|
209
|
+
feed.generatedAt = nowIso()
|
|
210
|
+
feedCache.set(runtime, { signature, generatedAt: feed.generatedAt })
|
|
211
|
+
}
|
|
212
|
+
return feed
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Build one `connection.fetch` route definition for the drawer.
|
|
217
|
+
* @param {{ getRuntime: () => any, isDisabled?: () => boolean, limit?: number, approval?: { list?: () => any[] }, installWatcher?: { list?: () => any[], lastCheckAt?: string|null, lastError?: string|null, describe?: () => string }, version?: string|null }} deps
|
|
218
|
+
* @returns {{ path: string, methods: Array<'GET'|'HEAD'>, requestBody: 'buffered', fetch: (request: Request) => Promise<Response> }}
|
|
219
|
+
*/
|
|
220
|
+
export function createAuditFeedRoute(deps) {
|
|
221
|
+
return {
|
|
222
|
+
path: AUDIT_FEED_PATH,
|
|
223
|
+
methods: ['GET', 'HEAD'],
|
|
224
|
+
requestBody: 'buffered',
|
|
225
|
+
fetch: async request => {
|
|
226
|
+
let limit = deps.limit
|
|
227
|
+
try {
|
|
228
|
+
const requested = Number(new URL(request.url).searchParams.get('limit'))
|
|
229
|
+
if (Number.isInteger(requested) && requested > 0) limit = requested
|
|
230
|
+
} catch {
|
|
231
|
+
// malformed URL keeps the route default
|
|
232
|
+
}
|
|
233
|
+
let payload
|
|
234
|
+
try {
|
|
235
|
+
payload = {
|
|
236
|
+
ok: true,
|
|
237
|
+
...buildAuditFeed(deps.getRuntime(), { isDisabled: deps.isDisabled }, {
|
|
238
|
+
limit,
|
|
239
|
+
approval: deps.approval,
|
|
240
|
+
installWatcher: deps.installWatcher,
|
|
241
|
+
version: deps.version,
|
|
242
|
+
}),
|
|
243
|
+
}
|
|
244
|
+
} catch (error) {
|
|
245
|
+
payload = { ok: false, error: error instanceof Error ? error.message : String(error) }
|
|
246
|
+
}
|
|
247
|
+
const body = `${JSON.stringify(payload)}\n`
|
|
248
|
+
const hash = sha256(body)
|
|
249
|
+
const etag = `"${hash}"`
|
|
250
|
+
const headers = {
|
|
251
|
+
'content-type': 'application/json; charset=utf-8',
|
|
252
|
+
'cache-control': 'no-store',
|
|
253
|
+
'x-content-type-options': 'nosniff',
|
|
254
|
+
etag,
|
|
255
|
+
'x-dsh-vigil-feed-hash': hash,
|
|
256
|
+
}
|
|
257
|
+
const method = String(request?.method ?? 'GET').toUpperCase()
|
|
258
|
+
if (method === 'HEAD') return new Response(null, { status: 200, headers })
|
|
259
|
+
const known = request.headers?.get?.('x-dsh-vigil-feed-hash') ?? request.headers?.get?.('if-none-match')
|
|
260
|
+
// Unchanged feed: answer 204 and let the page keep its current state
|
|
261
|
+
// instead of re-downloading and re-rendering the same 100+ KB payload.
|
|
262
|
+
if (typeof known === 'string' && (known === hash || known === etag)) {
|
|
263
|
+
return new Response(null, { status: 204, headers })
|
|
264
|
+
}
|
|
265
|
+
return new Response(body, { status: 200, headers })
|
|
266
|
+
},
|
|
267
|
+
}
|
|
268
|
+
}
|