-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreference_monitor.test.ts
More file actions
352 lines (318 loc) · 14 KB
/
Copy pathreference_monitor.test.ts
File metadata and controls
352 lines (318 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* Tests for v0.12.1 Reference Monitor (Tier 2).
*
* Validates the HTTP API enforcement point for telemetry writes:
* - /api/v1/telemetry/tool_call requires Authorization: Bearer <session_token>
* - The token's bound agent_id MUST match body.agentId (cross-agent forgery blocked)
* - Same for /api/v1/telemetry/outcome (with token-presence check; no per-row agent)
*
* Red-team IDs:
* RT-S2-02: agent A holding token-A cannot POST a row claiming agent B
* RT-S2-03: a missing/malformed Authorization header gets 401
* RT-S2-04: a revoked token gets 401
* RT-S2-05: a token from project X cannot write to project Y
* RT-S2-06: end-to-end via the client helper recordToolCallViaApi
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach } from "vitest";
import { existsSync, unlinkSync, rmSync, mkdtempSync } from "node:fs";
import { join } from "node:path";
import { homedir, tmpdir } from "node:os";
import { createHash, randomBytes } from "node:crypto";
import { DatabaseSync } from "node:sqlite";
import { createApiServer } from "./api-server.js";
import { issueToken, revokeToken } from "./access-control.js";
import type { FastifyInstance as _FI } from "fastify";
import { runMigrations } from "./migrations.js";
import {
_resetCacheForTesting as resetMachineSecret,
MACHINE_SECRET_PATH,
} from "./security/machine_secret.js";
import { _resetPricingVerificationForTesting } from "./pricing.js";
import { _resetTelemetryCacheForTesting, newCallId } from "./telemetry.js";
import {
recordToolCallViaApi,
_resetSessionTokenCacheForTesting,
} from "./telemetry_client.js";
const PRICING_SIG_PATH = join(homedir(), ".claude", "zc-ctx", ".pricing_signature");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let serverApp: any;
let port: number;
let baseUrl: string;
let projectA: string;
let projectB: string;
let testApiKey: string;
function projectDbPath(p: string): string {
const hash = createHash("sha256").update(p).digest("hex").slice(0, 16);
return join(homedir(), ".claude", "zc-ctx", "sessions", hash + ".db");
}
function cleanProjectDb(p: string): void {
for (const sfx of ["", "-wal", "-shm"]) {
try { if (existsSync(projectDbPath(p) + sfx)) unlinkSync(projectDbPath(p) + sfx); } catch {}
}
}
function cleanPricingBaseline(): void {
try { if (existsSync(PRICING_SIG_PATH)) unlinkSync(PRICING_SIG_PATH); } catch {}
}
function cleanMachineSecret(): void {
try { if (existsSync(MACHINE_SECRET_PATH)) unlinkSync(MACHINE_SECRET_PATH); } catch {}
}
/**
* Issue a session_token directly against the project DB (bypassing the API)
* so we can construct test scenarios with controlled tokens.
*/
function mintTokenForTest(projectPath: string, agentId: string, role: string): string {
const dbPath = projectDbPath(projectPath);
const db = new DatabaseSync(dbPath);
db.exec("PRAGMA journal_mode = WAL");
runMigrations(db);
const token = issueToken(db, projectPath, agentId, role as never);
db.close();
return token;
}
beforeAll(async () => {
// Start a local fastify instance on a random port for these tests
port = 13099 + Math.floor(Math.random() * 1000);
testApiKey = randomBytes(32).toString("hex");
process.env.ZC_API_KEY = testApiKey;
baseUrl = `http://localhost:${port}`;
process.env.ZC_API_URL = baseUrl; // tell telemetry_client where to find us
// v0.18.9 — pin ZC_TELEMETRY_MODE=local. With the new 'auto' default, the
// test setup (which sets ZC_API_URL+ZC_API_KEY) would otherwise route every
// intra-test recordToolCall through the same fastify instance — that floods
// the per-IP rate limiter (500/min) and turns later auth-check tests into
// 429s instead of 401s/403s. The reference monitor itself is exercised
// explicitly via fetch() calls; we don't need recordToolCall going via API.
process.env.ZC_TELEMETRY_MODE = "local";
const { app } = await createApiServer();
await new Promise<void>((resolve, reject) => {
app.listen({ port, host: "127.0.0.1" }, (err) => err ? reject(err) : resolve());
});
serverApp = app;
});
afterAll(async () => {
try { await serverApp.close(); } catch {}
delete process.env.ZC_API_KEY;
delete process.env.ZC_API_URL;
delete process.env.ZC_TELEMETRY_MODE;
});
beforeEach(() => {
projectA = mkdtempSync(join(tmpdir(), "zc-rm-A-"));
projectB = mkdtempSync(join(tmpdir(), "zc-rm-B-"));
cleanProjectDb(projectA);
cleanProjectDb(projectB);
cleanMachineSecret();
cleanPricingBaseline();
resetMachineSecret();
_resetPricingVerificationForTesting();
_resetTelemetryCacheForTesting();
_resetSessionTokenCacheForTesting();
});
afterEach(() => {
cleanProjectDb(projectA);
cleanProjectDb(projectB);
cleanMachineSecret();
cleanPricingBaseline();
resetMachineSecret();
_resetPricingVerificationForTesting();
_resetTelemetryCacheForTesting();
_resetSessionTokenCacheForTesting();
try { rmSync(projectA, { recursive: true, force: true }); } catch {}
try { rmSync(projectB, { recursive: true, force: true }); } catch {}
});
describe("Reference Monitor — telemetry endpoints", () => {
// ── Happy path ─────────────────────────────────────────────────────────
it("POST /api/v1/telemetry/tool_call with valid token + matching agentId succeeds", async () => {
const aliceToken = mintTokenForTest(projectA, "alice", "developer");
const callId = newCallId();
const res = await fetch(`${baseUrl}/api/v1/telemetry/tool_call`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${aliceToken}`,
},
body: JSON.stringify({
projectPath: projectA, callId, sessionId: "s1", agentId: "alice",
toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 50, outputTokens: 25, latencyMs: 5, status: "ok",
}),
});
expect(res.status).toBe(200);
const json = await res.json() as { ok: boolean; record?: { call_id?: string } };
expect(json.ok).toBe(true);
expect(json.record?.call_id).toBe(callId);
});
it("POST /api/v1/telemetry/outcome with valid token succeeds", async () => {
const orchToken = mintTokenForTest(projectA, "orchestrator", "orchestrator");
const res = await fetch(`${baseUrl}/api/v1/telemetry/outcome`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${orchToken}`,
},
body: JSON.stringify({
projectPath: projectA,
refType: "tool_call", refId: "fake-call-1",
outcomeKind: "accepted", signalSource: "user_prompt",
confidence: 0.5, evidence: { sentiment: "positive" },
}),
});
expect(res.status).toBe(200);
const json = await res.json() as { ok: boolean; record?: { outcome_kind?: string } };
expect(json.ok).toBe(true);
expect(json.record?.outcome_kind).toBe("accepted");
});
// ── RT-S2-02: cross-agent forgery via API ─────────────────────────────
it("[RT-S2-02] alice's token cannot write a row claiming bob — 403", async () => {
const aliceToken = mintTokenForTest(projectA, "alice", "developer");
const res = await fetch(`${baseUrl}/api/v1/telemetry/tool_call`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${aliceToken}`,
},
body: JSON.stringify({
projectPath: projectA, callId: newCallId(), sessionId: "s2",
agentId: "bob", // ← forgery attempt
toolName: "Edit", model: "claude-sonnet-4-6",
inputTokens: 100, outputTokens: 5, latencyMs: 10, status: "ok",
}),
});
expect(res.status).toBe(403);
const json = await res.json() as { error?: string };
expect(json.error).toMatch(/Agent ID mismatch/i);
expect(json.error).toMatch(/cross-agent forgery blocked/i);
});
// ── RT-S2-03: missing/malformed Auth header ──────────────────────────
it("[RT-S2-03] missing Authorization header is rejected with 401", async () => {
const res = await fetch(`${baseUrl}/api/v1/telemetry/tool_call`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
projectPath: projectA, callId: newCallId(), sessionId: "s",
agentId: "alice", toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 1, outputTokens: 1, latencyMs: 1, status: "ok",
}),
});
expect(res.status).toBe(401);
const json = await res.json() as { error?: string };
expect(json.error).toMatch(/Missing or malformed Authorization header/i);
});
it("[RT-S2-03] malformed Authorization header (no Bearer prefix) is rejected with 401", async () => {
const aliceToken = mintTokenForTest(projectA, "alice", "developer");
const res = await fetch(`${baseUrl}/api/v1/telemetry/tool_call`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": aliceToken, // missing "Bearer " prefix
},
body: JSON.stringify({
projectPath: projectA, callId: newCallId(), sessionId: "s",
agentId: "alice", toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 1, outputTokens: 1, latencyMs: 1, status: "ok",
}),
});
expect(res.status).toBe(401);
});
it("[RT-S2-03] empty Bearer is rejected with 401", async () => {
const res = await fetch(`${baseUrl}/api/v1/telemetry/tool_call`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer ",
},
body: JSON.stringify({
projectPath: projectA, callId: newCallId(), sessionId: "s",
agentId: "alice", toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 1, outputTokens: 1, latencyMs: 1, status: "ok",
}),
});
expect(res.status).toBe(401);
});
// ── RT-S2-04: revoked token ──────────────────────────────────────────
it("[RT-S2-04] a revoked token is rejected with 401", async () => {
const aliceToken = mintTokenForTest(projectA, "alice", "developer");
// Revoke directly in the DB
const dbPath = projectDbPath(projectA);
const db = new DatabaseSync(dbPath);
db.exec("PRAGMA journal_mode = WAL");
// Get the token_id from the token (zcst.<payload>.<sig> — payload is base64 JSON)
const parts = aliceToken.split(".");
const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
revokeToken(db, payload.tid);
db.close();
const res = await fetch(`${baseUrl}/api/v1/telemetry/tool_call`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${aliceToken}`,
},
body: JSON.stringify({
projectPath: projectA, callId: newCallId(), sessionId: "s",
agentId: "alice", toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 1, outputTokens: 1, latencyMs: 1, status: "ok",
}),
});
expect(res.status).toBe(401);
const json = await res.json() as { error?: string };
expect(json.error).toMatch(/invalid|expired|revoked/i);
});
// ── RT-S2-05: cross-project token ────────────────────────────────────
it("[RT-S2-05] a token from project A cannot write to project B", async () => {
const aliceTokenForA = mintTokenForTest(projectA, "alice", "developer");
const res = await fetch(`${baseUrl}/api/v1/telemetry/tool_call`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${aliceTokenForA}`,
},
body: JSON.stringify({
projectPath: projectB, // ← different project
callId: newCallId(), sessionId: "s",
agentId: "alice", toolName: "Read", model: "claude-sonnet-4-6",
inputTokens: 1, outputTokens: 1, latencyMs: 1, status: "ok",
}),
});
expect(res.status).toBe(401);
// verifyToken returns null when project_hash doesn't match (Chapter 11 — scoped capability)
});
// ── RT-S2-06: end-to-end via client helper ───────────────────────────
it("[RT-S2-06] recordToolCallViaApi succeeds end-to-end with valid token", async () => {
const aliceToken = mintTokenForTest(projectA, "alice", "developer");
const callId = newCallId();
const r = await recordToolCallViaApi(
{
callId, sessionId: "s-e2e", agentId: "alice",
projectPath: projectA, toolName: "zc_search",
model: "claude-sonnet-4-6",
inputTokens: 200, outputTokens: 80, latencyMs: 30, status: "ok",
},
aliceToken,
);
expect(r).not.toBeNull();
expect(r!.call_id).toBe(callId);
// And the row really landed in the DB (via Reference Monitor)
const dbPath = projectDbPath(projectA);
const db = new DatabaseSync(dbPath);
const row = db.prepare("SELECT call_id, agent_id, tool_name FROM tool_calls WHERE call_id = ?").get(callId) as { call_id: string; agent_id: string; tool_name: string };
db.close();
expect(row).toBeDefined();
expect(row.agent_id).toBe("alice");
expect(row.tool_name).toBe("zc_search");
});
it("recordToolCallViaApi returns null on cross-agent forgery (server returns 403)", async () => {
const aliceToken = mintTokenForTest(projectA, "alice", "developer");
const r = await recordToolCallViaApi(
{
callId: newCallId(), sessionId: "s-forge", agentId: "bob",
projectPath: projectA, toolName: "Edit",
model: "claude-sonnet-4-6",
inputTokens: 10, outputTokens: 5, latencyMs: 5, status: "ok",
},
aliceToken,
);
expect(r).toBeNull();
});
});
// Note: startServer is the runtime entry point of api-server.ts. We need
// to expose it — see the import. If it's not exported, that's a 1-line
// addition.