dsh-net-consent 1.0.1 → 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 +10 -0
- package/client.js +71 -13
- package/index.js +88 -5
- 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
|
三个选项:`放行一次`、`本会话放行该目标`、`拒绝`。点「其它」填文字则默认拒绝,并把你的原话回给模型让它改路线。
|
|
@@ -74,6 +82,8 @@
|
|
|
74
82
|
| `purposes` | `true` | 卡片展示模型自述用途 |
|
|
75
83
|
| `internalTools` | 一长串只读工具 | 完全不看的工具(UI 不可改) |
|
|
76
84
|
|
|
85
|
+
会话覆盖(`sessions.json` / 审批开关)可覆盖其中:`mode`、`shellMode`、`allowLocal`、`askTimeoutMs`、`onTimeout`、`purposes`。
|
|
86
|
+
|
|
77
87
|
## 审计
|
|
78
88
|
|
|
79
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
|
@@ -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
|
@@ -8,7 +8,9 @@ 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
|
|
@@ -275,34 +317,49 @@ const VERSION_PROMISE = readFile(new URL('./package.json', import.meta.url), 'ut
|
|
|
275
317
|
export const _test = {
|
|
276
318
|
classify, hostsIn, isPrivateHost, purposeOf, DEFAULTS, normalizePolicy,
|
|
277
319
|
allowlisted, isHostEntry, hostAllowed, toolMatches, commandMatches, migrate,
|
|
320
|
+
sanitizeSessionOverride, SESSION_WRITABLE,
|
|
278
321
|
}
|
|
279
322
|
|
|
280
323
|
export function apply(ctx, config) {
|
|
281
324
|
let stored = {}
|
|
325
|
+
let sessionOverrides = {}
|
|
282
326
|
let version = '0.0.0'
|
|
283
327
|
let lastPrune = 0
|
|
284
|
-
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]) => {
|
|
285
329
|
const migrated = migrate(raw)
|
|
286
330
|
stored = migrated.state
|
|
287
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
|
|
288
338
|
version = tag || '0.0.0'
|
|
289
339
|
void pruneAudit(cfg().auditRetentionDays)
|
|
290
340
|
lastPrune = Date.now()
|
|
291
341
|
})
|
|
292
342
|
const cfg = () => ({ ...DEFAULTS, ...(config ?? {}), ...stored })
|
|
293
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
|
+
|
|
294
351
|
const grants = new Map()
|
|
295
352
|
const inFlight = new Set()
|
|
296
353
|
const stats = { asked: 0, allowed: 0, denied: 0 }
|
|
297
354
|
let lastAgent = null
|
|
298
355
|
|
|
299
|
-
async function judge({ tool, target, hosts, command, preview, purpose, signal, callId }) {
|
|
356
|
+
async function judge({ tool, target, hosts, command, preview, purpose, signal, callId, effective }) {
|
|
300
357
|
let decision = 'allowed'
|
|
301
358
|
let via = 'once'
|
|
302
359
|
let custom = ''
|
|
303
360
|
|
|
304
|
-
const timeoutMs = Math.max(5000, Math.min(Number(
|
|
305
|
-
const autoAllow = (
|
|
361
|
+
const timeoutMs = Math.max(5000, Math.min(Number(effective.askTimeoutMs ?? 30000) || 30000, 600000))
|
|
362
|
+
const autoAllow = (effective.onTimeout ?? 'deny') === 'allow'
|
|
306
363
|
const autoAction = autoAllow ? '放行' : '拒绝'
|
|
307
364
|
const timer = new Promise((_, reject) => setTimeout(
|
|
308
365
|
() => reject(Object.assign(new Error('timeout'), { code: 'NET_CONSENT_TIMEOUT' })),
|
|
@@ -374,7 +431,8 @@ export function apply(ctx, config) {
|
|
|
374
431
|
try { verdict = classify(exec, cfg()) } catch { verdict = null }
|
|
375
432
|
if (!verdict || !verdict.network) return next()
|
|
376
433
|
|
|
377
|
-
const
|
|
434
|
+
const sessionId = exec.agent?.session?.id ?? exec.agent?.id
|
|
435
|
+
const current = cfgFor(sessionId === undefined ? undefined : String(sessionId))
|
|
378
436
|
const layer = verdict.kind === 'shell' ? 'shell' : 'tool'
|
|
379
437
|
const policy = normalizePolicy(layer === 'shell' ? current.shellMode : current.mode, 'ask')
|
|
380
438
|
if (policy === 'allow') return next()
|
|
@@ -428,6 +486,7 @@ export function apply(ctx, config) {
|
|
|
428
486
|
purpose: current.purposes === false ? '' : purposeOf(exec.agent),
|
|
429
487
|
signal: exec.signal,
|
|
430
488
|
callId: exec.callId,
|
|
489
|
+
effective: current,
|
|
431
490
|
})
|
|
432
491
|
} finally {
|
|
433
492
|
inFlight.delete(askId)
|
|
@@ -474,6 +533,7 @@ export function apply(ctx, config) {
|
|
|
474
533
|
auditRetentionDays: Number(current.auditRetentionDays ?? 30),
|
|
475
534
|
allowlist: (current.allowlist ?? []).map(String),
|
|
476
535
|
purposes: current.purposes !== false,
|
|
536
|
+
sessionOverrides: { ...sessionOverrides },
|
|
477
537
|
stats: { asked: stats.asked, allowed: stats.allowed, denied: stats.denied },
|
|
478
538
|
version,
|
|
479
539
|
}
|
|
@@ -505,6 +565,29 @@ export function apply(ctx, config) {
|
|
|
505
565
|
if (request.method === 'POST') {
|
|
506
566
|
let patch
|
|
507
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
|
+
|
|
508
591
|
const next = {}
|
|
509
592
|
for (const key of WRITABLE) if (key in (patch ?? {})) next[key] = patch[key]
|
|
510
593
|
for (const key of ['mode', 'shellMode']) {
|
package/package.json
CHANGED