dsh-net-consent 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/client.js +72 -14
- package/index.js +121 -47
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -32,6 +32,14 @@
|
|
|
32
32
|
|
|
33
33
|
`deny` 优先于「本会话放行」;本机 / 局域网地址与白名单命中项先放行。
|
|
34
34
|
|
|
35
|
+
## 每会话配置
|
|
36
|
+
|
|
37
|
+
设置面板改的是**全局默认**,对所有会话生效。单个会话可以在输入框旁点「联网审查」开关单独覆盖:
|
|
38
|
+
|
|
39
|
+
- 点击在 **跟随默认 → 本会话询问 → 本会话放行 → 本会话拒绝** 之间循环;双击恢复跟随默认。
|
|
40
|
+
- 覆盖只对该会话有效,存在 `~/.dsh/net-consent/sessions.json`,设置面板可查看和删除。
|
|
41
|
+
- 判定顺序:内置默认 < 面板全局配置 < 会话覆盖。白名单、审计开关、审计保留天数不参与会话覆盖,始终用全局配置。
|
|
42
|
+
|
|
35
43
|
## 审批卡片
|
|
36
44
|
|
|
37
45
|
三个选项:`放行一次`、`本会话放行该目标`、`拒绝`。点「其它」填文字则默认拒绝,并把你的原话回给模型让它改路线。
|
|
@@ -53,6 +61,10 @@
|
|
|
53
61
|
|
|
54
62
|
白名单是显式例外表,先于 `deny` 策略生效。
|
|
55
63
|
|
|
64
|
+
## 安全
|
|
65
|
+
|
|
66
|
+
设置页的状态接口挂在 DSH 的 `/api` 通道上(`ctx.connection.fetch.register` 的精确路由)。Host / Origin 校验与浏览器认证由 DSH 自己完成,只有已认证的属主会话能读写;插件不再向 `webServer` 注册裸路由。
|
|
67
|
+
|
|
56
68
|
## 配置
|
|
57
69
|
|
|
58
70
|
`cordis.patch.yml` 里 `net-consent` 行的 `config:`:
|
|
@@ -70,6 +82,8 @@
|
|
|
70
82
|
| `purposes` | `true` | 卡片展示模型自述用途 |
|
|
71
83
|
| `internalTools` | 一长串只读工具 | 完全不看的工具(UI 不可改) |
|
|
72
84
|
|
|
85
|
+
会话覆盖(`sessions.json` / 审批开关)可覆盖其中:`mode`、`shellMode`、`allowLocal`、`askTimeoutMs`、`onTimeout`、`purposes`。
|
|
86
|
+
|
|
73
87
|
## 审计
|
|
74
88
|
|
|
75
89
|
每次判定写一行 JSON 到 `~/.dsh/net-consent/audit/YYYY-MM-DD.jsonl`,`via` 字段区分放行 / 拒绝原因(`once`、`session-grant`、`user-reject`、`policy=deny`、`timeout-allow`、`NET_CONSENT_TIMEOUT` …)。超过保留天数的按天文件在启动时与写入后自动清理。
|
package/client.js
CHANGED
|
@@ -5,7 +5,7 @@ window.__ModuleLoader__.load({
|
|
|
5
5
|
const h = React.createElement
|
|
6
6
|
const { useState, useEffect, useCallback } = React
|
|
7
7
|
|
|
8
|
-
const STATE_URL = '/net-consent/state'
|
|
8
|
+
const STATE_URL = '/api/net-consent/state'
|
|
9
9
|
|
|
10
10
|
const store = {
|
|
11
11
|
value: null,
|
|
@@ -59,6 +59,14 @@ window.__ModuleLoader__.load({
|
|
|
59
59
|
const policyOf = value => (value === 'allow' || value === 'deny' ? value : 'ask')
|
|
60
60
|
const labelOf = value => (POLICY.find(([key]) => key === policyOf(value)) ?? POLICY[1])[1]
|
|
61
61
|
|
|
62
|
+
// Per-session override stored on the Host, keyed by session id.
|
|
63
|
+
const SESSION_WRITABLE = ['mode', 'shellMode', 'allowLocal', 'askTimeoutMs', 'onTimeout', 'purposes']
|
|
64
|
+
const effectiveFor = (value, sessionId) => {
|
|
65
|
+
if (!value || sessionId === undefined || sessionId === null) return value
|
|
66
|
+
const override = value.sessionOverrides?.[String(sessionId)]
|
|
67
|
+
return override ? { ...value, ...override } : value
|
|
68
|
+
}
|
|
69
|
+
|
|
62
70
|
const TIMEOUTS = [
|
|
63
71
|
['15000', '15 秒'], ['30000', '30 秒'], ['60000', '1 分钟'],
|
|
64
72
|
['180000', '3 分钟'], ['600000', '10 分钟'],
|
|
@@ -108,28 +116,55 @@ window.__ModuleLoader__.load({
|
|
|
108
116
|
warn: { fontSize: 12, color: 'rgb(230,160,60)', marginTop: 8 },
|
|
109
117
|
}
|
|
110
118
|
|
|
111
|
-
function ConsentToggle() {
|
|
119
|
+
function ConsentToggle(props) {
|
|
112
120
|
const [value, error] = useStore()
|
|
113
|
-
|
|
114
|
-
const
|
|
121
|
+
// session scope: the renderer injects sessionId for session-scoped slots.
|
|
122
|
+
const sessionId = props?.sessionId
|
|
123
|
+
const effective = effectiveFor(value, sessionId)
|
|
124
|
+
const mode = policyOf(effective?.mode)
|
|
125
|
+
const shellMode = policyOf(effective?.shellMode)
|
|
115
126
|
const on = mode !== 'allow' || shellMode !== 'allow'
|
|
127
|
+
const override = value?.sessionOverrides?.[String(sessionId)]
|
|
128
|
+
const scoped = override != null
|
|
116
129
|
const title = value
|
|
117
|
-
?
|
|
130
|
+
? (scoped
|
|
131
|
+
? `本会话覆盖:工具 ${labelOf(mode)} · 命令行 ${labelOf(shellMode)}。点击循环切换,长按/双击恢复跟随默认。`
|
|
132
|
+
: `工具 ${labelOf(mode)} · 命令行 ${labelOf(shellMode)};本次已询问 ${value?.stats?.asked ?? 0} 次。点击开启本会话审查。`)
|
|
118
133
|
: (error || '正在读取联网审查状态…')
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
134
|
+
const cycle = useCallback(() => {
|
|
135
|
+
if (sessionId === undefined || sessionId === null) {
|
|
136
|
+
// No session context (should not happen): fall back to the global toggle.
|
|
137
|
+
const next = on ? 'allow' : 'ask'
|
|
138
|
+
store.patch({ mode: next, shellMode: next })
|
|
139
|
+
return
|
|
140
|
+
}
|
|
141
|
+
const id = String(sessionId)
|
|
142
|
+
const current = value?.sessionOverrides?.[id]
|
|
143
|
+
const curMode = policyOf(current?.mode ?? value?.mode)
|
|
144
|
+
const curShell = policyOf(current?.shellMode ?? value?.shellMode)
|
|
145
|
+
const following = current == null || (curMode === policyOf(value?.mode) && curShell === policyOf(value?.shellMode))
|
|
146
|
+
// Cycle: follow default → this session ask → this session allow → this session deny → follow default.
|
|
147
|
+
const nextFollowing = following ? { mode: 'ask', shellMode: 'ask' }
|
|
148
|
+
: curMode !== 'allow' ? { mode: 'allow', shellMode: curShell === 'ask' ? 'allow' : curShell }
|
|
149
|
+
: { mode: 'deny', shellMode: 'deny' }
|
|
150
|
+
store.patch({ sessionId: id, override: following ? nextFollowing : null })
|
|
151
|
+
}, [value, on, sessionId])
|
|
152
|
+
const reset = useCallback(() => {
|
|
153
|
+
if (sessionId === undefined || sessionId === null) return
|
|
154
|
+
store.patch({ sessionId: String(sessionId), override: null })
|
|
155
|
+
}, [sessionId])
|
|
123
156
|
return h('button', {
|
|
124
157
|
type: 'button',
|
|
125
|
-
onClick:
|
|
158
|
+
onClick: cycle,
|
|
159
|
+
onDoubleClick: reset,
|
|
126
160
|
title,
|
|
127
|
-
style: S.pill(on),
|
|
128
|
-
'aria-pressed': on,
|
|
161
|
+
style: S.pill(on || scoped),
|
|
162
|
+
'aria-pressed': on || scoped,
|
|
129
163
|
},
|
|
130
|
-
h('span', { style: S.dot(on) }),
|
|
164
|
+
h('span', { style: S.dot(on || scoped) }),
|
|
131
165
|
h('span', null, '联网审查'),
|
|
132
|
-
h('span', { style: { opacity: on ? 0.85 : 0.55 } },
|
|
166
|
+
h('span', { style: { opacity: on || scoped ? 0.85 : 0.55 } },
|
|
167
|
+
scoped ? (on ? '会话开' : '会话关') : (on ? '开' : '关')),
|
|
133
168
|
)
|
|
134
169
|
}
|
|
135
170
|
|
|
@@ -173,6 +208,7 @@ window.__ModuleLoader__.load({
|
|
|
173
208
|
}
|
|
174
209
|
const set = patch => store.patch(patch)
|
|
175
210
|
const stats = value.stats ?? {}
|
|
211
|
+
const overrides = value.sessionOverrides ?? {}
|
|
176
212
|
return h('div', { style: S.page },
|
|
177
213
|
h('div', { style: S.h1 }, '联网审查'),
|
|
178
214
|
|
|
@@ -201,6 +237,28 @@ window.__ModuleLoader__.load({
|
|
|
201
237
|
})),
|
|
202
238
|
)),
|
|
203
239
|
|
|
240
|
+
h('div', { style: S.group },
|
|
241
|
+
h('div', { style: S.groupTitle }, '本会话覆盖'),
|
|
242
|
+
h('div', null,
|
|
243
|
+
h(Row, {
|
|
244
|
+
name: '会话覆盖列表',
|
|
245
|
+
hint: '在会话输入框旁点击「联网审查」开关可为本会话单独设置;这里是全部覆盖,删除后该会话恢复使用上面的默认配置。',
|
|
246
|
+
},
|
|
247
|
+
h('div', { style: { display: 'flex', flexDirection: 'column', gap: 6, alignItems: 'stretch' } },
|
|
248
|
+
Object.keys(overrides).length === 0
|
|
249
|
+
? h('div', { style: { fontSize: 12, opacity: 0.55 } }, '没有会话覆盖。')
|
|
250
|
+
: Object.entries(overrides).map(([id, ov]) => h('div', {
|
|
251
|
+
key: id,
|
|
252
|
+
style: { display: 'flex', alignItems: 'center', gap: 8, fontSize: 12 },
|
|
253
|
+
},
|
|
254
|
+
h('span', { style: { flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }, title: id },
|
|
255
|
+
id.slice(0, 12) + '…:工具 ' + labelOf(ov.mode) + ' · 命令行 ' + labelOf(ov.shellMode)),
|
|
256
|
+
h('button', {
|
|
257
|
+
type: 'button', style: S.select, onClick: () => set({ sessionId: id, override: null }),
|
|
258
|
+
}, '删除'),
|
|
259
|
+
)))),
|
|
260
|
+
)),
|
|
261
|
+
|
|
204
262
|
h('div', { style: S.group },
|
|
205
263
|
h('div', { style: S.groupTitle }, '审批卡片'),
|
|
206
264
|
h('div', null,
|
package/index.js
CHANGED
|
@@ -4,11 +4,13 @@ import { join } from 'node:path'
|
|
|
4
4
|
|
|
5
5
|
export const name = 'net-consent'
|
|
6
6
|
|
|
7
|
-
export const inject = ['tools', 'userQuestions'
|
|
7
|
+
export const inject = ['tools', 'userQuestions']
|
|
8
8
|
|
|
9
9
|
const STATE_DIR = join(homedir(), '.dsh', 'net-consent')
|
|
10
10
|
const STATE_FILE = join(STATE_DIR, 'state.json')
|
|
11
|
+
const SESSIONS_FILE = join(STATE_DIR, 'sessions.json')
|
|
11
12
|
const AUDIT_DIR = join(STATE_DIR, 'audit')
|
|
13
|
+
const MAX_SESSION_OVERRIDES = 100
|
|
12
14
|
|
|
13
15
|
const POLICIES = ['allow', 'ask', 'deny']
|
|
14
16
|
const LEGACY_POLICY = { off: 'allow', observe: 'allow' }
|
|
@@ -46,6 +48,10 @@ const WRITABLE = [
|
|
|
46
48
|
'audit', 'auditRetentionDays', 'allowlist', 'purposes',
|
|
47
49
|
]
|
|
48
50
|
|
|
51
|
+
// Per-session override keys: policy layers plus the ask-card behaviour that
|
|
52
|
+
// a single session may want different from the global default.
|
|
53
|
+
const SESSION_WRITABLE = ['mode', 'shellMode', 'allowLocal', 'askTimeoutMs', 'onTimeout', 'purposes']
|
|
54
|
+
|
|
49
55
|
const NET_TOOL = /(^web_(fetch|search)$)|(^fetch$)|(^http_)|(browser|playwright|puppeteer|websearch|search_web|selenium|chrome_|navigate)/i
|
|
50
56
|
|
|
51
57
|
const SHELL_TOOL = /^(pwsh|powershell|bash|sh|zsh|cmd|shell|terminal|exec|run_code|process|spawn)/i
|
|
@@ -245,6 +251,42 @@ async function saveState(state) {
|
|
|
245
251
|
} catch { return false }
|
|
246
252
|
}
|
|
247
253
|
|
|
254
|
+
async function loadSessionOverrides() {
|
|
255
|
+
try {
|
|
256
|
+
const raw = JSON.parse(await readFile(SESSIONS_FILE, 'utf8'))
|
|
257
|
+
return raw && typeof raw === 'object' && !Array.isArray(raw) ? raw : {}
|
|
258
|
+
} catch { return {} }
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async function saveSessionOverrides(map) {
|
|
262
|
+
try {
|
|
263
|
+
await mkdir(STATE_DIR, { recursive: true })
|
|
264
|
+
await writeFile(SESSIONS_FILE, JSON.stringify(map, null, 2), 'utf8')
|
|
265
|
+
return true
|
|
266
|
+
} catch { return false }
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function sanitizeSessionOverride(raw) {
|
|
270
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null
|
|
271
|
+
const next = {}
|
|
272
|
+
for (const key of SESSION_WRITABLE) {
|
|
273
|
+
if (!(key in raw)) continue
|
|
274
|
+
const value = raw[key]
|
|
275
|
+
if (key === 'mode' || key === 'shellMode') {
|
|
276
|
+
const mapped = LEGACY_POLICY[value] ?? value
|
|
277
|
+
if (POLICIES.includes(mapped)) next[key] = mapped
|
|
278
|
+
} else if (key === 'askTimeoutMs') {
|
|
279
|
+
const ms = Math.round(Number(value))
|
|
280
|
+
if (Number.isFinite(ms) && ms >= 5000 && ms <= 600000) next[key] = ms
|
|
281
|
+
} else if (key === 'onTimeout') {
|
|
282
|
+
if (value === 'allow' || value === 'deny') next[key] = value
|
|
283
|
+
} else {
|
|
284
|
+
next[key] = value !== false
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return Object.keys(next).length > 0 ? next : null
|
|
288
|
+
}
|
|
289
|
+
|
|
248
290
|
function migrate(raw) {
|
|
249
291
|
const state = { ...(raw ?? {}) }
|
|
250
292
|
let changed = false
|
|
@@ -272,53 +314,52 @@ const VERSION_PROMISE = readFile(new URL('./package.json', import.meta.url), 'ut
|
|
|
272
314
|
})
|
|
273
315
|
.catch(() => '')
|
|
274
316
|
|
|
275
|
-
function readJsonBody(req, limit = 64 * 1024) {
|
|
276
|
-
return new Promise((resolve, reject) => {
|
|
277
|
-
let size = 0
|
|
278
|
-
const chunks = []
|
|
279
|
-
req.on('data', chunk => {
|
|
280
|
-
size += chunk.length
|
|
281
|
-
if (size > limit) { req.destroy(); reject(new Error('body too large')); return }
|
|
282
|
-
chunks.push(chunk)
|
|
283
|
-
})
|
|
284
|
-
req.on('end', () => {
|
|
285
|
-
try { resolve(chunks.length ? JSON.parse(Buffer.concat(chunks).toString('utf8')) : {}) } catch (error) { reject(error) }
|
|
286
|
-
})
|
|
287
|
-
req.on('error', reject)
|
|
288
|
-
})
|
|
289
|
-
}
|
|
290
|
-
|
|
291
317
|
export const _test = {
|
|
292
318
|
classify, hostsIn, isPrivateHost, purposeOf, DEFAULTS, normalizePolicy,
|
|
293
319
|
allowlisted, isHostEntry, hostAllowed, toolMatches, commandMatches, migrate,
|
|
320
|
+
sanitizeSessionOverride, SESSION_WRITABLE,
|
|
294
321
|
}
|
|
295
322
|
|
|
296
323
|
export function apply(ctx, config) {
|
|
297
324
|
let stored = {}
|
|
325
|
+
let sessionOverrides = {}
|
|
298
326
|
let version = '0.0.0'
|
|
299
327
|
let lastPrune = 0
|
|
300
|
-
const ready = Promise.all([loadState(), VERSION_PROMISE]).then(async ([raw, tag]) => {
|
|
328
|
+
const ready = Promise.all([loadState(), loadSessionOverrides(), VERSION_PROMISE]).then(async ([raw, rawSessions, tag]) => {
|
|
301
329
|
const migrated = migrate(raw)
|
|
302
330
|
stored = migrated.state
|
|
303
331
|
if (migrated.changed) void saveState(stored)
|
|
332
|
+
const clean = {}
|
|
333
|
+
for (const [id, value] of Object.entries(rawSessions ?? {})) {
|
|
334
|
+
const override = sanitizeSessionOverride(value)
|
|
335
|
+
if (override) clean[id] = override
|
|
336
|
+
}
|
|
337
|
+
sessionOverrides = clean
|
|
304
338
|
version = tag || '0.0.0'
|
|
305
339
|
void pruneAudit(cfg().auditRetentionDays)
|
|
306
340
|
lastPrune = Date.now()
|
|
307
341
|
})
|
|
308
342
|
const cfg = () => ({ ...DEFAULTS, ...(config ?? {}), ...stored })
|
|
309
343
|
|
|
344
|
+
// Effective config for one session: defaults < panel (global) < session override.
|
|
345
|
+
const cfgFor = (sessionId) => {
|
|
346
|
+
const base = cfg()
|
|
347
|
+
const override = sessionId === undefined ? undefined : sessionOverrides[String(sessionId)]
|
|
348
|
+
return override ? { ...base, ...override } : base
|
|
349
|
+
}
|
|
350
|
+
|
|
310
351
|
const grants = new Map()
|
|
311
352
|
const inFlight = new Set()
|
|
312
353
|
const stats = { asked: 0, allowed: 0, denied: 0 }
|
|
313
354
|
let lastAgent = null
|
|
314
355
|
|
|
315
|
-
async function judge({ tool, target, hosts, command, preview, purpose, signal, callId }) {
|
|
356
|
+
async function judge({ tool, target, hosts, command, preview, purpose, signal, callId, effective }) {
|
|
316
357
|
let decision = 'allowed'
|
|
317
358
|
let via = 'once'
|
|
318
359
|
let custom = ''
|
|
319
360
|
|
|
320
|
-
const timeoutMs = Math.max(5000, Math.min(Number(
|
|
321
|
-
const autoAllow = (
|
|
361
|
+
const timeoutMs = Math.max(5000, Math.min(Number(effective.askTimeoutMs ?? 30000) || 30000, 600000))
|
|
362
|
+
const autoAllow = (effective.onTimeout ?? 'deny') === 'allow'
|
|
322
363
|
const autoAction = autoAllow ? '放行' : '拒绝'
|
|
323
364
|
const timer = new Promise((_, reject) => setTimeout(
|
|
324
365
|
() => reject(Object.assign(new Error('timeout'), { code: 'NET_CONSENT_TIMEOUT' })),
|
|
@@ -390,7 +431,8 @@ export function apply(ctx, config) {
|
|
|
390
431
|
try { verdict = classify(exec, cfg()) } catch { verdict = null }
|
|
391
432
|
if (!verdict || !verdict.network) return next()
|
|
392
433
|
|
|
393
|
-
const
|
|
434
|
+
const sessionId = exec.agent?.session?.id ?? exec.agent?.id
|
|
435
|
+
const current = cfgFor(sessionId === undefined ? undefined : String(sessionId))
|
|
394
436
|
const layer = verdict.kind === 'shell' ? 'shell' : 'tool'
|
|
395
437
|
const policy = normalizePolicy(layer === 'shell' ? current.shellMode : current.mode, 'ask')
|
|
396
438
|
if (policy === 'allow') return next()
|
|
@@ -444,6 +486,7 @@ export function apply(ctx, config) {
|
|
|
444
486
|
purpose: current.purposes === false ? '' : purposeOf(exec.agent),
|
|
445
487
|
signal: exec.signal,
|
|
446
488
|
callId: exec.callId,
|
|
489
|
+
effective: current,
|
|
447
490
|
})
|
|
448
491
|
} finally {
|
|
449
492
|
inFlight.delete(askId)
|
|
@@ -490,13 +533,17 @@ export function apply(ctx, config) {
|
|
|
490
533
|
auditRetentionDays: Number(current.auditRetentionDays ?? 30),
|
|
491
534
|
allowlist: (current.allowlist ?? []).map(String),
|
|
492
535
|
purposes: current.purposes !== false,
|
|
536
|
+
sessionOverrides: { ...sessionOverrides },
|
|
493
537
|
stats: { asked: stats.asked, allowed: stats.allowed, denied: stats.denied },
|
|
494
538
|
version,
|
|
495
539
|
}
|
|
496
540
|
}
|
|
497
541
|
|
|
498
542
|
const ROUTE_KEY = Symbol.for('dsh-net-consent.routes')
|
|
499
|
-
|
|
543
|
+
// Mounted on Connection's shared /api channel, so DSH itself applies its
|
|
544
|
+
// Host/Origin fence and browser authentication before this handler runs, and
|
|
545
|
+
// only the authenticated owner Peer reaches it.
|
|
546
|
+
const ROUTE_PATH = '/api/net-consent/state'
|
|
500
547
|
|
|
501
548
|
const writeDebug = async (line) => {
|
|
502
549
|
try {
|
|
@@ -505,21 +552,42 @@ export function apply(ctx, config) {
|
|
|
505
552
|
} catch { }
|
|
506
553
|
}
|
|
507
554
|
|
|
508
|
-
const
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
}
|
|
515
|
-
if (url.pathname !== `${ROUTE_BASE}/state`) return send(404, { error: 'not found' })
|
|
555
|
+
const jsonResponse = (status, payload) => Response.json(payload, {
|
|
556
|
+
status,
|
|
557
|
+
headers: { 'cache-control': 'no-store' },
|
|
558
|
+
})
|
|
559
|
+
|
|
560
|
+
const routeFetch = async (request) => {
|
|
516
561
|
await ready
|
|
517
562
|
|
|
518
|
-
if (
|
|
563
|
+
if (request.method === 'GET' || request.method === 'HEAD') return jsonResponse(200, snapshot())
|
|
519
564
|
|
|
520
|
-
if (
|
|
565
|
+
if (request.method === 'POST') {
|
|
521
566
|
let patch
|
|
522
|
-
try { patch = await
|
|
567
|
+
try { patch = await request.json() } catch { return jsonResponse(400, { error: 'bad json' }) }
|
|
568
|
+
|
|
569
|
+
// Session-scoped override: { sessionId, override } where override is a
|
|
570
|
+
// partial SESSION_WRITABLE object; null clears the override so the
|
|
571
|
+
// session falls back to the panel defaults.
|
|
572
|
+
if (patch && typeof patch === 'object' && 'sessionId' in patch) {
|
|
573
|
+
const id = String(patch.sessionId ?? '')
|
|
574
|
+
if (!id) return jsonResponse(400, { error: 'sessionId required' })
|
|
575
|
+
const override = patch.override === null || patch.override === undefined
|
|
576
|
+
? null
|
|
577
|
+
: sanitizeSessionOverride(patch.override)
|
|
578
|
+
if (patch.override !== null && patch.override !== undefined && override === null) {
|
|
579
|
+
return jsonResponse(400, { error: 'no valid override keys' })
|
|
580
|
+
}
|
|
581
|
+
if (override === null) delete sessionOverrides[id]
|
|
582
|
+
else sessionOverrides[id] = override
|
|
583
|
+
if (Object.keys(sessionOverrides).length > MAX_SESSION_OVERRIDES) {
|
|
584
|
+
const keys = Object.keys(sessionOverrides)
|
|
585
|
+
for (const k of keys.slice(0, keys.length - MAX_SESSION_OVERRIDES)) delete sessionOverrides[k]
|
|
586
|
+
}
|
|
587
|
+
const ok = await saveSessionOverrides(sessionOverrides)
|
|
588
|
+
return jsonResponse(ok ? 200 : 500, { ...snapshot(), saved: ok })
|
|
589
|
+
}
|
|
590
|
+
|
|
523
591
|
const next = {}
|
|
524
592
|
for (const key of WRITABLE) if (key in (patch ?? {})) next[key] = patch[key]
|
|
525
593
|
for (const key of ['mode', 'shellMode']) {
|
|
@@ -553,28 +621,34 @@ export function apply(ctx, config) {
|
|
|
553
621
|
stored = { ...stored, ...next }
|
|
554
622
|
const ok = await saveState(stored)
|
|
555
623
|
void pruneAudit(cfg().auditRetentionDays)
|
|
556
|
-
return
|
|
624
|
+
return jsonResponse(ok ? 200 : 500, { ...snapshot(), saved: ok })
|
|
557
625
|
}
|
|
558
|
-
return
|
|
626
|
+
return jsonResponse(405, { error: 'method not allowed' })
|
|
559
627
|
}
|
|
560
628
|
|
|
561
629
|
try {
|
|
562
630
|
const previous = globalThis[ROUTE_KEY]
|
|
563
631
|
if (Array.isArray(previous)) {
|
|
564
|
-
for (const dispose of previous) { try { dispose() } catch { } }
|
|
632
|
+
for (const dispose of previous) { try { void Promise.resolve(dispose()).catch(() => {}) } catch { } }
|
|
565
633
|
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
634
|
+
if (typeof ctx.inject === 'function') {
|
|
635
|
+
ctx.inject(['connection'], (connectionCtx) => {
|
|
636
|
+
const dispose = connectionCtx.connection.fetch.register({
|
|
637
|
+
path: ROUTE_PATH,
|
|
638
|
+
methods: ['GET', 'HEAD', 'POST'],
|
|
639
|
+
requestBody: 'buffered',
|
|
640
|
+
fetch: routeFetch,
|
|
641
|
+
})
|
|
642
|
+
globalThis[ROUTE_KEY] = [dispose]
|
|
643
|
+
void writeDebug(`route registered: ${ROUTE_PATH} (connection.fetch exact)`)
|
|
644
|
+
connectionCtx.on('dispose', () => {
|
|
645
|
+
if (globalThis[ROUTE_KEY] === undefined) return
|
|
646
|
+
if (globalThis[ROUTE_KEY][0] !== dispose) return
|
|
647
|
+
globalThis[ROUTE_KEY] = undefined
|
|
648
|
+
})
|
|
575
649
|
})
|
|
576
650
|
} else {
|
|
577
|
-
void writeDebug(
|
|
651
|
+
void writeDebug('route skipped: ctx.inject unavailable')
|
|
578
652
|
}
|
|
579
653
|
} catch (error) {
|
|
580
654
|
void writeDebug(`route registration failed: ${error?.stack ?? String(error)}`)
|
package/package.json
CHANGED