@sleep2agi/commhub-server 0.9.0-preview.205 → 0.9.0-preview.207
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/package.json +1 -1
- package/src/node-permissions-http.test.ts +88 -0
- package/src/node-permissions.ts +5 -1
- package/src/server.ts +46 -2
- package/src/tools.ts +5 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sleep2agi/commhub-server",
|
|
3
|
-
"version": "0.9.0-preview.
|
|
3
|
+
"version": "0.9.0-preview.207",
|
|
4
4
|
"description": "CommHub Server — AI Agent communication hub with MCP protocol, multi-network isolation, user auth, and MCP tools (17 collaboration-core + node/provider ops tools; authoritative list: docs-site/docs/api/mcp-tools.md).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -251,6 +251,27 @@ describe("GET /api/networks/:id/node-permission-report", () => {
|
|
|
251
251
|
expect((await call(node.nN.token, "GET", `/api/networks/${net}/node-permission-report`)).status).toBe(403);
|
|
252
252
|
expect((await call(user.boss.token, "GET", `/api/networks/${net}/node-permission-report?since=${encodeURIComponent(new Date(Date.now() + 3_600_000).toISOString())}`)).body.total).toBe(0);
|
|
253
253
|
});
|
|
254
|
+
|
|
255
|
+
// #2969 —— 设白名单之前,管理员要能看「这台节点实际用过哪些工具」,
|
|
256
|
+
// 否则白名单只能凭想象写,第一次上线就拦掉在用的东西。
|
|
257
|
+
//
|
|
258
|
+
// 关键的第二条:一台**从不越界**但天天在用的节点,过去在报表里根本不存在
|
|
259
|
+
// (nodes 只从「被拒过的行」聚合)。它恰恰是最该出现在名单里的那台。
|
|
260
|
+
test("按节点的工具调用明细:从不越界的节点也要出现", async () => {
|
|
261
|
+
await mcp(node.nN.token, "list_tasks", { network_id: net, limit: 1 });
|
|
262
|
+
await mcp(node.nN.token, "list_tasks", { network_id: net, limit: 1 });
|
|
263
|
+
const r = await call(user.boss.token, "GET", `/api/networks/${net}/node-permission-report`);
|
|
264
|
+
expect(r.status).toBe(200);
|
|
265
|
+
const nN = r.body.nodes.find((n: any) => n.node_id === node.nN.id);
|
|
266
|
+
expect(nN).toBeTruthy();
|
|
267
|
+
expect(nN.tool_calls_total).toBeGreaterThanOrEqual(2);
|
|
268
|
+
const listTasks = nN.tool_calls.find((t: any) => t.tool === "mcp:list_tasks");
|
|
269
|
+
expect(listTasks.hits).toBeGreaterThanOrEqual(2);
|
|
270
|
+
// 排序也变了:按调用量降序,让「用得最多」的排在最前
|
|
271
|
+
expect(r.body.nodes[0].tool_calls_total).toBeGreaterThanOrEqual(nN.tool_calls_total);
|
|
272
|
+
// 网络级合计保持不变(旧口径不能因为这次改动而变)
|
|
273
|
+
expect(r.body.tool_calls.total).toBeGreaterThanOrEqual(nN.tool_calls_total);
|
|
274
|
+
});
|
|
254
275
|
});
|
|
255
276
|
|
|
256
277
|
// #2823 —— 调用面计数:拒绝日志给不了分母。
|
|
@@ -464,3 +485,70 @@ describe("空数组不猜:拒收,清空请传 null", () => {
|
|
|
464
485
|
expect(row.body.nodes[0].tool_allowlist).toBeNull();
|
|
465
486
|
});
|
|
466
487
|
});
|
|
488
|
+
|
|
489
|
+
// #2969 / #2971 —— 未绑定节点的令牌是权限体系的一个真实盲点:
|
|
490
|
+
// 账本用 `token:<token_id>` 当键,它们没有 nodes 行 ⇒ 两张白名单都管不到(toolAllowlist 为 null)。
|
|
491
|
+
// 报表必须把它们标出来,否则管理员会以为「名单之外没有别人在派活」。
|
|
492
|
+
describe("未绑定节点的令牌在报表里必须可见", () => {
|
|
493
|
+
test("token: 键的条目带 unbound_token=true,真实节点不带", async () => {
|
|
494
|
+
db.run(`INSERT INTO node_tool_calls (network_id, node_id, tool, hour, hits) VALUES (?1, ?2, 'mcp:send_task', ?3, 4)
|
|
495
|
+
ON CONFLICT(network_id, node_id, tool, hour) DO UPDATE SET hits = node_tool_calls.hits + 4`,
|
|
496
|
+
[net, "token:tok_unbound_test", new Date().toISOString().slice(0, 13)]);
|
|
497
|
+
const r = await call(user.boss.token, "GET", `/api/networks/${net}/node-permission-report`);
|
|
498
|
+
expect(r.status).toBe(200);
|
|
499
|
+
const unbound = r.body.nodes.find((n: any) => n.node_id === "token:tok_unbound_test");
|
|
500
|
+
expect(unbound).toBeTruthy();
|
|
501
|
+
expect(unbound.unbound_token).toBe(true);
|
|
502
|
+
expect(unbound.tool_calls_total).toBeGreaterThanOrEqual(4);
|
|
503
|
+
const real = r.body.nodes.find((n: any) => n.node_id === node.nN.id);
|
|
504
|
+
expect(real.unbound_token).toBeUndefined();
|
|
505
|
+
// 令牌名要带出来 —— 只看 `token:tok_xxx` 无法处理;名字才是「该改哪个令牌」的答案。
|
|
506
|
+
const tokRow = db.get<{ token_id: string }>("SELECT token_id FROM api_tokens LIMIT 1");
|
|
507
|
+
db.run(`INSERT INTO node_tool_calls (network_id, node_id, tool, hour, hits) VALUES (?1, ?2, 'mcp:send_task', ?3, 1)
|
|
508
|
+
ON CONFLICT(network_id, node_id, tool, hour) DO UPDATE SET hits = node_tool_calls.hits + 1`,
|
|
509
|
+
[net, `token:${tokRow.token_id}`, new Date().toISOString().slice(0, 13)]);
|
|
510
|
+
const r2 = await call(user.boss.token, "GET", `/api/networks/${net}/node-permission-report`);
|
|
511
|
+
const withName = r2.body.nodes.find((n: any) => n.node_id === `token:${tokRow.token_id}`);
|
|
512
|
+
expect(withName.unbound_token_name).toBeTruthy();
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
// #2969 续 —— 令牌解析不到节点行时,两张白名单都管不到它。
|
|
517
|
+
// 先按「记录」跑:`unbound_token` 不在 MODE_REASONS 里 ⇒ 默认 log 模式只记账、不改变行为。
|
|
518
|
+
// 这样「要不要强制要求绑定令牌」这个决定,将来自有数据可依据(而不是靠猜会不会炸)。
|
|
519
|
+
describe("未绑定令牌的写调用:默认只记录,不拦", () => {
|
|
520
|
+
// 造一个**解析不到节点**的令牌:名字里的 alias 在本网络没有对应 nodes 行。
|
|
521
|
+
// 这就是线上那 4 个令牌的真实形状(节点改名、令牌名没跟着改)。
|
|
522
|
+
const unboundKey = () => {
|
|
523
|
+
const alias = `ghost-alias-${stamp}`;
|
|
524
|
+
const tokenId = `tok_ghost_${stamp}`;
|
|
525
|
+
db.run("INSERT OR IGNORE INTO api_tokens (token_id, token_hash, user_id, network_id, name, scope) VALUES (?1, ?2, ?3, ?4, ?5, 'network')",
|
|
526
|
+
[tokenId, `hash_${tokenId}`, user.boss.id, net, `node:${alias}`]);
|
|
527
|
+
return tokenId;
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
test("log 模式:写调用照常成功,但进 node_permission_log(reason=unbound_token)", async () => {
|
|
531
|
+
const tokenId = unboundKey();
|
|
532
|
+
const key = `token:${tokenId}`;
|
|
533
|
+
const before = Number(db.get<{ h: number | string | null }>(
|
|
534
|
+
"SELECT SUM(hits) AS h FROM node_permission_log WHERE network_id = ?1 AND node_id = ?2 AND reason = 'unbound_token'",
|
|
535
|
+
net, key)?.h ?? 0);
|
|
536
|
+
// 用一个「解析不到节点」的身份直接走判定,绕开 HTTP 令牌构造(这里验的是判据与记账)
|
|
537
|
+
const { nodeIdentity, nodeDecide } = await import("./node-permissions.js");
|
|
538
|
+
const id = nodeIdentity(tokenId, net)!;
|
|
539
|
+
expect(id.nodeId).toBeNull();
|
|
540
|
+
const allowed = nodeDecide(id, "mcp:send_task", { reason: "unbound_token", sample: "send_task" });
|
|
541
|
+
expect(allowed).toBe(false); // log 模式:不拦
|
|
542
|
+
const after = Number(db.get<{ h: number | string | null }>(
|
|
543
|
+
"SELECT SUM(hits) AS h FROM node_permission_log WHERE network_id = ?1 AND node_id = ?2 AND reason = 'unbound_token'",
|
|
544
|
+
net, key)?.h ?? 0);
|
|
545
|
+
expect(after).toBe(before + 1); // 但记了一笔
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
test("enforce 模式:同一个调用被拒,原因是 unbound_token", async () => {
|
|
549
|
+
process.env.COMMHUB_NODE_PERMISSIONS = "enforce";
|
|
550
|
+
const { nodeIdentity, nodeDecide } = await import("./node-permissions.js");
|
|
551
|
+
const id = nodeIdentity(unboundKey(), net)!;
|
|
552
|
+
expect(nodeDecide(id, "mcp:send_task", { reason: "unbound_token", sample: "send_task" })).toBe(true);
|
|
553
|
+
});
|
|
554
|
+
});
|
package/src/node-permissions.ts
CHANGED
|
@@ -31,7 +31,10 @@ export type NodePermissionReason =
|
|
|
31
31
|
/** #2969 —— 主人给这个节点设了对端白名单,目标不在名单里。 */
|
|
32
32
|
| "peer_not_allowed"
|
|
33
33
|
/** #2969 后半 —— 主人在这个节点的接口白名单里没有列这个工具/路由。 */
|
|
34
|
-
| "tool_not_allowed"
|
|
34
|
+
| "tool_not_allowed"
|
|
35
|
+
/** #2969 续 —— 令牌解析不到节点行(常见成因:节点改过名、令牌名没跟着改)。
|
|
36
|
+
* 这类令牌**没有 nodes 行**,两张白名单都管不到它;先按「记录」模式跑,看有多少。 */
|
|
37
|
+
| "unbound_token";
|
|
35
38
|
|
|
36
39
|
/** 显式模式的原因:不看开关,永远拒。 */
|
|
37
40
|
// 白名单和 readonly/restricted 同类:**主人主动设的,设了就立刻生效**,不看开关。
|
|
@@ -256,6 +259,7 @@ const HINTS: Record<NodePermissionReason, string> = {
|
|
|
256
259
|
owner_unknown: "this node's owner is unknown; recreate the node token from the owner's account",
|
|
257
260
|
peer_not_allowed: "this node is limited to an explicit peer allowlist; the target is not on it — ask the node's owner to add it, or clear the allowlist",
|
|
258
261
|
tool_not_allowed: "this node is limited to an explicit tool allowlist; this tool is not on it — ask the node's owner to add it, or clear the allowlist",
|
|
262
|
+
unbound_token: "this token does not resolve to a node (usually the node was renamed and the token was not); rename or bind the token, or issue a node-bound token",
|
|
259
263
|
};
|
|
260
264
|
|
|
261
265
|
export function nodePermissionDeniedBody(reason: NodePermissionReason, route: string): { ok: false; error: "node_permission_denied"; reason: NodePermissionReason; route: string; hint: string } {
|
package/src/server.ts
CHANGED
|
@@ -3044,7 +3044,7 @@ return Bun.serve({
|
|
|
3044
3044
|
FROM node_permission_log WHERE network_id = ?1 AND hour >= ?2
|
|
3045
3045
|
GROUP BY node_id, route, reason ORDER BY node_id, route, reason`,
|
|
3046
3046
|
networkId, sinceHour);
|
|
3047
|
-
const byNode = new Map<string, { node_id: string; alias: string | null; permission_mode: string; total: number; by_reason: Record<string, number>; routes: Array<{ route: string; reason: string; hits: number; sample: string | null; last_hour: string }
|
|
3047
|
+
const byNode = new Map<string, { node_id: string; alias: string | null; permission_mode: string; total: number; by_reason: Record<string, number>; routes: Array<{ route: string; reason: string; hits: number; sample: string | null; last_hour: string }>; tool_calls?: Array<{ tool: string; hits: number }>; tool_calls_total?: number; unbound_token?: boolean; unbound_token_name?: string | null }>();
|
|
3048
3048
|
for (const r of rows) {
|
|
3049
3049
|
let entry = byNode.get(r.node_id);
|
|
3050
3050
|
if (!entry) {
|
|
@@ -3057,7 +3057,51 @@ return Bun.serve({
|
|
|
3057
3057
|
entry.by_reason[r.reason] = (entry.by_reason[r.reason] ?? 0) + hits;
|
|
3058
3058
|
entry.routes.push({ route: r.route, reason: r.reason, hits, sample: r.sample, last_hour: r.last_hour });
|
|
3059
3059
|
}
|
|
3060
|
-
|
|
3060
|
+
// #2969 —— 按节点的工具调用明细。
|
|
3061
|
+
// 为什么要这个:管理员在设「只能用哪些工具」之前,需要先看**这台节点实际用过哪些**,
|
|
3062
|
+
// 否则白名单只能凭想象写,第一次上线就是拦掉在用的工具。
|
|
3063
|
+
// 之前账本只有网络级合计(by_tool),且 nodes 只从「被拒过的行」聚合 ——
|
|
3064
|
+
// 一台从不越界但天天在用的节点,在报表里根本不存在。
|
|
3065
|
+
//
|
|
3066
|
+
// #2971 之后账本包含读类工具,这里的明细才第一次是「全部调用面」而不是「会被拦的那部分」。
|
|
3067
|
+
const perNodeCalls = db.all<{ node_id: string; tool: string; hits: number | string }>(
|
|
3068
|
+
`SELECT node_id, tool, SUM(hits) AS hits FROM node_tool_calls
|
|
3069
|
+
WHERE network_id = ?1 AND hour >= ?2
|
|
3070
|
+
GROUP BY node_id, tool ORDER BY node_id, SUM(hits) DESC`,
|
|
3071
|
+
networkId, sinceHour,
|
|
3072
|
+
);
|
|
3073
|
+
const callsByNode = new Map<string, Array<{ tool: string; hits: number }>>();
|
|
3074
|
+
for (const r of perNodeCalls) {
|
|
3075
|
+
const list = callsByNode.get(r.node_id) ?? [];
|
|
3076
|
+
list.push({ tool: r.tool, hits: Number(r.hits) });
|
|
3077
|
+
callsByNode.set(r.node_id, list);
|
|
3078
|
+
}
|
|
3079
|
+
// 有调用但没被拒过的节点也要出现在结果里(total = 0)。
|
|
3080
|
+
for (const nodeId of callsByNode.keys()) {
|
|
3081
|
+
if (byNode.has(nodeId)) continue;
|
|
3082
|
+
const n = db.get<{ alias: string | null; permission_mode: string | null }>("SELECT alias, permission_mode FROM nodes WHERE node_id = ?1", nodeId);
|
|
3083
|
+
byNode.set(nodeId, { node_id: nodeId, alias: n?.alias ?? null, permission_mode: n?.permission_mode || "normal", total: 0, by_reason: {}, routes: [] });
|
|
3084
|
+
}
|
|
3085
|
+
for (const entry of byNode.values()) {
|
|
3086
|
+
const list = callsByNode.get(entry.node_id) ?? [];
|
|
3087
|
+
entry.tool_calls = list;
|
|
3088
|
+
entry.tool_calls_total = list.reduce((n, e) => n + e.hits, 0);
|
|
3089
|
+
// 未绑定节点的令牌:账本用 `token:<token_id>` 当键(nodeIdentity 的老路径)。
|
|
3090
|
+
// 它们**没有 nodes 行**,所以两张白名单都管不到它们(toolAllowlist 为 null ⇒ 不限制)。
|
|
3091
|
+
// 报表里必须显式标出来,否则管理员会以为「名单之外没有别人在派活」。
|
|
3092
|
+
// 这里只标注,不改变任何判定。
|
|
3093
|
+
if (entry.node_id.startsWith("token:")) {
|
|
3094
|
+
entry.unbound_token = true;
|
|
3095
|
+
// 令牌名往往就是答案:账本里是 `token:tok_xxx`,光看这个没法处理。
|
|
3096
|
+
// 线上实测这 4 个令牌的名字是 `node:ANet基建` / `node:TMWork` …,而 nodes 表里
|
|
3097
|
+
// 已经没有这些 alias 了 —— 节点改过名、令牌没跟着改,于是解析成「未注册」,
|
|
3098
|
+
// 白名单静默不生效。把名字带出来,管理员才知道该改哪个令牌。
|
|
3099
|
+
const tok = db.get<{ name: string | null }>("SELECT name FROM api_tokens WHERE token_id = ?1", entry.node_id.slice(6));
|
|
3100
|
+
entry.unbound_token_name = tok?.name ?? null;
|
|
3101
|
+
}
|
|
3102
|
+
}
|
|
3103
|
+
const nodes = [...byNode.values()].sort((a, b) =>
|
|
3104
|
+
b.tool_calls_total - a.tool_calls_total || b.total - a.total || a.node_id.localeCompare(b.node_id));
|
|
3061
3105
|
// #2823 —— 调用面分母。`total` 只数「本来会被拒」的次数;没有分母时
|
|
3062
3106
|
// 「0 命中」既可能是没人越界,也可能是没人碰过这些工具,两者结论相反。
|
|
3063
3107
|
// 这里单独给一份 classified 工具的真实调用数(按小时合并后的 hits 之和),
|
package/src/tools.ts
CHANGED
|
@@ -306,7 +306,11 @@ function guardNodePermissionTools(server: McpServer, tokenId: string, networkId:
|
|
|
306
306
|
if (cls !== "always") recordNodeToolCall(id, `mcp:${name}`);
|
|
307
307
|
} else {
|
|
308
308
|
const args = callArgs[0] && typeof callArgs[0] === "object" ? callArgs[0] as Record<string, unknown> : {};
|
|
309
|
-
|
|
309
|
+
// #2969 续 —— 令牌解析不到节点行时(nodeId 为 null),两张白名单都管不到它。
|
|
310
|
+
// 先按「记录」模式跑:`unbound_token` **不在 MODE_REASONS 里**,
|
|
311
|
+
// 所以 log(默认)只记账、不改变行为;只有显式 enforce 才会拒。
|
|
312
|
+
// 这样「要不要强制要求绑定令牌」这个决定,将来自有数据可依据。
|
|
313
|
+
const verdict = id.nodeId ? verdictFor(id, name, args) : { reason: "unbound_token" as const, sample: name };
|
|
310
314
|
if (nodeDecide(id, `mcp:${name}`, verdict)) return denied(verdict!.reason, `mcp:${name}`);
|
|
311
315
|
}
|
|
312
316
|
}
|