@sleep2agi/commhub-server 0.9.0-preview.2 → 0.9.0-preview.21
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 +2 -2
- package/src/ack-create-request.test.ts +331 -0
- package/src/api-host-supervisors-fallback.test.ts +262 -0
- package/src/api-nodes-shape.test.ts +172 -0
- package/src/auth.ts +3 -1
- package/src/config-apply-sec1.test.ts +107 -0
- package/src/config-apply-validate.test.ts +92 -0
- package/src/config-apply-validate.ts +81 -3
- package/src/create-node-validate.test.ts +187 -0
- package/src/create-node-validate.ts +206 -0
- package/src/create-node.test.ts +354 -0
- package/src/create-node.ts +346 -0
- package/src/db.ts +196 -0
- package/src/index.ts +216 -3
- package/src/lifecycle-guard.ts +50 -0
- package/src/list-host-supervisors.test.ts +314 -0
- package/src/probe-validate.test.ts +221 -0
- package/src/probe-validate.ts +104 -0
- package/src/probe.test.ts +80 -0
- package/src/probe.ts +200 -0
- package/src/shared/probe-host-allowlist-drift.test.ts +54 -0
- package/src/shared/probe-host-allowlist.ts +128 -0
- package/src/shared/reserved-env-drift.test.ts +43 -0
- package/src/shared/reserved-env.test.ts +35 -0
- package/src/shared/reserved-env.ts +31 -0
- package/src/stop-delete-node.test.ts +720 -0
- package/src/tools.ts +1776 -13
- package/src/update-provider.test.ts +234 -0
- package/src/uploads.test.ts +66 -0
- package/src/uploads.ts +36 -0
- package/src/vault.test.ts +200 -0
- package/src/vault.ts +156 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { afterAll, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { db } from "./db.js";
|
|
3
|
+
|
|
4
|
+
// Regression test for SELECT * fragility on GET /api/nodes.
|
|
5
|
+
//
|
|
6
|
+
// Prior behavior: `SELECT * FROM nodes` returned whatever columns the
|
|
7
|
+
// table happened to have. When RFC-024 added config_revision +
|
|
8
|
+
// config_snapshot columns (read by the per-node GET /api/nodes/:id/config
|
|
9
|
+
// endpoint, NOT meant for the list endpoint), they silently leaked into
|
|
10
|
+
// the list response and broke V3 Networks tests that asserted on the
|
|
11
|
+
// nodes-list row shape.
|
|
12
|
+
//
|
|
13
|
+
// Current behavior: explicit column list — the response contract is:
|
|
14
|
+
// node_id, node_name, alias, runtime, model, config_path, channels,
|
|
15
|
+
// server, hostname, network_id, created_at, updated_at
|
|
16
|
+
// This test runs the same SQL the handler uses, on a row that DOES have
|
|
17
|
+
// config_revision + config_snapshot populated, and asserts those fields
|
|
18
|
+
// do NOT appear in the returned row. If anyone reverts the handler to
|
|
19
|
+
// SELECT *, or adds an internal column to the SELECT, this test breaks.
|
|
20
|
+
|
|
21
|
+
const TEST_NETWORK = "net_test_shape";
|
|
22
|
+
const TEST_NODE = "node_test_shape_abc";
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
// Best-effort cleanup (test runs in shared test DB; rows are isolated by id)
|
|
26
|
+
try { db.run("DELETE FROM nodes WHERE node_id = ?1", [TEST_NODE]); } catch {}
|
|
27
|
+
});
|
|
28
|
+
afterAll(() => {
|
|
29
|
+
try { db.run("DELETE FROM nodes WHERE node_id = ?1", [TEST_NODE]); } catch {}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Helper: mirror handler's role-extraction pass to mimic the production
|
|
33
|
+
// response shape exactly. Keeps the test the source of truth for the
|
|
34
|
+
// contract; if the handler changes, the assertions still lock what
|
|
35
|
+
// the dashboard / external clients see.
|
|
36
|
+
function mapRow(r: Record<string, unknown>): Record<string, unknown> {
|
|
37
|
+
let role: string | null = null;
|
|
38
|
+
const snap = r.config_snapshot;
|
|
39
|
+
if (snap) {
|
|
40
|
+
try { role = (typeof snap === "string" ? JSON.parse(snap) : snap)?.role ?? null; }
|
|
41
|
+
catch { /* malformed */ }
|
|
42
|
+
}
|
|
43
|
+
const { config_snapshot, ...rest } = r;
|
|
44
|
+
return { ...rest, role };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe("RFC-024 regression — GET /api/nodes response shape (no SELECT *)", () => {
|
|
48
|
+
test("explicit columns + role extracted: config_snapshot NOT in response, role IS", () => {
|
|
49
|
+
// Insert a row with the leak-prone columns populated. Use INSERT OR
|
|
50
|
+
// REPLACE so concurrent test runs don't trip on PRIMARY KEY.
|
|
51
|
+
db.run(
|
|
52
|
+
`INSERT OR REPLACE INTO nodes (
|
|
53
|
+
node_id, node_name, alias, runtime, model, config_path,
|
|
54
|
+
channels, server, hostname, network_id,
|
|
55
|
+
config_revision, config_snapshot
|
|
56
|
+
) VALUES (
|
|
57
|
+
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12
|
|
58
|
+
)`,
|
|
59
|
+
[TEST_NODE, "test-shape", "test-shape", "claude-agent-sdk", "claude-opus",
|
|
60
|
+
"/tmp/cfg.json", "[]", "localhost", "test-host", TEST_NETWORK,
|
|
61
|
+
7, JSON.stringify({ model: "claude-opus", flags: { x: true }, role: "host_supervisor" })],
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
// Mirror handler SQL + mapping
|
|
65
|
+
const raw = db.all<Record<string, unknown>>(
|
|
66
|
+
`SELECT node_id, node_name, alias, runtime, model,
|
|
67
|
+
config_path, channels, server, hostname,
|
|
68
|
+
network_id, created_at, updated_at,
|
|
69
|
+
config_snapshot
|
|
70
|
+
FROM nodes WHERE node_id = ?1`,
|
|
71
|
+
TEST_NODE,
|
|
72
|
+
);
|
|
73
|
+
const rows = raw.map(mapRow);
|
|
74
|
+
|
|
75
|
+
expect(rows).toHaveLength(1);
|
|
76
|
+
const row = rows[0]!;
|
|
77
|
+
// Expected fields present (13 = 12 contract + new role)
|
|
78
|
+
for (const k of ["node_id", "node_name", "alias", "runtime", "model",
|
|
79
|
+
"config_path", "channels", "server", "hostname",
|
|
80
|
+
"network_id", "created_at", "updated_at", "role"]) {
|
|
81
|
+
expect(row).toHaveProperty(k);
|
|
82
|
+
}
|
|
83
|
+
// Internal fields NOT present
|
|
84
|
+
expect(row).not.toHaveProperty("config_revision");
|
|
85
|
+
expect(row).not.toHaveProperty("config_snapshot");
|
|
86
|
+
// role is EXTRACTED from config_snapshot JSON, not the raw blob
|
|
87
|
+
expect(row.role).toBe("host_supervisor");
|
|
88
|
+
// Belt: full key set is exactly the 13 contract fields
|
|
89
|
+
expect(Object.keys(row).sort()).toEqual([
|
|
90
|
+
"alias", "channels", "config_path", "created_at", "hostname",
|
|
91
|
+
"model", "network_id", "node_id", "node_name", "role", "runtime",
|
|
92
|
+
"server", "updated_at",
|
|
93
|
+
]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test("role is null when config_snapshot is empty / missing / malformed", () => {
|
|
97
|
+
// Empty snapshot → null
|
|
98
|
+
db.run(
|
|
99
|
+
`INSERT OR REPLACE INTO nodes (node_id, node_name, alias, network_id, config_snapshot)
|
|
100
|
+
VALUES (?1, ?2, ?3, ?4, NULL)`,
|
|
101
|
+
[TEST_NODE, "test-shape", "test-shape", TEST_NETWORK],
|
|
102
|
+
);
|
|
103
|
+
let row = mapRow(db.get<Record<string, unknown>>(
|
|
104
|
+
`SELECT node_id, node_name, alias, runtime, model, config_path, channels,
|
|
105
|
+
server, hostname, network_id, created_at, updated_at, config_snapshot
|
|
106
|
+
FROM nodes WHERE node_id = ?1`,
|
|
107
|
+
TEST_NODE,
|
|
108
|
+
)!);
|
|
109
|
+
expect(row.role).toBeNull();
|
|
110
|
+
expect(row).not.toHaveProperty("config_snapshot");
|
|
111
|
+
|
|
112
|
+
// Malformed JSON → null (handler doesn't throw, just leaves role null)
|
|
113
|
+
db.run(`UPDATE nodes SET config_snapshot = ?1 WHERE node_id = ?2`, ["not-json{", TEST_NODE]);
|
|
114
|
+
row = mapRow(db.get<Record<string, unknown>>(
|
|
115
|
+
`SELECT node_id, node_name, alias, runtime, model, config_path, channels,
|
|
116
|
+
server, hostname, network_id, created_at, updated_at, config_snapshot
|
|
117
|
+
FROM nodes WHERE node_id = ?1`,
|
|
118
|
+
TEST_NODE,
|
|
119
|
+
)!);
|
|
120
|
+
expect(row.role).toBeNull();
|
|
121
|
+
|
|
122
|
+
// Snapshot without role key → null
|
|
123
|
+
db.run(`UPDATE nodes SET config_snapshot = ?1 WHERE node_id = ?2`,
|
|
124
|
+
[JSON.stringify({ model: "x", flags: {} }), TEST_NODE]);
|
|
125
|
+
row = mapRow(db.get<Record<string, unknown>>(
|
|
126
|
+
`SELECT node_id, node_name, alias, runtime, model, config_path, channels,
|
|
127
|
+
server, hostname, network_id, created_at, updated_at, config_snapshot
|
|
128
|
+
FROM nodes WHERE node_id = ?1`,
|
|
129
|
+
TEST_NODE,
|
|
130
|
+
)!);
|
|
131
|
+
expect(row.role).toBeNull();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("daemon discovery use-case: dashboard finds role=host_supervisor without prefix heuristic", () => {
|
|
135
|
+
// Daemon registered with arbitrary node_id (NO `node_daemon_` prefix)
|
|
136
|
+
db.run(
|
|
137
|
+
`INSERT OR REPLACE INTO nodes (node_id, node_name, alias, network_id, config_snapshot)
|
|
138
|
+
VALUES (?1, ?2, ?3, ?4, ?5)`,
|
|
139
|
+
[TEST_NODE, "my-daemon", "my-daemon", TEST_NETWORK,
|
|
140
|
+
JSON.stringify({ role: "host_supervisor", runtime: "claude-agent-sdk" })],
|
|
141
|
+
);
|
|
142
|
+
const rows = db.all<Record<string, unknown>>(
|
|
143
|
+
`SELECT node_id, node_name, alias, runtime, model, config_path, channels,
|
|
144
|
+
server, hostname, network_id, created_at, updated_at, config_snapshot
|
|
145
|
+
FROM nodes WHERE network_id = ?1`,
|
|
146
|
+
TEST_NETWORK,
|
|
147
|
+
).map(mapRow);
|
|
148
|
+
// Dashboard's lookup: find first node with role==='host_supervisor'
|
|
149
|
+
const daemon = rows.find(r => r.role === "host_supervisor");
|
|
150
|
+
expect(daemon).toBeTruthy();
|
|
151
|
+
expect(daemon!.node_id).toBe(TEST_NODE);
|
|
152
|
+
// Even though node_id does NOT start with `node_daemon_` (the prior heuristic)
|
|
153
|
+
expect(String(daemon!.node_id).startsWith("node_daemon_")).toBe(false);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("internal columns are still readable via dedicated query (config snapshot endpoint path)", () => {
|
|
157
|
+
db.run(
|
|
158
|
+
`INSERT OR REPLACE INTO nodes (node_id, node_name, alias, network_id,
|
|
159
|
+
config_revision, config_snapshot)
|
|
160
|
+
VALUES (?1, ?2, ?3, ?4, ?5, ?6)`,
|
|
161
|
+
[TEST_NODE, "test-shape", "test-shape", TEST_NETWORK,
|
|
162
|
+
42, JSON.stringify({ model: "claude-opus" })],
|
|
163
|
+
);
|
|
164
|
+
// Mirrors the SELECT in GET /api/nodes/:id/config (server/src/index.ts):
|
|
165
|
+
const cfgRow = db.get<{ config_revision: number; config_snapshot: string }>(
|
|
166
|
+
"SELECT config_revision, config_snapshot FROM nodes WHERE node_id = ?1",
|
|
167
|
+
TEST_NODE,
|
|
168
|
+
);
|
|
169
|
+
expect(cfgRow?.config_revision).toBe(42);
|
|
170
|
+
expect(cfgRow?.config_snapshot).toContain("claude-opus");
|
|
171
|
+
});
|
|
172
|
+
});
|
package/src/auth.ts
CHANGED
|
@@ -211,7 +211,9 @@ export function resolveToken(token: string): { user: AuthUser; networkId: string
|
|
|
211
211
|
`SELECT t.token_id, t.user_id, t.network_id, t.scope, t.name AS token_name,
|
|
212
212
|
u.username, u.display_name, u.email, u.role
|
|
213
213
|
FROM api_tokens t JOIN users u ON t.user_id = u.user_id
|
|
214
|
-
WHERE t.token_hash = ?1
|
|
214
|
+
WHERE t.token_hash = ?1
|
|
215
|
+
AND (t.expires_at IS NULL OR t.expires_at > datetime('now'))
|
|
216
|
+
AND t.revoked_at IS NULL`,
|
|
215
217
|
tHash);
|
|
216
218
|
|
|
217
219
|
if (!row) return null;
|
|
@@ -721,3 +721,110 @@ describe("SEC-1 — update lifecycle: single-flight + cross-network pending look
|
|
|
721
721
|
expect(inFlight).toBeDefined();
|
|
722
722
|
});
|
|
723
723
|
});
|
|
724
|
+
|
|
725
|
+
// ── #260 P5 codex-catch regression: channels finalize ─────────────
|
|
726
|
+
describe("finalizePendingMatchingUpdates — #260 P5 channels content-match", () => {
|
|
727
|
+
function insertPendingUpdate(opts: {
|
|
728
|
+
update_id?: string;
|
|
729
|
+
node_id: string;
|
|
730
|
+
network_id: string;
|
|
731
|
+
patch: any;
|
|
732
|
+
apply_mode: "hot" | "restart" | "restart_only";
|
|
733
|
+
status?: "pending" | "restarting";
|
|
734
|
+
}): string {
|
|
735
|
+
const id = opts.update_id ?? `cu_${uuidv4()}`;
|
|
736
|
+
db.run(
|
|
737
|
+
`INSERT INTO node_config_updates (update_id, node_id, network_id, patch_json, apply_mode, base_revision, status, created_at, created_by_token) VALUES (?1, ?2, ?3, ?4, ?5, 0, ?6, ?7, 'test')`,
|
|
738
|
+
[id, opts.node_id, opts.network_id, JSON.stringify(opts.patch), opts.apply_mode, opts.status ?? "pending", Date.now()],
|
|
739
|
+
);
|
|
740
|
+
return id;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
test("channels-only patch: snapshot BEFORE apply (first boot) must NOT finalize", () => {
|
|
744
|
+
const nid = insertNode({ alias: "n-chan-1", network_id: "netA" });
|
|
745
|
+
const uid = insertPendingUpdate({
|
|
746
|
+
node_id: nid, network_id: "netA",
|
|
747
|
+
// Note the empty flags object — the pre-fix bug was that this
|
|
748
|
+
// trivially matched the flags loop and hub falsely finalized.
|
|
749
|
+
patch: { flags: {}, channels: ["feishu"] },
|
|
750
|
+
apply_mode: "restart",
|
|
751
|
+
});
|
|
752
|
+
// Node's startup snapshot BEFORE it consumed the update — no
|
|
753
|
+
// channels forked yet, so buildConfigSnapshot emits channels=[].
|
|
754
|
+
const r = finalizePendingMatchingUpdates(nid, { model: "x", flags: {}, channels: [] });
|
|
755
|
+
expect(r.finalizedCount).toBe(0);
|
|
756
|
+
expect(db.get<any>("SELECT status FROM node_config_updates WHERE update_id = ?1", uid).status).toBe("pending");
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
test("channels-only patch: snapshot AFTER apply (restart complete) finalizes", () => {
|
|
760
|
+
const nid = insertNode({ alias: "n-chan-2", network_id: "netA" });
|
|
761
|
+
const uid = insertPendingUpdate({
|
|
762
|
+
node_id: nid, network_id: "netA",
|
|
763
|
+
patch: { flags: {}, channels: ["feishu"] },
|
|
764
|
+
apply_mode: "restart",
|
|
765
|
+
});
|
|
766
|
+
const r = finalizePendingMatchingUpdates(nid, { model: "x", flags: {}, channels: ["feishu"] });
|
|
767
|
+
expect(r.finalizedCount).toBe(1);
|
|
768
|
+
expect(r.finalizedIds).toContain(uid);
|
|
769
|
+
expect(db.get<any>("SELECT status FROM node_config_updates WHERE update_id = ?1", uid).status).toBe("applied");
|
|
770
|
+
});
|
|
771
|
+
|
|
772
|
+
test("channels set-equality: order doesn't matter", () => {
|
|
773
|
+
const nid = insertNode({ alias: "n-chan-3", network_id: "netA" });
|
|
774
|
+
insertPendingUpdate({
|
|
775
|
+
node_id: nid, network_id: "netA",
|
|
776
|
+
patch: { flags: {}, channels: ["telegram", "feishu"] },
|
|
777
|
+
apply_mode: "restart",
|
|
778
|
+
});
|
|
779
|
+
// Snapshot order swapped — still matches.
|
|
780
|
+
const r = finalizePendingMatchingUpdates(nid, { model: "x", flags: {}, channels: ["feishu", "telegram"] });
|
|
781
|
+
expect(r.finalizedCount).toBe(1);
|
|
782
|
+
});
|
|
783
|
+
|
|
784
|
+
test("channels: [] disable-all — finalize when snapshot also [] (post-restart)", () => {
|
|
785
|
+
const nid = insertNode({ alias: "n-chan-4", network_id: "netA" });
|
|
786
|
+
insertPendingUpdate({
|
|
787
|
+
node_id: nid, network_id: "netA",
|
|
788
|
+
patch: { flags: {}, channels: [] },
|
|
789
|
+
apply_mode: "restart",
|
|
790
|
+
});
|
|
791
|
+
const r = finalizePendingMatchingUpdates(nid, { model: "x", flags: {}, channels: [] });
|
|
792
|
+
expect(r.finalizedCount).toBe(1);
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
test("channels: [] disable-all — do NOT finalize when snapshot still has channels", () => {
|
|
796
|
+
const nid = insertNode({ alias: "n-chan-5", network_id: "netA" });
|
|
797
|
+
insertPendingUpdate({
|
|
798
|
+
node_id: nid, network_id: "netA",
|
|
799
|
+
patch: { flags: {}, channels: [] },
|
|
800
|
+
apply_mode: "restart",
|
|
801
|
+
});
|
|
802
|
+
// Node hasn't restarted yet — old telegram worker still forked.
|
|
803
|
+
const r = finalizePendingMatchingUpdates(nid, { model: "x", flags: {}, channels: ["telegram"] });
|
|
804
|
+
expect(r.finalizedCount).toBe(0);
|
|
805
|
+
});
|
|
806
|
+
|
|
807
|
+
test("snapshot omits channels field entirely (pre-P5 agent-node) → NOT finalized on channels-carrying patch", () => {
|
|
808
|
+
const nid = insertNode({ alias: "n-chan-6", network_id: "netA" });
|
|
809
|
+
insertPendingUpdate({
|
|
810
|
+
node_id: nid, network_id: "netA",
|
|
811
|
+
patch: { flags: {}, channels: ["feishu"] },
|
|
812
|
+
apply_mode: "restart",
|
|
813
|
+
});
|
|
814
|
+
const r = finalizePendingMatchingUpdates(nid, { model: "x", flags: {} }); // no channels key
|
|
815
|
+
expect(r.finalizedCount).toBe(0);
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
test("model + channels compound match: both must satisfy", () => {
|
|
819
|
+
const nid = insertNode({ alias: "n-chan-7", network_id: "netA" });
|
|
820
|
+
insertPendingUpdate({
|
|
821
|
+
node_id: nid, network_id: "netA",
|
|
822
|
+
patch: { model: "opus-new", flags: {}, channels: ["telegram"] },
|
|
823
|
+
apply_mode: "restart",
|
|
824
|
+
});
|
|
825
|
+
// Wrong channels → not finalized even though model matches.
|
|
826
|
+
expect(finalizePendingMatchingUpdates(nid, { model: "opus-new", flags: {}, channels: [] }).finalizedCount).toBe(0);
|
|
827
|
+
// Both match → finalized.
|
|
828
|
+
expect(finalizePendingMatchingUpdates(nid, { model: "opus-new", flags: {}, channels: ["telegram"] }).finalizedCount).toBe(1);
|
|
829
|
+
});
|
|
830
|
+
});
|
|
@@ -9,9 +9,11 @@ import { describe, expect, test } from "bun:test";
|
|
|
9
9
|
import {
|
|
10
10
|
ALLOWED_FLAGS,
|
|
11
11
|
SECURITY_SENSITIVE_FLAGS,
|
|
12
|
+
EDITABLE_CHANNELS,
|
|
12
13
|
isAllowedToChangeFlag,
|
|
13
14
|
computeApplyMode,
|
|
14
15
|
validatePatch,
|
|
16
|
+
narrowChannelsPatch,
|
|
15
17
|
} from "./config-apply-validate";
|
|
16
18
|
|
|
17
19
|
describe("ALLOWED_FLAGS — exact contract (no silent extension)", () => {
|
|
@@ -258,3 +260,93 @@ describe("validatePatch — combinations", () => {
|
|
|
258
260
|
expect(r?.field).toBe("flags.maxTurns");
|
|
259
261
|
});
|
|
260
262
|
});
|
|
263
|
+
|
|
264
|
+
// ── #260 P5 — channels support (restart-tier, non-security-sensitive) ─
|
|
265
|
+
describe("EDITABLE_CHANNELS — mirrors agent-node runtime capability (cli.ts:671-676)", () => {
|
|
266
|
+
test("contains exactly telegram + feishu (channels agent-node actually forks workers for)", () => {
|
|
267
|
+
expect(EDITABLE_CHANNELS.size).toBe(2);
|
|
268
|
+
expect(EDITABLE_CHANNELS.has("telegram")).toBe(true);
|
|
269
|
+
expect(EDITABLE_CHANNELS.has("feishu")).toBe(true);
|
|
270
|
+
});
|
|
271
|
+
test("commhub is NOT editable — it's the RPC transport, not a per-node channel worker", () => {
|
|
272
|
+
expect(EDITABLE_CHANNELS.has("commhub")).toBe(false);
|
|
273
|
+
});
|
|
274
|
+
test("wechat is NOT editable — roadmap only on dashboard", () => {
|
|
275
|
+
expect(EDITABLE_CHANNELS.has("wechat")).toBe(false);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe("narrowChannelsPatch — hostile input hygiene at the wire boundary", () => {
|
|
280
|
+
test("straight allow-listed input passes verbatim", () => {
|
|
281
|
+
expect(narrowChannelsPatch(["telegram", "feishu"])).toEqual(["telegram", "feishu"]);
|
|
282
|
+
});
|
|
283
|
+
test("empty array preserved — 'disable all editable channels' is a real state", () => {
|
|
284
|
+
expect(narrowChannelsPatch([])).toEqual([]);
|
|
285
|
+
});
|
|
286
|
+
test("non-array returns null (patch didn't touch channels)", () => {
|
|
287
|
+
expect(narrowChannelsPatch(undefined)).toBeNull();
|
|
288
|
+
expect(narrowChannelsPatch("telegram" as any)).toBeNull();
|
|
289
|
+
expect(narrowChannelsPatch({} as any)).toBeNull();
|
|
290
|
+
});
|
|
291
|
+
test("case-folded to lower", () => {
|
|
292
|
+
expect(narrowChannelsPatch(["FEISHU", "Telegram"])).toEqual(["feishu", "telegram"]);
|
|
293
|
+
});
|
|
294
|
+
test("duplicates deduped (Set)", () => {
|
|
295
|
+
expect(narrowChannelsPatch(["telegram", "telegram", "feishu", "TELEGRAM"])).toEqual(["telegram", "feishu"]);
|
|
296
|
+
});
|
|
297
|
+
test("unknown channel keys dropped silently (roadmap wechat, transport commhub, evil keys)", () => {
|
|
298
|
+
expect(narrowChannelsPatch(["wechat", "telegram", "evil-hacker", "commhub"])).toEqual(["telegram"]);
|
|
299
|
+
});
|
|
300
|
+
test("non-string entries dropped (numbers, objects, nulls)", () => {
|
|
301
|
+
expect(narrowChannelsPatch(["telegram", 42, {}, null, "feishu"] as any)).toEqual(["telegram", "feishu"]);
|
|
302
|
+
});
|
|
303
|
+
test("SQL-injection-shaped value dropped (not exact key match)", () => {
|
|
304
|
+
expect(narrowChannelsPatch(["telegram; drop table users;", "feishu"])).toEqual(["feishu"]);
|
|
305
|
+
});
|
|
306
|
+
test("whitespace around a key still trims to the key", () => {
|
|
307
|
+
expect(narrowChannelsPatch([" feishu "])).toEqual(["feishu"]);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
describe("computeApplyMode — channels tier (restart)", () => {
|
|
312
|
+
test("channels-only patch → restart (boot forks per-channel workers)", () => {
|
|
313
|
+
expect(computeApplyMode(undefined, {}, ["feishu"])).toBe("restart");
|
|
314
|
+
});
|
|
315
|
+
test("empty-array channels → restart (still a state change)", () => {
|
|
316
|
+
expect(computeApplyMode(undefined, {}, [])).toBe("restart");
|
|
317
|
+
});
|
|
318
|
+
test("no channels key + no flags + no model → restart_only", () => {
|
|
319
|
+
expect(computeApplyMode(undefined, {}, undefined)).toBe("restart_only");
|
|
320
|
+
});
|
|
321
|
+
test("channels + hot-tier flag together stays restart (upgrade rules)", () => {
|
|
322
|
+
expect(computeApplyMode(undefined, { maxTurns: 5 }, ["telegram"])).toBe("restart");
|
|
323
|
+
});
|
|
324
|
+
test("channels + model → restart (model was already restart)", () => {
|
|
325
|
+
expect(computeApplyMode("gpt-5", {}, ["feishu"])).toBe("restart");
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
describe("validatePatch — channels defensive gate", () => {
|
|
330
|
+
test("valid channels array passes", () => {
|
|
331
|
+
expect(validatePatch(undefined, {}, ["telegram", "feishu"])).toBeNull();
|
|
332
|
+
expect(validatePatch(undefined, {}, [])).toBeNull();
|
|
333
|
+
expect(validatePatch(undefined, {}, ["telegram"])).toBeNull();
|
|
334
|
+
});
|
|
335
|
+
test("commhub rejected — RPC transport, not a per-node channel", () => {
|
|
336
|
+
const r = validatePatch(undefined, {}, ["commhub"]);
|
|
337
|
+
expect(r?.field).toBe("channels.commhub");
|
|
338
|
+
});
|
|
339
|
+
test("out-of-allowlist channel rejected (caller bypassed narrowing)", () => {
|
|
340
|
+
const r = validatePatch(undefined, {}, ["wechat"]);
|
|
341
|
+
expect(r?.field).toBe("channels.wechat");
|
|
342
|
+
expect(r?.reason).toMatch(/allowlist/);
|
|
343
|
+
});
|
|
344
|
+
test("non-string entry rejected", () => {
|
|
345
|
+
const r = validatePatch(undefined, {}, [42] as any);
|
|
346
|
+
expect(r?.field).toBe("channels");
|
|
347
|
+
});
|
|
348
|
+
test("non-array rejected", () => {
|
|
349
|
+
const r = validatePatch(undefined, {}, "telegram" as any);
|
|
350
|
+
expect(r?.field).toBe("channels");
|
|
351
|
+
});
|
|
352
|
+
});
|
|
@@ -57,6 +57,35 @@ export const RESTART_REQUIRED_FLAGS = new Set<string>([
|
|
|
57
57
|
"timeout",
|
|
58
58
|
]);
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Channels the dashboard may enable/disable on a node via
|
|
62
|
+
* update_node_config. Intentionally narrow — the panel only exposes
|
|
63
|
+
* enable/disable, per-channel secrets stay in the node's local
|
|
64
|
+
* config.json (see #260 wire discipline).
|
|
65
|
+
*
|
|
66
|
+
* The list mirrors agent-node's actual runtime capability. See
|
|
67
|
+
* agent-node/src/cli.ts:671-676 — CHANNELS are parsed into per-type
|
|
68
|
+
* worker inits (initTelegramChannel + initFeishuChannel), and any
|
|
69
|
+
* other channel type triggers `process.exit(1)` at boot. So the
|
|
70
|
+
* hub-side allow-list here is telegram + feishu ONLY. `commhub` is
|
|
71
|
+
* the RPC transport that every node speaks unconditionally; treating
|
|
72
|
+
* it as a per-node channel would be a UX lie (toggle looks off but
|
|
73
|
+
* commhub is always on).
|
|
74
|
+
*
|
|
75
|
+
* Dashboard PR #31 currently includes `commhub` in its own
|
|
76
|
+
* EDITABLE_CHANNELS whitelist — that follow-up narrowing is filed for
|
|
77
|
+
* the next dashboard rally, and until then hub silently drops any
|
|
78
|
+
* `commhub` entry via narrowChannelsPatch.
|
|
79
|
+
*
|
|
80
|
+
* Not part of SECURITY_SENSITIVE_FLAGS (通信龙 P5 派工): flipping a
|
|
81
|
+
* channel on/off is a lifecycle-tier operation like `restart_node`, not
|
|
82
|
+
* a privilege elevation. Cross-tenant reach is already gated by SEC-1.
|
|
83
|
+
*/
|
|
84
|
+
export const EDITABLE_CHANNELS = new Set<string>([
|
|
85
|
+
"telegram",
|
|
86
|
+
"feishu",
|
|
87
|
+
]);
|
|
88
|
+
|
|
60
89
|
/**
|
|
61
90
|
* Role-gate for the patch's flag set. SEC-2 enforcement lives here.
|
|
62
91
|
*
|
|
@@ -99,22 +128,53 @@ export function isAllowedToChangeFlag(
|
|
|
99
128
|
|
|
100
129
|
/**
|
|
101
130
|
* Classify the patch's required apply mode. Empty patch (model + flags
|
|
102
|
-
* both empty) → "restart_only" (used by restart_node tool).
|
|
103
|
-
* restart-required field present
|
|
131
|
+
* both empty, no channels) → "restart_only" (used by restart_node tool).
|
|
132
|
+
* Any restart-required field present, OR a channels change → "restart"
|
|
133
|
+
* (channels is restart-tier by design: the node reads its channel set
|
|
134
|
+
* once at boot in agent-node/src/cli.ts and forks per-channel workers
|
|
135
|
+
* from it, so a channel enable/disable takes effect on process restart
|
|
136
|
+
* only — see #260 P5 wire discipline). Otherwise → "hot".
|
|
104
137
|
*/
|
|
105
138
|
export function computeApplyMode(
|
|
106
139
|
model: string | undefined,
|
|
107
140
|
flags: Record<string, unknown>,
|
|
141
|
+
channels?: string[] | undefined,
|
|
108
142
|
): "hot" | "restart" | "restart_only" {
|
|
109
|
-
const fieldCount = (model !== undefined ? 1 : 0) + Object.keys(flags).length;
|
|
143
|
+
const fieldCount = (model !== undefined ? 1 : 0) + Object.keys(flags).length + (channels !== undefined ? 1 : 0);
|
|
110
144
|
if (fieldCount === 0) return "restart_only";
|
|
111
145
|
if (model !== undefined) return "restart";
|
|
146
|
+
if (channels !== undefined) return "restart";
|
|
112
147
|
for (const key of Object.keys(flags)) {
|
|
113
148
|
if (RESTART_REQUIRED_FLAGS.has(key)) return "restart";
|
|
114
149
|
}
|
|
115
150
|
return "hot";
|
|
116
151
|
}
|
|
117
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Narrow + allow-list `channels` from untrusted patch JSON. Returns the
|
|
155
|
+
* final channel key list (case-folded, deduped, in EDITABLE_CHANNELS
|
|
156
|
+
* order-preserving), OR null if the input isn't an array. Non-strings,
|
|
157
|
+
* unknown keys, empty strings, and duplicates are silently dropped —
|
|
158
|
+
* same fail-shape as the dashboard route's whitelist so the two sides
|
|
159
|
+
* behave identically end-to-end. Callers pass `undefined` through when
|
|
160
|
+
* the patch didn't include a channels key at all (a distinct case from
|
|
161
|
+
* `channels: []` which means "disable all editable channels").
|
|
162
|
+
*/
|
|
163
|
+
export function narrowChannelsPatch(raw: unknown): string[] | null {
|
|
164
|
+
if (!Array.isArray(raw)) return null;
|
|
165
|
+
const allow = EDITABLE_CHANNELS;
|
|
166
|
+
const seen = new Set<string>();
|
|
167
|
+
const out: string[] = [];
|
|
168
|
+
for (const v of raw) {
|
|
169
|
+
if (typeof v !== "string") continue;
|
|
170
|
+
const key = v.trim().toLowerCase();
|
|
171
|
+
if (!allow.has(key) || seen.has(key)) continue;
|
|
172
|
+
seen.add(key);
|
|
173
|
+
out.push(key);
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
|
|
118
178
|
/**
|
|
119
179
|
* Validate the patch shape — allowlist + per-field range / enum. Returns
|
|
120
180
|
* null on pass, or `{ field, reason }` on reject. Caller wraps in error
|
|
@@ -123,12 +183,30 @@ export function computeApplyMode(
|
|
|
123
183
|
export function validatePatch(
|
|
124
184
|
model: string | undefined,
|
|
125
185
|
flags: Record<string, unknown>,
|
|
186
|
+
channels?: string[] | undefined,
|
|
126
187
|
): { field: string; reason: string } | null {
|
|
127
188
|
if (model !== undefined) {
|
|
128
189
|
if (typeof model !== "string" || model.length === 0 || model.length > 200) {
|
|
129
190
|
return { field: "model", reason: "must be a non-empty string ≤ 200 chars" };
|
|
130
191
|
}
|
|
131
192
|
}
|
|
193
|
+
if (channels !== undefined) {
|
|
194
|
+
// Zod already enforced string[], and narrowChannelsPatch dropped
|
|
195
|
+
// unknown keys before this point — but re-verify defensively so a
|
|
196
|
+
// future caller that skips narrowChannelsPatch can't smuggle
|
|
197
|
+
// unknown keys through validatePatch alone.
|
|
198
|
+
if (!Array.isArray(channels)) {
|
|
199
|
+
return { field: "channels", reason: "must be an array of strings" };
|
|
200
|
+
}
|
|
201
|
+
for (const c of channels) {
|
|
202
|
+
if (typeof c !== "string") {
|
|
203
|
+
return { field: "channels", reason: "must contain only strings" };
|
|
204
|
+
}
|
|
205
|
+
if (!EDITABLE_CHANNELS.has(c)) {
|
|
206
|
+
return { field: `channels.${c}`, reason: "not in editable channels allowlist" };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
132
210
|
for (const [key, val] of Object.entries(flags)) {
|
|
133
211
|
if (!ALLOWED_FLAGS.has(key)) {
|
|
134
212
|
return { field: `flags.${key}`, reason: "not in allowlist" };
|