@sleep2agi/commhub-server 0.9.0-preview.206 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleep2agi/commhub-server",
3
- "version": "0.9.0-preview.206",
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",
@@ -502,5 +502,53 @@ describe("未绑定节点的令牌在报表里必须可见", () => {
502
502
  expect(unbound.tool_calls_total).toBeGreaterThanOrEqual(4);
503
503
  const real = r.body.nodes.find((n: any) => n.node_id === node.nN.id);
504
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);
505
553
  });
506
554
  });
@@ -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 }>; tool_calls?: Array<{ tool: string; hits: number }>; tool_calls_total?: number; unbound_token?: boolean }>();
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) {
@@ -3090,7 +3090,15 @@ return Bun.serve({
3090
3090
  // 它们**没有 nodes 行**,所以两张白名单都管不到它们(toolAllowlist 为 null ⇒ 不限制)。
3091
3091
  // 报表里必须显式标出来,否则管理员会以为「名单之外没有别人在派活」。
3092
3092
  // 这里只标注,不改变任何判定。
3093
- if (entry.node_id.startsWith("token:")) entry.unbound_token = true;
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
+ }
3094
3102
  }
3095
3103
  const nodes = [...byNode.values()].sort((a, b) =>
3096
3104
  b.tool_calls_total - a.tool_calls_total || b.total - a.total || a.node_id.localeCompare(b.node_id));
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
- const verdict = verdictFor(id, name, args);
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
  }