diff --git a/src/index.ts b/src/index.ts index a9919f0..4d7eef6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,12 +9,14 @@ import { } from "./pipelines"; import { secretXrpc } from "./secrets"; import { buildkiteWebhook } from "./webhook"; +import { webhookXrpc } from "./webhooks"; import { XrpcError } from "./xrpc"; export { Logs } from "./logs"; export { Runner } from "./runner"; export { Secrets } from "./secrets"; export { Store } from "./store"; +export { Webhooks } from "./webhooks"; const corsHeaders = { "Access-Control-Allow-Origin": "*", @@ -99,6 +101,13 @@ async function xrpc( return await secretXrpc("listSecrets", request, env, ctx.exports); case "org.tangled.secret.removeSecret": return await secretXrpc("removeSecret", request, env, ctx.exports); + case "org.tangled.webhook.createWebhook": + case "org.tangled.webhook.updateWebhook": + case "org.tangled.webhook.deleteWebhook": + case "org.tangled.webhook.listWebhooks": + case "org.tangled.webhook.listWebhookDeliveries": + case "org.tangled.webhook.retryWebhookDelivery": + return await webhookXrpc(method, request, env, ctx.exports); case "org.tangled.event.notifyEvent": return await notifyEvent(request, env, ctx.exports); case "org.tangled.ci.retryPipeline": diff --git a/src/notify.ts b/src/notify.ts index a3b9875..65497f3 100644 --- a/src/notify.ts +++ b/src/notify.ts @@ -365,7 +365,10 @@ export function authorize({ claims, repo, env }: Pick { +export async function claimToken({ + claims, + store, +}: Pick): Promise { if (claims.jti === undefined || claims.exp - Date.now() / 1000 > maxTokenLifetimeSeconds) { throw new XrpcError(403, "Auth", "token has no jti or lives too long"); } diff --git a/src/webhooks.ts b/src/webhooks.ts new file mode 100644 index 0000000..8249da5 --- /dev/null +++ b/src/webhooks.ts @@ -0,0 +1,697 @@ +import { DurableObject } from "cloudflare:workers"; +import { authenticate } from "./auth"; +import { authorize, claimToken, servedRepo } from "./notify"; +import { errorMessage, invalidRequest, isDid, isObject, XrpcError } from "./xrpc"; + +const eventNames = new Set([ + "push", + "repository:renamed", + "pull_request:created", + "pull_request:resubmitted", + "pull_request:merged", + "pull_request:closed", + "pull_request:reopened", +]); +const maxAttempts = 3; +const maxResponseBytes = 10 * 1024; +const kept = 50; +const pushTtlMs = 7 * 24 * 60 * 60_000; +const batch = 20; + +export interface Hook { + url: string; + secret: string; + active: boolean; + events: string[]; +} + +export interface Repository { + name: string; + full_name: string; + description: string; + fork: boolean; + html_url: string; + clone_url: string; + ssh_url: string; + created_at: string; + updated_at: string; + owner: { did: string }; +} + +// The parts of a pull_request payload that come from the ticket. +export interface Pull { + title: string; + body: string; + targetBranch: string; + author: string; + createdAt: string; + sender: string; + repository: Repository; +} + +export interface PatchHead { + rkey: string; + head: string; + branch: string; + repo: string; +} + +type Job = { + id: string; + hook: number; + event: string; + body: string; + agent: string; + attempts: number; +}; + +interface Attempt { + code: number | null; + response: Uint8Array; + retry: boolean; +} + +export const ticketEvents: Record = { + open: { state: "open", event: "pull_request:reopened" }, + closed: { state: "closed", event: "pull_request:closed" }, + completed: { state: "merged", event: "pull_request:merged" }, +}; + +export class Webhooks extends DurableObject { + constructor(ctx: DurableObjectState, env: Env) { + super(ctx, env); + ctx.storage.sql.exec( + `CREATE TABLE IF NOT EXISTS hooks ( + id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT NOT NULL, secret TEXT NOT NULL, + active INTEGER NOT NULL, events TEXT NOT NULL, created_at TEXT NOT NULL, + updated_at TEXT NOT NULL + ); + CREATE TABLE IF NOT EXISTS jobs ( + id TEXT PRIMARY KEY, hook INTEGER NOT NULL, event TEXT NOT NULL, body TEXT NOT NULL, + agent TEXT NOT NULL, attempts INTEGER NOT NULL DEFAULT 0, due INTEGER NOT NULL + ); + CREATE TABLE IF NOT EXISTS deliveries ( + seq INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT NOT NULL, hook INTEGER NOT NULL, + event TEXT NOT NULL, url TEXT NOT NULL, body TEXT NOT NULL, code INTEGER, + response BLOB NOT NULL, success INTEGER NOT NULL, created_at TEXT NOT NULL + ); + CREATE TABLE IF NOT EXISTS pushes (key TEXT PRIMARY KEY, at INTEGER NOT NULL); + CREATE TABLE IF NOT EXISTS tickets (rkey TEXT PRIMARY KEY, state TEXT NOT NULL); + CREATE TABLE IF NOT EXISTS patches ( + rkey TEXT PRIMARY KEY, ticket TEXT NOT NULL, head TEXT NOT NULL, + branch TEXT NOT NULL, repo TEXT NOT NULL + )`, + ); + } + + create(hook: Hook): string { + const now = new Date().toISOString(); + return String( + this.ctx.storage.sql + .exec<{ id: number }>( + `INSERT INTO hooks (url, secret, active, events, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, ?) RETURNING id`, + hook.url, + hook.secret, + Number(hook.active), + JSON.stringify(hook.events), + now, + now, + ) + .one().id, + ); + } + + update(id: number, changes: { [K in keyof Hook]?: Hook[K] | undefined }): boolean { + return ( + this.ctx.storage.sql.exec( + `UPDATE hooks SET url = coalesce(?, url), secret = coalesce(?, secret), + active = coalesce(?, active), events = coalesce(?, events), + updated_at = ? + WHERE id = ?`, + changes.url ?? null, + changes.secret ?? null, + changes.active === undefined ? null : Number(changes.active), + changes.events === undefined ? null : JSON.stringify(changes.events), + new Date().toISOString(), + id, + ).rowsWritten > 0 + ); + } + + remove(id: number): boolean { + const sql = this.ctx.storage.sql; + sql.exec("DELETE FROM jobs WHERE hook = ?", id); + sql.exec("DELETE FROM deliveries WHERE hook = ?", id); + return sql.exec("DELETE FROM hooks WHERE id = ?", id).rowsWritten > 0; + } + + list(): object[] { + return this.ctx.storage.sql + .exec<{ + id: number; + url: string; + active: number; + events: string; + createdAt: string; + updatedAt: string; + }>( + "SELECT id, url, active, events, created_at AS createdAt, updated_at AS updatedAt FROM hooks ORDER BY id", + ) + .toArray() + .map((row) => ({ + id: String(row.id), + url: row.url, + active: row.active === 1, + events: JSON.parse(row.events) as unknown, + createdAt: row.createdAt, + updatedAt: row.updatedAt, + })); + } + + deliveries(ids: number[], limit: number): object[] { + return this.ctx.storage.sql + .exec<{ + id: string; + hook: number; + event: string; + url: string; + body: string; + code: number | null; + response: ArrayBuffer; + success: number; + createdAt: string; + }>( + `SELECT id, hook, event, url, body, code, response, success, created_at AS createdAt + FROM (SELECT *, row_number() OVER (PARTITION BY hook ORDER BY seq DESC) AS n + FROM deliveries WHERE hook IN (SELECT value FROM json_each(?))) + WHERE n <= ? ORDER BY hook, seq DESC`, + JSON.stringify(ids), + limit, + ) + .toArray() + .map((row) => { + const delivery: Record = { + id: row.id, + webhook: String(row.hook), + event: row.event, + url: row.url, + success: row.success === 1, + createdAt: row.createdAt, + }; + if (row.body !== "") { + delivery["requestBody"] = bytes(new TextEncoder().encode(row.body)); + } + if (row.code !== null) { + delivery["responseCode"] = row.code; + } + if (row.response.byteLength > 0) { + delivery["responseBody"] = bytes(new Uint8Array(row.response)); + } + return delivery; + }); + } + + async retry(id: number, delivery: string): Promise { + const sql = this.ctx.storage.sql; + if (sql.exec("SELECT 1 FROM hooks WHERE id = ?", id).toArray().length === 0) { + return "WebhookNotFound"; + } + const [stored] = sql + .exec<{ event: string; body: string }>( + "SELECT event, body FROM deliveries WHERE hook = ? AND id = ?", + id, + delivery, + ) + .toArray(); + if (stored === undefined) { + return "DeliveryNotFound"; + } + this.#enqueue(id, stored.event, stored.body, "Tangled-Hook/retry"); + await this.ctx.storage.setAlarm(Date.now()); + return undefined; + } + + subscribed(wanted: string[]): boolean { + return this.#subscribers(wanted).length > 0; + } + + async firePush(key: string, payload: object, agent: string): Promise { + const sql = this.ctx.storage.sql; + const now = Date.now(); + sql.exec("DELETE FROM pushes WHERE at < ?", now - pushTtlMs); + if ( + sql.exec("INSERT INTO pushes VALUES (?, ?) ON CONFLICT DO NOTHING", key, now) + .rowsWritten > 0 + ) { + await this.#fire("push", payload, agent); + } + } + + async patch(ticket: string, patch: PatchHead, pull?: Pull): Promise { + const sql = this.ctx.storage.sql; + sql.exec("INSERT INTO tickets VALUES (?, 'open') ON CONFLICT DO NOTHING", ticket); + const added = sql.exec( + "INSERT INTO patches VALUES (?, ?, ?, ?, ?) ON CONFLICT DO NOTHING", + patch.rkey, + ticket, + patch.head, + patch.branch, + patch.repo, + ).rowsWritten; + if (added > 0 && pull !== undefined) { + const rounds = sql + .exec<{ n: number }>("SELECT count(*) AS n FROM patches WHERE ticket = ?", ticket) + .one().n; + const event = rounds === 1 ? "pull_request:created" : "pull_request:resubmitted"; + await this.#firePull(event, ticket, "open", pull); + } + } + + async ticket(ticket: string, state: string, pull?: Pull): Promise { + const previous = this.ctx.storage.sql + .exec<{ state: string }>("SELECT state FROM tickets WHERE rkey = ?", ticket) + .toArray()[0]?.state; + this.ctx.storage.sql.exec( + "INSERT INTO tickets VALUES (?, ?) ON CONFLICT DO UPDATE SET state = excluded.state", + ticket, + state, + ); + const change = ticketEvents[state]; + if ( + previous === undefined || + previous === state || + pull === undefined || + change === undefined + ) { + return; + } + await this.#firePull(change.event, ticket, change.state, pull); + } + + async #firePull(event: string, ticket: string, state: string, pull: Pull): Promise { + const sql = this.ctx.storage.sql; + const rounds = sql + .exec<{ n: number }>("SELECT count(*) AS n FROM patches WHERE ticket = ?", ticket) + .one().n; + const head = sql + .exec<{ + head: string; + branch: string; + repo: string; + }>( + "SELECT head, branch, repo FROM patches WHERE ticket = ? ORDER BY rowid DESC LIMIT 1", + ticket, + ) + .toArray()[0]; + const payload = { + action: event.slice("pull_request:".length), + pull_request: { + number: 0, + title: pull.title, + body: pull.body, + state, + target_branch: pull.targetBranch, + ...(head === undefined + ? {} + : { + source: { + branch: head.branch, + ...(head.repo === "" ? {} : { repo: head.repo }), + sha: head.head, + }, + }), + round_number: rounds, + owner: { did: pull.author }, + created_at: pull.createdAt, + }, + repository: pull.repository, + sender: { did: pull.sender }, + }; + await this.#fire(event, payload, "Tangled-Hook/pull_request"); + } + + #subscribers(wanted: string[]): number[] { + return this.ctx.storage.sql + .exec<{ id: number }>( + `SELECT DISTINCT h.id FROM hooks h, json_each(h.events) e + WHERE h.active = 1 AND e.value IN (SELECT value FROM json_each(?))`, + JSON.stringify(wanted), + ) + .toArray() + .map(({ id }) => id); + } + + async #fire(event: string, payload: object, agent: string): Promise { + const hooks = this.#subscribers([event]); + if (hooks.length === 0) { + return; + } + const body = JSON.stringify(payload); + for (const hook of hooks) { + this.#enqueue(hook, event, body, agent); + } + await this.ctx.storage.setAlarm(Date.now()); + } + + #enqueue(hook: number, event: string, body: string, agent: string): void { + this.ctx.storage.sql.exec( + "INSERT INTO jobs (id, hook, event, body, agent, due) VALUES (?, ?, ?, ?, ?, ?)", + crypto.randomUUID(), + hook, + event, + body, + agent, + Date.now(), + ); + } + + override async alarm(): Promise { + const sql = this.ctx.storage.sql; + const jobs = sql + .exec( + "SELECT id, hook, event, body, agent, attempts FROM jobs WHERE due <= ? ORDER BY due LIMIT ?", + Date.now(), + batch, + ) + .toArray(); + await Promise.all(jobs.map((job) => this.#send(job))); + const next = sql.exec<{ due: number | null }>("SELECT min(due) AS due FROM jobs").one().due; + if (next !== null) { + await this.ctx.storage.setAlarm(Math.max(next, Date.now())); + } + } + + async #send(job: Job): Promise { + const sql = this.ctx.storage.sql; + const [hook] = sql + .exec<{ url: string; secret: string }>( + "SELECT url, secret FROM hooks WHERE id = ?", + job.hook, + ) + .toArray(); + if (hook === undefined) { + sql.exec("DELETE FROM jobs WHERE id = ?", job.id); + return; + } + // Counted first, so a crash during the request still uses up an attempt. + const attempts = job.attempts + 1; + sql.exec("UPDATE jobs SET attempts = ? WHERE id = ?", attempts, job.id); + const result = + job.attempts >= maxAttempts + ? { code: null, response: encode("delivery interrupted"), retry: false } + : await post(hook, job); + if (result.retry && attempts < maxAttempts) { + sql.exec( + "UPDATE jobs SET due = ? WHERE id = ?", + Date.now() + 1000 * 2 ** (attempts - 1), + job.id, + ); + return; + } + this.ctx.storage.transactionSync(() => { + sql.exec( + `INSERT INTO deliveries (id, hook, event, url, body, code, response, success, created_at) + SELECT ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM hooks WHERE id = ?)`, + job.id, + job.hook, + job.event, + hook.url, + job.body, + result.code, + result.response, + Number(result.code !== null && result.code >= 200 && result.code < 300), + new Date().toISOString(), + job.hook, + ); + sql.exec("DELETE FROM jobs WHERE id = ?", job.id); + sql.exec( + `DELETE FROM deliveries WHERE hook = ? AND seq NOT IN + (SELECT seq FROM deliveries WHERE hook = ? ORDER BY seq DESC LIMIT ?)`, + job.hook, + job.hook, + kept, + ); + }); + } +} + +function bytes(data: Uint8Array): { $bytes: string } { + return { $bytes: data.toBase64({ omitPadding: true }) }; +} + +function encode(text: string): Uint8Array { + return new TextEncoder().encode(text); +} + +async function sign(secret: string, body: string): Promise { + const key = await crypto.subtle.importKey( + "raw", + new TextEncoder().encode(secret), + { name: "HMAC", hash: "SHA-256" }, + false, + ["sign"], + ); + const mac = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(body)); + return `sha256=${new Uint8Array(mac).toHex()}`; +} + +async function readHead(response: Response): Promise { + const reader = response.body?.getReader(); + if (reader === undefined) { + return new Uint8Array(); + } + const head = new Uint8Array(maxResponseBytes); + let size = 0; + try { + while (size < maxResponseBytes) { + // oxlint-disable-next-line no-await-in-loop + const chunk = await reader.read(); + if (chunk.done || !(chunk.value instanceof Uint8Array)) { + break; + } + const part = chunk.value.subarray(0, maxResponseBytes - size); + head.set(part, size); + size += part.length; + } + } catch { + // The receiver already answered, so a broken body does not fail the delivery. + } + await reader.cancel().catch(() => {}); + return head.slice(0, size); +} + +async function post(hook: { url: string; secret: string }, job: Job): Promise { + let repo = ""; + try { + const parsed: unknown = JSON.parse(job.body); + const named = + isObject(parsed) && isObject(parsed["repository"]) ? parsed["repository"] : {}; + repo = typeof named["full_name"] === "string" ? named["full_name"] : ""; + } catch { + // A stored body is always JSON. + } + const headers: Record = { + "Content-Type": "application/json", + "User-Agent": job.agent, + "X-Tangled-Event": job.event, + "X-Tangled-Hook-ID": String(job.hook), + "X-Tangled-Delivery": job.id, + "X-Tangled-Repo": repo, + }; + if (hook.secret !== "") { + headers["X-Tangled-Signature-256"] = await sign(hook.secret, job.body); + } + try { + const response = await fetch(hook.url, { + method: "POST", + headers, + body: job.body, + redirect: "manual", + signal: AbortSignal.timeout(10_000), + }); + if (response.status >= 500) { + await response.body?.cancel(); + return { + code: response.status, + response: encode(`server error: ${response.status}`), + retry: true, + }; + } + return { code: response.status, response: await readHead(response), retry: false }; + } catch (error) { + return { code: null, response: encode(errorMessage(error)), retry: true }; + } +} + +function isPublicIp(host: string): boolean { + const v4 = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(host); + if (v4 !== null) { + const [a = 0, b = 0, c = 0] = v4.slice(1).map(Number); + return !( + a === 0 || + a === 10 || + a === 127 || + a >= 224 || + (a === 100 && b >= 64 && b < 128) || + (a === 169 && b === 254) || + (a === 172 && b >= 16 && b < 32) || + (a === 192 && b === 168) || + (a === 192 && b === 88 && c === 99) || + (a === 192 && b === 0 && (c === 0 || c === 2)) || + (a === 198 && (b === 18 || b === 19 || (b === 51 && c === 100))) || + (a === 203 && b === 0 && c === 113) + ); + } + if (host.startsWith("[")) { + // Only global unicast, without the documentation, Teredo and 6to4 ranges. + const [a = 0, b = 0] = host + .slice(1) + .split(":") + .map((group) => Number.parseInt(group || "0", 16)); + return ( + a >= 0x2000 && + a <= 0x3fff && + a !== 0x2002 && + !(a === 0x2001 && (b === 0 || b === 0xdb8)) + ); + } + return host !== "localhost" && !host.endsWith(".localhost"); +} + +function hookUrl(value: unknown): string { + const url = typeof value === "string" ? value.trim() : ""; + const parsed = URL.parse(url); + if ( + parsed === null || + (parsed.protocol !== "http:" && parsed.protocol !== "https:") || + parsed.hostname === "" || + !isPublicIp(parsed.hostname) + ) { + throw invalidRequest("url must be an http or https URL with a public host"); + } + return url; +} + +function hookEvents(value: unknown): string[] { + if (!Array.isArray(value) || !value.every((item) => typeof item === "string")) { + throw invalidRequest("events must be a list of event names"); + } + const names = [...new Set(value.map((item) => item.trim()))]; + const unknown = names.find((name) => !eventNames.has(name)); + if (unknown !== undefined) { + throw new XrpcError(400, "InvalidEvent", `unknown webhook event "${unknown}"`); + } + if (names.length === 0) { + throw new XrpcError(400, "NoEventsSelected", "at least one event must be specified"); + } + return names; +} + +function hookId(value: unknown): number { + if (typeof value !== "string" || !/^\d{1,15}$/.test(value)) { + throw invalidRequest("invalid webhook id"); + } + return Number(value); +} + +function optionalString(value: unknown, name: string): string | undefined { + if (value !== undefined && typeof value !== "string") { + throw invalidRequest(`${name} must be a string`); + } + return value; +} + +function optionalBoolean(value: unknown, name: string): boolean | undefined { + if (value !== undefined && typeof value !== "boolean") { + throw invalidRequest(`${name} must be a boolean`); + } + return value; +} + +export async function webhookXrpc( + method: string, + request: Request, + env: Env, + exports: Cloudflare.Exports, +): Promise { + const claims = await authenticate(request, `did:web:${env.HOSTNAME}`, method); + const params = new URL(request.url).searchParams; + const input: unknown = method.startsWith("org.tangled.webhook.list") + ? Object.fromEntries(params) + : await request.json().catch(() => null); + if (!isObject(input) || !isDid(input["repo"])) { + throw invalidRequest("repo must be a DID"); + } + const repo = input["repo"]; + authorize({ claims, repo, env }); + await servedRepo({ env, repo }); + const hooks = exports.Webhooks.getByName(repo); + switch (method) { + case "org.tangled.webhook.listWebhooks": { + return Response.json({ webhooks: await hooks.list() }); + } + case "org.tangled.webhook.listWebhookDeliveries": { + const ids = params.getAll("webhooks"); + if (ids.length === 0 || ids.length > 50) { + throw invalidRequest("webhooks must name 1 to 50 webhook ids"); + } + const limit = Number(params.get("limit") ?? 50); + if (!Number.isInteger(limit) || limit < 1 || limit > 100) { + throw invalidRequest("limit must be an integer from 1 to 100"); + } + return Response.json({ deliveries: await hooks.deliveries(ids.map(hookId), limit) }); + } + case "org.tangled.webhook.createWebhook": { + const hook = { + url: hookUrl(input["url"]), + events: hookEvents(input["events"]), + secret: optionalString(input["secret"], "secret")?.trim() ?? "", + active: optionalBoolean(input["active"], "active") ?? true, + }; + return Response.json({ id: await hooks.create(hook) }); + } + case "org.tangled.webhook.updateWebhook": { + const id = hookId(input["webhook"]); + const url = optionalString(input["url"], "url"); + const events = input["events"]; + const changes = { + ...(url === undefined || url.trim() === "" ? {} : { url: hookUrl(url) }), + ...(events === undefined || (Array.isArray(events) && events.length === 0) + ? {} + : { events: hookEvents(events) }), + secret: optionalString(input["secret"], "secret")?.trim(), + active: optionalBoolean(input["active"], "active"), + }; + if (!(await hooks.update(id, changes))) { + throw new XrpcError(404, "WebhookNotFound", "webhook not found"); + } + return new Response(null); + } + case "org.tangled.webhook.deleteWebhook": { + if (!(await hooks.remove(hookId(input["webhook"])))) { + throw new XrpcError(404, "WebhookNotFound", "webhook not found"); + } + return new Response(null); + } + case "org.tangled.webhook.retryWebhookDelivery": { + const id = hookId(input["webhook"]); + const delivery = input["delivery"]; + if (typeof delivery !== "string") { + throw invalidRequest("delivery must be a delivery id"); + } + await claimToken({ claims, store: exports.Store.getByName("main") }); + const error = await hooks.retry(id, delivery); + if (error !== undefined) { + throw new XrpcError( + 404, + error, + `${error === "WebhookNotFound" ? "webhook" : "delivery"} not found`, + ); + } + return new Response(null); + } + default: + throw new XrpcError(501, "MethodNotImplemented", `${method} is not implemented`); + } +} diff --git a/test/fixtures/lexicons/org.tangled.webhook.createWebhook.json b/test/fixtures/lexicons/org.tangled.webhook.createWebhook.json new file mode 100644 index 0000000..b074a49 --- /dev/null +++ b/test/fixtures/lexicons/org.tangled.webhook.createWebhook.json @@ -0,0 +1,62 @@ +{ + "lexicon": 1, + "id": "org.tangled.webhook.createWebhook", + "defs": { + "main": { + "type": "procedure", + "description": "Create a new webhook for a repository.", + "input": { + "encoding": "application/json", + "schema": { + "type": "object", + "required": ["repo", "url", "events"], + "properties": { + "repo": { + "type": "string", + "format": "did", + "description": "DID of the repository as minted by the knot." + }, + "url": { + "type": "string", + "format": "uri", + "description": "Endpoint URL that will receive webhook payloads." + }, + "active": { + "type": "boolean", + "default": true, + "description": "Whether the webhook should be active immediately." + }, + "events": { + "type": "array", + "description": "Event types to subscribe to.", + "items": { + "type": "ref", + "ref": "org.tangled.webhook.defs#eventType" + } + }, + "secret": { + "type": "string", + "description": "Optional HMAC secret used to sign payloads. If omitted, payloads are not signed." + } + } + } + }, + "output": { + "encoding": "application/json", + "schema": { + "type": "object", + "required": ["id"], + "properties": { + "id": { + "type": "string", + "description": "ID of the newly created webhook." + } + } + } + }, + "errors": [ + { "name": "NoEventsSelected", "description": "At least one event type must be specified." } + ] + } + } +} diff --git a/test/fixtures/lexicons/org.tangled.webhook.defs.json b/test/fixtures/lexicons/org.tangled.webhook.defs.json new file mode 100644 index 0000000..33d2eb5 --- /dev/null +++ b/test/fixtures/lexicons/org.tangled.webhook.defs.json @@ -0,0 +1,73 @@ +{ + "lexicon": 1, + "id": "org.tangled.webhook.defs", + "defs": { + "eventType": { + "type": "string", + "knownValues": [ + "push", + "repository:renamed", + "pull_request:created", + "pull_request:resubmitted", + "pull_request:merged", + "pull_request:closed", + "pull_request:reopened", + "org.tangled.event.pullRequest" + ] + }, + "webhook": { + "type": "object", + "required": ["id", "url", "active", "events", "createdAt"], + "properties": { + "id": { + "type": "string", + "description": "Webhook identifier." + }, + "url": { + "type": "string", + "format": "uri", + "description": "Endpoint URL that receives webhook payloads." + }, + "active": { + "type": "boolean", + "description": "Whether the webhook is currently enabled." + }, + "events": { + "type": "array", + "description": "Event types this webhook is subscribed to.", + "items": { + "type": "ref", + "ref": "#eventType" + } + }, + "createdAt": { "type": "string", "format": "datetime" }, + "updatedAt": { "type": "string", "format": "datetime" } + } + }, + "delivery": { + "type": "object", + "required": ["id", "webhook", "event", "url", "success", "createdAt"], + "properties": { + "id": { + "type": "string", + "description": "Unique ID for tracking this delivery attempt." + }, + "webhook": { + "type": "string", + "description": "ID of the webhook this delivery belongs to." + }, + "event": { + "type": "ref", + "ref": "#eventType", + "description": "Event type that triggered the delivery." + }, + "url": { "type": "string", "format": "uri" }, + "requestBody": { "type": "bytes" }, + "responseCode": { "type": "integer" }, + "responseBody": { "type": "bytes" }, + "success": { "type": "boolean" }, + "createdAt": { "type": "string", "format": "datetime" } + } + } + } +} diff --git a/test/fixtures/lexicons/org.tangled.webhook.deleteWebhook.json b/test/fixtures/lexicons/org.tangled.webhook.deleteWebhook.json new file mode 100644 index 0000000..ddfa59e --- /dev/null +++ b/test/fixtures/lexicons/org.tangled.webhook.deleteWebhook.json @@ -0,0 +1,31 @@ +{ + "lexicon": 1, + "id": "org.tangled.webhook.deleteWebhook", + "defs": { + "main": { + "type": "procedure", + "description": "Delete a webhook from a repository.", + "input": { + "encoding": "application/json", + "schema": { + "type": "object", + "required": ["repo", "webhook"], + "properties": { + "repo": { + "type": "string", + "format": "did", + "description": "DID of the repository as minted by the knot." + }, + "webhook": { + "type": "string", + "description": "Webhook ID to delete." + } + } + } + }, + "errors": [ + { "name": "WebhookNotFound" } + ] + } + } +} diff --git a/test/fixtures/lexicons/org.tangled.webhook.listWebhookDeliveries.json b/test/fixtures/lexicons/org.tangled.webhook.listWebhookDeliveries.json new file mode 100644 index 0000000..3dc1de5 --- /dev/null +++ b/test/fixtures/lexicons/org.tangled.webhook.listWebhookDeliveries.json @@ -0,0 +1,48 @@ +{ + "lexicon": 1, + "id": "org.tangled.webhook.listWebhookDeliveries", + "defs": { + "main": { + "type": "query", + "description": "List recent delivery attempts for a webhook.", + "parameters": { + "type": "params", + "required": ["repo", "webhooks"], + "properties": { + "repo": { + "type": "string", + "format": "did", + "description": "DID of the repository as minted by the knot." + }, + "webhooks": { + "type": "array", + "items": { "type": "string", "description": "Webhook ID." }, + "maxLength": 50 + }, + "limit": { + "type": "integer", + "minimum": 1, + "maximum": 100, + "default": 50 + } + } + }, + "output": { + "encoding": "application/json", + "schema": { + "type": "object", + "required": ["deliveries"], + "properties": { + "deliveries": { + "type": "array", + "items": { + "type": "ref", + "ref": "org.tangled.webhook.defs#delivery" + } + } + } + } + } + } + } +} diff --git a/test/fixtures/lexicons/org.tangled.webhook.listWebhooks.json b/test/fixtures/lexicons/org.tangled.webhook.listWebhooks.json new file mode 100644 index 0000000..4196422 --- /dev/null +++ b/test/fixtures/lexicons/org.tangled.webhook.listWebhooks.json @@ -0,0 +1,37 @@ +{ + "lexicon": 1, + "id": "org.tangled.webhook.listWebhooks", + "defs": { + "main": { + "type": "query", + "description": "List webhooks configured for a repository.", + "parameters": { + "type": "params", + "required": ["repo"], + "properties": { + "repo": { + "type": "string", + "format": "did", + "description": "DID of the repository as minted by the knot." + } + } + }, + "output": { + "encoding": "application/json", + "schema": { + "type": "object", + "required": ["webhooks"], + "properties": { + "webhooks": { + "type": "array", + "items": { + "type": "ref", + "ref": "org.tangled.webhook.defs#webhook" + } + } + } + } + } + } + } +} diff --git a/test/fixtures/lexicons/org.tangled.webhook.retryWebhookDelivery.json b/test/fixtures/lexicons/org.tangled.webhook.retryWebhookDelivery.json new file mode 100644 index 0000000..960080a --- /dev/null +++ b/test/fixtures/lexicons/org.tangled.webhook.retryWebhookDelivery.json @@ -0,0 +1,36 @@ +{ + "lexicon": 1, + "id": "org.tangled.webhook.retryWebhookDelivery", + "defs": { + "main": { + "type": "procedure", + "description": "Re-send a specific webhook delivery.", + "input": { + "encoding": "application/json", + "schema": { + "type": "object", + "required": ["repo", "webhook", "delivery"], + "properties": { + "repo": { + "type": "string", + "format": "did", + "description": "DID of the repository as minted by the knot." + }, + "webhook": { + "type": "string", + "description": "Webhook ID that owns the delivery." + }, + "delivery": { + "type": "string", + "description": "Delivery ID to retry." + } + } + } + }, + "errors": [ + { "name": "WebhookNotFound" }, + { "name": "DeliveryNotFound" } + ] + } + } +} diff --git a/test/fixtures/lexicons/org.tangled.webhook.updateWebhook.json b/test/fixtures/lexicons/org.tangled.webhook.updateWebhook.json new file mode 100644 index 0000000..f881ed3 --- /dev/null +++ b/test/fixtures/lexicons/org.tangled.webhook.updateWebhook.json @@ -0,0 +1,48 @@ +{ + "lexicon": 1, + "id": "org.tangled.webhook.updateWebhook", + "defs": { + "main": { + "type": "procedure", + "description": "Update an existing webhook. Only supplied fields are changed.", + "input": { + "encoding": "application/json", + "schema": { + "type": "object", + "required": ["repo", "webhook"], + "properties": { + "repo": { + "type": "string", + "format": "did", + "description": "DID of the repository as minted by the knot." + }, + "webhook": { + "type": "string", + "description": "Webhook ID to update." + }, + "url": { + "type": "string", + "format": "uri" + }, + "secret": { + "type": "string" + }, + "active": { + "type": "boolean" + }, + "events": { + "type": "array", + "items": { + "type": "ref", + "ref": "org.tangled.webhook.defs#eventType" + } + } + } + } + }, + "errors": [ + { "name": "WebhookNotFound" } + ] + } + } +} diff --git a/test/helpers.ts b/test/helpers.ts index 1e5628e..2ee3938 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -1,4 +1,4 @@ -import { Lexicons, lexiconDoc } from "@atproto/lexicon"; +import { jsonToLex, Lexicons, lexiconDoc } from "@atproto/lexicon"; import { p256 } from "@noble/curves/nist.js"; import { secp256k1 } from "@noble/curves/secp256k1.js"; import { expect, onTestFinished, vi } from "vitest"; @@ -14,6 +14,13 @@ import manual from "./fixtures/lexicons/org.tangled.ci.trigger.manual.json"; import pullRequest from "./fixtures/lexicons/org.tangled.event.pullRequest.json"; import push from "./fixtures/lexicons/org.tangled.event.push.json"; import listSecrets from "./fixtures/lexicons/org.tangled.secret.listSecrets.json"; +import createWebhook from "./fixtures/lexicons/org.tangled.webhook.createWebhook.json"; +import webhookDefs from "./fixtures/lexicons/org.tangled.webhook.defs.json"; +import deleteWebhook from "./fixtures/lexicons/org.tangled.webhook.deleteWebhook.json"; +import listWebhookDeliveries from "./fixtures/lexicons/org.tangled.webhook.listWebhookDeliveries.json"; +import listWebhooks from "./fixtures/lexicons/org.tangled.webhook.listWebhooks.json"; +import retryWebhookDelivery from "./fixtures/lexicons/org.tangled.webhook.retryWebhookDelivery.json"; +import updateWebhook from "./fixtures/lexicons/org.tangled.webhook.updateWebhook.json"; type Curve = PublicKey["curve"]; @@ -157,11 +164,18 @@ const lexicons = new Lexicons( getWorkflowDefinition, listWorkflowDefinitions, listSecrets, + webhookDefs, + createWebhook, + updateWebhook, + deleteWebhook, + listWebhooks, + listWebhookDeliveries, + retryWebhookDelivery, ].map((doc) => lexiconDoc.parse(doc)), ); export function assertOutput(nsid: string, value: unknown): void { - lexicons.assertValidXrpcOutput(nsid, value); + lexicons.assertValidXrpcOutput(nsid, jsonToLex(value)); } export async function signature(body: string, timestamp: number, secret: string): Promise { @@ -188,13 +202,14 @@ export function commitMessage( did: string, rkey: string, operation = "create", + collection = "org.tangled.review.patch", ): object { return { $type: "network.bsky.jetstream.subscribeEvents#event", payload: { $type: "network.bsky.jetstream.subscribeEvents#commit", did, - collection: "org.tangled.review.patch", + collection, rkey, cid: "bafy", rev: "3abc", diff --git a/test/webhooks.test.ts b/test/webhooks.test.ts new file mode 100644 index 0000000..4bce84c --- /dev/null +++ b/test/webhooks.test.ts @@ -0,0 +1,524 @@ +import { runDurableObjectAlarm, runInDurableObject } from "cloudflare:test"; +import { env, exports } from "cloudflare:workers"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { + assertOutput, + didDoc, + getRecordRoute, + keypair, + knot, + knotService, + offline, + pds, + pdsService, + record, + repoDid as repo, + routeFetch, + serviceToken, + spindle, +} from "./helpers"; +import type { Route } from "./helpers"; + +const host = "treadle.example.com"; +const owner = env.OWNER_DID; +const stranger = "did:plc:ssssssssssssssssssssssss"; +const ownerKey = keypair("p256"); +const strangerKey = keypair(); +const repoKey = keypair(); +const target = "https://hooks.example.com/tangled"; +const hooks = exports.Webhooks.getByName(repo); +const pushBody = { ref: "refs/heads/main", repository: { full_name: `${owner}/my.repo` } }; +let manifestSpindle: string; +let routes: Record; +let sent: { headers: Headers; body: string; redirect: string }[]; + +beforeEach(async () => { + manifestSpindle = spindle; + sent = []; + await runInDurableObject(exports.Store.getByName("main"), (_instance, state) => + state.storage.deleteAlarm(), + ); + await runInDurableObject(hooks, async (_instance, state) => { + state.storage.sql.exec( + "DELETE FROM hooks; DELETE FROM jobs; DELETE FROM deliveries; DELETE FROM pushes", + ); + await state.storage.deleteAlarm(); + }); + routes = { + [`GET https://plc.directory/${owner}`]: () => + Response.json(didDoc(owner, ownerKey, [pdsService])), + [`GET https://plc.directory/${stranger}`]: () => + Response.json(didDoc(stranger, strangerKey)), + [`GET https://plc.directory/${repo}`]: () => + Response.json(didDoc(repo, repoKey, [knotService])), + [getRecordRoute(knot, repo, "org.tangled.repo.manifest", "self")]: () => + record({ declaration: { owner, name: "My.Repo" }, spindle: manifestSpindle }), + [getRecordRoute(pds, owner, "org.tangled.repo.declaration", "my.repo")]: () => + record({ repo }), + [`POST ${target}`]: () => new Response("thanks"), + }; + routeFetch(routes, async (request) => { + if (request.url.startsWith(target)) { + sent.push({ + headers: request.headers, + body: await request.clone().text(), + redirect: request.redirect, + }); + } + }); +}); + +function call( + method: string, + params: Record = {}, + { + key = ownerKey, + iss = owner, + jti = crypto.randomUUID(), + }: { key?: typeof ownerKey; iss?: string; jti?: string } = {}, +): Promise { + const nsid = `org.tangled.webhook.${method}`; + const headers = { + Authorization: `Bearer ${serviceToken(key, { iss, lxm: nsid, jti })}`, + "Content-Type": "application/json", + }; + const url = `https://${host}/xrpc/${nsid}`; + if (method.startsWith("list")) { + const query = new URLSearchParams({ repo }); + for (const [name, value] of Object.entries(params)) { + for (const item of [value].flat()) { + query.append(name, String(item)); + } + } + query.sort(); + return exports.default.fetch(`${url}?${query.toString()}`, { headers }); + } + return exports.default.fetch(url, { + method: "POST", + headers, + body: JSON.stringify({ repo, ...params }), + }); +} + +async function create(input: object = {}): Promise { + const res = await call("createWebhook", { url: target, events: ["push"], ...input }); + expect(res.status).toBe(200); + const body: { id: string } = await res.json(); + assertOutput("org.tangled.webhook.createWebhook", body); + return body.id; +} + +async function list(): Promise[]> { + const res = await call("listWebhooks"); + expect(res.status).toBe(200); + const body: { webhooks: Record[] } = await res.json(); + assertOutput("org.tangled.webhook.listWebhooks", body); + return body.webhooks; +} + +interface Delivery { + id: string; + webhook: string; + event: string; + url: string; + success: boolean; + responseCode?: number; + requestBody?: { $bytes: string }; + responseBody?: { $bytes: string }; +} + +async function deliveries(ids: string[], limit?: number): Promise { + const res = await call("listWebhookDeliveries", { + webhooks: ids, + ...(limit === undefined ? {} : { limit }), + }); + expect(res.status).toBe(200); + const body: { deliveries: Delivery[] } = await res.json(); + assertOutput("org.tangled.webhook.listWebhookDeliveries", body); + return body.deliveries; +} + +function text(bytes: { $bytes: string } | undefined): string { + return new TextDecoder().decode(Uint8Array.fromBase64(bytes?.$bytes ?? "")); +} + +function dueJobs(): Promise { + return runInDurableObject( + hooks, + (_instance, state) => + state.storage.sql + .exec<{ n: number }>("SELECT count(*) AS n FROM jobs WHERE due <= ?", Date.now()) + .one().n, + ); +} + +async function drain(): Promise { + await runDurableObjectAlarm(hooks); + await vi.waitFor(async () => expect(await dueJobs()).toBe(0)); +} + +// Makes the retries that wait for their backoff due now. +async function retryNow(): Promise { + await runInDurableObject(hooks, (_instance, state) => { + state.storage.sql.exec("UPDATE jobs SET due = 0"); + }); + await drain(); +} + +async function firePush(key = "push-1"): Promise { + await hooks.firePush(key, pushBody, "Tangled-Hook/abcdef0"); + await drain(); +} + +async function hmac(secret: string, body: string): Promise { + const key = await crypto.subtle.importKey( + "raw", + new TextEncoder().encode(secret), + { name: "HMAC", hash: "SHA-256" }, + false, + ["sign"], + ); + const mac = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(body)); + return `sha256=${new Uint8Array(mac).toHex()}`; +} + +describe("org.tangled.webhook methods", () => { + it("creates, lists, updates and deletes a webhook without returning its secret", async () => { + const id = await create({ secret: " s3cret ", events: ["push", "push"] }); + expect(id).toMatch(/^\d+$/); + const [hook, ...rest] = await list(); + expect(rest).toEqual([]); + expect(hook).toMatchObject({ id, url: target, active: true, events: ["push"] }); + expect(JSON.stringify(hook)).not.toContain("s3cret"); + + const update = await call("updateWebhook", { + webhook: id, + url: `${target}/v2`, + active: false, + events: ["pull_request:merged"], + }); + expect(update.status).toBe(200); + expect(await list()).toMatchObject([ + { id, url: `${target}/v2`, active: false, events: ["pull_request:merged"] }, + ]); + + expect((await call("deleteWebhook", { webhook: id })).status).toBe(200); + expect(await list()).toEqual([]); + const missing = await call("deleteWebhook", { webhook: id }); + expect(missing.status).toBe(404); + expect(await missing.json()).toMatchObject({ error: "WebhookNotFound" }); + }); + + it("keeps the fields that an update omits", async () => { + const id = await create({ events: ["push", "pull_request:created"], active: false }); + await call("updateWebhook", { webhook: id, url: " ", events: [] }); + expect(await list()).toMatchObject([ + { url: target, active: false, events: ["push", "pull_request:created"] }, + ]); + }); + + it("accepts every upstream event name", async () => { + const events = [ + "push", + "repository:renamed", + "pull_request:created", + "pull_request:resubmitted", + "pull_request:merged", + "pull_request:closed", + "pull_request:reopened", + ]; + await create({ events }); + expect(await list()).toMatchObject([{ events }]); + }); + + it.each([ + ["no events", { events: [] }, "NoEventsSelected"], + ["an unknown event", { events: ["org.tangled.event.pullRequest"] }, "InvalidEvent"], + ["events that are not a list", { events: "push" }, "InvalidRequest"], + ["no URL", { url: undefined }, "InvalidRequest"], + ["an ftp URL", { url: "ftp://hooks.example.com/" }, "InvalidRequest"], + ["a URL that does not parse", { url: "not a url" }, "InvalidRequest"], + ["a private IPv4 address", { url: "http://10.1.2.3/hook" }, "InvalidRequest"], + ["a loopback address", { url: "http://127.0.0.1:8080/hook" }, "InvalidRequest"], + ["a hex loopback address", { url: "http://0x7f.1/hook" }, "InvalidRequest"], + ["a link-local address", { url: "http://169.254.169.254/" }, "InvalidRequest"], + ["an IPv6 loopback address", { url: "http://[::1]/hook" }, "InvalidRequest"], + ["a mapped IPv4 address", { url: "http://[::ffff:10.0.0.1]/" }, "InvalidRequest"], + ["a unique local IPv6 address", { url: "http://[fd00::1]/" }, "InvalidRequest"], + ["a 6to4 address", { url: "http://[2002:a00:1::]/" }, "InvalidRequest"], + ["a Teredo address", { url: "http://[2001::1]/" }, "InvalidRequest"], + ["localhost", { url: "http://localhost:8080/" }, "InvalidRequest"], + ["a secret that is not a string", { secret: 1 }, "InvalidRequest"], + ["an active flag that is not a boolean", { active: "yes" }, "InvalidRequest"], + ])("refuses a webhook with %s", async (_, input, error) => { + const res = await call("createWebhook", { url: target, events: ["push"], ...input }); + expect(res.status).toBe(400); + expect(await res.json()).toMatchObject({ error }); + }); + + it.each([ + ["a public IPv4 address", "http://93.184.216.34/hook"], + ["a public IPv6 address", "https://[2606:4700::1111]/hook"], + ])("accepts %s", async (_, url) => { + await create({ url }); + expect(await list()).toMatchObject([{ url }]); + }); + + it.each([ + ["updateWebhook", { webhook: "abc" }, 400, "InvalidRequest"], + ["updateWebhook", { webhook: "99" }, 404, "WebhookNotFound"], + ["deleteWebhook", { webhook: "99" }, 404, "WebhookNotFound"], + ["retryWebhookDelivery", { webhook: "99", delivery: "x" }, 404, "WebhookNotFound"], + ["listWebhookDeliveries", {}, 400, "InvalidRequest"], + ["listWebhookDeliveries", { webhooks: "abc" }, 400, "InvalidRequest"], + ["listWebhookDeliveries", { webhooks: "1", limit: 0 }, 400, "InvalidRequest"], + ["listWebhookDeliveries", { webhooks: "1", limit: 101 }, 400, "InvalidRequest"], + [ + "listWebhookDeliveries", + { webhooks: Array.from({ length: 51 }, (_, i) => i) }, + 400, + "InvalidRequest", + ], + ])("%s answers %j with %i", async (method, input, status, error) => { + const res = await call(method, input); + expect(res.status).toBe(status); + expect(await res.json()).toMatchObject({ error }); + }); + + it("refuses an update with an invalid URL or event", async () => { + const id = await create(); + for (const input of [{ url: "http://10.0.0.1/" }, { events: ["nope"] }]) { + // oxlint-disable-next-line no-await-in-loop + const res = await call("updateWebhook", { webhook: id, ...input }); + expect(res.status).toBe(400); + } + expect(await list()).toMatchObject([{ url: target, events: ["push"] }]); + }); + + it.each([ + ["createWebhook", { url: target, events: ["push"] }], + ["updateWebhook", { webhook: "1" }], + ["deleteWebhook", { webhook: "1" }], + ["listWebhooks", {}], + ["listWebhookDeliveries", { webhooks: "1" }], + ["retryWebhookDelivery", { webhook: "1", delivery: "x" }], + ])("guards %s", async (method, input) => { + const denied = await call(method, input, { key: strangerKey, iss: stranger }); + expect(denied.status).toBe(403); + const unauthenticated = await exports.default.fetch( + `https://${host}/xrpc/org.tangled.webhook.${method}?repo=${repo}`, + { method: method.startsWith("list") ? "GET" : "POST" }, + ); + expect(unauthenticated.status).toBe(401); + const byRepo = await call(method, input, { key: repoKey, iss: repo }); + expect([200, 404]).toContain(byRepo.status); + manifestSpindle = "did:web:other.example.com"; + const unserved = await call(method, input); + expect(unserved.status).toBe(404); + expect(await unserved.json()).toMatchObject({ error: "RepoNotFound" }); + }); +}); + +describe("delivery", () => { + it("posts the event with its headers and signature, and records the delivery", async () => { + const id = await create({ secret: " s3cret " }); + await firePush(); + const [request, ...more] = sent; + expect(more).toEqual([]); + expect(request?.redirect).toBe("manual"); + const body = request?.body ?? ""; + expect(JSON.parse(body)).toEqual(pushBody); + const headers = Object.fromEntries(request?.headers ?? []); + expect(headers).toMatchObject({ + "content-type": "application/json", + "user-agent": "Tangled-Hook/abcdef0", + "x-tangled-event": "push", + "x-tangled-hook-id": id, + "x-tangled-repo": `${owner}/my.repo`, + "x-tangled-signature-256": await hmac("s3cret", body), + }); + expect(headers["x-tangled-delivery"]).toMatch( + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, + ); + const [delivery] = await deliveries([id]); + expect(delivery).toMatchObject({ + id: headers["x-tangled-delivery"], + webhook: id, + event: "push", + url: target, + success: true, + responseCode: 200, + }); + expect(text(delivery?.requestBody)).toBe(body); + expect(text(delivery?.responseBody)).toBe("thanks"); + }); + + it("sends no signature without a secret, and after the secret is cleared", async () => { + await create(); + const signed = await create({ secret: "s3cret" }); + await call("updateWebhook", { webhook: signed, secret: "" }); + await firePush(); + expect(sent).toHaveLength(2); + for (const request of sent) { + expect(request.headers.has("x-tangled-signature-256")).toBe(false); + } + }); + + it("keeps the secret when an update omits it, and trims a new secret", async () => { + const kept = await create({ secret: "s3cret" }); + await call("updateWebhook", { webhook: kept, active: true }); + const changed = await create({ secret: "s3cret" }); + await call("updateWebhook", { webhook: changed, secret: " n3w " }); + await firePush(); + const signature = (hook: string): string | null | undefined => + sent + .find((request) => request.headers.get("x-tangled-hook-id") === hook) + ?.headers.get("x-tangled-signature-256"); + const body = sent[0]?.body ?? ""; + expect(signature(kept)).toBe(await hmac("s3cret", body)); + expect(signature(changed)).toBe(await hmac("n3w", body)); + }); + + it("sends only to active webhooks of the event, once for each push", async () => { + await create({ active: false }); + await create({ events: ["pull_request:created"] }); + const id = await create(); + expect(await hooks.subscribed(["push"])).toBe(true); + expect(await hooks.subscribed(["pull_request:merged"])).toBe(false); + await firePush(); + await firePush(); + expect(sent).toHaveLength(1); + expect(sent[0]?.headers.get("x-tangled-hook-id")).toBe(id); + await firePush("push-2"); + expect(sent).toHaveLength(2); + }); + + it("tries a server error three times and records one failed delivery", async () => { + const id = await create(); + routes[`POST ${target}`] = () => new Response("down", { status: 503 }); + await firePush(); + await retryNow(); + await retryNow(); + await retryNow(); + expect(sent).toHaveLength(3); + expect(new Set(sent.map((r) => r.headers.get("x-tangled-delivery"))).size).toBe(1); + const [delivery, ...rest] = await deliveries([id]); + expect(rest).toEqual([]); + expect(delivery).toMatchObject({ success: false, responseCode: 503 }); + expect(text(delivery?.responseBody)).toBe("server error: 503"); + }); + + it("tries a network error again and records the later success", async () => { + const id = await create(); + let calls = 0; + routes[`POST ${target}`] = () => (++calls === 1 ? offline() : new Response("ok")); + await firePush(); + expect(await deliveries([id])).toEqual([]); + await retryNow(); + expect(calls).toBe(2); + expect(await deliveries([id])).toMatchObject([{ success: true, responseCode: 200 }]); + }); + + it("records a network error after three attempts", async () => { + const id = await create(); + routes[`POST ${target}`] = offline; + await firePush(); + await retryNow(); + await retryNow(); + const [delivery] = await deliveries([id]); + expect(delivery).toMatchObject({ success: false }); + expect(delivery).not.toHaveProperty("responseCode"); + expect(text(delivery?.responseBody)).toBe("offline"); + }); + + it.each([ + ["a client error", () => new Response("no", { status: 404 }), 404], + [ + "a redirect", + () => + new Response(null, { + status: 302, + headers: { Location: "https://x.example.com/" }, + }), + 302, + ], + ])("does not try %s again", async (_, response, code) => { + const id = await create(); + routes[`POST ${target}`] = response; + await firePush(); + await retryNow(); + expect(sent).toHaveLength(1); + expect(await deliveries([id])).toMatchObject([{ success: false, responseCode: code }]); + }); + + it("keeps the first 10 KiB of the response bytes", async () => { + const id = await create(); + const data = Uint8Array.from({ length: 20_000 }, (_, i) => i % 256); + routes[`POST ${target}`] = () => new Response(data); + await firePush(); + const [delivery] = await deliveries([id]); + expect(Uint8Array.fromBase64(delivery?.responseBody?.$bytes ?? "")).toEqual( + data.subarray(0, 10_240), + ); + }); + + it("keeps the last 50 deliveries of each webhook and lists the newest first", async () => { + const id = await create(); + const other = await create(); + for (let i = 0; i < 52; i++) { + // oxlint-disable-next-line no-await-in-loop + await hooks.firePush(`push-${i}`, { ...pushBody, n: i }, "Tangled-Hook/abcdef0"); + } + await drain(); + const all = await deliveries([id], 100); + expect(all).toHaveLength(50); + expect(JSON.parse(text(all[0]?.requestBody))).toMatchObject({ n: 51 }); + expect(await deliveries([id])).toHaveLength(50); + expect(await deliveries([id], 5)).toHaveLength(5); + const both = await deliveries([id, other], 2); + expect(both.map((d) => d.webhook)).toEqual([id, id, other, other]); + }); + + it("sends a delivery again with a new id and the stored body", async () => { + const id = await create({ secret: "s3cret" }); + await firePush(); + const [first] = await deliveries([id]); + const res = await call("retryWebhookDelivery", { webhook: id, delivery: first?.id }); + expect(res.status).toBe(200); + await drain(); + expect(sent).toHaveLength(2); + const retried = sent[1]; + expect(retried?.headers.get("user-agent")).toBe("Tangled-Hook/retry"); + expect(retried?.headers.get("x-tangled-repo")).toBe(`${owner}/my.repo`); + expect(retried?.headers.get("x-tangled-delivery")).not.toBe(first?.id); + const body = retried?.body ?? ""; + expect(body).toBe(text(first?.requestBody)); + expect(retried?.headers.get("x-tangled-signature-256")).toBe(await hmac("s3cret", body)); + expect(await deliveries([id])).toHaveLength(2); + + const other = await create(); + const wrong = await call("retryWebhookDelivery", { webhook: other, delivery: first?.id }); + expect(wrong.status).toBe(404); + expect(await wrong.json()).toMatchObject({ error: "DeliveryNotFound" }); + }); + + it("refuses a replayed retry token", async () => { + const id = await create(); + await firePush(); + const [first] = await deliveries([id]); + const input = { webhook: id, delivery: first?.id }; + expect((await call("retryWebhookDelivery", input, { jti: "once" })).status).toBe(200); + const replay = await call("retryWebhookDelivery", input, { jti: "once" }); + expect(replay.status).toBe(403); + expect(await replay.json()).toMatchObject({ error: "Auth" }); + }); + + it("drops the queued deliveries of a deleted webhook", async () => { + const id = await create(); + routes[`POST ${target}`] = () => new Response("down", { status: 500 }); + await firePush(); + await call("deleteWebhook", { webhook: id }); + await retryNow(); + expect(sent).toHaveLength(1); + }); +}); diff --git a/worker-configuration.d.ts b/worker-configuration.d.ts index dcdb8ca..a83f147 100644 --- a/worker-configuration.d.ts +++ b/worker-configuration.d.ts @@ -1,5 +1,5 @@ /* eslint-disable */ -// Generated by Wrangler by running `wrangler types` (hash: 86e221d38b80f6aee4afc7f54dcd53e2) +// Generated by Wrangler by running `wrangler types` (hash: 154c829abe697b8f6353e2718195834a) // Runtime types generated with workerd@1.20261104.1 2026-11-04 interface __BaseEnv_Env { LOG_LIMIT: RateLimit; @@ -11,7 +11,7 @@ interface __BaseEnv_Env { declare namespace Cloudflare { interface GlobalProps { mainModule: typeof import("./src/index"); - durableNamespaces: "Store" | "Logs" | "Runner" | "Secrets"; + durableNamespaces: "Store" | "Logs" | "Runner" | "Secrets" | "Webhooks"; } interface Env extends __BaseEnv_Env {} } diff --git a/wrangler.jsonc b/wrangler.jsonc index 6a8aff3..a1b5e92 100644 --- a/wrangler.jsonc +++ b/wrangler.jsonc @@ -31,6 +31,7 @@ "Store": { "type": "durable-object", "storage": "sqlite" }, "Logs": { "type": "durable-object", "storage": "sqlite" }, "Runner": { "type": "durable-object", "storage": "sqlite", "container": "treadle-runner" }, - "Secrets": { "type": "durable-object", "storage": "sqlite" } + "Secrets": { "type": "durable-object", "storage": "sqlite" }, + "Webhooks": { "type": "durable-object", "storage": "sqlite" } } }