Multiplayer agent harness for work.
Tighten webhook registration checks (#1922)
Joshua France8ff4a4e77088
diff --git a/src/api/control-service.ts b/src/api/control-service.tsindex e8191dc77..28a36a4a7 100644--- a/src/api/control-service.ts+++ b/src/api/control-service.ts@@ -83,7 +83,7 @@ export interface WebhookCreateRequest {export type WebhookCreateResult =| { ok: true; webhook: Webhook; url: string; secret?: string }- | { ok: false; code: "bad_request" | "unknown_destination" | "webhook_create_failed"; message: string };+ | { ok: false; code: "bad_request" | "forbidden" | "unknown_destination" | "webhook_create_failed"; message: string };export interface CronPatchRequest {runtime?: Cron["runtime"];@@ -809,6 +809,13 @@ export function createControlService(app: App, scheduler?: Scheduler, admin?: Ad},async createWebhook(req, capability, publicBase): Promise<WebhookCreateResult> {+ if (capability.triggered || !livePersonCapability(capability)) {+ return {+ ok: false,+ code: "forbidden",+ message: "webhook registration requires a live turn started by its owner",+ };+ }const resolved = resolveCapabilityDestination(capability, req.destinationKey);if (!resolved.ok) {return {diff --git a/src/api/routes/webhooks.ts b/src/api/routes/webhooks.tsindex 1131fbe3b..bfc20beb3 100644--- a/src/api/routes/webhooks.ts+++ b/src/api/routes/webhooks.ts@@ -80,6 +80,7 @@ async function incomingWebhook(ctx: BaseCtx): Promise<void> {const WEBHOOK_ERROR_STATUS: Record<string, number> = {bad_request: 400,+ forbidden: 403,unknown_destination: 400,webhook_create_failed: 400,};diff --git a/test/capability-routes.test.ts b/test/capability-routes.test.tsindex 6d1fc74b2..c617184db 100644--- a/test/capability-routes.test.ts+++ b/test/capability-routes.test.ts@@ -44,7 +44,7 @@ describe("capability-token control plane (crons + webhooks + SOUL)", () => {audienceScopeId: scopeId("channel", "C"),label: "#eng (the whole channel)",} as const;- const capChannel = async (actorId: string) =>+ const capChannel = async (actorId: string, extra: Partial<CapabilityClaims> = {}) =>await mintCapabilityToken({actorId,@@ -53,6 +53,7 @@ describe("capability-token control plane (crons + webhooks + SOUL)", () => {destinations: [THREAD, ROOT],defaultDestinationKey: THREAD.key,exp: Date.now() + CAPABILITY_TTL_MS,+ ...extra,},SECRET,);@@ -814,6 +815,30 @@ describe("capability-token control plane (crons + webhooks + SOUL)", () => {);});+ it("refuses webhook registration without a live person in personal and channel scopes", async () => {+ const webhooksBefore = await built.app.listWebhooks();+ const auditBefore = await built.auditLog.events();+ for (const scope of [scopeId("personal", "U1"), scopeId("channel", "C")]) {+ for (const extra of [+ {},+ { liveActor: false, liveAuthor: false },+ { triggered: true },+ { triggered: true, liveActor: true },+ { triggered: true, liveAuthor: true },+ ]) {+ const res = await post(+ "/v1/webhooks",+ { action: "handle the event", verification: { scheme: "hmac-sha256", secret: "secret" } },+ { "x-agent-capability": await capFor("U1", scope, { aud: "control-plane", ...extra }) },+ );+ assert.equal(res.status, 403, JSON.stringify({ scope, extra }));+ assert.equal(((await res.json()) as { error: string }).error, "forbidden");+ assert.deepEqual(await built.app.listWebhooks(), webhooksBefore);+ assert.deepEqual(await built.auditLog.events(), auditBefore);+ }+ }+ });+it("registers a webhook as the TOKEN's actor, ignoring a forged owner in the body", async () => {const res = await post("/v1/webhooks",@@ -825,7 +850,7 @@ describe("capability-token control plane (crons + webhooks + SOUL)", () => {ownerScopeId: "personal:U2",destination: { type: "evil", target: "attacker" },},- { "x-agent-capability": await capFor("U1") },+ { "x-agent-capability": await capFor("U1", scopeId("personal", "U1"), { liveActor: true }) },);assert.equal(res.status, 200);const { webhook, url } = (await res.json()) as any;@@ -842,7 +867,7 @@ describe("capability-token control plane (crons + webhooks + SOUL)", () => {it("registers a destination-less webhook when the token carries no destination (side-effect-only)", async () => {const noDest = await mintCapabilityToken(- { actorId: "U9", scopeId: "personal:U9", exp: Date.now() + CAPABILITY_TTL_MS },+ { actorId: "U9", scopeId: "personal:U9", liveAuthor: true, exp: Date.now() + CAPABILITY_TTL_MS },SECRET,);const res = await post(@@ -865,7 +890,7 @@ describe("capability-token control plane (crons + webhooks + SOUL)", () => {destinationKey: "k-root",destination: { type: "slack", target: "C", audienceScopeId: "personal:U1" },},- { "x-agent-capability": await capChannel("U1") },+ { "x-agent-capability": await capChannel("U1", { liveActor: true }) },);assert.equal(res.status, 200);const { webhook } = (await res.json()) as any;@@ -882,7 +907,7 @@ describe("capability-token control plane (crons + webhooks + SOUL)", () => {verification: { scheme: "hmac-sha256", secret: "unknown-destination-secret" },destinationKey: "k-not-in-set",},- { "x-agent-capability": await capChannel("U1") },+ { "x-agent-capability": await capChannel("U1", { liveActor: true }) },);assert.equal(res.status, 400);assert.equal(((await res.json()) as any).error, "unknown_destination");@@ -892,7 +917,7 @@ describe("capability-token control plane (crons + webhooks + SOUL)", () => {await post("/v1/webhooks",{ action: "u2 hook", verification: { scheme: "hmac-sha256", secret: "s2" } },- { "x-agent-capability": await capFor("U2") },+ { "x-agent-capability": await capFor("U2", scopeId("personal", "U2"), { liveActor: true }) },);const mine = (await (await get("/v1/webhooks", { "x-agent-capability": await capFor("U1") })).json()) as any;assert.ok(mine.webhooks.length >= 1);@@ -908,7 +933,7 @@ describe("capability-token control plane (crons + webhooks + SOUL)", () => {await post("/v1/webhooks",{ action: "u1 owned", verification: { scheme: "hmac-sha256", secret: "owned-secret" } },- { "x-agent-capability": await capFor("U1") },+ { "x-agent-capability": await capFor("U1", scopeId("personal", "U1"), { liveActor: true }) },)).json()) as any;const forbidden = await post(diff --git a/test/control-service.test.ts b/test/control-service.test.tsindex 8d6e20edd..1b4af87d5 100644--- a/test/control-service.test.ts+++ b/test/control-service.test.ts@@ -986,7 +986,7 @@ test("webhook create surfaces the inbound url + the secret verbatim; list elidesverification: { scheme: "github", secret: "shh-secret" },filters: [{ path: "action", in: ["opened"] }],},- claims("U1"),+ claims("U1", scopeId("personal", "U1"), { liveActor: true }),"https://portal.example",);assert.ok(r.ok, JSON.stringify(r));diff --git a/test/control-tool-context.test.ts b/test/control-tool-context.test.tsindex 322c311a6..48f26d991 100644--- a/test/control-tool-context.test.ts+++ b/test/control-tool-context.test.ts@@ -82,6 +82,44 @@ test("createToolContext.cronCreate creates a real cron through the shared servicassert.equal(stored.title, "digest");});+test("webhook registration through the tool requires a live person and leaves refused calls without effects", async () => {+ const { built, control } = build();+ const request = { action: "handle the event", verification: { scheme: "hmac-sha256" as const, secret: "secret" } };+ const auditBefore = await built.auditLog.events();+ for (const scope of ["personal:U1", "channel:C1"] as const) {+ for (const extra of [+ {},+ { liveActor: false, liveAuthor: false },+ { triggered: true },+ { triggered: true, liveActor: true },+ { triggered: true, liveAuthor: true },+ ]) {+ const ctx = ctxFor({ control, controlClaims: { ...claims("U1"), scopeId: scope, ...extra } });+ const result = await ctx.webhookCreate(request);+ assert.equal(result.ok, false, JSON.stringify({ scope, extra }));+ assert.equal(result.ok ? undefined : result.code, "forbidden");+ assert.deepEqual(await built.app.listWebhooks(), []);+ assert.deepEqual(await built.auditLog.events(), auditBefore);+ }+ }+ for (const extra of [{ liveActor: true }, { liveAuthor: true }]) {+ const ctx = ctxFor({ control, controlClaims: { ...claims("U1"), ...extra } });+ const result = await ctx.webhookCreate(request);+ assert.ok(result.ok, JSON.stringify(result));+ assert.equal(result.webhook.owner, "U1");+ assert.equal(result.secret, "secret");+ }+ const [webhook] = await built.app.listWebhooks();+ await built.app.setWebhookEnabled(webhook!.id, false);+ const disabled = await built.app.listWebhooks();+ const auditAfter = await built.auditLog.events();+ const ctx = ctxFor({ control, controlClaims: { ...claims("U1"), triggered: true } });+ const result = await ctx.webhookCreate(request);+ assert.equal(result.ok ? undefined : result.code, "forbidden");+ assert.deepEqual(await built.app.listWebhooks(), disabled);+ assert.deepEqual(await built.auditLog.events(), auditAfter);+});+test("ledger replay: a cron create is CACHED — a crash-replay does NOT create a second cron", async () => {const { built, control } = build();const { ledger } = memoryLedger();