From 88800611e523e4c339c120ee9d124958cc27cd08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=85=E8=86=80?= Date: Fri, 23 Oct 2026 23:23:38 +0800 Subject: [PATCH] fix(webhook): confirm only accepted deliveries Acknowledging before queue acceptance permanently loses deliveries. Confirm only accepted work, release failed claims, scope replay identities and strip tenant-supplied installation authority. --- server/lib/providers/custom/index.ts | 57 +- server/lib/webhook.ts | 408 +++- tests/custom-replay.test.ts | 23 +- tests/ingress-dispatch.test.ts | 560 +++++ tests/lifecycle-safety.test.ts | 3034 ++++++++++++++++++++++++++ 5 files changed, 3985 insertions(+), 97 deletions(-) create mode 100644 tests/ingress-dispatch.test.ts create mode 100644 tests/lifecycle-safety.test.ts diff --git a/server/lib/providers/custom/index.ts b/server/lib/providers/custom/index.ts index f77e885..39233c0 100644 --- a/server/lib/providers/custom/index.ts +++ b/server/lib/providers/custom/index.ts @@ -2,9 +2,28 @@ import type { Env, WebhookEvent } from "../../types"; import type { Provider } from "../types"; import { hmacSha256Hex, timingSafeEqual } from "../hmac"; import { verifySignature } from "../github/verify"; +import { claimOnce, idempotencyStore } from "../../lib/idempotency"; const REPLAY_WINDOW_SECONDS = 300; -const NONCE_TTL_SECONDS = 600; +/** How long an accepted nonce is remembered (longer than the signature window). */ +export const NONCE_TTL_SECONDS = 600; +/** + * How long a nonce stays held while its request is processed. Short, so a + * request whose attempt failed (and could not give the nonce back) can be + * retried well within the signature window. + */ +export const NONCE_CLAIM_SECONDS = 120; + +/** + * The request cannot be judged now: its nonce is held by an attempt not + * confirmed as accepted (still running, or failed without giving the nonce + * back), or the nonce store is unavailable. Not a proven replay: retry later. + */ +export class ReplayPendingError extends Error { + constructor(message = "Replay nonce is held by an unconfirmed attempt") { + super(message); + } +} function parseTimestamp(value: string | undefined): number | null { if (!value) return null; @@ -17,14 +36,18 @@ function parseTimestamp(value: string | undefined): number | null { * (Unix seconds) and `X-WebHooker-Nonce` and signs * `timestamp + "." + nonce + "." + body` instead of the bare body. We reject * timestamps outside a ±5min window and reject any nonce already seen (nonces - * live in KV for 10min). Senders without those headers keep the legacy + * are kept for 10min). Senders without those headers keep the legacy * body-only signature so existing integrations continue to work. + * + * Nonces are scoped to the signing secret, so two tenants picking the same + * nonce never block each other, and claimed atomically (D1 when available), + * so two concurrent replays cannot both pass. */ async function verifyReplayProtected( body: string, headers: Record, secret: string, - kv: KVNamespace, + env: Env, ): Promise { const timestamp = parseTimestamp(headers["x-webhooker-timestamp"]); const nonce = headers["x-webhooker-nonce"]; @@ -37,11 +60,23 @@ async function verifyReplayProtected( const expected = `sha256=${await hmacSha256Hex(secret, `${timestamp}.${nonce}.${body}`)}`; if (!timingSafeEqual(signature ?? "", expected)) return false; - const nonceKey = `nonce:${nonce}`; - const seen = await kv.get(nonceKey); - if (seen !== null && seen !== undefined) return false; - await kv.put(nonceKey, "1", { expirationTtl: NONCE_TTL_SECONDS }); - return true; + const key = await replayNonceKey(secret, nonce); + let state; + try { + state = await claimOnce(idempotencyStore(env.DB, env.KV), key, NONCE_CLAIM_SECONDS); + } catch { + // A storage failure says nothing about the signature. + throw new ReplayPendingError("Replay protection is temporarily unavailable"); + } + // Only a nonce whose request was accepted makes this a replay. + if (state === "pending") throw new ReplayPendingError(); + return state === "claimed"; +} + +/** Claim key of a replay-protection nonce, scoped to the signing secret. */ +export async function replayNonceKey(secret: string, nonce: string): Promise { + const scope = (await hmacSha256Hex(secret, "webhooker:nonce-scope")).slice(0, 16); + return `nonce:${scope}:${nonce}`; } /** @@ -72,7 +107,7 @@ export const customProvider: Provider = { headers["x-webhooker-timestamp"] !== undefined && headers["x-webhooker-nonce"] !== undefined ) { - return verifyReplayProtected(body, headers, secret, env.KV); + return verifyReplayProtected(body, headers, secret, env); } return verifySignature(body, headers["x-webhooker-signature"], secret); }, @@ -80,7 +115,9 @@ export const customProvider: Provider = { parse(body, _headers): WebhookEvent | null { try { const payload = JSON.parse(body) as Record; - if (!payload || typeof payload !== "object") return null; + // An object only: a top-level array (or scalar) could not be parked and + // restored like every other payload, so it is refused up front. + if (!payload || typeof payload !== "object" || Array.isArray(payload)) return null; // Optional id for sender-side dedup (retries from monitoring systems). const deliveryId = typeof payload.deliveryId === "string" && payload.deliveryId diff --git a/server/lib/webhook.ts b/server/lib/webhook.ts index 47f87d1..3c0759d 100644 --- a/server/lib/webhook.ts +++ b/server/lib/webhook.ts @@ -1,22 +1,46 @@ import type { H3Event } from "h3"; import { getHeader, readRawBody, setResponseStatus } from "h3"; -import type { Env } from "./types"; +import type { Env, Group } from "./types"; import { detectProvider } from "./providers"; import { dispatchEvent } from "./core/dispatch"; -import { loadConfig, initConfigStore } from "./config"; -import { loadGroups, ensureInstallationGroup } from "./web/groups"; +import { loadConfigSnapshot, loadGroupFresh, initConfigStore } from "./config"; +import { ensureInstallationGroup } from "./web/groups"; import { getTenantSecret } from "./web/tenants"; +import { NONCE_TTL_SECONDS, ReplayPendingError, replayNonceKey } from "./providers/custom"; import { recordAudit } from "./lib/audit"; import { cfEnv, cfWaitUntil, headersFrom } from "./cf"; import { log } from "./lib/log"; -import { deliveryKey, idempotencyStore } from "./lib/idempotency"; +import { claimOnce, confirmClaim, deliveryKey, idempotencyStore } from "./lib/idempotency"; import { newCorrelationId } from "./lib/correlation"; -import { enqueueWebhook, type DeliveryMessage } from "./queue/delivery"; +import { + deliveryStateKey, + enqueueWebhook, + hadPermanentFailure, + isRetryableFailure, + markTargetsDone, + targetDoneChecker, + type DeliveryMessage, +} from "./queue/delivery"; const MAX_BODY_SIZE = 1024 * 1024; +/** How long a delivery's dedup claim lasts when it is queued. */ +const QUEUED_CLAIM_SECONDS = 120; +/** How long it lasts for an inline dispatch, which may run for minutes. */ +const INLINE_CLAIM_SECONDS = 600; +/** An installation's auto-provisioning is remembered as long as delivery state (7 days). */ +const PROVISIONED_TTL_SECONDS = 7 * 86400; +/** Sending time of one inline dispatch: within its claim, with a margin. */ +const INLINE_SEND_BUDGET_MS = 8 * 60_000; + +/** Result of an inline dispatch: whether the sender should retry. */ +interface InlineOutcome { + retry: boolean; + /** A target failed for good (in this or an earlier attempt). */ + failed: boolean; +} export interface WebhookResult { - status: 200 | 400 | 401 | 404 | 413; + status: 200 | 400 | 401 | 404 | 413 | 503; body: unknown; } @@ -27,6 +51,10 @@ export interface WebhookResult { * `POST /webhook/{groupId}`) the group's own secret is used for verification * (GITHUB_WEBHOOK_SECRET/GITEA_WEBHOOK_SECRET are overridden per request) and * only that group's routes are eligible. + * + * A tenant-signed payload is only as trustworthy as the tenant: its claimed + * GitHub App installation is dropped, so it can neither borrow the operator's + * App credentials nor provision or rebind installation groups. */ export async function processWebhook( env: Env, @@ -38,30 +66,39 @@ export async function processWebhook( const requestId = newCorrelationId(); let effectiveEnv = env; initConfigStore(env); - const groups = await loadGroups(env.KV); + // Until the request is verified, nothing but the receiving group's own row + // is read (fresh): an unsigned flood must not read the whole configuration + // each time. The full snapshot is read once the event is accepted. + const provider = detectProvider(headers); + if (!provider) { + return { status: 400, body: { error: "Unknown webhook provider" } }; + } + let tenant: Group | undefined; if (tenantId) { - if (!groups.some((g) => g.id === tenantId)) { + const found = await loadGroupFresh(env, tenantId); + if (!found.group) { + // Unreadable is not missing: the sender is asked to retry. + if (found.unreadable) { + return { status: 503, body: { error: "Group configuration unreadable; retry later" } }; + } return { status: 404, body: { error: "Group not found" } }; } - const secret = await getTenantSecret( - env.KV, - groups.find((g) => g.id === tenantId)!, - ); + tenant = found.group; + const secret = await getTenantSecret(env.KV, tenant); if (!secret) { return { status: 404, body: { error: "Webhook disabled for this group" } }; } effectiveEnv = { ...env, GITHUB_WEBHOOK_SECRET: secret, GITEA_WEBHOOK_SECRET: secret }; } - const provider = detectProvider(headers); - if (!provider) { - return { status: 400, body: { error: "Unknown webhook provider" } }; - } - let verified = false; try { verified = await provider.verify(body, headers, effectiveEnv); } catch (err) { + if (err instanceof ReplayPendingError) { + // The nonce is held by an attempt not (yet) accepted: not a replay. + return { status: 503, body: { error: "Request is already being processed; retry later" } }; + } // A malformed secret or an unavailable crypto implementation must fail as // a clean 401, never an uncaught 500. log.warn( @@ -88,82 +125,281 @@ export async function processWebhook( return { status: 401, body: { error: "Invalid signature" } }; } - const event = provider.parse(body, headers); - if (!event) { - return { status: 400, body: { error: "Invalid event" } }; + // From here on, one-time claims are held (the custom-webhook nonce taken + // by `verify`, the delivery dedup key). Only once the event is accepted + // (queued, or dispatched without a transient failure) are they confirmed; + // otherwise they are given back, so the sender's retry is not refused. A + // claim that could not be given back is never mistaken for an accepted + // event: a retry finds it unconfirmed and is asked to come back later. + const claims = idempotencyStore(env.DB, env.KV); + const held: { key: string; ttl: number }[] = []; + const nonce = headers["x-webhooker-nonce"]; + if (provider.id === "custom" && nonce && headers["x-webhooker-timestamp"]) { + held.push({ + key: await replayNonceKey(effectiveEnv.GITHUB_WEBHOOK_SECRET, nonce), + ttl: NONCE_TTL_SECONDS, + }); } - - // Auto-provision GitHub App installations so tenant isolation is configured - // without manual id entry: a group is created (or existing matching groups - // are bound) before the event is dispatched. - if ( - provider.id === "github" && - event.event === "installation" && - event.payload.action === "created" && - event.installationId != null - ) { - const install = event.payload.installation as { account?: { login?: string } } | undefined; - const account = install?.account?.login ?? ""; - try { - const group = await ensureInstallationGroup(env.KV, event.installationId, account); - if (group) { - await recordAudit(env.DB, { - ts: Date.now(), - actorLogin: account || undefined, - action: "installation.created", - targetType: "group", - targetId: group.id, - groupId: group.id, - }); - } - } catch (err) { - log.warn( - { err, installationId: event.installationId }, - "Failed to auto-provision installation group", - ); + const releaseClaims = async (): Promise => { + for (const { key } of held.splice(0)) { + await claims.release(key).catch((err: unknown) => { + log.warn({ requestId, key, err }, "Failed to release a webhook claim"); + }); } - } + }; + const confirmClaims = async (): Promise => { + for (const { key, ttl } of held.splice(0)) { + // Unconfirmed, a claim only delays a duplicate until it expires. + await confirmClaim(claims, key, ttl).catch((err: unknown) => { + log.warn({ requestId, key, err }, "Failed to confirm a webhook claim"); + }); + } + }; + const retryLater = (error: string): WebhookResult => ({ + status: 503, + body: { error, requestId }, + }); + + try { + const event = provider.parse(body, headers); + if (!event) { + await releaseClaims(); + return { status: 400, body: { error: "Invalid event" } }; + } + if (tenantId) event.installationId = undefined; - if (event.deliveryId) { // Dedup via the idempotency store: a provider/tenant-scoped key means // different accounts may reuse a delivery id without colliding, while - // retries of the same delivery never dispatch twice. - const store = idempotencyStore(env.DB, env.KV); - const key = deliveryKey(provider.id, tenantId, event.deliveryId); - if (!(await store.claim(key, 120))) { - return { status: 200, body: { ok: true, duplicate: true, requestId } }; + // retries of the same delivery never dispatch twice. An inline dispatch + // holds its claim for as long as it may run. + // A custom sender's ids are namespaced (`d:`) as its delivery identity is + // below: no id it picks can take the key of a nonce-identified request. + const dedupKey = event.deliveryId + ? deliveryKey( + provider.id, + tenantId, + provider.id === "custom" ? `d:${event.deliveryId}` : event.deliveryId, + tenant?.instance, + ) + : null; + if (dedupKey) { + const ttl = env.QUEUE ? QUEUED_CLAIM_SECONDS : INLINE_CLAIM_SECONDS; + const state = await claimOnce(claims, dedupKey, ttl); + if (state === "done") { + // Already accepted: this request's own nonce is used up as well. + await confirmClaims(); + return { status: 200, body: { ok: true, duplicate: true, requestId } }; + } + if (state === "pending") { + // Still in flight elsewhere, or left over from an attempt that was + // not accepted: the event may not be on its way yet. + await releaseClaims(); + return retryLater("Delivery is already being processed; retry later"); + } + held.push({ key: dedupKey, ttl }); } + + // Provisioning runs only for an event not yet accepted (a duplicate + // returned above): a redelivery must not redo a binding an admin undid. + // Auto-provision GitHub App installations so tenant isolation is + // configured without manual id entry: a group is created (or existing + // matching groups are bound) before the event is dispatched. Only events + // signed with the operator's own secret (the global endpoint) may do this. + if ( + !tenantId && + provider.id === "github" && + event.event === "installation" && + event.payload.action === "created" && + event.installationId != null + ) { + const install = event.payload.installation as { account?: { login?: string } } | undefined; + const account = install?.account?.login ?? ""; + // Done once per installation, on record as long as delivery state is + // kept: a redelivery after the dedup claim lapsed (minutes) must not + // redo a binding an admin undid since. Recorded only once it succeeded, + // so a provisioning that failed midway is completed by the retry. + const doneKey = `installation-provisioned:${event.installationId}`; + try { + if (!(await claims.has(doneKey))) { + const group = await ensureInstallationGroup( + env.KV, + event.installationId, + account, + env.DB, + ); + await claims.claim(doneKey, PROVISIONED_TTL_SECONDS); + if (group) { + await recordAudit(env.DB, { + ts: Date.now(), + actorLogin: account || undefined, + action: "installation.created", + targetType: "group", + targetId: group.id, + groupId: group.id, + groupInstance: group.instance, + }); + } + } + } catch (err) { + // Not accepted: the consumer never provisions, so a sender retry is + // the only way the group gets created (provisioning is idempotent). + log.warn( + { err, installationId: event.installationId }, + "Failed to auto-provision installation group", + ); + await releaseClaims(); + return retryLater("Installation provisioning failed; retry later"); + } + } + + // A stable identity across the sender's retries: the delivery id, else the + // replay-protection nonce of a signed request. Queued or inline, it keys + // the delivery's state and target ledger, so a retry of a request whose + // first attempt went through after all is recognized, not sent again. + // A custom sender picks its delivery ids freely: they get a namespace of + // their own, so none can coincide with a nonce-derived identity. + const nonceKey = held.find((h) => h.key.startsWith("nonce:"))?.key; + const identity = + event.deliveryId != null + ? provider.id === "custom" + ? `d:${event.deliveryId}` + : event.deliveryId + : nonceKey + ? `n:${nonceKey}` + : undefined; + + // Inline, a request identified by its nonce alone is held for the whole + // dispatch, as a delivery id is above: its nonce claim is short (shorter + // than a long dispatch), and a copy arriving meanwhile must not send the + // targets not yet on the ledger a second time. + if (!env.QUEUE && event.deliveryId == null && identity) { + const key = deliveryKey(provider.id, tenantId, identity, tenant?.instance); + const state = await claimOnce(claims, key, INLINE_CLAIM_SECONDS); + if (state === "done") { + await confirmClaims(); + return { status: 200, body: { ok: true, duplicate: true, requestId } }; + } + if (state === "pending") { + await releaseClaims(); + return retryLater("Delivery is already being processed; retry later"); + } + held.push({ key, ttl: INLINE_CLAIM_SECONDS }); + } + + if (env.QUEUE) { + const message: DeliveryMessage = { + deliveryId: identity ?? requestId, + groupId: tenantId, + groupInstance: tenant?.instance, + provider: provider.id, + event: event.event, + payload: event.payload, + installationId: event.installationId, + receivedAt: Date.now(), + requestId, + stateVersion: 2, + }; + // Acknowledge only once the event is durably queued. + await enqueueWebhook(env, message); + await confirmClaims(); + return { status: 200, body: { ok: true, requestId } }; + } + + // Inline dispatch (no queue): there is no durable retry, so the sender + // is answered only after the dispatch, and asked to retry while a target + // failed transiently. A delivery id gets a target ledger, so targets + // already sent are skipped when the sender redelivers. + // One consistent snapshot of routes and groups serves the dispatch — read + // fresh: a route or target removed moments ago in another isolate must + // not still be dispatched to from this isolate's cache. + const snapshot = await loadConfigSnapshot(env, { fresh: true }); + if (tenantId) { + const current = snapshot.groups.find((g) => g.id === tenantId); + if (!current || current.instance !== tenant?.instance) { + // Gone or replaced since its row was read: not this group's event now. + await releaseClaims(); + if (snapshot.config.unreadableGroups?.includes(tenantId)) { + return retryLater("Group configuration unreadable; retry later"); + } + return { status: 404, body: { error: "Group not found" } }; + } + } + const scoped = tenantId + ? { + ...snapshot.config, + routes: snapshot.config.routes.filter((r) => r.groupId === tenantId), + } + : snapshot.config; + const ledgerKey = identity + ? deliveryStateKey(provider.id, tenantId, identity, tenant?.instance) + : null; + const run = (async (): Promise => { + const recorded = new Set(); + // Dispatched under the delivery's identity (as a queued copy is), so the + // platform idempotency keys stay the same when the sender retries. + const delivered = identity ? { ...event, deliveryId: identity } : event; + // Sending stops well before the claim held for this dispatch expires + // (a target takes under a minute at worst): a copy arriving after that + // could otherwise send the targets still in flight. What is left is a + // transient failure, so the sender retries it (the ledger skips the rest). + const sendUntil = Date.now() + INLINE_SEND_BUDGET_MS; + const summary = await dispatchEvent(scoped, delivered, env, snapshot.groups, { + tenantId, + beforeTarget: async (): Promise => Date.now() < sendUntil, + skipTarget: ledgerKey ? targetDoneChecker(env, ledgerKey) : undefined, + afterTarget: ledgerKey + ? async (targetKey): Promise => { + await markTargetsDone(env, ledgerKey, [targetKey]); + recorded.add(targetKey); + } + : undefined, + }); + const permanent = summary.failures.filter((f) => !isRetryableFailure(f)); + if (ledgerKey) { + await markTargetsDone( + env, + ledgerKey, + [...(summary.succeeded ?? []), ...permanent.flatMap((f) => f.key ?? [])].filter( + (k) => !recorded.has(k), + ), + { permanentFailure: permanent.length > 0 }, + ); + } + // A transient failure is always retried: without a ledger the retry + // sends the other targets again too — duplicates rather than a loss. + return { + retry: summary.failures.some(isRetryableFailure), + // Targets that failed for good in an earlier attempt still count. + failed: + permanent.length > 0 || + (ledgerKey != null && (await hadPermanentFailure(env, ledgerKey))), + }; + })(); + // The dispatch keeps running should the sender hang up meanwhile. + waitUntil(run.catch(() => undefined)); + let outcome: InlineOutcome; + try { + outcome = await run; + } catch (err) { + log.error({ requestId, err }, "Dispatch failed"); + await releaseClaims(); + return retryLater("Dispatch failed; retry later"); + } + if (outcome.retry) { + log.warn({ requestId }, "Inline dispatch failed transiently"); + await releaseClaims(); + return retryLater("Some targets failed temporarily; retry later"); + } + await confirmClaims(); + // Accepted for good; a target that can never take the event is reported. + return outcome.failed + ? { status: 200, body: { ok: true, requestId, failed: true } } + : { status: 200, body: { ok: true, requestId } }; + } catch (err) { + log.error({ requestId, err }, "Failed to accept webhook"); + await releaseClaims(); + return retryLater("Temporarily unable to accept webhook"); } - - const config = await loadConfig(env); - if (tenantId) { - config.routes = config.routes.filter((r) => r.groupId === tenantId); - } - - if (env.QUEUE) { - const message: DeliveryMessage = { - deliveryId: event.deliveryId ?? requestId, - groupId: tenantId, - provider: provider.id, - event: event.event, - payload: event.payload, - installationId: event.installationId, - receivedAt: Date.now(), - requestId, - }; - const enqueue = enqueueWebhook(env, message).catch((err) => - log.error({ requestId, err }, "Failed to enqueue webhook"), - ); - waitUntil(enqueue); - return { status: 200, body: { ok: true, requestId } }; - } - - const dispatch = dispatchEvent(config, event, env, groups).catch((err) => - log.error({ requestId, err }, "Dispatch failed"), - ); - waitUntil(dispatch); - - return { status: 200, body: { ok: true, requestId } }; } /** h3 wrapper for `POST /webhook` / `POST /webhook/:groupId`. */ diff --git a/tests/custom-replay.test.ts b/tests/custom-replay.test.ts index 4d6bbba..06c9029 100644 --- a/tests/custom-replay.test.ts +++ b/tests/custom-replay.test.ts @@ -1,6 +1,7 @@ import { describe, it, expect } from "bun:test"; import { createHmac } from "crypto"; -import { customProvider } from "../server/lib/providers/custom"; +import { customProvider, ReplayPendingError, replayNonceKey } from "../server/lib/providers/custom"; +import { acceptedKey, idempotencyStore } from "../server/lib/lib/idempotency"; import type { Env } from "../server/lib/types"; function createMockKV(): KVNamespace { @@ -60,9 +61,29 @@ describe("custom provider replay protection", () => { "x-webhooker-nonce": nonce, }; expect(await customProvider.verify(body, headers, env)).toBe(true); + // While the first request is not confirmed as accepted, the nonce is + // neither free nor a proven replay. + expect(customProvider.verify(body, headers, env)).rejects.toBeInstanceOf(ReplayPendingError); + const key = await replayNonceKey(secret, nonce); + await idempotencyStore(env.DB, env.KV).claim(acceptedKey(key), 600); expect(await customProvider.verify(body, headers, env)).toBe(false); }); + it("scopes nonces to the signing secret", async () => { + const kv = createMockKV(); + const body = JSON.stringify({ hello: "world" }); + const nonce = "shared-nonce"; + for (const secret of ["tenant-a", "tenant-b"]) { + const env = { ...createEnv(secret), KV: kv }; + const headers = { + "x-webhooker-signature": replaySignature(secret, now, nonce, body), + "x-webhooker-timestamp": String(now), + "x-webhooker-nonce": nonce, + }; + expect(await customProvider.verify(body, headers, env)).toBe(true); + } + }); + it("rejects a timestamp outside the window", async () => { const secret = "s3cret"; const env = createEnv(secret); diff --git a/tests/ingress-dispatch.test.ts b/tests/ingress-dispatch.test.ts new file mode 100644 index 0000000..2c3ce07 --- /dev/null +++ b/tests/ingress-dispatch.test.ts @@ -0,0 +1,560 @@ +import { describe, it, expect, afterEach } from "bun:test"; +import { createHmac } from "crypto"; +import { processWebhook } from "../server/lib/webhook"; +import { dispatchEvent } from "../server/lib/core/dispatch"; +import { resetConfigStore } from "../server/lib/config"; +import { githubApiUrl } from "../server/lib/github/app-token"; +import { htmlToText } from "../server/lib/formatters/helpers"; +import { formatEvent } from "../server/lib/formatters"; +import { renderNeutralMessage as renderTelegram } from "../server/lib/drivers/telegram/render"; +import { createMemoryKV, createSqliteD1 } from "./d1-sqlite"; +import type { Config, Env, Group, Route } from "../server/lib/types"; +import type { DeliveryMessage, DispatchSummary } from "../server/lib/queue/delivery"; + +const restoredFetch = globalThis.fetch; +afterEach(() => { + globalThis.fetch = restoredFetch; +}); + +function sign(body: string, secret: string): string { + return `sha256=${createHmac("sha256", secret).update(body).digest("hex")}`; +} + +function config(routes: Route[]): Config { + return { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes, + }; +} + +function route(id: string, groupId: string, extra: Partial = {}): Route { + return { + id, + name: id, + enabled: true, + filters: [{ type: "event", match: ["push", "workflow_run", "issues"] }], + targets: [{ platform: "discord", channelId: "c1" }], + groupId, + ...extra, + }; +} + +const group = (id: string, extra: Partial = {}): Group => ({ + id, + name: id, + adminIds: [], + ...extra, +}); + +describe("webhook ingress", () => { + function tenantEnv(queue?: Queue, routes: Route[] = [route("r", "team")]): Env { + const { kv, store } = createMemoryKV(); + store.set("config:groups", JSON.stringify([group("team")])); + store.set("config:routes", JSON.stringify(routes)); + store.set("tenant:team", "tenant-secret"); + resetConfigStore(kv); + return { + GITHUB_WEBHOOK_SECRET: "global", + DISCORD_TOKEN: "t", + KV: kv, + DB: {} as D1Database, + QUEUE: queue, + }; + } + + it("never lets an accepted installation event provision again when redelivered", async () => { + const sent: DeliveryMessage[] = []; + const queue = { send: async (m: DeliveryMessage) => void sent.push(m) } as unknown as Queue; + const env = tenantEnv(queue); + const body = JSON.stringify({ + action: "created", + installation: { id: 123, account: { login: "acme" } }, + }); + const headers = { + "x-github-event": "installation", + "x-github-delivery": "inst-dup", + "x-hub-signature-256": sign(body, "global"), + }; + expect((await processWebhook(env, body, headers, () => undefined)).status).toBe(200); + const { loadGroups, updateGroup } = await import("../server/lib/web/groups"); + const created = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "inst-123"); + expect(created?.installationId).toBe(123); + // A super admin unbinds it; the same delivery arrives again. + await updateGroup(env.KV, "inst-123", (g) => ({ ...g, installationId: undefined })); + const again = await processWebhook(env, body, headers, () => undefined); + expect(again.body).toMatchObject({ duplicate: true }); + const after = (await loadGroups(env.KV, { fresh: true })).find((g) => g.id === "inst-123"); + expect(after?.installationId).toBeUndefined(); + }); + + it("asks for a retry when installation provisioning failed, queueing nothing", async () => { + const sent: DeliveryMessage[] = []; + const queue = { send: async (m: DeliveryMessage) => void sent.push(m) } as unknown as Queue; + const env = tenantEnv(queue); + const kvPut = env.KV.put.bind(env.KV); + env.KV.put = (async (key: string, ...rest: unknown[]): Promise => + key === "config:groups" + ? Promise.reject(new Error("KV down")) + : (kvPut as (...a: unknown[]) => Promise)(key, ...rest)) as KVNamespace["put"]; + const body = JSON.stringify({ + action: "created", + installation: { id: 99, account: { login: "acme" } }, + }); + const res = await processWebhook( + env, + body, + { + "x-github-event": "installation", + "x-github-delivery": "inst-1", + "x-hub-signature-256": sign(body, "global"), + }, + () => undefined, + ); + expect(res.status).toBe(503); + expect(sent).toHaveLength(0); + }); + + it("never passes a claim it could not give back for an accepted duplicate", async () => { + let fail = true; + const sent: DeliveryMessage[] = []; + const queue = { + send: async (m: DeliveryMessage) => { + if (fail) throw new Error("queue down"); + sent.push(m); + }, + } as unknown as Queue; + const env = tenantEnv(queue); + // Giving the claim back fails too. + const kvDelete = env.KV.delete.bind(env.KV); + env.KV.delete = async (key: string): Promise => + key.startsWith("delivery:") ? Promise.reject(new Error("KV down")) : kvDelete(key); + const body = JSON.stringify({ ref: "refs/heads/main" }); + const headers = { + "x-github-event": "push", + "x-github-delivery": "d-3", + "x-hub-signature-256": sign(body, "tenant-secret"), + }; + expect((await processWebhook(env, body, headers, () => undefined, "team")).status).toBe(503); + fail = false; + // The leftover claim is not confirmed: the retry is told to come back, + // never acknowledged as a duplicate of an event that was never queued. + const retry = await processWebhook(env, body, headers, () => undefined, "team"); + expect(retry.status).toBe(503); + expect(sent).toHaveLength(0); + }); + + it("keeps one delivery identity for a nonce-signed request retried after an unclear enqueue", async () => { + const sent: DeliveryMessage[] = []; + let first = true; + const queue = { + send: async (m: DeliveryMessage) => { + sent.push(m); + // The queue took it, but the answer was lost. + if (first) { + first = false; + throw new Error("connection reset"); + } + }, + } as unknown as Queue; + const env = tenantEnv(queue); + const body = JSON.stringify({ alert: "disk full" }); + const ts = String(Math.floor(Date.now() / 1000)); + const headers = { + "x-webhooker-signature": `sha256=${createHmac("sha256", "tenant-secret") + .update(`${ts}.n-1.${body}`) + .digest("hex")}`, + "x-webhooker-timestamp": ts, + "x-webhooker-nonce": "n-1", + }; + expect((await processWebhook(env, body, headers, () => undefined, "team")).status).toBe(503); + expect((await processWebhook(env, body, headers, () => undefined, "team")).status).toBe(200); + expect(sent).toHaveLength(2); + // Same delivery: the consumer recognizes the second copy. + expect(sent[1]!.deliveryId).toBe(sent[0]!.deliveryId); + }); + + it("asks for a retry on a transient failure even without a delivery id", async () => { + const env = tenantEnv(undefined, [ + route("r", "team", { + event: undefined, + filters: [{ type: "event", match: "custom" }], + targets: [ + { platform: "discord", channelId: "ok" }, + { platform: "discord", channelId: "flaky" }, + ], + }), + ]); + globalThis.fetch = async (input): Promise => + String(input).includes("/channels/flaky/") + ? new Response(JSON.stringify({ retry_after: 999 }), { status: 429 }) + : new Response(JSON.stringify({ id: "m" }), { status: 200 }); + const body = JSON.stringify({ alert: "disk full" }); + const headers = { "x-webhooker-signature": sign(body, "tenant-secret") }; + // No identity to skip what was sent: a duplicate beats a lost target. + const res = await processWebhook(env, body, headers, () => undefined, "team"); + expect(res.status).toBe(503); + }); + + it("answers an inline dispatch after it ran and asks for a retry on a transient failure", async () => { + const env = tenantEnv(undefined, [ + route("r", "team", { + targets: [ + { platform: "discord", channelId: "ok" }, + { platform: "discord", channelId: "flaky" }, + ], + }), + ]); + const calls: Record = {}; + let limited = true; + globalThis.fetch = async (input): Promise => { + const channel = String(input).split("/channels/")[1]!.split("/")[0]!; + calls[channel] = (calls[channel] ?? 0) + 1; + if (channel === "flaky" && limited) { + return new Response(JSON.stringify({ retry_after: 999 }), { status: 429 }); + } + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }; + const body = JSON.stringify({ ref: "refs/heads/main" }); + const headers = { + "x-github-event": "push", + "x-github-delivery": "d-4", + "x-hub-signature-256": sign(body, "tenant-secret"), + }; + const first = await processWebhook(env, body, headers, () => undefined, "team"); + expect(first.status).toBe(503); + limited = false; + const retry = await processWebhook(env, body, headers, () => undefined, "team"); + expect(retry.status).toBe(200); + // The target sent the first time is not sent again. + expect(calls).toEqual({ ok: 1, flaky: 2 }); + const again = await processWebhook(env, body, headers, () => undefined, "team"); + expect(again.body).toMatchObject({ duplicate: true }); + }); + + it("answers 503 and releases the dedup claim when the queue rejects the event", async () => { + let fail = true; + const sent: DeliveryMessage[] = []; + const queue = { + send: async (m: DeliveryMessage) => { + if (fail) throw new Error("queue down"); + sent.push(m); + }, + } as unknown as Queue; + const env = tenantEnv(queue); + const body = JSON.stringify({ ref: "refs/heads/main" }); + const headers = { + "x-github-event": "push", + "x-github-delivery": "d-1", + "x-hub-signature-256": sign(body, "tenant-secret"), + }; + const first = await processWebhook(env, body, headers, () => undefined, "team"); + expect(first.status).toBe(503); + fail = false; + // The sender's redelivery is accepted, not dropped as a duplicate. + const retry = await processWebhook(env, body, headers, () => undefined, "team"); + expect(retry.status).toBe(200); + expect((retry.body as { duplicate?: boolean }).duplicate).toBeUndefined(); + expect(sent).toHaveLength(1); + }); + + it("drops the installation claim of tenant-signed payloads", async () => { + const sent: DeliveryMessage[] = []; + const queue = { send: async (m: DeliveryMessage) => void sent.push(m) } as unknown as Queue; + const env = tenantEnv(queue); + const body = JSON.stringify({ + action: "created", + installation: { id: 999, account: { login: "victim" } }, + }); + const headers = { + "x-github-event": "installation", + "x-github-delivery": "d-2", + "x-hub-signature-256": sign(body, "tenant-secret"), + }; + const res = await processWebhook(env, body, headers, () => undefined, "team"); + expect(res.status).toBe(200); + expect(sent[0]!.installationId).toBeUndefined(); + // No group was provisioned for the claimed installation. + const groups = JSON.parse((await env.KV.get("config:groups"))!) as Group[]; + expect(groups.map((g) => g.id)).toEqual(["team"]); + }); + + it("never lets a custom delivery id take the key of a nonce-identified request", async () => { + const env = tenantEnv(undefined, [ + route("r", "team", { + event: undefined, + filters: [{ type: "event", match: "custom" }], + targets: [{ platform: "discord", channelId: "ok" }], + }), + ]); + let sends = 0; + globalThis.fetch = async (): Promise => { + sends++; + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }; + const { replayNonceKey } = await import("../server/lib/providers/custom"); + const nonce = "n-77"; + // Event A names, as its delivery id, exactly the identity event B's nonce gets. + const a = JSON.stringify({ deliveryId: `n:${await replayNonceKey("tenant-secret", nonce)}` }); + expect( + ( + await processWebhook( + env, + a, + { "x-webhooker-signature": sign(a, "tenant-secret") }, + () => undefined, + "team", + ) + ).status, + ).toBe(200); + const b = JSON.stringify({ alert: "other event" }); + const ts = String(Math.floor(Date.now() / 1000)); + const res = await processWebhook( + env, + b, + { + "x-webhooker-signature": `sha256=${createHmac("sha256", "tenant-secret") + .update(`${ts}.${nonce}.${b}`) + .digest("hex")}`, + "x-webhooker-timestamp": ts, + "x-webhooker-nonce": nonce, + }, + () => undefined, + "team", + ); + expect(res.status).toBe(200); + expect((res.body as { duplicate?: boolean }).duplicate).toBeUndefined(); + expect(sends).toBe(2); + }); +}); + +describe("dispatch", () => { + function env(): Env { + return { GITHUB_WEBHOOK_SECRET: "", KV: createMemoryKV().kv, DB: createSqliteD1().db }; + } + + it("records a formatter crash as a permanent failure instead of dropping it", async () => { + globalThis.fetch = async (): Promise => new Response("{}", { status: 200 }); + const e = env(); + const summary = await dispatchEvent( + config([route("r", "g")]), + // `issue: null` makes the issues formatter throw. + { event: "issues", payload: { issue: null } as never }, + e, + [group("g")], + ); + expect(summary.failures).toHaveLength(1); + expect(summary.failures[0]!.errorCode).toBe("FORMAT"); + }); + + it("skips targets a previous attempt already finished", async () => { + const urls: string[] = []; + globalThis.fetch = async (input): Promise => { + urls.push(String(input)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }; + const r = route("r", "g", { + targets: [ + { platform: "discord", channelId: "done" }, + { platform: "discord", channelId: "todo" }, + ], + }); + const summary = await dispatchEvent( + config([r]), + { event: "push", payload: {} }, + env(), + [group("g")], + { skipTarget: async (key) => key.endsWith(":done") }, + ); + expect(urls.some((u) => u.includes("/done/"))).toBe(false); + expect(urls.some((u) => u.includes("/todo/"))).toBe(true); + expect(summary.succeeded).toEqual(["g:r:discord:todo"]); + }); + + it("never mints App tokens for tenant events and only calls the GitHub API", async () => { + const urls: string[] = []; + globalThis.fetch = async (input): Promise => { + urls.push(String(input)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }; + const e = { ...env(), GITHUB_APP_ID: "1", GITHUB_PRIVATE_KEY: "pem" }; + await dispatchEvent( + config([route("r", "g")]), + { + event: "workflow_run", + payload: { workflow_run: { id: 1, jobs_url: "https://evil.example/steal" } }, + installationId: 42, + }, + e, + [group("g")], + { tenantId: "g" }, + ); + expect(urls.some((u) => u.includes("evil.example"))).toBe(false); + expect(urls.some((u) => u.includes("access_tokens"))).toBe(false); + }); + + it("keeps editing the same message after a transient edit failure", async () => { + const calls: Array<{ method: string; url: string }> = []; + let editStatus = 200; + globalThis.fetch = async (input, init): Promise => { + const method = init?.method ?? "GET"; + calls.push({ method, url: String(input) }); + if (method === "PATCH") { + return editStatus === 200 + ? new Response(JSON.stringify({ id: "m1" }), { status: 200 }) + : new Response("bad request", { status: editStatus }); + } + return new Response(JSON.stringify({ id: "m1" }), { status: 200 }); + }; + const e = env(); + const run = (status: string, updated: string): Promise => + dispatchEvent( + config([route("r", "g")]), + { + event: "workflow_run", + provider: "github", + payload: { + action: "in_progress", + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + workflow_run: { id: 7, status, name: "CI", updated_at: updated, jobs: [{}] }, + }, + }, + e, + [group("g")], + ); + await run("queued", "2026-01-01T00:00:00Z"); + expect(calls.filter((c) => c.method === "POST")).toHaveLength(1); + editStatus = 400; + const failed = await run("in_progress", "2026-01-01T00:01:00Z"); + expect(failed.failures).toHaveLength(1); + // No second message was posted for the same chain. + expect(calls.filter((c) => c.method === "POST")).toHaveLength(1); + editStatus = 200; + // An older event arriving late does not overwrite the newer state. + const before = calls.length; + await run("queued", "2025-12-31T00:00:00Z"); + expect(calls.length).toBe(before); + }); + + it("separates update chains of two forges with the same repository name", async () => { + const posts: string[] = []; + globalThis.fetch = async (input, init): Promise => { + if ((init?.method ?? "GET") === "POST") posts.push(String(input)); + return new Response(JSON.stringify({ id: `m${posts.length}` }), { status: 200 }); + }; + const e = env(); + for (const host of ["git1.example.com", "git2.example.com"]) { + await dispatchEvent( + config([route("r", "g")]), + { + event: "workflow_run", + provider: "gitea", + payload: { + repository: { full_name: "o/r", html_url: `https://${host}/o/r` }, + workflow_run: { id: 7, status: "queued", name: "CI", jobs: [{}] }, + }, + }, + e, + [group("g")], + ); + } + expect(posts).toHaveLength(2); + }); +}); + +describe("formatting safety", () => { + it("keeps out-of-range character references verbatim", () => { + expect(htmlToText("a � b A")).toBe("a � b A"); + }); + + it("offers merge/close buttons only for github.com", () => { + const payload = { + action: "opened", + repository: { full_name: "o/r", html_url: "https://gitea.example/o/r" }, + pull_request: { number: 1, state: "open", title: "t" }, + }; + const r = route("r", "g"); + expect(formatEvent(r, { event: "pull_request", provider: "gitea", payload }).actions).toBe( + undefined, + ); + const gh = { + ...payload, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }; + expect( + formatEvent(r, { event: "pull_request", provider: "github", payload: gh }).actions, + ).toHaveLength(2); + }); + + it("shows the deployment state, not the status object", () => { + const message = formatEvent(route("r", "g"), { + event: "deployment_status", + payload: { + repository: { full_name: "o/r" }, + deployment_status: { state: "success", environment: "prod" }, + deployment: { id: 1 }, + }, + }); + const status = message.fields!.find((f) => /status/i.test(f.name))!; + expect(status.value).toContain("success"); + expect(status.value).not.toContain("[object Object]"); + }); + + it("drops Telegram's author emoji when the group turned emoji off", () => { + const html = renderTelegram({ title: "t", author: { name: "bob" }, emoji: false }); + expect(html).not.toContain("👤"); + }); + + it("accepts only GitHub REST repository URLs for enrichment", () => { + expect(githubApiUrl("https://api.github.com/repos/o/r/actions/runs/1/jobs")).not.toBeNull(); + expect(githubApiUrl("https://evil.example/repos/o/r")).toBeNull(); + expect(githubApiUrl("http://api.github.com/repos/o/r")).toBeNull(); + expect(githubApiUrl("https://api.github.com/user")).toBeNull(); + }); +}); + +describe("round-trip safety", () => { + it("records an unreadable delivery ledger as a retryable failure", async () => { + globalThis.fetch = async (): Promise => + new Response(JSON.stringify({ id: "m" }), { status: 200 }); + const summary = await dispatchEvent( + config([route("r", "g")]), + { event: "push", payload: {} }, + { GITHUB_WEBHOOK_SECRET: "", KV: createMemoryKV().kv, DB: createSqliteD1().db }, + [group("g")], + { + skipTarget: async () => { + throw new Error("D1 down"); + }, + }, + ); + expect(summary.failures).toHaveLength(1); + expect(summary.failures[0]!.errorCode).toBeUndefined(); + }); + + it("gives the custom-webhook nonce back when the queue rejects the event", async () => { + const { kv, store } = createMemoryKV(); + store.set("config:groups", JSON.stringify([group("team")])); + store.set("tenant:team", "tenant-secret"); + resetConfigStore(kv); + let fail = true; + const queue = { + send: async (): Promise => { + if (fail) throw new Error("queue down"); + }, + } as unknown as Queue; + const env: Env = { GITHUB_WEBHOOK_SECRET: "g", KV: kv, DB: {} as D1Database, QUEUE: queue }; + const body = JSON.stringify({ title: "hi" }); + const ts = String(Math.floor(Date.now() / 1000)); + const nonce = "n-1"; + const headers = { + "x-webhooker-signature": `sha256=${createHmac("sha256", "tenant-secret").update(`${ts}.${nonce}.${body}`).digest("hex")}`, + "x-webhooker-timestamp": ts, + "x-webhooker-nonce": nonce, + }; + expect((await processWebhook(env, body, headers, () => undefined, "team")).status).toBe(503); + fail = false; + expect((await processWebhook(env, body, headers, () => undefined, "team")).status).toBe(200); + }); +}); diff --git a/tests/lifecycle-safety.test.ts b/tests/lifecycle-safety.test.ts new file mode 100644 index 0000000..e092abc --- /dev/null +++ b/tests/lifecycle-safety.test.ts @@ -0,0 +1,3034 @@ +import { describe, it, expect } from "bun:test"; +import { authLogin, claimLogin } from "../server/lib/web/session"; +import { BeforeGitHubError, commentAsUser } from "../server/lib/github/oauth"; +import { kvConfigStore, d1ConfigStore } from "../server/lib/storage/config-store"; +import { createInvite, deleteGroupInvites, getInvite } from "../server/lib/web/invites"; +import { loadFragments, saveGroupFragments } from "../server/lib/fragments"; +import { handleQueueBatch } from "../server/lib/queue/consumer"; +import { dispatchEvent } from "../server/lib/core/dispatch"; +import { customProvider } from "../server/lib/providers/custom"; +import { renderNeutralMessage as renderTelegram } from "../server/lib/drivers/telegram/render"; +import { listInvites } from "../server/lib/web/invites"; +import { sendMessage as discordSend } from "../server/lib/drivers/discord/rest"; +import type { DeliveryMessage } from "../server/lib/queue/delivery"; +import type { Config, Env, Group, Route } from "../server/lib/types"; +import { createMemoryKV, createSqliteD1 } from "./d1-sqlite"; + +const route = (id: string, groupId: string): Route => ({ + id, + name: id, + enabled: true, + filters: [{ type: "event", match: "push" }], + targets: [{ platform: "discord", channelId: "c1" }], + groupId, +}); + +describe("login ownership", () => { + it("never lets a second account inherit what was granted to a login", async () => { + const { kv } = createMemoryKV(); + expect(await claimLogin(kv, "42", "alice")).toBe(true); + expect(await claimLogin(kv, "42", "Alice")).toBe(true); + // The name was renamed away and registered by someone else. + expect(await claimLogin(kv, "77", "alice")).toBe(false); + expect(authLogin({ userId: "77", login: "alice", loginTrusted: false })).toBe(""); + expect(authLogin({ userId: "42", login: "alice" })).toBe("alice"); + }); +}); + +describe("payload shapes", () => { + it("refuses a top-level JSON array from the custom provider", () => { + expect(customProvider.parse("[1,2,3]", {})).toBeNull(); + expect(customProvider.parse('{"a":1}', {})?.payload).toEqual({ a: 1 }); + }); + + it("never renders a Markdown hard break as the
Telegram rejects", () => { + const html = renderTelegram({ title: "t", description: "first line \nsecond line" }); + expect(html).not.toContain(" { + it("tells a failure before GitHub was called from a GitHub answer", async () => { + const { kv } = createMemoryKV(); + kv.get = (async () => { + throw new Error("KV down"); + }) as typeof kv.get; + await expect(commentAsUser(kv, "42", "o", "r", 1, "hi")).rejects.toBeInstanceOf( + BeforeGitHubError, + ); + }); +}); + +describe("configuration leftovers", () => { + it("never hands routes left under an id to a new group created with it (KV-only)", async () => { + const { kv, store } = createMemoryKV(); + store.set("config:groups", JSON.stringify([])); + // Copies an interrupted rename left behind. + store.set("config:routes", JSON.stringify([route("leftover", "b")])); + const cfg = kvConfigStore(kv); + expect(await cfg.createGroup({ id: "b", name: "B", adminIds: [] })).toBe(true); + expect((await cfg.loadRoutes()).filter((r) => r.groupId === "b")).toEqual([]); + }); + + it("seeds routes of a missing group into an isolated placeholder, not an open one", async () => { + const { db } = createSqliteD1(); + const { kv, store } = createMemoryKV(); + store.set("config:groups", JSON.stringify([])); + store.set( + "config:routes", + JSON.stringify([route("r1", "deleted-private"), { ...route("r2", ""), groupId: undefined }]), + ); + const groups = await d1ConfigStore(db, kv).loadGroups({ fresh: true }); + const placeholder = groups.find((g) => g.id === "deleted-private"); + expect(placeholder?.isolated).toBe(true); + // A new identity: the deleted group's secret and invites never apply to it. + expect(placeholder?.instance).toBeTruthy(); + expect(placeholder?.legacyId).toBeUndefined(); + expect(groups.find((g) => g.id !== "deleted-private")?.isolated).toBeUndefined(); + }); + + it("refuses to replace a group's fragments while one of them is unreadable", async () => { + const { db, sqlite } = createSqliteD1(); + const { kv } = createMemoryKV(); + await d1ConfigStore(db, kv).createGroup({ id: "g", name: "G", adminIds: [] }); + sqlite.exec( + "INSERT INTO d1_fragments (id, group_id, name, node, created_at, updated_at) VALUES ('bad', 'g', 'B', '{oops', 1, 1)", + ); + expect(await loadFragments(db, "g")).toEqual([]); + await expect(saveGroupFragments(db, "g", [])).rejects.toThrow(/unreadable/); + expect(sqlite.query("SELECT COUNT(*) AS n FROM d1_fragments").get()).toEqual({ n: 1 }); + }); +}); + +describe("invites of a former id holder", () => { + it("lists a pre-instance invite still filed under the group's former id", async () => { + const { kv } = createMemoryKV(); + const token = await createInvite(kv, { + groupId: "old", + role: "viewer", + expiresAt: Date.now() + 86400_000, + createdBy: "x", + }); + // Renamed old -> new; moving the invite failed. + const listed = await listInvites(kv, "new", { instance: "i1", legacyId: "old" }); + expect(listed.map((i) => i.token)).toEqual([token]); + }); + + it("deleting a group keeps the invites another group issued under the same id", async () => { + const { kv } = createMemoryKV(); + const foreign = await createInvite(kv, { + groupId: "a", + role: "viewer", + expiresAt: Date.now() + 86400_000, + createdBy: "x", + groupInstance: "renamed-away", + }); + const own = await createInvite(kv, { + groupId: "a", + role: "viewer", + expiresAt: Date.now() + 86400_000, + createdBy: "y", + groupInstance: "current", + }); + await deleteGroupInvites(kv, "a", { instance: "current" }); + expect(await getInvite(kv, own)).toBeNull(); + expect(await getInvite(kv, foreign)).not.toBeNull(); + }); +}); + +describe("queue safety", () => { + it("leaves a delivery and its payload alone while another copy holds a live lease", async () => { + const { db, sqlite } = createSqliteD1(); + const { kv, store } = createMemoryKV(); + const env: Env = { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: db }; + sqlite.exec( + `INSERT INTO delivery_state (key, status, updated_at) VALUES ('delivery-state:github:@global:d1', 'processing', ${Date.now()})`, + ); + store.set("queue:payload:x", "{}"); + const body: DeliveryMessage = { + deliveryId: "d1", + provider: "github", + event: "push", + payloadRef: "queue:payload:x", + payloadRefType: "kv", + receivedAt: Date.now(), + }; + let acked = false; + let retried: unknown = null; + const message = { + body, + attempts: 1, + ack: (): void => { + acked = true; + }, + retry: (opts: unknown): void => { + retried = opts; + }, + }; + await handleQueueBatch( + { + queue: "webhooker-delivery-dlq", + messages: [message], + } as unknown as MessageBatch, + env, + ); + expect(acked).toBe(false); + expect(retried).toEqual({ delaySeconds: 310 }); + expect(store.has("queue:payload:x")).toBe(true); + }); + + it("leaves a group with an unreadable route out whole — its fallback included", async () => { + let sends = 0; + const realFetch = globalThis.fetch; + globalThis.fetch = (async () => { + sends += 1; + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + // The regular route that would have matched is the unreadable one. + routes: [{ ...route("fb", "g"), fallback: true }], + unreadableGroups: ["g"], + }; + const groups: Group[] = [{ id: "g", name: "G", adminIds: [] }]; + const summary = await dispatchEvent(config, { event: "push", payload: {} }, env, groups); + expect(sends).toBe(0); + expect(summary.failures).toHaveLength(1); + } finally { + globalThis.fetch = realFetch; + } + }); + + it("matches each regular route once per dispatch", async () => { + const { matchRoute } = await import("../server/lib/events/match"); + let reads = 0; + const probed = (): Record => { + const payload: Record = {}; + Object.defineProperty(payload, "probe", { + enumerable: true, + get: () => { + reads++; + return "no"; + }, + }); + return payload; + }; + const r: Route = { + ...route("r", "g"), + filters: [{ type: "field", path: "probe", op: "eq", match: "yes" }], + }; + expect(matchRoute(r, { event: "push", payload: probed() })).toBe(false); + const once = reads; + reads = 0; + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [r], + }; + const groups: Group[] = [{ id: "g", name: "G", adminIds: [] }]; + await dispatchEvent(config, { event: "push", payload: probed() }, env, groups); + expect(once).toBeGreaterThan(0); + expect(reads).toBe(once); + }); + + it("reports a group with an unreadable route as a transient failure", async () => { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [], + unreadableGroups: ["g"], + }; + const groups: Group[] = [{ id: "g", name: "G", adminIds: [] }]; + const summary = await dispatchEvent(config, { event: "push", payload: {} }, env, groups); + expect(summary.failures).toHaveLength(1); + expect(summary.failures[0]!.errorCode).toBeUndefined(); + }); +}); + +describe("lost send responses", () => { + it("retries a Discord create with the same enforced nonce", async () => { + const bodies: Array<{ nonce?: string; enforce_nonce?: boolean }> = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + bodies.push(JSON.parse(String(init?.body))); + // The first create went through, but its response was lost. + if (bodies.length === 1) throw new Error("connection reset"); + return new Response(JSON.stringify({ id: "m1" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const result = await discordSend("t", "123", { content: "hi" }); + expect(result.ok).toBe(true); + } finally { + globalThis.fetch = realFetch; + } + expect(bodies).toHaveLength(2); + expect(bodies[0]!.enforce_nonce).toBe(true); + expect(bodies[1]!.nonce).toBe(bodies[0]!.nonce); + expect(bodies[0]!.nonce!.length).toBeLessThanOrEqual(25); + }); +}); + +describe("send keys", () => { + it("gives every attempt of a delivery to a target the same platform nonce", async () => { + const nonces: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + nonces.push((JSON.parse(String(init?.body)) as { nonce: string }).nonce); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [route("r", "g")], + }; + const groups: Group[] = [{ id: "g", name: "G", adminIds: [], instance: "i" }]; + for (const deliveryId of ["d1", "d1", "d2"]) { + await dispatchEvent(config, { event: "push", payload: {}, deliveryId }, env, groups); + } + } finally { + globalThis.fetch = realFetch; + } + expect(nonces).toHaveLength(3); + expect(nonces[1]).toBe(nonces[0]); + expect(nonces[2]).not.toBe(nonces[0]); + }); +}); + +describe("pre-instance message chains", () => { + it("are adopted only by the group that had the id back then", async () => { + const event = { + event: "check_run", + deliveryId: "d", + payload: { + action: "completed", + check_run: { + id: 1, + name: "build", + head_sha: "abc", + status: "completed", + conclusion: "success", + check_suite: { id: 7 }, + }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }, + }; + for (const [legacyId, expected] of [ + [undefined, "POST"], + ["g", "PATCH"], + ] as const) { + const { db, sqlite } = createSqliteD1(); + sqlite.exec( + `INSERT INTO message_tracking (event_id, target_id, message_id, updated_at) + VALUES ('g:r:check_run:o/r:build:abc', 'c1', 'old-message', 1)`, + ); + const methods: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + methods.push(String(init?.method)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [{ ...route("r", "g"), filters: [{ type: "event", match: "check_run" }] }], + }; + // A group re-created under the id has a fresh instance and no legacyId. + const groups: Group[] = [{ id: "g", name: "G", adminIds: [], instance: "i", legacyId }]; + await dispatchEvent(config, event, env, groups); + } finally { + globalThis.fetch = realFetch; + } + expect(methods).toEqual([expected]); + } + }); +}); + +describe("one-time identity migration", () => { + it("trusts no identity field stored before the server assigned them", async () => { + const { db, sqlite } = createSqliteD1(); + const { kv } = createMemoryKV(); + // Rows as the old API stored them: an owner wrote identity fields freely. + sqlite.exec( + `INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES + ('victim', 'V', '{"id":"victim","name":"V","adminIds":[]}', 1, 0, 0), + ('zz-attacker', 'A', '{"id":"zz-attacker","name":"A","adminIds":[],"instance":"same","legacyId":"victim","instanceBackfilled":true}', 1, 0, 0), + ('zz-other', 'O', '{"id":"zz-other","name":"O","adminIds":[],"instance":"same"}', 1, 0, 0)`, + ); + const groups = await d1ConfigStore(db, kv).loadGroups({ fresh: true }); + const byId = new Map(groups.map((g) => [g.id, g])); + expect(byId.get("zz-attacker")!.legacyId).toBe("zz-attacker"); + expect(byId.get("victim")!.legacyId).toBe("victim"); + const instances = groups.map((g) => g.instance); + expect(new Set(instances).size).toBe(3); + expect(instances).not.toContain("same"); + }); + + it("seeds ungrouped legacy routes into a group of their own, never a tenant's", async () => { + const { db } = createSqliteD1(); + const { kv, store } = createMemoryKV(); + store.set( + "config:groups", + JSON.stringify([{ id: "ungrouped", name: "Tenant", adminIds: ["mallory"] }]), + ); + store.set("config:routes", JSON.stringify([{ ...route("global", ""), groupId: undefined }])); + const cfg = d1ConfigStore(db, kv); + const routes = await cfg.loadRoutes({ fresh: true }); + expect(routes.map((r) => r.groupId)).toEqual(["ungrouped-2"]); + const placeholder = (await cfg.loadGroups()).find((g) => g.id === "ungrouped-2")!; + expect(placeholder.adminIds).toEqual([]); + }); +}); + +describe("update chain versions", () => { + it("never lets a late in-progress event undo a completion stamped the same time", async () => { + const calls: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + calls.push(String(init?.method)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + const run = (status: string, conclusion: string | null): Record => ({ + workflow_run: { + id: 5, + workflow_id: 9, + name: "CI", + status, + conclusion, + updated_at: "2026-10-01T00:00:00Z", + html_url: "https://github.com/o/r/actions/runs/5", + }, + workflow: { id: 9, name: "CI" }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }); + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [{ ...route("r", "g"), filters: [{ type: "event", match: "workflow_run" }] }], + }; + const groups: Group[] = [{ id: "g", name: "G", adminIds: [], instance: "i" }]; + await dispatchEvent( + config, + { event: "workflow_run", payload: run("completed", "failure") }, + env, + groups, + ); + await dispatchEvent( + config, + { event: "workflow_run", payload: run("in_progress", null) }, + env, + groups, + ); + } finally { + globalThis.fetch = realFetch; + } + // The completion was posted; the stale in-progress state never edits it. + expect(calls).toEqual(["POST"]); + }); +}); + +describe("identity migration edge cases", () => { + it("keeps identities a KV-only deployment already manages when moving to D1", async () => { + const { db } = createSqliteD1(); + const { kv, store } = createMemoryKV(); + store.set( + "config:groups", + JSON.stringify([ + { + id: "g", + name: "G", + adminIds: [], + instance: "A", + legacyId: "g", + instanceBackfilled: true, + }, + ]), + ); + store.set("config:identity-migrated", "1"); + const groups = await d1ConfigStore(db, kv).loadGroups({ fresh: true }); + expect(groups[0]!.instance).toBe("A"); + }); + + it("only records the migration once the table exists, after a KV-marked run", async () => { + const { db, sqlite } = createSqliteD1(); + const { kv, store } = createMemoryKV(); + store.set("config:identity-migrated", "1"); + sqlite.exec( + `INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES + ('new', 'N', '{"id":"new","name":"N","adminIds":[],"instance":"i1","legacyId":"old","instanceBackfilled":true}', 1, 0, 0)`, + ); + const groups = await d1ConfigStore(db, kv).loadGroups({ fresh: true }); + expect(groups[0]!.legacyId).toBe("old"); + expect(groups[0]!.instance).toBe("i1"); + }); + + it("never keeps a stored instance in the KV-only migration", async () => { + // The instance the migration derives for "victim"... + const { kv, store } = createMemoryKV(); + store.set("config:groups", JSON.stringify([{ id: "victim", name: "V", adminIds: [] }])); + const derived = (await kvConfigStore(kv).loadGroups())[0]!.instance!; + // ...pre-filled by an attacker on their own group before the upgrade. + const { kv: kv2, store: store2 } = createMemoryKV(); + store2.set( + "config:groups", + JSON.stringify([ + { id: "victim", name: "V", adminIds: [] }, + { id: "zz", name: "Z", adminIds: [], instance: derived }, + ]), + ); + const groups = await kvConfigStore(kv2).loadGroups(); + expect(new Set(groups.map((g) => g.instance)).size).toBe(2); + }); +}); + +describe("pattern usability", () => { + it("judges what the matcher runs: AST over flat filters, literals not as regexes", async () => { + const { db, sqlite } = createSqliteD1(); + const { kv } = createMemoryKV(); + const cfg = d1ConfigStore(db, kv); + await cfg.createGroup({ id: "g", name: "G", adminIds: [] }); + await cfg.saveGroupRoutes("g", [route("r1", "g"), route("r2", "g"), route("r3", "g")]); + sqlite.exec(`UPDATE d1_routes SET filters = '[{"type":"repo","match":"/^a(?=b)/"}]', + ast = '{"type":"event","match":"push"}' WHERE id = 'r1'`); + sqlite.exec( + `UPDATE d1_routes SET filters = '[{"type":"repo","op":"contains","match":"/(?=foo)/"}]' WHERE id = 'r2'`, + ); + expect((await cfg.loadSnapshot({ fresh: true })).unreadable).toEqual([]); + // A glob longer than the matcher compiles is not usable. + sqlite.exec( + `UPDATE d1_routes SET filters = '[{"type":"branch","match":"${"a".repeat(201)}*","exclude":true}]' WHERE id = 'r3'`, + ); + expect((await cfg.loadSnapshot({ fresh: true })).unreadable).toEqual(["g"]); + }); +}); + +describe("upgrade compatibility", () => { + it("never re-sends a tenant delivery finished under the key used before the upgrade", async () => { + const { kv, store } = createMemoryKV(); + store.set( + "config:groups", + JSON.stringify([ + { + id: "team", + name: "T", + adminIds: [], + instance: "ti", + legacyId: "team", + instanceBackfilled: true, + }, + ]), + ); + store.set("config:identity-migrated", "1"); + // Finished before the upgrade (state keyed `{provider}:{groupId}:{id}`). + store.set("delivery-state:github:team:d1", JSON.stringify({ status: "delivered", at: 1 })); + // The global endpoint's old key: its owner is never certain. + store.set("delivery-state:github:global:d2", JSON.stringify({ status: "delivered", at: 1 })); + const run = async (body: Partial): Promise => { + let dispatched = 0; + const message = { + body: { + provider: "github", + event: "push", + payload: {}, + receivedAt: Date.now(), + stateVersion: 2, + ...body, + } as DeliveryMessage, + attempts: 1, + ack: (): void => {}, + retry: (): void => {}, + }; + await handleQueueBatch( + { + queue: "webhooker-delivery", + messages: [message], + } as unknown as MessageBatch, + { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: {} as D1Database }, + async () => { + dispatched += 1; + return { attempts: 0, failures: [] }; + }, + ); + return dispatched; + }; + expect(await run({ deliveryId: "d1", groupId: "team", groupInstance: "ti" })).toBe(0); + // Sent again rather than dropped. + expect(await run({ deliveryId: "d2" })).toBe(1); + }); + + it("refuses to serve configuration while the marker table is missing", async () => { + const { db, sqlite } = createSqliteD1({ + migrations: (name) => !name.startsWith("0008") && !name.startsWith("0013"), + }); + sqlite.exec( + `INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) + VALUES ('g', 'G', '{"id":"g","name":"G","adminIds":[]}', 1, 0, 0)`, + ); + // Identities can neither be trusted unmigrated nor migrated safely: fail. + await expect( + d1ConfigStore(db, createMemoryKV().kv).loadGroups({ fresh: true }), + ).rejects.toThrow(/migrations are incomplete/); + }); +}); + +describe("mixed filter nodes", () => { + it("counts a stored node mixing condition kinds as one unreadable route", async () => { + const { db, sqlite } = createSqliteD1(); + const { kv } = createMemoryKV(); + const cfg = d1ConfigStore(db, kv); + await cfg.createGroup({ id: "g", name: "G", adminIds: [] }); + await cfg.createGroup({ id: "h", name: "H", adminIds: [] }); + await cfg.saveGroupRoutes("g", [route("r1", "g")]); + await cfg.saveGroupRoutes("h", [route("r2", "h")]); + sqlite.exec( + `UPDATE d1_routes SET ast = '{"all":0,"any":[{"type":"event","match":"push"}]}' WHERE id = 'r1'`, + ); + // The snapshot still loads: only that group is affected. + const snapshot = await cfg.loadSnapshot({ fresh: true }); + expect(snapshot.unreadable).toEqual(["g"]); + expect(snapshot.routes.map((r) => r.id)).toEqual(["r2"]); + }); +}); + +describe("old global-endpoint state", () => { + it("is never trusted when a tenant named global existed at the upgrade", async () => { + const { db, sqlite } = createSqliteD1(); + const { kv } = createMemoryKV(); + sqlite.exec( + `INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) + VALUES ('global', 'Tenant', '{"id":"global","name":"Tenant","adminIds":[]}', 1, 0, 0)`, + ); + // Its state, written before the upgrade under the shared key. + sqlite.exec( + `INSERT INTO delivery_state (key, status, updated_at) VALUES ('delivery-state:github:global:d1', 'delivered', 1)`, + ); + let dispatched = 0; + const message = { + body: { + deliveryId: "d1", + provider: "github", + event: "push", + payload: {}, + receivedAt: Date.now(), + stateVersion: 2, + } as DeliveryMessage, + attempts: 1, + ack: (): void => {}, + retry: (): void => {}, + }; + await handleQueueBatch( + { + queue: "webhooker-delivery", + messages: [message], + } as unknown as MessageBatch, + { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: db }, + async () => { + dispatched += 1; + return { attempts: 0, failures: [] }; + }, + ); + // A global-endpoint event with that id is dispatched, not taken for done. + expect(dispatched).toBe(1); + }); +}); + +describe("old tenant state", () => { + it("is not read for a group created since (no older id of that name)", async () => { + const { db, sqlite } = createSqliteD1(); + const { kv, store } = createMemoryKV(); + // Identities managed by this server already (the group keeps its instance). + store.set("config:identity-migrated", "1"); + sqlite.exec( + `INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) + VALUES ('team', 'Team', '{"id":"team","name":"Team","adminIds":[],"instance":"ti"}', 1, 0, 0)`, + ); + // Left by an earlier group of that id. + sqlite.exec( + `INSERT INTO delivery_state (key, status, updated_at) VALUES ('delivery-state:github:team:d1', 'delivered', 1)`, + ); + const legacyReads: unknown[] = []; + const prepare = db.prepare.bind(db); + db.prepare = ((sql: string) => { + const stmt = prepare(sql); + const bind = stmt.bind.bind(stmt); + stmt.bind = ((...args: unknown[]) => { + if (args.includes("delivery-state:github:team:d1")) legacyReads.push(sql); + return bind(...args); + }) as typeof stmt.bind; + return stmt; + }) as typeof db.prepare; + let dispatched = 0; + const message = { + body: { + deliveryId: "d1", + provider: "github", + groupId: "team", + groupInstance: "ti", + event: "push", + payload: {}, + receivedAt: Date.now(), + stateVersion: 2, + } as DeliveryMessage, + attempts: 1, + ack: (): void => {}, + retry: (): void => {}, + }; + await handleQueueBatch( + { + queue: "webhooker-delivery", + messages: [message], + } as unknown as MessageBatch, + { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: db }, + async () => { + dispatched += 1; + return { attempts: 0, failures: [] }; + }, + ); + expect(dispatched).toBe(1); + expect(legacyReads).toEqual([]); + }); +}); + +describe("legacy global tenant", () => { + it("never takes the shared old state key for its own", async () => { + const { kv, store } = createMemoryKV(); + store.set( + "config:groups", + JSON.stringify([ + { + id: "global", + name: "T", + adminIds: [], + instance: "gi", + legacyId: "global", + instanceBackfilled: true, + }, + ]), + ); + store.set("config:identity-migrated", "1"); + // The global endpoint's delivery, finished before the upgrade. + store.set("delivery-state:custom:global:42", JSON.stringify({ status: "delivered", at: 1 })); + let dispatched = 0; + const message = { + body: { + deliveryId: "42", + provider: "custom", + groupId: "global", + groupInstance: "gi", + event: "custom", + payload: {}, + receivedAt: Date.now(), + stateVersion: 2, + } as DeliveryMessage, + attempts: 1, + ack: (): void => {}, + retry: (): void => {}, + }; + await handleQueueBatch( + { + queue: "webhooker-delivery", + messages: [message], + } as unknown as MessageBatch, + { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: {} as D1Database }, + async () => { + dispatched += 1; + return { attempts: 0, failures: [] }; + }, + ); + expect(dispatched).toBe(1); + }); +}); + +describe("revocation reaches every location at once", () => { + it("stops a signed-out session and a revoked token even where KV still has them", async () => { + const { createAdminSession, getAdminSession, destroyAdminSession, adminCookie } = + await import("../server/lib/web/session"); + const { saveToken, removeToken, findUserIdByToken } = + await import("../server/lib/github/store"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + const id = await createAdminSession(kv, "42", "alice"); + const cookie = adminCookie(id).split(";")[0]!; + const stale = await kv.get(`session:${id}`); + await destroyAdminSession(kv, cookie, db); + // A location that has not seen the delete yet still holds the session. + await kv.put(`session:${id}`, stale!); + expect(await getAdminSession(kv, cookie, db)).toBeNull(); + + await saveToken(kv, "42", "tok-1", 3600, db); + const staleToken = await kv.get("token:42"); + await removeToken(kv, "42", db); + await kv.put("token:42", staleToken!); + expect(await findUserIdByToken(kv, "tok-1", db)).toBeNull(); + }); + + it("never falls back to an older KV secret once the group's secret was removed", async () => { + const { getTenantSecret } = await import("../server/lib/web/tenants"); + const { kv } = createMemoryKV(); + await kv.put("tenant:i:inst", "old-secret"); + expect(await getTenantSecret(kv, { id: "g", instance: "inst", webhookSecret: "" })).toBeNull(); + expect(await getTenantSecret(kv, { id: "g", instance: "inst" })).toBe("old-secret"); + }); + + it("isolates a group row whose fields have the wrong shape", async () => { + const { db, sqlite } = createSqliteD1(); + const { kv } = createMemoryKV(); + const cfg = d1ConfigStore(db, kv); + await cfg.createGroup({ id: "g", name: "G", adminIds: [] }); + await cfg.saveGroupRoutes("g", [route("r1", "g")]); + sqlite.exec( + `UPDATE d1_groups SET data = json_set(data, '$.providers', 'github') WHERE id = 'g'`, + ); + expect((await cfg.loadSnapshot({ fresh: true })).unreadable).toEqual(["g"]); + }); +}); + +describe("round 23", () => { + it("never takes a Feishu 200 it could not read for a sent message", async () => { + const { sendMessage } = await import("../server/lib/drivers/feishu/rest"); + const realFetch = globalThis.fetch; + globalThis.fetch = (async () => + new Response("{cut off", { status: 200 })) as unknown as typeof fetch; + try { + const result = await sendMessage("t", "oc_1", { a: 1 }); + expect(result.ok).toBe(false); + } finally { + globalThis.fetch = realFetch; + } + }, 15_000); + + it("gives bot actions no token that was revoked, even where KV still has it", async () => { + const { saveToken, removeToken, getToken } = await import("../server/lib/github/store"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + await saveToken(kv, "42", "tok-1", 3600, db); + const stale = await kv.get("token:42"); + await removeToken(kv, "42", db); + await kv.put("token:42", stale!); + expect(await getToken({ KV: kv, DB: db }, "42")).toBeNull(); + }); + + it("serves the configuration of a fresh isolate despite a malformed group row", async () => { + const { db, sqlite } = createSqliteD1(); + const { kv } = createMemoryKV(); + await d1ConfigStore(db, kv).createGroup({ id: "ok", name: "OK", adminIds: [] }); + sqlite.exec( + "INSERT INTO d1_groups (id, name, data, version, created_at, updated_at) VALUES ('bad', 'B', '{', 1, 0, 0)", + ); + const groups = await d1ConfigStore(db, kv).loadGroups({ fresh: true }); + expect(groups.map((g) => g.id)).toEqual(["ok"]); + }); +}); + +describe("round 24", () => { + it("keys platform idempotency by the receiving endpoint too", async () => { + const nonces: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + nonces.push((JSON.parse(String(init?.body)) as { nonce: string }).nonce); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [route("r", "g")], + }; + const groups: Group[] = [{ id: "g", name: "G", adminIds: [], instance: "i" }]; + const event = { event: "push", payload: {}, deliveryId: "42" }; + await dispatchEvent(config, event, env, groups); + await dispatchEvent(config, event, env, groups, { tenantId: "g" }); + } finally { + globalThis.fetch = realFetch; + } + // Same delivery id on two endpoints: two events, two messages. + expect(nonces).toHaveLength(2); + expect(nonces[1]).not.toBe(nonces[0]); + }); + + it("revokes the old token before a rotation overwrites it", async () => { + const { saveToken } = await import("../server/lib/github/store"); + const { kv } = createMemoryKV(); + const { db } = createSqliteD1(); + await saveToken(kv, "42", "tok-A", 3600, db); + const broken = { + ...db, + batch: db.batch.bind(db), + prepare: (): never => { + throw new Error("D1 down"); + }, + } as unknown as D1Database; + await expect(saveToken(kv, "42", "tok-B", 3600, broken)).rejects.toThrow(); + // Not overwritten: a retry still knows which token to revoke. + expect(JSON.parse((await kv.get("token:42"))!).accessToken).toBe("tok-A"); + }); + + it("never lets a late pending deployment status undo a success stamped the same time", async () => { + const calls: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + calls.push(String(init?.method)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + const status = (state: string): Record => ({ + deployment_status: { id: 1, state, updated_at: "2026-10-01T00:00:00Z" }, + deployment: { id: 7, environment: "prod", ref: "main", sha: "abc" }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }); + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [{ ...route("r", "g"), filters: [{ type: "event", match: "deployment_status" }] }], + }; + const groups: Group[] = [{ id: "g", name: "G", adminIds: [], instance: "i" }]; + await dispatchEvent( + config, + { event: "deployment_status", payload: status("success") }, + env, + groups, + ); + await dispatchEvent( + config, + { event: "deployment_status", payload: status("pending") }, + env, + groups, + ); + } finally { + globalThis.fetch = realFetch; + } + expect(calls).toEqual(["POST"]); + }); +}); + +describe("group webhook log", () => { + it("posts one summary per delivery outcome, not one per retry", async () => { + const posts: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (url: unknown) => { + posts.push(String(url)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [route("r", "g")], + }; + const groups: Group[] = [ + { + id: "g", + name: "G", + adminIds: [], + instance: "i", + logTarget: { platform: "discord", channelId: "log" }, + }, + ]; + const event = { event: "push", payload: {}, deliveryId: "d1" }; + await dispatchEvent(config, event, env, groups); + await dispatchEvent(config, event, env, groups); + } finally { + globalThis.fetch = realFetch; + } + expect(posts.filter((u) => u.includes("/channels/log/"))).toHaveLength(1); + }); +}); + +describe("round 26", () => { + it("stops a newer token even when the removal read an older one from KV", async () => { + const { saveToken, removeToken, getToken, findUserIdByToken } = + await import("../server/lib/github/store"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + await saveToken(kv, "42", "tok-0", 3600, db); + const older = await kv.get("token:42"); + await saveToken(kv, "42", "tok-1", 3600, db); + const newer = await kv.get("token:42"); + const newerReverse = await kv.get("token-reverse:" + (await sha256("tok-1"))); + // The removing location still sees tok-0. + await kv.put("token:42", older!); + await removeToken(kv, "42", db); + // Another location still sees tok-1. + await kv.put("token:42", newer!); + await kv.put("token-reverse:" + (await sha256("tok-1")), newerReverse!); + expect(await getToken({ KV: kv, DB: db }, "42")).toBeNull(); + expect(await findUserIdByToken(kv, "tok-1", db)).toBeNull(); + // A later sign-in works again. + await saveToken(kv, "42", "tok-2", 3600, db); + expect(await getToken({ KV: kv, DB: db }, "42")).toBe("tok-2"); + expect(await findUserIdByToken(kv, "tok-2", db)).toBe("42"); + }); + + it("keeps a rotation's older token out even where KV still shows it", async () => { + const { saveToken, getToken } = await import("../server/lib/github/store"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + await saveToken(kv, "42", "tok-0", 3600, db); + await saveToken(kv, "42", "tok-1", 3600, db); + const newer = await kv.get("token:42"); + // A rotation working from a stale copy (tok-0) still retires tok-1. + await kv.put("token:42", JSON.stringify({ ...JSON.parse(newer!), accessToken: "tok-0" })); + await saveToken(kv, "42", "tok-2", 3600, db); + await kv.put("token:42", newer!); + expect(await getToken({ KV: kv, DB: db }, "42")).toBeNull(); + }); + + it("filters deployments by the deployed ref", async () => { + const { matchRoute } = await import("../server/lib/events/match"); + const r: Route = { ...route("r", "g"), filters: [{ type: "branch", match: "main" }] }; + for (const name of ["deployment", "deployment_status"]) { + expect(matchRoute(r, { event: name, payload: { deployment: { ref: "main" } } })).toBe(true); + expect(matchRoute(r, { event: name, payload: { deployment: { ref: "dev" } } })).toBe(false); + } + }); + + it("deduplicates group summaries whatever order the targets finished in", async () => { + const posts: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (url: unknown) => { + posts.push(String(url)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const base: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [], + }; + const a = { + ...route("a", "g"), + targets: [{ platform: "discord" as const, channelId: "c1" }], + }; + const b = { + ...route("b", "g"), + targets: [{ platform: "discord" as const, channelId: "c2" }], + }; + const groups: Group[] = [ + { + id: "g", + name: "G", + adminIds: [], + instance: "i", + logTarget: { platform: "discord", channelId: "log" }, + }, + ]; + const event = { event: "push", payload: {}, deliveryId: "d1" }; + await dispatchEvent({ ...base, routes: [a, b] }, event, env, groups); + await dispatchEvent({ ...base, routes: [b, a] }, event, env, groups); + } finally { + globalThis.fetch = realFetch; + } + expect(posts.filter((u) => u.includes("/channels/log/"))).toHaveLength(1); + }); +}); + +async function sha256(text: string): Promise { + const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text)); + return [...new Uint8Array(digest)].map((x) => x.toString(16).padStart(2, "0")).join(""); +} + +describe("round 27", () => { + it("never lets a late creation bring back a deleted comment's message", async () => { + const calls: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + calls.push(String(init?.method)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + const payload = (action: string): Record => ({ + action, + issue: { number: 7, title: "Bug", html_url: "https://github.com/o/r/issues/7" }, + comment: { + id: 99, + body: "hi", + updated_at: "2026-10-01T00:00:00Z", + html_url: "https://github.com/o/r/issues/7#issuecomment-99", + user: { login: "a" }, + }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }); + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [{ ...route("r", "g"), filters: [{ type: "event", match: "issue_comment" }] }], + }; + const groups: Group[] = [{ id: "g", name: "G", adminIds: [], instance: "i" }]; + await dispatchEvent( + config, + { event: "issue_comment", payload: payload("deleted") }, + env, + groups, + ); + await dispatchEvent( + config, + { event: "issue_comment", payload: payload("created") }, + env, + groups, + ); + } finally { + globalThis.fetch = realFetch; + } + // The deletion was posted; the creation retried late never edits it back. + expect(calls).toEqual(["POST"]); + }); + + it("filters check runs and code scanning alerts by branch name", async () => { + const { matchRoute } = await import("../server/lib/events/match"); + const r: Route = { ...route("r", "g"), filters: [{ type: "branch", match: "main" }] }; + expect( + matchRoute(r, { + event: "check_run", + payload: { check_run: { check_suite: { head_branch: "main" } } }, + }), + ).toBe(true); + expect( + matchRoute(r, { event: "code_scanning_alert", payload: { ref: "refs/heads/main" } }), + ).toBe(true); + expect( + matchRoute(r, { event: "code_scanning_alert", payload: { ref: "refs/heads/dev" } }), + ).toBe(false); + }); + + it("keeps what a delivery finished as long as its state (7 days)", async () => { + const { markTargetsDone } = await import("../server/lib/queue/delivery"); + const { db, sqlite } = createSqliteD1(); + const env: Env = { GITHUB_WEBHOOK_SECRET: "", KV: createMemoryKV().kv, DB: db }; + await markTargetsDone(env, "k", ["t"], { permanentFailure: true }); + const rows = sqlite.query("SELECT expires_at FROM dedup_keys").all() as Array<{ + expires_at: number; + }>; + expect(rows).toHaveLength(2); + for (const row of rows) expect(row.expires_at).toBeGreaterThan(Date.now() + 6.9 * 86400_000); + }); + + it("scopes logs for many groups with one bound parameter", async () => { + const { groupScopeClause } = await import("../server/lib/lib/send-log"); + const ids = Array.from({ length: 150 }, (_, i) => `g${i}`); + const clause = groupScopeClause(ids)!; + expect(clause.args).toHaveLength(1); + const { db } = createSqliteD1(); + const { getSendLog } = await import("../server/lib/lib/send-log"); + await expect(getSendLog(db, 10, ids)).resolves.toEqual([]); + }); + + it("summarizes a retry that skips finished targets as nothing new", async () => { + const posts: string[] = []; + const realFetch = globalThis.fetch; + let failC2 = true; + globalThis.fetch = (async (url: unknown) => { + const u = String(url); + posts.push(u); + if (u.includes("/channels/c2/") && failC2) return new Response("{}", { status: 502 }); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [ + { ...route("a", "g"), targets: [{ platform: "discord", channelId: "c1" }] }, + { ...route("b", "g"), targets: [{ platform: "discord", channelId: "c2" }] }, + ], + }; + const groups: Group[] = [ + { + id: "g", + name: "G", + adminIds: [], + instance: "i", + logTarget: { platform: "discord", channelId: "log" }, + }, + ]; + const done = new Set(); + const options = { + skipTarget: async (key: string): Promise => done.has(key), + afterTarget: async (key: string): Promise => { + done.add(key); + }, + }; + const event = { event: "push", payload: {}, deliveryId: "d1" }; + await dispatchEvent(config, event, env, groups, options); + // The retry skips c1 and fails c2 again: nothing new to report. + await dispatchEvent(config, event, env, groups, options); + expect(posts.filter((u) => u.includes("/channels/log/"))).toHaveLength(1); + // c2 recovers: a new summary. + failC2 = false; + await dispatchEvent(config, event, env, groups, options); + expect(posts.filter((u) => u.includes("/channels/log/"))).toHaveLength(2); + // The same id from another forge is another delivery, with its own summary. + done.clear(); + await dispatchEvent(config, { ...event, provider: "gitea" }, env, groups, options); + expect(posts.filter((u) => u.includes("/channels/log/"))).toHaveLength(3); + } finally { + globalThis.fetch = realFetch; + } + }, 30_000); +}); + +describe("round 28", () => { + async function runChain( + payloads: Array<{ event: string; payload: Record }>, + groups: Group[] = [{ id: "g", name: "G", adminIds: [], instance: "i" }], + seed?: (sqlite: import("bun:sqlite").Database) => void, + ): Promise { + const calls: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + calls.push(String(init?.method)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const { db, sqlite } = createSqliteD1(); + seed?.(sqlite); + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [{ ...route("r", "g"), filters: [] }], + }; + for (const p of payloads) await dispatchEvent(config, p, env, groups); + } finally { + globalThis.fetch = realFetch; + } + return calls; + } + + it("never lets an untimed queued run undo a completion", async () => { + const run = (status: string, updated?: string): Record => ({ + workflow_run: { + id: 5, + workflow_id: 9, + name: "CI", + status, + conclusion: status === "completed" ? "success" : null, + ...(updated ? { updated_at: updated } : {}), + html_url: "https://github.com/o/r/actions/runs/5", + }, + workflow: { id: 9, name: "CI" }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }); + const calls = await runChain([ + { event: "workflow_run", payload: run("completed", "2026-10-01T00:00:00Z") }, + { event: "workflow_run", payload: run("queued") }, + ]); + expect(calls).toEqual(["POST"]); + }); + + it("never lets a late submission undo a dismissed review", async () => { + const review = (action: string, state: string): Record => ({ + action, + review: { + id: 3, + state, + submitted_at: "2026-10-01T00:00:00Z", + html_url: "https://github.com/o/r/pull/1#pullrequestreview-3", + user: { login: "a" }, + }, + pull_request: { number: 1, title: "PR", html_url: "https://github.com/o/r/pull/1" }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }); + const calls = await runChain([ + { event: "pull_request_review", payload: review("dismissed", "dismissed") }, + { event: "pull_request_review", payload: review("submitted", "approved") }, + ]); + expect(calls).toEqual(["POST"]); + }); + + it("lets exactly one new chain adopt an old message", async () => { + const checkRun = (suite: number): Record => ({ + action: "completed", + check_run: { + id: suite, + name: "build", + head_sha: "abc", + status: "completed", + conclusion: "success", + check_suite: { id: suite }, + }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }); + const calls = await runChain( + [ + { event: "check_run", payload: checkRun(1) }, + { event: "check_run", payload: checkRun(2) }, + ], + [{ id: "g", name: "G", adminIds: [], instance: "i", legacyId: "g" }], + (sqlite) => + sqlite.exec( + `INSERT INTO message_tracking (event_id, target_id, message_id, updated_at) + VALUES ('g:r:check_run:o/r:build:abc', 'c1', 'old-message', 1)`, + ), + ); + expect(calls).toEqual(["PATCH", "POST"]); + }); + + it("takes no old-key lock when there is no old entry to adopt", async () => { + const run = { + workflow_run: { + id: 5, + workflow_id: 9, + name: "CI", + status: "queued", + conclusion: null, + html_url: "https://github.com/o/r/actions/runs/5", + }, + workflow: { id: 9, name: "CI" }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }; + let db: import("bun:sqlite").Database | undefined; + const calls = await runChain( + [{ event: "workflow_run", payload: run }], + [{ id: "g", name: "G", adminIds: [], instance: "i", legacyId: "g" }], + (sqlite) => { + sqlite.exec("CREATE TABLE lock_log (key TEXT)"); + sqlite.exec( + `CREATE TRIGGER log_legacy_lock AFTER INSERT ON dedup_keys + WHEN NEW.key LIKE 'msg:lock:legacy:%' + BEGIN INSERT INTO lock_log VALUES (NEW.key); END`, + ); + db = sqlite; + }, + ); + expect(calls).toEqual(["POST"]); + expect(db!.prepare("SELECT COUNT(*) AS n FROM lock_log").get()).toEqual({ n: 0 }); + }); + + it("lets routes that had no group adopt their unprefixed chains", async () => { + const calls = await runChain( + [ + { + event: "check_run", + payload: { + action: "completed", + check_run: { + id: 1, + name: "build", + head_sha: "abc", + status: "completed", + conclusion: "success", + check_suite: { id: 7 }, + }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }, + }, + ], + [{ id: "g", name: "G", adminIds: [], instance: "i", legacyId: "" }], + (sqlite) => + sqlite.exec( + `INSERT INTO message_tracking (event_id, target_id, message_id, updated_at) + VALUES ('r:check_run:o/r:build:abc', 'c1', 'old-message', 1)`, + ), + ); + expect(calls).toEqual(["PATCH"]); + }); + + it("drops a Feishu token the reply was refused with", async () => { + const { forgetRejectedToken, tokenKey } = await import("../server/lib/drivers/feishu/rest"); + const { kv } = createMemoryKV(); + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + KV: kv, + DB: createSqliteD1().db, + FEISHU_APP_ID: "cli_a", + }; + await kv.put(tokenKey(env), "stale"); + await forgetRejectedToken({ ok: false, errorCode: "FEISHU_TOKEN" }, env, "stale"); + expect(await kv.get(tokenKey(env))).toBeNull(); + }); +}); + +describe("round 29", () => { + it("keeps the stage an untimed completion reached over a timed queued state", async () => { + const calls: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + calls.push(String(init?.method)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + const run = (status: string, updated?: string): Record => ({ + workflow_run: { + id: 5, + workflow_id: 9, + name: "CI", + status, + conclusion: status === "completed" ? "success" : null, + ...(updated ? { updated_at: updated } : {}), + html_url: "https://github.com/o/r/actions/runs/5", + }, + workflow: { id: 9, name: "CI" }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }); + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [{ ...route("r", "g"), filters: [] }], + }; + const groups: Group[] = [{ id: "g", name: "G", adminIds: [], instance: "i" }]; + for (const payload of [ + run("queued", "2026-10-01T00:00:00Z"), + run("completed"), + run("queued"), + ]) { + await dispatchEvent(config, { event: "workflow_run", payload }, env, groups); + } + } finally { + globalThis.fetch = realFetch; + } + expect(calls).toEqual(["POST", "PATCH"]); + }); + + it("moves an adopted chain entry in one step, leaving nothing behind", async () => { + const { messageTracker } = await import("../server/lib/lib/message-tracker"); + const { db, sqlite } = createSqliteD1(); + const tracker = messageTracker(db, createMemoryKV().kv); + await tracker.set("old", "c1", { messageId: "m", version: 1 }); + await tracker.move( + { eventId: "old", targetId: "c1" }, + { eventId: "new", targetId: "d:c1" }, + { messageId: "m", version: 1 }, + ); + expect(await tracker.get("old", "c1")).toBeNull(); + expect((await tracker.get("new", "d:c1"))?.messageId).toBe("m"); + // A failing move leaves the old entry where it was (one transaction). + await tracker.set("old2", "c1", { messageId: "m2", version: 1 }); + sqlite.exec( + "CREATE TRIGGER no_new BEFORE INSERT ON message_tracking WHEN NEW.event_id = 'new2' BEGIN SELECT RAISE(ABORT, 'boom'); END", + ); + await expect( + tracker.move( + { eventId: "old2", targetId: "c1" }, + { eventId: "new2", targetId: "d:c1" }, + { messageId: "m2", version: 1 }, + ), + ).rejects.toThrow(); + expect((await tracker.get("old2", "c1"))?.messageId).toBe("m2"); + }); + + it("rejects keyword conditions with an operator the matcher would ignore", async () => { + const { validateRoutes } = await import("../server/lib/web/admin"); + const result = validateRoutes([ + { + id: "r", + name: "r", + enabled: true, + groupId: "g", + filters: [{ type: "keyword", op: "ne", match: "secret" }], + targets: [{ platform: "discord", channelId: "1" }], + }, + ]); + expect(result.ok).toBe(false); + }); + + it("caps Discord role mentions", async () => { + const { validateRoutes } = await import("../server/lib/web/admin"); + const ids = Array.from({ length: 60 }, (_, i) => String(1000000000000000000n + BigInt(i))); + const result = validateRoutes([ + { + id: "r", + name: "r", + enabled: true, + groupId: "g", + filters: [], + discordRoleIds: ids, + targets: [{ platform: "discord", channelId: "1" }], + }, + ]); + expect(result.ok).toBe(false); + const { renderNeutralMessage } = await import("../server/lib/drivers/discord/render"); + const rendered = renderNeutralMessage({ title: "t", mentionRoleIds: [...ids, ...ids] }); + expect((rendered.content ?? "").length).toBeLessThanOrEqual(2000); + expect(rendered.allowed_mentions?.roles?.length).toBeLessThanOrEqual(100); + }); +}); + +describe("round 30", () => { + it("never lets a forge title mention everyone in a Feishu chat", async () => { + const { renderNeutralMessage } = await import("../server/lib/drivers/feishu/render"); + const card = JSON.stringify( + renderNeutralMessage({ + title: "o/r#1: hi", + url: "https://github.com/o/r/issues/1", + description: "", + fields: [{ name: "x", value: 'a' }], + }), + ); + expect(/<\/?at\b/i.test(card)).toBe(false); + }); + + it("reads a delivery's finished targets in one query", async () => { + const { markTargetsDone, targetDoneChecker } = await import("../server/lib/queue/delivery"); + const { db } = createSqliteD1(); + let queries = 0; + const counted = new Proxy(db, { + get(target, prop, receiver): unknown { + if (prop === "prepare") { + return (sql: string): D1PreparedStatement => { + queries++; + return target.prepare(sql); + }; + } + return Reflect.get(target, prop, receiver); + }, + }) as D1Database; + const env: Env = { GITHUB_WEBHOOK_SECRET: "", KV: createMemoryKV().kv, DB: counted }; + await markTargetsDone(env, "k", ["a", "b"]); + queries = 0; + const done = targetDoneChecker(env, "k"); + const results = await Promise.all(["a", "b", "c", "d", "e"].map((t) => done(t))); + expect(results).toEqual([true, true, false, false, false]); + expect(queries).toBe(1); + }); + + it("refuses a route listing the same destination twice, and sends a stored one once", async () => { + const { validateRoutes } = await import("../server/lib/web/admin"); + const result = validateRoutes([ + { + id: "r", + name: "r", + enabled: true, + groupId: "g", + filters: [{ type: "event", match: "push" }], + targets: [ + { platform: "telegram", chatId: "-100123" }, + { platform: "telegram", chatId: "-100123" }, + ], + }, + ]); + expect(result).toMatchObject({ ok: false, error: expect.stringContaining("repeats") }); + const posts: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (url: unknown) => { + posts.push(String(url)); + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [ + { + ...route("r", "g"), + targets: [ + { platform: "discord", channelId: "c1" }, + { platform: "discord", channelId: "c1" }, + ], + }, + ], + }; + await dispatchEvent(config, { event: "push", payload: {} }, env, [ + { id: "g", name: "G", adminIds: [], instance: "i" }, + ]); + } finally { + globalThis.fetch = realFetch; + } + expect(posts).toHaveLength(1); + }); + + it("keeps sign-in redirects on this site", async () => { + const { safeRedirectPath } = await import("../server/lib/web/oauth"); + expect(safeRedirectPath("/\t/evil.example")).toBe("/"); + expect(safeRedirectPath("/%09/evil.example")).toBe("/%09/evil.example"); + expect(safeRedirectPath("/admin/groups?x=1#y")).toBe("/admin/groups?x=1#y"); + expect(safeRedirectPath("//evil.example")).toBe("/"); + }); + + it("resolves the Discord application per bot token", async () => { + const { getApplicationId } = await import("../server/lib/drivers/discord/commands"); + const { kv } = createMemoryKV(); + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + const auth = new Headers(init?.headers).get("authorization") ?? ""; + return new Response(JSON.stringify({ id: auth.endsWith("one") ? "app-1" : "app-2" }), { + status: 200, + }); + }) as unknown as typeof fetch; + try { + const base = { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: createSqliteD1().db }; + expect(await getApplicationId({ ...base, DISCORD_TOKEN: "one" })).toBe("app-1"); + expect(await getApplicationId({ ...base, DISCORD_TOKEN: "two" })).toBe("app-2"); + } finally { + globalThis.fetch = realFetch; + } + }); + + it("gives an auto-provisioned installation group to its audit with its instance", async () => { + const { ensureInstallationGroup } = await import("../server/lib/web/groups"); + const { kv } = createMemoryKV(); + const group = await ensureInstallationGroup(kv, 42, "acme"); + expect(group?.instance).toBeTruthy(); + }); +}); + +describe("round 31", () => { + it("filters mentions out of the Feishu note too", async () => { + const { renderNeutralMessage } = await import("../server/lib/drivers/feishu/render"); + const card = JSON.stringify( + renderNeutralMessage({ + title: "t", + footer: "", + forge: { name: "" }, + }), + ); + expect(/<\/?at\b/i.test(card)).toBe(false); + }); + + it("caches the Feishu token per app", async () => { + const { tokenKey } = await import("../server/lib/drivers/feishu/rest"); + const base = { GITHUB_WEBHOOK_SECRET: "", KV: createMemoryKV().kv, DB: createSqliteD1().db }; + expect(tokenKey({ ...base, FEISHU_APP_ID: "cli_a" })).not.toBe( + tokenKey({ ...base, FEISHU_APP_ID: "cli_b" }), + ); + }); + + it("continues a chain whose message another Discord application sent", async () => { + const calls: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + const method = String(init?.method); + calls.push(method); + if (method === "PATCH") { + return new Response( + JSON.stringify({ + message: "Cannot edit a message authored by another user", + code: 50005, + }), + { status: 403 }, + ); + } + return new Response(JSON.stringify({ id: "m2" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const { db } = createSqliteD1(); + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + KV: createMemoryKV().kv, + DB: db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: [{ ...route("r", "g"), filters: [{ type: "event", match: "workflow_run" }] }], + }; + const payload = { + workflow_run: { + id: 5, + workflow_id: 9, + name: "CI", + status: "completed", + conclusion: "success", + updated_at: "2026-10-01T00:00:00Z", + html_url: "https://github.com/o/r/actions/runs/5", + }, + workflow: { id: 9, name: "CI" }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }; + const groups: Group[] = [{ id: "g", name: "G", adminIds: [], instance: "i" }]; + // First event posts and tracks a message; then it is "another app's". + await dispatchEvent(config, { event: "workflow_run", payload }, env, groups); + + calls.length = 0; + const later = { + ...payload, + workflow_run: { ...payload.workflow_run, updated_at: "2026-10-01T00:01:00Z" }, + }; + await dispatchEvent(config, { event: "workflow_run", payload: later }, env, groups); + } finally { + globalThis.fetch = realFetch; + } + expect(calls).toEqual(["PATCH", "POST"]); + }); + + it("refuses a redirect that dot segments turn protocol-relative", async () => { + const { safeRedirectPath } = await import("../server/lib/web/oauth"); + expect(safeRedirectPath("/a/..//evil.example")).toBe("/"); + expect(safeRedirectPath("/a/%2e%2e//evil.example")).toBe("/"); + }); + + it("refuses a Discord thread listed alone and with its parent", async () => { + const { validateRoutes } = await import("../server/lib/web/admin"); + const result = validateRoutes([ + { + id: "r", + name: "r", + enabled: true, + groupId: "g", + filters: [{ type: "event", match: "push" }], + targets: [ + { platform: "discord", channelId: "222" }, + { platform: "discord", channelId: "111", threadId: "222" }, + ], + }, + ]); + expect(result).toMatchObject({ ok: false, error: expect.stringContaining("repeats") }); + }); + + it("registers the commands again after a rollback", async () => { + const { registerGlobalCommands } = await import("../server/lib/drivers/discord/commands"); + const { kv } = createMemoryKV(); + // A marker left by other command definitions (a newer version, rolled back). + await kv.put("cmd:registered:global:app", "other-definitions"); + let puts = 0; + const realFetch = globalThis.fetch; + globalThis.fetch = (async () => { + puts++; + return new Response("[]", { status: 200 }); + }) as unknown as typeof fetch; + try { + await registerGlobalCommands({ + GITHUB_WEBHOOK_SECRET: "", + KV: kv, + DB: createSqliteD1().db, + DISCORD_TOKEN: "t", + DISCORD_APPLICATION_ID: "app", + }); + } finally { + globalThis.fetch = realFetch; + } + expect(puts).toBe(1); + }); +}); + +describe("round 32", () => { + it("accepts Telegram topic ids written one way only", async () => { + const { validateRoutes } = await import("../server/lib/web/admin"); + const routeWith = (topicId: string): unknown => ({ + id: "r", + name: "r", + enabled: true, + groupId: "g", + filters: [{ type: "event", match: "push" }], + targets: [{ platform: "telegram", chatId: "-100123", topicId }], + }); + expect(validateRoutes([routeWith("12")]).ok).toBe(true); + for (const bad of ["01", "1e0", "abc", "-1", "99999999999"]) { + expect(validateRoutes([routeWith(bad)]).ok).toBe(false); + } + }); + + it("pages through every guild when syncing commands", async () => { + const { syncGuildCommands } = await import("../server/lib/drivers/discord/commands"); + const realFetch = globalThis.fetch; + const registered: string[] = []; + globalThis.fetch = (async (input: unknown, init?: RequestInit) => { + const url = new URL(String(input)); + if (url.pathname.endsWith("/users/@me/guilds")) { + const after = Number(url.searchParams.get("after") ?? "0"); + const size = Number(url.searchParams.get("limit")); + const ids = Array.from({ length: 250 }, (_, i) => i + 1).filter((i) => i > after); + return new Response(JSON.stringify(ids.slice(0, size).map((id) => ({ id: String(id) }))), { + status: 200, + }); + } + if (init?.method === "PUT") registered.push(url.pathname); + return new Response("[]", { status: 200 }); + }) as unknown as typeof fetch; + const env = { + GITHUB_WEBHOOK_SECRET: "", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + DISCORD_TOKEN: "t", + DISCORD_APPLICATION_ID: "app", + }; + try { + // Each run registers a bounded share; the record carries the progress on. + for (let run = 0; run < 12; run++) await syncGuildCommands(env); + } finally { + globalThis.fetch = realFetch; + } + expect(new Set(registered).size).toBe(250); + expect(registered).toHaveLength(250); + }); + + it("gives a link confirmation back when the link could not be written", async () => { + const { handleLinkConfirm } = await import("../server/lib/web/oauth"); + const { makeEvent } = await import("./helpers"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + const nonce = "a".repeat(32); + await kv.put( + `link:${nonce}`, + JSON.stringify({ platform: "telegram", userId: "5", githubUserId: "7", login: "alice" }), + ); + let fail = true; + const flaky = new Proxy(db, { + get(target, prop, receiver): unknown { + if (prop === "prepare") { + return (sql: string): D1PreparedStatement => { + if (fail && sql.includes("telegram_links")) throw new Error("D1 down"); + return target.prepare(sql); + }; + } + return Reflect.get(target, prop, receiver); + }, + }) as D1Database; + const env: Env = { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: flaky }; + const submit = (): ReturnType => + makeEvent("/auth/github/link", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ nonce }), + env, + }); + await expect(handleLinkConfirm(submit())).rejects.toThrow(); + fail = false; + const page = await handleLinkConfirm(submit()); + expect(page).not.toContain("链接已失效"); + const row = await db + .prepare("SELECT github_user_id FROM telegram_links WHERE telegram_user_id = '5'") + .first<{ github_user_id: string }>(); + expect(row?.github_user_id).toBe("7"); + }); +}); + +describe("round 33", () => { + /** A D1 whose batches commit, then answer with an error (a lost answer). */ + function lostAnswers(db: D1Database): D1Database { + return new Proxy(db, { + get(target, prop, receiver): unknown { + if (prop === "batch") { + return async (statements: D1PreparedStatement[]): Promise => { + await target.batch(statements); + throw new Error("connection reset"); + }; + } + return Reflect.get(target, prop, receiver); + }, + }) as D1Database; + } + + it("never lets a link confirmation whose answer was lost be replayed", async () => { + const { handleLinkConfirm } = await import("../server/lib/web/oauth"); + const { makeEvent } = await import("./helpers"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + const nonce = "b".repeat(32); + await kv.put( + `link:${nonce}`, + JSON.stringify({ platform: "telegram", userId: "5", githubUserId: "A", login: "a" }), + ); + const submit = (DB: D1Database): ReturnType => + makeEvent("/auth/github/link", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ nonce }), + env: { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB }, + }); + // Committed, answer lost: still a success. + expect(await handleLinkConfirm(submit(lostAnswers(db)))).not.toContain("链接已失效"); + // Linked to B since; replaying the old confirmation changes nothing. + await db + .prepare( + "INSERT OR REPLACE INTO telegram_links (telegram_user_id, github_user_id) VALUES ('5', 'B')", + ) + .run(); + await kv.put( + `link:${nonce}`, + JSON.stringify({ platform: "telegram", userId: "5", githubUserId: "A", login: "a" }), + ); + expect(await handleLinkConfirm(submit(db))).toContain("链接已失效"); + const row = await db + .prepare("SELECT github_user_id FROM telegram_links WHERE telegram_user_id = '5'") + .first<{ github_user_id: string }>(); + expect(row?.github_user_id).toBe("B"); + }); + + it("treats an unlink whose answer was lost as done", async () => { + const { unlinkAsCommand } = await import("../server/lib/github/store"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + await db + .prepare("INSERT INTO telegram_links (telegram_user_id, github_user_id) VALUES ('5', 'A')") + .run(); + await unlinkAsCommand({ DB: lostAnswers(db), KV: kv }, "telegram", "5", { + key: "accepted:bot:telegram:x:1", + ttl: 600, + }); + const { idempotencyStore } = await import("../server/lib/lib/idempotency"); + expect(await idempotencyStore(db, kv).has("accepted:bot:telegram:x:1")).toBe(true); + }); + + it("reads no configuration for a request without a session", async () => { + const { loadAuth } = await import("../server/lib/web/auth"); + const { makeEvent } = await import("./helpers"); + let reads = 0; + const { db } = createSqliteD1(); + const counted = new Proxy(db, { + get(target, prop, receiver): unknown { + if (prop === "prepare") { + return (sql: string): D1PreparedStatement => { + if (sql.includes("d1_groups") || sql.includes("d1_routes")) reads++; + return target.prepare(sql); + }; + } + return Reflect.get(target, prop, receiver); + }, + }) as D1Database; + const env: Env = { GITHUB_WEBHOOK_SECRET: "", KV: createMemoryKV().kv, DB: counted }; + expect(await loadAuth(makeEvent("/admin/api/groups", { env }))).toBeNull(); + expect(reads).toBe(0); + }); + + it("sends once to one Telegram topic written two ways", async () => { + const posts: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (url: unknown) => { + posts.push(String(url)); + return new Response(JSON.stringify({ ok: true, result: { message_id: 1 } }), { + status: 200, + }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + TELEGRAM_TOKEN: "t", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "" }, + routes: [ + { + ...route("r", "g"), + targets: [ + { platform: "telegram", chatId: "-100123", topicId: "1" }, + { platform: "telegram", chatId: "-100123", topicId: "01" }, + ], + }, + ], + }; + await dispatchEvent(config, { event: "push", payload: {} }, env, [ + { id: "g", name: "G", adminIds: [], instance: "i" }, + ]); + } finally { + globalThis.fetch = realFetch; + } + expect(posts.filter((u) => u.includes("/sendMessage"))).toHaveLength(1); + }); + + it("counts Telegram's limit on the text shown, not the HTML source", async () => { + const { renderNeutralMessage } = await import("../server/lib/drivers/telegram/render"); + const html = renderNeutralMessage({ title: "t", description: "&".repeat(2000) }); + expect(html).toContain("&".repeat(2000)); + const long = renderNeutralMessage({ title: "t", description: "&".repeat(5000) }); + expect(long.endsWith("…") || long.includes("…<")).toBe(true); + expect(long).not.toMatch(/&a?m?p?…/); + }); + + it("shows a code scanning alert's security severity", async () => { + const { formatEvent } = await import("../server/lib/formatters"); + const message = formatEvent(route("r", "g"), { + event: "code_scanning_alert", + payload: { + action: "created", + alert: { + rule: { id: "x", severity: "error", security_severity_level: "critical" }, + state: "open", + }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }, + }); + expect(JSON.stringify(message)).toContain("critical"); + }); +}); + +describe("round 34", () => { + it("never lets a run that outlived its claim unlink a link made since", async () => { + const { unlinkAsCommand } = await import("../server/lib/github/store"); + const { idempotencyStore } = await import("../server/lib/lib/idempotency"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + // Another run finished the logout; the user linked again. + await idempotencyStore(db, kv).claim("accepted:bot:telegram:x:9", 600); + await db + .prepare("INSERT INTO telegram_links (telegram_user_id, github_user_id) VALUES ('5', 'new')") + .run(); + await unlinkAsCommand({ DB: db, KV: kv }, "telegram", "5", { + key: "accepted:bot:telegram:x:9", + ttl: 600, + }); + const row = await db + .prepare("SELECT github_user_id FROM telegram_links WHERE telegram_user_id = '5'") + .first<{ github_user_id: string }>(); + expect(row?.github_user_id).toBe("new"); + }); + + it("keeps Markdown code that looks like HTML", async () => { + const { htmlToText } = await import("../server/lib/formatters/helpers"); + expect(htmlToText("Repro: `` fails.
")).toBe( + "Repro: `` fails.", + ); + expect(htmlToText("```\n&\n```")).toBe("```\n&\n```"); + }); + + it("links a Gitea commit author on the Gitea host", async () => { + const { formatEvent } = await import("../server/lib/formatters"); + const message = formatEvent(route("r", "g"), { + event: "push", + provider: "gitea", + payload: { + ref: "refs/heads/main", + pusher: { login: "bob", username: "bob" }, + sender: { login: "bob" }, + repository: { full_name: "o/r", html_url: "https://git.example/o/r" }, + commits: [{ id: "abcdef1234567", message: "fix", author: { username: "alice" } }], + }, + }); + const text = JSON.stringify(message); + expect(text).toContain("https://git.example/alice"); + expect(text).not.toContain("https://github.com/alice"); + }); +}); + +describe("round 35", () => { + it("keeps more Markdown code forms and autolinks", async () => { + const { htmlToText } = await import("../server/lib/formatters/helpers"); + expect(htmlToText("Use ```` here")).toBe("Use ```` here"); + expect(htmlToText('~~~\nconst x = "";\n~~~')).toBe('~~~\nconst x = "";\n~~~'); + expect(htmlToText("See ")).toBe("See "); + expect(htmlToText("a
b c")).toBe("a\nb c"); + }); + + it("refuses an exists condition whose match could not be read back", async () => { + const { validateRoutes } = await import("../server/lib/web/admin"); + const withMatch = (match: unknown): unknown => ({ + id: "r", + name: "r", + enabled: true, + groupId: "g", + filters: [{ type: "field", path: "action", op: "exists", match }], + targets: [{ platform: "discord", channelId: "1" }], + }); + expect(validateRoutes([withMatch(null)]).ok).toBe(false); + expect(validateRoutes([withMatch({ a: 1 })]).ok).toBe(false); + const { match: _m, ...noMatch } = ( + withMatch(undefined) as { filters: Array> } + ).filters[0]!; + expect(validateRoutes([{ ...(withMatch(undefined) as object), filters: [noMatch] }]).ok).toBe( + true, + ); + }); + + it("binds the matching groups an earlier attempt left unbound", async () => { + const { ensureInstallationGroup } = await import("../server/lib/web/groups"); + const { createGroup, loadGroups } = await import("../server/lib/web/groups"); + const { kv } = createMemoryKV(); + await createGroup(kv, { + id: "a", + name: "a", + adminIds: [], + owners: ["acme"], + installationId: 7, + }); + await createGroup(kv, { id: "b", name: "b", adminIds: [], owners: ["acme"] }); + await ensureInstallationGroup(kv, 7, "acme"); + const b = (await loadGroups(kv, { fresh: true })).find((g) => g.id === "b"); + expect(b?.installationId).toBe(7); + }); +}); + +describe("round 36", () => { + it("keeps Markdown code whose delimiters vary in length", async () => { + const { htmlToText } = await import("../server/lib/formatters/helpers"); + expect(htmlToText("``a ` ``")).toBe("``a ` ``"); + expect(htmlToText("````\n```\n\n```\n````")).toBe("````\n```\n\n```\n````"); + expect(htmlToText("`multi\n` code")).toBe("`multi\n` code"); + expect(htmlToText("lone ` tick y")).toBe("lone ` tick y"); + }); + + it("never lets a stale KV copy of a moved chain entry be adopted again", async () => { + const { messageTracker } = await import("../server/lib/lib/message-tracker"); + const { noteKvFallback } = await import("../server/lib/storage/d1"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + await noteKvFallback(kv); + // The old entry lives in KV (written while the table was missing). + await kv.put("msg:old:c1", JSON.stringify({ id: "m", v: 1 })); + const tracker = messageTracker(db, kv); + await tracker.move( + { eventId: "old", targetId: "c1" }, + { eventId: "new", targetId: "d:c1" }, + { messageId: "m", version: 1 }, + ); + // Another location still sees the KV copy (its delete not there yet). + await kv.put("msg:old:c1", JSON.stringify({ id: "m", v: 1 })); + expect(await tracker.get("old", "c1")).toBeNull(); + }); +}); + +describe("round 37", () => { + it("keeps progressing through guilds within each run's call budget", async () => { + const { syncGuildCommands } = await import("../server/lib/drivers/discord/commands"); + const realFetch = globalThis.fetch; + const registered: string[] = []; + let callsThisRun = 0; + globalThis.fetch = (async (input: unknown, init?: RequestInit) => { + callsThisRun++; + const url = new URL(String(input)); + if (url.pathname.endsWith("/users/@me/guilds")) { + const after = Number(url.searchParams.get("after") ?? "0"); + const ids = Array.from({ length: 2000 }, (_, i) => i + 1).filter((i) => i > after); + return new Response(JSON.stringify(ids.slice(0, 200).map((id) => ({ id: String(id) }))), { + status: 200, + }); + } + if (init?.method === "PUT") registered.push(url.pathname); + return new Response("[]", { status: 200 }); + }) as unknown as typeof fetch; + const env = { + GITHUB_WEBHOOK_SECRET: "", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + DISCORD_TOKEN: "t", + DISCORD_APPLICATION_ID: "app", + }; + try { + for (let run = 0; run < 3; run++) { + callsThisRun = 0; + await syncGuildCommands(env); + expect(callsThisRun).toBeLessThanOrEqual(30); + } + } finally { + globalThis.fetch = realFetch; + } + // Each run made progress from where the last one stopped. + expect(new Set(registered).size).toBe(registered.length); + expect(registered.length).toBeGreaterThan(80); + }); + + it("never provisions an installation again after its claim lapsed", async () => { + const { processWebhook } = await import("../server/lib/webhook"); + const { resetConfigStore } = await import("../server/lib/config"); + const { loadGroups, updateGroup } = await import("../server/lib/web/groups"); + const { createHmac } = await import("crypto"); + const { db, sqlite } = createSqliteD1(); + const { kv } = createMemoryKV(); + resetConfigStore(kv); + const env: Env = { + GITHUB_WEBHOOK_SECRET: "global", + KV: kv, + DB: db, + QUEUE: { send: async () => undefined } as unknown as Queue, + }; + const body = JSON.stringify({ + action: "created", + installation: { id: 321, account: { login: "acme" } }, + }); + const headers = { + "x-github-event": "installation", + "x-github-delivery": "inst-late", + "x-hub-signature-256": `sha256=${createHmac("sha256", "global").update(body).digest("hex")}`, + }; + expect((await processWebhook(env, body, headers, () => undefined)).status).toBe(200); + await updateGroup(kv, "inst-321", (g) => ({ ...g, installationId: undefined })); + // The ingress dedup claim and its confirmation lapse (minutes later). + sqlite.exec( + "DELETE FROM dedup_keys WHERE key LIKE 'delivery:%' OR key LIKE 'accepted:delivery:%'", + ); + expect((await processWebhook(env, body, headers, () => undefined)).status).toBe(200); + const group = (await loadGroups(kv, { fresh: true })).find((g) => g.id === "inst-321"); + expect(group?.installationId).toBeUndefined(); + }); +}); + +describe("round 37 markdown", () => { + it("closes fences with their own character, keeps indented code and e-mail links", async () => { + const { htmlToText } = await import("../server/lib/formatters/helpers"); + expect(htmlToText("~~~~\n\n~~~~`\n\n~~~~")).toBe("~~~~\n\n~~~~`\n\n~~~~"); + expect(htmlToText("text\n\n ")).toBe("text\n\n "); + expect(htmlToText("mail now")).toBe("mail now"); + }); +}); + +describe("round 38", () => { + it("looks up a failed check suite's build log once per dispatch", async () => { + const keys = (await crypto.subtle.generateKey( + { + name: "RSASSA-PKCS1-v1_5", + modulusLength: 2048, + publicExponent: new Uint8Array([1, 0, 1]), + hash: "SHA-256", + }, + true, + ["sign", "verify"], + )) as CryptoKeyPair; + const pkcs8 = Buffer.from(await crypto.subtle.exportKey("pkcs8", keys.privateKey)).toString( + "base64", + ); + const pem = `-----BEGIN PRIVATE KEY-----\n${pkcs8.match(/.{1,64}/g)!.join("\n")}\n-----END PRIVATE KEY-----`; + let githubCalls = 0; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (input: unknown) => { + const url = String(input); + if (url.includes("api.github.com")) { + githubCalls++; + if (url.includes("access_tokens")) { + return new Response(JSON.stringify({ token: "inst" }), { status: 201 }); + } + return new Response(JSON.stringify({ check_runs: [] }), { status: 200 }); + } + return new Response(JSON.stringify({ id: "m" }), { status: 200 }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + DISCORD_TOKEN: "t", + GITHUB_APP_ID: "1", + GITHUB_PRIVATE_KEY: pem, + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + }; + const config: Config = { + baseUrl: "https://x", + github: { webhookSecret: "", appId: 0, privateKey: "", clientId: "", clientSecret: "" }, + discord: { token: "t" }, + routes: ["a", "b", "c"].map((id) => ({ + ...route(id, "g"), + filters: [{ type: "event" as const, match: "check_suite" }], + targets: [{ platform: "discord" as const, channelId: id }], + })), + }; + await dispatchEvent( + config, + { + event: "check_suite", + installationId: 5, + payload: { + action: "completed", + check_suite: { + id: 1, + conclusion: "failure", + check_runs_url: "https://api.github.com/repos/o/r/check-suites/1/check-runs", + }, + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + }, + }, + env, + [{ id: "g", name: "G", adminIds: [], instance: "i" }], + ); + } finally { + globalThis.fetch = realFetch; + } + // One token and one check list for the three routes. + expect(githubCalls).toBe(2); + }); + + it("cleans a body full of unclosed brackets in linear time", async () => { + const { htmlToText } = await import("../server/lib/formatters/helpers"); + const started = performance.now(); + htmlToText(("<" + "x".repeat(30)).repeat(8000)); + expect(performance.now() - started).toBeLessThan(500); + }); + + it("keeps the indent of code that opens the body", async () => { + const { htmlToText } = await import("../server/lib/formatters/helpers"); + expect(htmlToText(" \n ")).toBe(" \n "); + expect(htmlToText(" hi x ")).toBe("hi x"); + }); + + it("never binds again a group an earlier provisioning bound", async () => { + const { ensureInstallationGroup, createGroup, loadGroups, updateGroup } = + await import("../server/lib/web/groups"); + const { resetConfigStore, initConfigStore } = await import("../server/lib/config"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + resetConfigStore(kv); + initConfigStore({ GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: db }); + await createGroup(kv, { id: "team", name: "team", adminIds: [], owners: ["acme"] }); + await ensureInstallationGroup(kv, 77, "acme", db); + expect( + (await loadGroups(kv, { fresh: true })).find((g) => g.id === "team")?.installationId, + ).toBe(77); + // An admin unbinds it; provisioning runs again (a retry of a failed attempt). + await updateGroup(kv, "team", (g) => ({ ...g, installationId: undefined })); + await ensureInstallationGroup(kv, 77, "acme", db); + expect( + (await loadGroups(kv, { fresh: true })).find((g) => g.id === "team")?.installationId, + ).toBeUndefined(); + }); +}); + +describe("round 39", () => { + it("strips only the leading refs/ prefix of a branch or tag", async () => { + const { branchLink, refUrl, shortRef } = await import("../server/lib/formatters/helpers"); + expect(shortRef("refs/heads/feature/refs/tags/test")).toBe("feature/refs/tags/test"); + expect(shortRef("refs/tags/v1/refs/tags/x")).toBe("v1/refs/tags/x"); + // Short names are taken as written (a branch may be called `refs/heads/x`). + expect(refUrl("https://g/o/r", "refs/heads/release", "branch")).toBe( + "https://g/o/r/tree/refs/heads/release", + ); + expect(branchLink("https://g/o/r", "feature/refs/heads/test")).toContain( + "/tree/feature/refs/heads/test", + ); + }); + + it("never binds a group that another attempt bound after this one checked", async () => { + const { ensureInstallationGroup, createGroup, loadGroups } = + await import("../server/lib/web/groups"); + const { resetConfigStore, initConfigStore } = await import("../server/lib/config"); + const { db, sqlite } = createSqliteD1(); + const { kv } = createMemoryKV(); + resetConfigStore(kv); + initConfigStore({ GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: db }); + await createGroup(kv, { id: "team", name: "team", adminIds: [], owners: ["acme"] }); + const team = (await loadGroups(kv, { fresh: true })).find((g) => g.id === "team")!; + // This attempt's check sees no record; the record lands before its write + // (another attempt bound the group and an admin unbound it meanwhile). + const late = new Proxy(db, { + get(target, prop, receiver): unknown { + if (prop === "prepare") { + return (sql: string): D1PreparedStatement => { + if (sql.includes("SELECT 1 AS hit FROM dedup_keys")) { + sqlite.exec( + `INSERT OR REPLACE INTO dedup_keys (key, claimed_at, expires_at) VALUES ('installation-bound:88:${team.instance}', 0, ${Date.now() + 86400_000})`, + ); + return target.prepare("SELECT 1 AS hit WHERE 0"); + } + return target.prepare(sql); + }; + } + return Reflect.get(target, prop, receiver); + }, + }) as D1Database; + await ensureInstallationGroup(kv, 88, "acme", late); + const after = (await loadGroups(kv, { fresh: true })).find((g) => g.id === "team"); + expect(after?.installationId).toBeUndefined(); + }); + + it("cleans one body once for many routes", async () => { + const { htmlToText } = await import("../server/lib/formatters/helpers"); + const body = Array.from({ length: 140 }, (_, i) => "`".repeat(i + 1) + "x") + .join(" ") + .slice(0, 10000); + const started = performance.now(); + for (let i = 0; i < 50; i++) htmlToText(body); + expect(performance.now() - started).toBeLessThan(200); + }); +}); + +describe("round 40", () => { + it("remembers a missing fallback marker for a while (one KV read, not one per claim)", async () => { + const { kvFallbackActive } = await import("../server/lib/storage/d1"); + const { kv } = createMemoryKV(); + let reads = 0; + const get = kv.get.bind(kv); + kv.get = ((...args: unknown[]) => { + reads++; + return (get as (...a: unknown[]) => unknown)(...args); + }) as KVNamespace["get"]; + for (let i = 0; i < 20; i++) expect(await kvFallbackActive(kv)).toBe(false); + expect(reads).toBe(1); + }); + + it("checks a seen fallback marker again later, so an expired one stops the KV lookups", async () => { + const { kvFallbackActive, noteKvFallback } = await import("../server/lib/storage/d1"); + const { kv } = createMemoryKV(); + await noteKvFallback(kv); + expect(await kvFallbackActive(kv)).toBe(true); + // The marker expires in KV; an hour on, this isolate reads it again. + await kv.delete("storage:kv-fallback"); + const realNow = Date.now; + Date.now = (): number => realNow() + 2 * 3600_000; + try { + expect(await kvFallbackActive(kv)).toBe(false); + } finally { + Date.now = realNow; + } + }); + + it("keeps a refs/heads/ path inside a tag name out of the branch filter", async () => { + const { matchRoute } = await import("../server/lib/events/match"); + const r: Route = { + ...route("r", "g"), + filters: [{ type: "branch", match: "refs/tags/release/main" }], + }; + expect( + matchRoute(r, { event: "push", payload: { ref: "refs/tags/release/refs/heads/main" } }), + ).toBe(false); + }); + + it("names a newly pushed branch as it is", async () => { + const { formatEvent } = await import("../server/lib/formatters"); + const text = JSON.stringify( + formatEvent(route("r", "g"), { + event: "push", + payload: { + ref: "refs/heads/feature/refs/tags/test", + created: true, + commits: [], + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + sender: { login: "a" }, + }, + }), + ); + expect(text).toContain("feature/refs/tags/test"); + expect(text).toContain("/tree/feature/refs/tags/test"); + }); + + it("logs an error's message and stack", async () => { + const { log } = await import("../server/lib/lib/log"); + const lines: string[] = []; + const real = console.error; + console.error = (line: string): void => void lines.push(line); + try { + log.error({ requestId: "r", err: new Error("D1: database is locked") }, "Write failed"); + } finally { + console.error = real; + } + const entry = JSON.parse(lines[0]!) as { + msg: string; + requestId: string; + err: { message: string }; + }; + expect(entry.msg).toBe("Write failed"); + expect(entry.requestId).toBe("r"); + expect(entry.err.message).toBe("D1: database is locked"); + }); +}); + +describe("round 41", () => { + it("never takes a branch named refs/tags/… for a tag", async () => { + const { formatEvent } = await import("../server/lib/formatters"); + const text = JSON.stringify( + formatEvent(route("r", "g"), { + event: "push", + payload: { + ref: "refs/heads/refs/tags/release", + commits: [{ id: "abcdef1234567", message: "fix", author: { username: "a" } }], + repository: { full_name: "o/r", html_url: "https://github.com/o/r" }, + sender: { login: "a" }, + pusher: { name: "a" }, + }, + }), + ); + expect(text).not.toContain("tag: release"); + expect(text).toContain("refs/tags/release"); + }); +}); + +describe("quota", () => { + it("writes nothing when a guild command sync finds nothing to do", async () => { + const { syncGuildCommands } = await import("../server/lib/drivers/discord/commands"); + const realFetch = globalThis.fetch; + globalThis.fetch = (async (input: unknown) => { + const url = String(input); + if (url.includes("/users/@me/guilds")) { + return new Response(JSON.stringify([{ id: "1" }]), { status: 200 }); + } + return new Response("[]", { status: 200 }); + }) as unknown as typeof fetch; + const { kv } = createMemoryKV(); + const env = { + GITHUB_WEBHOOK_SECRET: "", + KV: kv, + DB: createSqliteD1().db, + DISCORD_TOKEN: "t", + DISCORD_APPLICATION_ID: "app", + }; + try { + await syncGuildCommands(env); + let writes = 0; + const put = kv.put.bind(kv); + kv.put = ((...args: unknown[]) => { + writes++; + return (put as (...a: unknown[]) => unknown)(...args); + }) as KVNamespace["put"]; + await syncGuildCommands(env); + await syncGuildCommands(env); + expect(writes).toBe(0); + } finally { + globalThis.fetch = realFetch; + } + }); +}); + +describe("round 42", () => { + it("keeps a KV-only chain entry when its move to D1 fails", async () => { + const { messageTracker } = await import("../server/lib/lib/message-tracker"); + const { noteKvFallback } = await import("../server/lib/storage/d1"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + await noteKvFallback(kv); + await kv.put("msg:old:c1", JSON.stringify({ id: "m", v: 1 })); + const failing = new Proxy(db, { + get(target, prop, receiver): unknown { + if (prop === "batch") + return async (): Promise => Promise.reject(new Error("D1 down")); + return Reflect.get(target, prop, receiver); + }, + }) as D1Database; + const tracker = messageTracker(failing, kv); + await expect( + tracker.move( + { eventId: "old", targetId: "c1" }, + { eventId: "new", targetId: "d:c1" }, + { messageId: "m", version: 1 }, + ), + ).rejects.toThrow(); + expect((await tracker.get("old", "c1"))?.messageId).toBe("m"); + }); +}); + +describe("round 43", () => { + it("reaches the guilds after a page whose registrations kept failing", async () => { + const { syncGuildCommands } = await import("../server/lib/drivers/discord/commands"); + const realFetch = globalThis.fetch; + const put: string[] = []; + globalThis.fetch = (async (input: unknown, _init?: RequestInit) => { + const url = new URL(String(input)); + if (url.pathname.endsWith("/users/@me/guilds")) { + const after = Number(url.searchParams.get("after") ?? "0"); + const ids = Array.from({ length: 201 }, (_, i) => i + 1).filter((i) => i > after); + return new Response(JSON.stringify(ids.slice(0, 200).map((id) => ({ id: String(id) }))), { + status: 200, + }); + } + const guild = Number(url.pathname.split("/guilds/")[1]?.split("/")[0]); + put.push(String(guild)); + return new Response("[]", { status: guild >= 172 && guild <= 200 ? 500 : 200 }); + }) as unknown as typeof fetch; + const env = { + GITHUB_WEBHOOK_SECRET: "", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + DISCORD_TOKEN: "t", + DISCORD_APPLICATION_ID: "app", + }; + try { + for (let run = 0; run < 12; run++) await syncGuildCommands(env); + } finally { + globalThis.fetch = realFetch; + } + expect(put).toContain("201"); + }); +}); + +describe("quota review 1", () => { + it("fetches and stores one Feishu token for parallel sends", async () => { + const { getTenantAccessToken } = await import("../server/lib/drivers/feishu/rest"); + const { kv } = createMemoryKV(); + let fetches = 0; + let writes = 0; + const put = kv.put.bind(kv); + kv.put = ((...args: unknown[]) => { + writes++; + return (put as (...a: unknown[]) => unknown)(...args); + }) as KVNamespace["put"]; + const realFetch = globalThis.fetch; + globalThis.fetch = (async () => { + fetches++; + return new Response(JSON.stringify({ code: 0, tenant_access_token: "t", expire: 7200 }), { + status: 200, + }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + KV: kv, + DB: createSqliteD1().db, + FEISHU_APP_ID: "cli_quota", + FEISHU_APP_SECRET: "s", + }; + const results = await Promise.all(Array.from({ length: 5 }, () => getTenantAccessToken(env))); + expect(results.every((r) => r.token === "t")).toBe(true); + } finally { + globalThis.fetch = realFetch; + } + expect(fetches).toBe(1); + expect(writes).toBe(1); + }); + + it("registers an unchanged Telegram webhook once, not on every cron run", async () => { + const { syncTelegramWebhook } = await import("../server/lib/drivers/telegram/commands"); + let calls = 0; + const realFetch = globalThis.fetch; + globalThis.fetch = (async () => { + calls++; + return new Response(JSON.stringify({ ok: true }), { status: 200 }); + }) as unknown as typeof fetch; + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + KV: createMemoryKV().kv, + DB: createSqliteD1().db, + TELEGRAM_TOKEN: "1:x", + BASE_URL: "https://hooks.example", + }; + try { + for (let i = 0; i < 3; i++) await syncTelegramWebhook(env); + } finally { + globalThis.fetch = realFetch; + } + expect(calls).toBe(1); + }); + + it("does not read KV again for a language without an override", async () => { + const { loadTranslations } = await import("../server/lib/lib/i18n"); + let reads = 0; + const kv = { + get: async (): Promise => { + reads++; + return null; + }, + }; + for (let i = 0; i < 5; i++) await loadTranslations("xx-quota", kv); + expect(reads).toBe(1); + }); +}); + +describe("quota review 5", () => { + const feishuEnv = (kv: KVNamespace, appId: string): Env => ({ + GITHUB_WEBHOOK_SECRET: "", + KV: kv, + DB: createSqliteD1().db, + FEISHU_APP_ID: appId, + FEISHU_APP_SECRET: "s", + }); + + it("answers targets after a failed Feishu token request from memory for a moment", async () => { + const { getTenantAccessToken } = await import("../server/lib/drivers/feishu/rest"); + const { kv } = createMemoryKV(); + let fetches = 0; + const realFetch = globalThis.fetch; + globalThis.fetch = (async () => { + fetches++; + return new Response(JSON.stringify({ code: 99991663, msg: "app ticket invalid" }), { + status: 200, + }); + }) as unknown as typeof fetch; + try { + const env = feishuEnv(kv, "cli_fail_cache"); + for (let i = 0; i < 5; i++) expect((await getTenantAccessToken(env)).ok).toBe(false); + } finally { + globalThis.fetch = realFetch; + } + expect(fetches).toBe(1); + }); + + it("keeps a Feishu token in KV only while a held copy would still be valid", async () => { + const { getTenantAccessToken, tokenKey } = await import("../server/lib/drivers/feishu/rest"); + const { kv } = createMemoryKV(); + const ttls: number[] = []; + const put = kv.put.bind(kv); + kv.put = ((key: string, value: string, opts?: { expirationTtl?: number }) => { + ttls.push(opts?.expirationTtl ?? 0); + return put(key, value, opts); + }) as KVNamespace["put"]; + const realFetch = globalThis.fetch; + globalThis.fetch = (async () => + new Response(JSON.stringify({ code: 0, tenant_access_token: "t", expire: 7200 }), { + status: 200, + })) as unknown as typeof fetch; + try { + const env = feishuEnv(kv, "cli_ttl"); + expect((await getTenantAccessToken(env)).token).toBe("t"); + expect(await kv.get(tokenKey(env))).toBe("t"); + } finally { + globalThis.fetch = realFetch; + } + // 7200 s token: out of KV 60 s + one hold (300 s) before it expires. + expect(ttls).toEqual([7200 - 60 - 300]); + }); + + it("drops a rejected Feishu token once, and never a newer one", async () => { + const { forgetRejectedToken, tokenKey } = await import("../server/lib/drivers/feishu/rest"); + const { kv } = createMemoryKV(); + const env = feishuEnv(kv, "cli_forget"); + let deletes = 0; + const del = kv.delete.bind(kv); + kv.delete = ((key: string) => { + deletes++; + return del(key); + }) as KVNamespace["delete"]; + await kv.put(tokenKey(env), "old"); + const refused = { ok: false, errorCode: "FEISHU_TOKEN" }; + for (let i = 0; i < 5; i++) await forgetRejectedToken(refused, env, "old"); + expect(deletes).toBe(1); + // A late refusal of an older token leaves the token stored since alone. + await kv.put(tokenKey(env), "new"); + await forgetRejectedToken(refused, env, "older"); + expect(await kv.get(tokenKey(env))).toBe("new"); + }); + + it("serves recent failures and a group's audit log from indexes", () => { + const { sqlite } = createSqliteD1(); + const plan = (sql: string): string => + (sqlite.prepare(`EXPLAIN QUERY PLAN ${sql}`).all() as Array<{ detail: string }>) + .map((r) => r.detail) + .join("; "); + expect(plan("SELECT id FROM send_logs WHERE ok = 0 ORDER BY ts DESC LIMIT 20")).toContain( + "idx_send_logs_failed_ts", + ); + expect( + plan("SELECT id FROM send_logs WHERE ok = 0 AND group_id = 'g' ORDER BY ts DESC LIMIT 20"), + ).toContain("idx_send_logs_failed_group_ts"); + const audit = plan("SELECT id FROM audit_logs WHERE group_id = 'g' ORDER BY ts DESC LIMIT 50"); + expect(audit).toContain("idx_audit_group_ts"); + expect(audit).not.toContain("TEMP B-TREE"); + }); +}); + +describe("quota review 6", () => { + it("shares one fallback marker read among concurrent checks", async () => { + const { kvFallbackActive } = await import("../server/lib/storage/d1"); + const { kv } = createMemoryKV(); + let reads = 0; + const get = kv.get.bind(kv); + kv.get = ((...args: unknown[]) => { + reads++; + return (get as (...a: unknown[]) => unknown)(...args); + }) as KVNamespace["get"]; + const results = await Promise.all(Array.from({ length: 20 }, () => kvFallbackActive(kv))); + expect(results.every((r) => r === false)).toBe(true); + expect(reads).toBe(1); + }); + + it("runs a cold store's setup once for concurrent requests", async () => { + const { initConfigStore, loadConfigSnapshot } = await import("../server/lib/config"); + const { db } = createSqliteD1(); + const { kv } = createMemoryKV(); + let seedReads = 0; + const get = kv.get.bind(kv); + kv.get = ((key: string, ...rest: unknown[]) => { + if (key === "config:d1-seeded") seedReads++; + return (get as (...a: unknown[]) => unknown)(key, ...rest); + }) as KVNamespace["get"]; + const env: Env = { GITHUB_WEBHOOK_SECRET: "", KV: kv, DB: db }; + initConfigStore(env); + await Promise.all(Array.from({ length: 20 }, () => loadConfigSnapshot(env, { fresh: true }))); + expect(seedReads).toBe(1); + }); + + it("holds a Feishu token it could not share in KV until it nears expiry", async () => { + const { getTenantAccessToken } = await import("../server/lib/drivers/feishu/rest"); + const { kv } = createMemoryKV(); + kv.put = (async () => { + throw new Error("KV put() limit exceeded for the day"); + }) as KVNamespace["put"]; + let fetches = 0; + const realFetch = globalThis.fetch; + const realNow = Date.now; + globalThis.fetch = (async () => { + fetches++; + return new Response(JSON.stringify({ code: 0, tenant_access_token: "t", expire: 7200 }), { + status: 200, + }); + }) as unknown as typeof fetch; + try { + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + KV: kv, + DB: createSqliteD1().db, + FEISHU_APP_ID: "cli_no_kv", + FEISHU_APP_SECRET: "s", + }; + expect((await getTenantAccessToken(env)).token).toBe("t"); + // An hour on (well past the usual few minutes in memory): still held. + Date.now = (): number => realNow() + 3600_000; + expect((await getTenantAccessToken(env)).token).toBe("t"); + } finally { + globalThis.fetch = realFetch; + Date.now = realNow; + } + expect(fetches).toBe(1); + }); +}); + +describe("quota review 7 (partial)", () => { + it("writes the fallback marker once for concurrent fallbacks", async () => { + const { noteKvFallback } = await import("../server/lib/storage/d1"); + const { kv } = createMemoryKV(); + let writes = 0; + const put = kv.put.bind(kv); + kv.put = ((...args: Parameters) => { + writes++; + return put(...args); + }) as KVNamespace["put"]; + await Promise.all(Array.from({ length: 20 }, () => noteKvFallback(kv))); + expect(writes).toBe(1); + }); + + it("reads a language override once for concurrent asks", async () => { + const { loadTranslations } = await import("../server/lib/lib/i18n"); + let reads = 0; + const kv = { + get: async (): Promise => { + reads++; + return null; + }, + }; + await Promise.all(Array.from({ length: 20 }, () => loadTranslations("xx-concurrent", kv))); + expect(reads).toBe(1); + }); + + it("does not ask GitHub again right after an installation token exchange failed", async () => { + const { getInstallationToken } = await import("../server/lib/github/app-token"); + const { generateKeyPairSync } = await import("node:crypto"); + const { privateKey } = generateKeyPairSync("rsa", { modulusLength: 2048 }); + const pem = privateKey.export({ type: "pkcs8", format: "pem" }).toString(); + let requests = 0; + const realFetch = globalThis.fetch; + globalThis.fetch = (async () => { + requests++; + return new Response("{}", { status: 500 }); + }) as unknown as typeof fetch; + try { + for (let i = 0; i < 3; i++) { + expect(await getInstallationToken("1", pem, 777001)).toBeUndefined(); + } + } finally { + globalThis.fetch = realFetch; + } + expect(requests).toBe(1); + }); +}); + +describe("round 52", () => { + it("never waits on another request's run past its limit", async () => { + const { sharedFlight } = await import("../server/lib/lib/shared-flight"); + const flights = new Map; startedAt: number }>(); + // A run whose request was canceled: it never settles, its cleanup never runs. + flights.set("k", { promise: new Promise(() => {}), startedAt: Date.now() }); + const started = performance.now(); + expect(await sharedFlight(flights, "k", async () => "own", 30)).toBe("own"); + expect(performance.now() - started).toBeLessThan(1000); + // A run that old is not joined at all. + flights.set("k", { promise: new Promise(() => {}), startedAt: Date.now() - 60 }); + expect(await sharedFlight(flights, "k", async () => "fresh", 30)).toBe("fresh"); + expect(flights.has("k")).toBe(false); + }); + + it("replaces a stuck run once for all its waiters", async () => { + const { sharedFlight } = await import("../server/lib/lib/shared-flight"); + const flights = new Map; startedAt: number }>(); + flights.set("k", { promise: new Promise(() => {}), startedAt: Date.now() }); + let runs = 0; + const run = async (): Promise => { + runs++; + await new Promise((r) => setTimeout(r, 20)); + return "replaced"; + }; + const results = await Promise.all( + Array.from({ length: 10 }, () => sharedFlight(flights, "k", run, 30)), + ); + expect(results.every((r) => r === "replaced")).toBe(true); + expect(runs).toBe(1); + }); + + it("lets every waiter of a stuck run take a replacement that already finished", async () => { + const { sharedFlight } = await import("../server/lib/lib/shared-flight"); + const flights = new Map; startedAt: number }>(); + flights.set("k", { promise: new Promise(() => {}), startedAt: Date.now() }); + let runs = 0; + // Finishes (and leaves the map) before the other waiters wake up. + const run = async (): Promise => { + runs++; + return "replaced"; + }; + const results = await Promise.all( + Array.from({ length: 10 }, () => sharedFlight(flights, "k", run, 30)), + ); + expect(results.every((r) => r === "replaced")).toBe(true); + expect(runs).toBe(1); + }); + + it("runs alone, unpublished, after finding three runs stuck", async () => { + const { sharedFlight } = await import("../server/lib/lib/shared-flight"); + type F = { promise: Promise; startedAt: number; replacedBy?: F }; + const stuck = (): F => ({ promise: new Promise(() => {}), startedAt: Date.now() }); + const first = stuck(); + first.replacedBy = stuck(); + first.replacedBy.replacedBy = stuck(); + const flights = new Map([["k", first]]); + expect(await sharedFlight(flights, "k", async () => "alone", 20)).toBe("alone"); + expect(flights.get("k")).toBe(first); + }); + + it("shares a run that is still in time", async () => { + const { sharedFlight } = await import("../server/lib/lib/shared-flight"); + const flights = new Map; startedAt: number }>(); + let runs = 0; + const run = async (): Promise => { + runs++; + await new Promise((r) => setTimeout(r, 10)); + return runs; + }; + const results = await Promise.all( + Array.from({ length: 5 }, () => sharedFlight(flights, "k", run, 1000)), + ); + expect(results).toEqual([1, 1, 1, 1, 1]); + expect(runs).toBe(1); + }); + + it("does not take a refused Feishu token back from KV when deleting it failed", async () => { + const { forgetRejectedToken, getTenantAccessToken, tokenKey } = + await import("../server/lib/drivers/feishu/rest"); + const { kv } = createMemoryKV(); + const env: Env = { + GITHUB_WEBHOOK_SECRET: "", + KV: kv, + DB: createSqliteD1().db, + FEISHU_APP_ID: "cli_refused", + FEISHU_APP_SECRET: "s", + }; + await kv.put(tokenKey(env), "refused"); + const del = kv.delete.bind(kv); + kv.delete = (async () => { + throw new Error("KV delete failed"); + }) as KVNamespace["delete"]; + await forgetRejectedToken({ ok: false, errorCode: "FEISHU_TOKEN" }, env, "refused"); + kv.delete = del; + let fetches = 0; + const realFetch = globalThis.fetch; + globalThis.fetch = (async () => { + fetches++; + return new Response(JSON.stringify({ code: 0, tenant_access_token: "new", expire: 7200 }), { + status: 200, + }); + }) as unknown as typeof fetch; + try { + expect((await getTenantAccessToken(env)).token).toBe("new"); + } finally { + globalThis.fetch = realFetch; + } + expect(fetches).toBe(1); + }); + + it("registers a Telegram webhook switched back to an earlier setting again", async () => { + const { syncTelegramWebhook } = await import("../server/lib/drivers/telegram/commands"); + const { kv } = createMemoryKV(); + const urls: string[] = []; + const realFetch = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + urls.push(String(JSON.parse(String(init?.body)).url)); + return new Response(JSON.stringify({ ok: true }), { status: 200 }); + }) as unknown as typeof fetch; + try { + for (const base of ["https://a.example", "https://b.example", "https://a.example"]) { + await syncTelegramWebhook({ + GITHUB_WEBHOOK_SECRET: "", + KV: kv, + TELEGRAM_TOKEN: "77:switch", + BASE_URL: base, + }); + } + } finally { + globalThis.fetch = realFetch; + } + expect(urls).toEqual([ + "https://a.example/telegram/webhook", + "https://b.example/telegram/webhook", + "https://a.example/telegram/webhook", + ]); + }); +});