第二步:让 ZCode 上的 Bash/Write/Edit 授权走 AgentMail 的人工审批,
而不是只靠本地界面。
钩子契约从 CLI 产物里逆出来(不猜协议):
- 输入走 stdin:{hook_event_name, tool_name, tool_input, session_id, permission_mode…}
- 输出走 stdout,schema **严格**:{"decision":"approve"} / {"decision":"block","reason"}
多一个键就会报 "Hook stdout failed HookJSONOutput schema validation"
- 空输出 / 不以 { 开头 = 不表态;exit 2 = 拒绝;其它非零 = 钩子失败
- 注入的环境变量含 ZCODE_PLUGIN_ROOT / ZCODE_PLUGIN_DATA / ZCODE_SESSION_ID
(MCP 配置里用 ZCODE_SESSION_ID 反而会抛「需要运行时会话上下文」)
档位判定与 pi 桥逐条对齐(plan 直接拒 / workspace 问人 / full 批准),
判定逻辑抽成纯函数 lib/hook-policy.mjs 以便穷举:
其中 full 档必须**返回批准而不是不表态** —— 钩子一旦触发说明 ZCode 本会去问人,
不表态等于让那个询问照常发生,full 档就退化成了 workspace 档。
钩子自己开 SSE 等决定,不依赖桥进程:网关的 SSE 是扇出的
(clients 按唯一 id 存,SendToAgent 推给该 Agent 的所有客户端),
一次性进程也能订阅到自己那条 permission_decision。这样交互模式下同样可用
(人自己开着 ZCode 干活时并没有桥在跑)。先建连再发请求是有意的:
反过来会有一个窗口,人在窗口内点的同意推送给当时还不存在的客户端。
fail closed 但区分模式:永久失败(409/4xx)一律拒绝;暂时失败在
AGENTMAIL_SESSION_ID 非空(邮件驱动、没有本地界面兜底)时拒绝,
交互模式则不表态让人就地决定。
「一直同意」落盘(lib/grants-file.mjs):钩子是一个事件一个进程,
不落盘那个选项就是骗人的。判定仍交给共用的 permission-grants.js。
共用模块同源范围扩到 9 个(新增 permission-mode / relay-key /
permission-grants / sse-client)—— 档位语义与决策判定分叉会让「同意」
在 ZCode 上悄悄变成另一种意思。
验证:
- 单元 229/229(新增 hook-policy 14 项、grants-file 8 项,含反向对照)
- 共用模块四方同源检查通过
- 授权桥端到端 5/5,全部带反向对照:
同意→approve;拒绝→block 且原因必须来自人的拒绝(不能是超时兜底);
plan 档拒绝且**不产生**任何权限邮件;无人可问(409)→fail closed;
非守卫工具→不表态
- `zcode plugins list` → agentmail@inline [enabled],hooks: 1,
mcp: plugin:agentmail:agentmail
我自己写错的两处判据(都已修,值得记下):
1. 待决权限列表里有历史积压(实测 6 条,含其它 Agent 的条目),
只按「第一条新的」取会拿到无关请求 —— 于是人点了同意而钩子在等自己那条,
最后超时。第一版还把这个超时误报成「拒绝路径通过」。
现在按「启动前快照差集 + session_id + agent_name」三重过滤。
2. 「无人可问」控制组最初传了个非 UUID 的 session id,走的是 400(参数错),
验不到 409 那条真实路径。改为真的造一条只有 Agent 没有人类的会话。
157 lines
5.2 KiB
JavaScript
157 lines
5.2 KiB
JavaScript
import assert from 'node:assert/strict';
|
||
import test from 'node:test';
|
||
|
||
import {
|
||
isAlwaysDecision,
|
||
isApproval,
|
||
createGrantStore,
|
||
} from '../lib/permission-grants.js';
|
||
|
||
// ─── isAlwaysDecision ───
|
||
//
|
||
// 这个函数是整个模块里最危险的一处:判宽了就把单次授权静默升级成永久授权。
|
||
|
||
test('「一直同意」判为永久', () => {
|
||
assert.equal(isAlwaysDecision('一直同意'), true);
|
||
});
|
||
|
||
test('「同意」不是永久 —— 前缀匹配会把单次授权升级成永久', () => {
|
||
// /^同意/ 之类的正则会让这条过,那意味着人点一次「同意」,
|
||
// 后面所有命令都不再问 —— 静默越权。
|
||
assert.equal(isAlwaysDecision('同意'), false);
|
||
});
|
||
|
||
test('always / allow-always 判为永久(英文界面)', () => {
|
||
for (const d of ['always', 'Always', 'ALWAYS', 'allow-always', 'allow_always']) {
|
||
assert.equal(isAlwaysDecision(d), true, d);
|
||
}
|
||
});
|
||
|
||
test('allow / approve / yes 不是永久', () => {
|
||
for (const d of ['allow', 'approve', 'yes']) {
|
||
assert.equal(isAlwaysDecision(d), false, d);
|
||
}
|
||
});
|
||
|
||
test('「拒绝」不是永久', () => {
|
||
assert.equal(isAlwaysDecision('拒绝'), false);
|
||
});
|
||
|
||
test('两侧空白不影响判定(界面传过来的值可能带空格)', () => {
|
||
assert.equal(isAlwaysDecision(' 一直同意 '), true);
|
||
});
|
||
|
||
test('空值与 null 不是永久', () => {
|
||
for (const d of ['', ' ', null, undefined]) {
|
||
assert.equal(isAlwaysDecision(d), false, String(d));
|
||
}
|
||
});
|
||
|
||
test('「一直同意吧」这类多余后缀不判为永久(精确匹配)', () => {
|
||
// 精确匹配的取舍:宁可漏判(多问一次)也不误判(静默永久放行)
|
||
assert.equal(isAlwaysDecision('一直同意吧'), false);
|
||
});
|
||
|
||
// ─── isApproval ───
|
||
|
||
test('同意与一直同意都是放行', () => {
|
||
assert.equal(isApproval('同意'), true);
|
||
assert.equal(isApproval('一直同意'), true);
|
||
});
|
||
|
||
test('英文放行选项', () => {
|
||
for (const d of ['allow', 'approve', 'always', 'yes', 'Allow']) {
|
||
assert.equal(isApproval(d), true, d);
|
||
}
|
||
});
|
||
|
||
test('拒绝不是放行', () => {
|
||
assert.equal(isApproval('拒绝'), false);
|
||
});
|
||
|
||
test('fail closed:认不出的文本一律当拒绝', () => {
|
||
// 关停哨兵、空值、乱码都必须落到拒绝一侧(N-9)
|
||
for (const d of ['shutdown', '', null, undefined, '也许吧', 'maybe']) {
|
||
assert.equal(isApproval(d), false, String(d));
|
||
}
|
||
});
|
||
|
||
// ─── createGrantStore ───
|
||
|
||
test('未授权时不放行', () => {
|
||
const s = createGrantStore();
|
||
assert.equal(s.isGranted('sess-1', 'bash'), false);
|
||
});
|
||
|
||
test('点「一直同意」后同会话同工具免批', () => {
|
||
const s = createGrantStore();
|
||
assert.equal(s.grant('sess-1', 'bash', '一直同意'), true);
|
||
assert.equal(s.isGranted('sess-1', 'bash'), true);
|
||
});
|
||
|
||
test('点「同意」不产生免批 —— 这正是修复前的 bug', () => {
|
||
const s = createGrantStore();
|
||
assert.equal(s.grant('sess-1', 'bash', '同意'), false);
|
||
assert.equal(s.isGranted('sess-1', 'bash'), false);
|
||
});
|
||
|
||
test('授权不跨工具:批了 bash 不等于批了 write', () => {
|
||
const s = createGrantStore();
|
||
s.grant('sess-1', 'bash', '一直同意');
|
||
assert.equal(s.isGranted('sess-1', 'write'), false);
|
||
});
|
||
|
||
test('授权不跨会话:这是防越权的关键', () => {
|
||
// 人为「审查 llmsproxy」这条会话批准的 bash,不该授权
|
||
// 另一个发件人派来的另一条任务
|
||
const s = createGrantStore();
|
||
s.grant('sess-1', 'bash', '一直同意');
|
||
assert.equal(s.isGranted('sess-2', 'bash'), false);
|
||
});
|
||
|
||
test('revokeSession 清掉整条会话的全部授权', () => {
|
||
const s = createGrantStore();
|
||
s.grant('sess-1', 'bash', '一直同意');
|
||
s.grant('sess-1', 'write', '一直同意');
|
||
s.grant('sess-2', 'bash', '一直同意');
|
||
assert.equal(s.size(), 3);
|
||
|
||
s.revokeSession('sess-1');
|
||
assert.equal(s.isGranted('sess-1', 'bash'), false);
|
||
assert.equal(s.isGranted('sess-1', 'write'), false);
|
||
// 别的会话不受影响
|
||
assert.equal(s.isGranted('sess-2', 'bash'), true);
|
||
assert.equal(s.size(), 1);
|
||
});
|
||
|
||
test('工具名里含 : 不会导致误删(这是不用拼接键的原因)', () => {
|
||
const s = createGrantStore();
|
||
s.grant('sess-1', 'mcp:bash', '一直同意');
|
||
s.grant('sess-1:extra', 'bash', '一直同意');
|
||
s.revokeSession('sess-1');
|
||
// 拼接键实现(`${session}:${tool}` 按前缀删)会把下面这条一起删掉
|
||
assert.equal(s.isGranted('sess-1:extra', 'bash'), true);
|
||
});
|
||
|
||
test('空会话 id / 空工具名不产生授权(防止一个空键放行一切)', () => {
|
||
const s = createGrantStore();
|
||
assert.equal(s.grant('', 'bash', '一直同意'), false);
|
||
assert.equal(s.grant('sess-1', '', '一直同意'), false);
|
||
assert.equal(s.isGranted('', 'bash'), false);
|
||
assert.equal(s.isGranted('sess-1', ''), false);
|
||
assert.equal(s.size(), 0);
|
||
});
|
||
|
||
test('重复授权同一对不重复计数', () => {
|
||
const s = createGrantStore();
|
||
s.grant('sess-1', 'bash', '一直同意');
|
||
s.grant('sess-1', 'bash', '一直同意');
|
||
assert.equal(s.size(), 1);
|
||
});
|
||
|
||
test('revokeSession 对没授权过的会话是安全的空操作', () => {
|
||
const s = createGrantStore();
|
||
s.revokeSession('never-seen');
|
||
assert.equal(s.size(), 0);
|
||
});
|