-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebhook-adapter.test.ts
More file actions
409 lines (362 loc) · 11.6 KB
/
Copy pathwebhook-adapter.test.ts
File metadata and controls
409 lines (362 loc) · 11.6 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import assert from "node:assert/strict";
import { createHmac } from "node:crypto";
import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import {
buildTaskFromEvent,
createConfig,
handleRequest,
type JsonObject,
} from "../src/adapter.js";
function tempStateDir(): string {
return mkdtempSync(join(tmpdir(), "coven-github-webhook-"));
}
function testConfig(stateDir: string, webhookSecret = "test-webhook-secret") {
return createConfig(
{
COVEN_GITHUB_STATE_DIR: stateDir,
COVEN_GITHUB_POLICY_PATH: join(stateDir, "policy.json"),
GITHUB_WEBHOOK_SECRET: webhookSecret,
},
process.cwd(),
);
}
function legacySecretConfig(stateDir: string, webhookSecret = "legacy-webhook-secret") {
return createConfig(
{
COVEN_GITHUB_STATE_DIR: stateDir,
COVEN_GITHUB_POLICY_PATH: join(stateDir, "policy.json"),
WEBHOOK_SECRET: webhookSecret,
},
process.cwd(),
);
}
function signature(secret: string, body: Buffer): string {
return `sha256=${createHmac("sha256", secret).update(body).digest("hex")}`;
}
async function callWebhook(
body: Buffer,
headers: Record<string, string> = {},
contentLength: string | null | "auto" = "auto",
config = testConfig(tempStateDir()),
) {
const requestHeaders = new Map<string, string>();
if (contentLength === "auto") {
requestHeaders.set("content-length", String(body.length));
} else if (contentLength !== null) {
requestHeaders.set("content-length", contentLength);
}
for (const [name, value] of Object.entries(headers)) {
requestHeaders.set(name.toLowerCase(), value);
}
return handleRequest(config, {
method: "POST",
path: "/webhook",
headers: requestHeaders,
rawBody: body,
});
}
test("webhook rejects missing and invalid signatures", async () => {
const body = Buffer.from('{"zen":"Keep it logically awesome."}');
const config = testConfig(tempStateDir());
const missing = await callWebhook(
body,
{"X-GitHub-Event": "ping", "X-GitHub-Delivery": "delivery-1"},
"auto",
config,
);
assert.equal(missing.status, 401);
assert.equal(missing.body.error, "missing signature");
const invalid = await callWebhook(
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-2",
"X-Hub-Signature-256": "sha256=deadbeef",
},
"auto",
config,
);
assert.equal(invalid.status, 401);
assert.equal(invalid.body.error, "invalid signature");
});
test("webhook accepts valid signed ping without runtime", async () => {
const secret = "valid-webhook-secret";
const config = testConfig(tempStateDir(), secret);
const body = Buffer.from('{"zen":"Keep it logically awesome."}');
const response = await callWebhook(
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-3",
"X-Hub-Signature-256": signature(secret, body),
},
"auto",
config,
);
assert.equal(response.status, 200);
assert.equal(response.body.ok, true);
assert.equal(response.body.action, "ignored");
assert.equal(response.body.reason, "no_policy_for_installation_repo");
});
test("webhook reads body when content length is missing", async () => {
const secret = "missing-length-secret";
const config = testConfig(tempStateDir(), secret);
const body = Buffer.from('{"zen":"Keep it logically awesome."}');
const response = await callWebhook(
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-missing-length",
"X-Hub-Signature-256": signature(secret, body),
},
null,
config,
);
assert.equal(response.status, 200);
assert.equal(response.body.ok, true);
});
test("webhook reads body when content length is unparsable", async () => {
const secret = "bad-length-secret";
const config = testConfig(tempStateDir(), secret);
const body = Buffer.from('{"zen":"Keep it logically awesome."}');
const response = await callWebhook(
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-bad-length",
"X-Hub-Signature-256": signature(secret, body),
},
"not-a-number",
config,
);
assert.equal(response.status, 200);
assert.equal(response.body.ok, true);
});
test("webhook treats partially numeric content length as unparsable", async () => {
const secret = "partial-length-secret";
const config = testConfig(tempStateDir(), secret);
const body = Buffer.from('{"zen":"Keep it logically awesome."}');
const response = await callWebhook(
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-partial-length",
"X-Hub-Signature-256": signature(secret, body),
},
"12oops",
config,
);
assert.equal(response.status, 200);
assert.equal(response.body.ok, true);
});
test("webhook treats zero content length as empty body", async () => {
const secret = "zero-length-secret";
const config = testConfig(tempStateDir(), secret);
const response = await callWebhook(
Buffer.from('{"zen":"Keep it logically awesome."}'),
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-zero-length",
"X-Hub-Signature-256": signature(secret, Buffer.alloc(0)),
},
"0",
config,
);
assert.equal(response.status, 400);
assert.equal(response.body.error, "invalid json");
});
test("webhook rejects oversized content length before signature check", async () => {
const response = await callWebhook(
Buffer.alloc(0),
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-large-body",
"X-Hub-Signature-256": "sha256=deadbeef",
},
String(10 * 1024 * 1024 + 1),
);
assert.equal(response.status, 413);
assert.equal(response.body.error, "payload too large");
});
test("webhook signature allows surrounding whitespace", async () => {
const secret = "whitespace-secret";
const config = testConfig(tempStateDir(), secret);
const body = Buffer.from('{"zen":"Keep it logically awesome."}');
const response = await callWebhook(
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-whitespace-signature",
"X-Hub-Signature-256": ` ${signature(secret, body)} `,
},
"auto",
config,
);
assert.equal(response.status, 200);
assert.equal(response.body.ok, true);
});
test("webhook reports missing secret as server misconfiguration", async () => {
const config = testConfig(tempStateDir(), "");
const body = Buffer.from('{"zen":"Keep it logically awesome."}');
const response = await callWebhook(
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-missing-secret",
"X-Hub-Signature-256": signature("ignored", body),
},
"auto",
config,
);
assert.equal(response.status, 500);
assert.equal(response.body.error, "webhook secret not configured");
});
test("webhook secret supports smoke script environment name", async () => {
const secret = "legacy-webhook-secret";
const config = legacySecretConfig(tempStateDir(), secret);
const body = Buffer.from('{"zen":"Keep it logically awesome."}');
const response = await callWebhook(
body,
{
"X-GitHub-Event": "ping",
"X-GitHub-Delivery": "delivery-legacy",
"X-Hub-Signature-256": signature(secret, body),
},
"auto",
config,
);
assert.equal(response.status, 200);
assert.equal(response.body.ok, true);
});
test("config accepts an inline GitHub App private key from env", () => {
const config = createConfig(
{
GITHUB_APP_PRIVATE_KEY: "-----BEGIN PRIVATE KEY-----\nexample\n-----END PRIVATE KEY-----",
},
process.cwd(),
);
assert.equal(config.privateKeyPem, "-----BEGIN PRIVATE KEY-----\nexample\n-----END PRIVATE KEY-----");
});
test("missing familiar policy does not fall back to hardcoded installation", () => {
const task = buildTaskFromEvent(
"issues",
"delivery-4",
{
action: "opened",
installation: {id: 111},
repository: {
id: 222,
full_name: "OpenCoven/example",
clone_url: "https://github.com/OpenCoven/example.git",
default_branch: "main",
},
issue: {number: 7, title: "Fix it", body: "Please fix it."},
} as JsonObject,
{
trigger_labels: ["coven:fix"],
bot_usernames: ["coven-github[bot]"],
publication: {mode: "record_only"},
} as JsonObject,
);
assert.equal(task.state, "ignored");
assert.equal(task.ignored_reason, "missing_familiar_policy");
});
test("example policy routes a labeled issue to the configured familiar", () => {
const policyFile = new URL("../config/example-policy.json", import.meta.url);
const policyRoot = JSON.parse(readFileSync(policyFile, "utf8")) as JsonObject;
const installation = (policyRoot.installations as JsonObject)["123456"] as JsonObject;
const policy = ((installation.repositories as JsonObject)["987654321"]) as JsonObject;
const task = buildTaskFromEvent(
"issues",
"delivery-example-policy",
{
action: "labeled",
installation: {id: 123456},
repository: {
id: 987654321,
full_name: "OpenCoven/example",
clone_url: "https://github.com/OpenCoven/example.git",
default_branch: "main",
},
issue: {
number: 42,
title: "Wire the app",
body: "Make the first app route functional.",
labels: [{name: "coven:fix"}],
},
} as JsonObject,
policy,
);
assert.equal(task.state, "queued");
assert.equal(task.trigger, "issue_mention");
assert.deepEqual(task.task, {
kind: "fix_issue",
issue_number: 42,
issue_title: "Wire the app",
issue_body: "Make the first app route functional.",
});
assert.deepEqual(task.familiar, {
id: "cody",
display_name: "Cody",
model: "openai/gpt-5.5",
skills: ["systematic-debugging", "test-driven-development"],
});
});
test("demo mode handles a signed labeled issue without external GitHub calls", async () => {
const secret = "demo-route-secret";
const stateDir = tempStateDir();
const policyPath = join(stateDir, "policy.json");
writeFileSync(
policyPath,
readFileSync(new URL("../config/example-policy.json", import.meta.url)),
);
const config = createConfig(
{
COVEN_GITHUB_DEMO_MODE: "1",
COVEN_GITHUB_STATE_DIR: stateDir,
COVEN_GITHUB_POLICY_PATH: policyPath,
GITHUB_WEBHOOK_SECRET: secret,
},
process.cwd(),
);
const body = Buffer.from(JSON.stringify({
action: "labeled",
installation: {id: 123456},
repository: {
id: 987654321,
full_name: "OpenCoven/example",
clone_url: "https://github.com/OpenCoven/example.git",
default_branch: "main",
},
issue: {
number: 42,
title: "Wire the app",
body: "Make the first app route functional.",
labels: [{name: "coven:fix"}],
},
}));
const response = await callWebhook(
body,
{
"X-GitHub-Event": "issues",
"X-GitHub-Delivery": "delivery-demo-mode",
"X-Hub-Signature-256": signature(secret, body),
},
"auto",
config,
);
assert.equal(response.status, 200);
assert.equal(response.body.action, "accepted");
assert.equal(response.body.state, "completed");
const taskPath = join(stateDir, "tasks", "delivery-demo-mode.json");
assert.equal(existsSync(taskPath), true);
const task = JSON.parse(readFileSync(taskPath, "utf8")) as JsonObject;
assert.equal(task.state, "completed");
assert.equal(task.demo_mode, true);
assert.equal(task.publication_state, "demo_mode_no_github_calls");
assert.equal(existsSync(String(task.session_brief_path)), true);
assert.equal(existsSync(String(task.result_path)), true);
});