From 8ec09ba0f1dc54ff76748d2956b6c501a56ae081 Mon Sep 17 00:00:00 2001 From: AppSolves Date: Sun, 15 Nov 2026 01:28:30 +0100 Subject: [PATCH] fix: publish durable feedback for rejected review commands --- CHANGELOG.md | 2 + docs/operations.md | 16 +++ migrations/0005_review_admission_feedback.sql | 13 ++ src/worker/admission.ts | 63 +++++++++ src/worker/index.ts | 3 +- src/worker/recovery.ts | 6 +- src/worker/rejections.ts | 93 +++++++++++++ src/worker/store.ts | 37 +++-- src/worker/webhook.ts | 43 ++++-- tests/admission-runtime-worker.ts | 17 +++ tests/admission.test.ts | 131 ++++++++++++++++++ tests/security-regressions.test.ts | 6 +- tests/sqlite.ts | 8 +- tests/worker.test.ts | 39 +++++- tests/workers-runtime.mjs | 6 +- 15 files changed, 447 insertions(+), 36 deletions(-) create mode 100644 migrations/0005_review_admission_feedback.sql create mode 100644 src/worker/admission.ts create mode 100644 src/worker/rejections.ts create mode 100644 tests/admission-runtime-worker.ts create mode 100644 tests/admission.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 4242fa4..3c46414 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Security and behavior changes +- Admission feedback: precise atomic quota/duplicate/cooldown/concurrency outcomes, calculated UTC retry times and durable App-owned rejection comments; deterministic denials no longer churn webhook retries. Additive migration 0005 and bounded ambiguity-safe publication recovery. + ## Unreleased — security remediation pass - Add bounded immutable file-index/context-socket exploration, simple Python/Rust dependencies, unchanged-caller fixtures and concise evidence-driven review guidance. Keep the reviewer read-only, network-disabled and credential-free; drain/cancel lookups and revoke the read token during cleanup. Improve native review summaries without changing verdict policy or publication fingerprints. diff --git a/docs/operations.md b/docs/operations.md index 3b530a3..74af77c 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -52,3 +52,19 @@ A newer authorized same-PR rereview supersedes an older queued merge at the last Migration 0004 and [the browser runbook](web-onboarding.md) add encrypted web transactions, sessions, hosted connection journals and repository operation leases. Apply the additive migration before deploying the new Worker; old backend code remains compatible with the extra tables. Never delete or overwrite existing CLI sessions as a migration shortcut. Inspect only fixed state/IDs, never encrypted-payload dumps in public logs. A connection stuck in pending after an ambiguous GitHub secret write is resumed through the owning administrator's repository dashboard. It uses the same journal and vault key. A refreshing/reconnect-required state is not a successful rotation; reconnect through a fresh validated OAuth flow. Leases use atomic owner-checked acquisition/heartbeat and ownership-aware release; do not manually reset a lease while its holder may still run. A lost owner cannot publish a new encrypted session or release a successor. Quiesce browser operations and keep master-key backups before any planned key rotation; production secret changes and provider revocation remain explicit operator actions. + +## Admission rejection feedback + +Migration `0005_review_admission_feedback.sql` must precede the Worker deployment. It adds a tenant-scoped outbox; existing sessions, quotas, requests and progress comments are unchanged. Do not manually apply its SQL or reset quota records. Wrangler applies only pending official migrations. + +Admission and its precise rejection outcome are persisted in one D1 batch transaction. Outcomes distinguish rerun/user/repository/installation sliding quotas, duplicate context, cooldown and active concurrency. The existing guarded admission predicates are unchanged. Retry timestamps use the actual persisted timestamps: the quota-th newest request expires after 24 hours (including a subsequently lowered limit), cooldown uses the latest matching request, and unknown completion/duplicate-context times have no ETA. The message describes when that particular limit frees a slot; other admission checks still apply. + +Only fresh recognized manual commands on verified PRs from authenticated, authorized human senders create public feedback. Sender IDs are checked against live GitHub permission responses. Messages contain fixed prose and validated counts/times, never the command text, hidden instructions, provider text or raw exception codes. Automatic events produce no rejection comments. Feedback never dispatches a workflow. Delivery/command identities, leases and App-owned markers prevent redelivery/parallel/cron comment spam; no user mention is inserted into feedback. + +Deterministic denials complete the delivery; the separate outbox retries temporary GitHub failures with jitter/backoff, up to five attempts. The two-minute recovery invocation handles at most one due rejection and one progress update, so neither queue starves the other. Deleted/revoked installation state is checked live, and current actor authorization is required before publishing. + +GitHub issue-comment POST has no idempotency key. Before POST, the outbox durably enters `creating`. A lost response or 5xx is reconciled by exact marker, numeric App ID, bot identity and PR issue URL, scanning at most three 100-comment pages since acceptance. It never blindly repeats an ambiguous POST. A definitive no-write 429 can retry. If a crash occurred before POST, a comment was deleted, or an accepted comment cannot be found within the bounded scan, the record becomes `failed` for operator inspection instead of risking duplicate notifications. This is an explicit API limitation, not a guarantee of exactly-once delivery under every failure. + +Inspect safe state with `SELECT id,outcome,phase,attempts,comment_id,retry_at FROM review_rejections`; do not select identity/payload/session data into logs. After an ambiguous terminal failure, inspect App-owned comments before any manual response; do not reset `creating` to `pending`. Recovery of an already-recorded denial never re-enters admission. Old deliveries without a durable outcome still obey the original freshness checks and must not be revived to start inference. + +After normal protected source merge, run `scripts/prepare-pin-upgrade.mjs` first with `--check-only`, then without it, using the actual source PR number. Merge both reviewed pin PRs, apply migration 0005, and deploy the new Worker with existing gates preserved. Verify version details, pins, `/health`, a fresh authorized exhausted-quota comment and the terminal delivery/outbox state. No paid inference or quota reset is needed for this test. diff --git a/migrations/0005_review_admission_feedback.sql b/migrations/0005_review_admission_feedback.sql new file mode 100644 index 0000000..839cbfc --- /dev/null +++ b/migrations/0005_review_admission_feedback.sql @@ -0,0 +1,13 @@ +-- Durable outcomes contain identifiers and fixed policy metadata, never prompts or credentials. +CREATE TABLE review_rejections ( + id TEXT PRIMARY KEY, request_id TEXT NOT NULL UNIQUE, + installation_id INTEGER NOT NULL, repository_id INTEGER NOT NULL, + identity TEXT NOT NULL, outcome TEXT NOT NULL, + quota INTEGER, window_ms INTEGER, retry_at INTEGER, + created_at INTEGER NOT NULL, + phase TEXT NOT NULL DEFAULT 'pending' CHECK(phase IN ('pending','creating','done','failed')), + comment_id INTEGER, lease TEXT, lease_until INTEGER NOT NULL DEFAULT 0, + attempts INTEGER NOT NULL DEFAULT 0, next_attempt INTEGER NOT NULL DEFAULT 0 +); +CREATE INDEX review_rejections_due ON review_rejections(phase,next_attempt,lease_until); +CREATE INDEX review_rejections_tenant ON review_rejections(installation_id,repository_id); diff --git a/src/worker/admission.ts b/src/worker/admission.ts new file mode 100644 index 0000000..f1cae01 --- /dev/null +++ b/src/worker/admission.ts @@ -0,0 +1,63 @@ +import { AppError, requireCondition } from '../core/errors.js'; +import { sha256 } from '../core/crypto.js'; +import { REQUEST_LIFETIME_MS } from '../core/limits.js'; +import type { RequestSnapshot } from './store.js'; + +export const QUOTA_WINDOW_MS = 86_400_000; +export type RejectionCode = 'rerun_limit_exceeded' | 'user_quota_exceeded' | 'repository_quota_exceeded' | 'installation_quota_exceeded' | + 'repository_concurrency_exceeded' | 'installation_concurrency_exceeded' | 'cooldown_active' | 'duplicate_review' | + 'invalid_command' | 'merge_not_authorized' | 'rerun_requires_write_permission' | 'pr_not_reviewable' | 'chatgpt_deployment_not_authorized'; +export interface RejectionIdentity { + id: string; installationId: number; repositoryId: number; ownerId: number; repository: string; pr: number; actor: string; actorId: number; commandId?: number; +} +export interface RejectionRow { + id: string; request_id: string; identity: string; outcome: RejectionCode; quota: number | null; window_ms: number | null; retry_at: number | null; + created_at: number; phase: string; comment_id: number | null; lease: string | null; lease_until: number; attempts: number; +} +export class AdmissionError extends AppError { + constructor(public readonly rejection: RejectionRow) { super(rejection.outcome, 429); } +} +export async function rejectionId(identity: RejectionIdentity): Promise { + return sha256(JSON.stringify([identity.installationId, identity.repositoryId, identity.actorId, identity.commandId ?? identity.id])); +} +export function rejectionIdentity(value: RejectionIdentity): RejectionIdentity { + return { id: value.id, installationId: value.installationId, repositoryId: value.repositoryId, ownerId: value.ownerId, + repository: value.repository, pr: value.pr, actor: value.actor, actorId: value.actorId, ...(value.commandId ? { commandId: value.commandId } : {}) }; +} +/** Run immediately after the guarded INSERT in the SAME D1 batch/transaction. */ +export async function admissionRejection(db: D1Database, snapshot: RequestSnapshot, key: string, now: number, feedback: boolean): Promise { + const q = snapshot.config.quotas, day = now - QUOTA_WINDOW_MS; + const candidates: { code: RejectionCode; where: string; args: number[]; limit: number; window: number | null }[] = [ + ...(snapshot.command.rerun ? [{ code: 'rerun_limit_exceeded' as const, where: 'installation_id=? AND actor_id=? AND rerun=1 AND created_at>?', args: [snapshot.installationId,snapshot.actorId,day], limit: q.reruns_per_day, window: QUOTA_WINDOW_MS }] : []), + { code: 'user_quota_exceeded', where: 'installation_id=? AND actor_id=? AND created_at>?', args: [snapshot.installationId,snapshot.actorId,day], limit: q.user_per_day, window: QUOTA_WINDOW_MS }, + { code: 'repository_quota_exceeded', where: 'installation_id=? AND repository_id=? AND created_at>?', args: [snapshot.installationId,snapshot.repositoryId,day], limit: q.repository_per_day, window: QUOTA_WINDOW_MS }, + { code: 'installation_quota_exceeded', where: 'installation_id=? AND created_at>?', args: [snapshot.installationId,day], limit: q.installation_per_day, window: QUOTA_WINDOW_MS }, + { code: 'repository_concurrency_exceeded', where: "installation_id=? AND repository_id=? AND state NOT IN ('published','failed') AND created_at>?", args: [snapshot.installationId,snapshot.repositoryId,now-REQUEST_LIFETIME_MS], limit: q.concurrent_repository, window: null }, + { code: 'installation_concurrency_exceeded', where: "installation_id=? AND state NOT IN ('published','failed') AND created_at>?", args: [snapshot.installationId,now-REQUEST_LIFETIME_MS], limit: q.concurrent_installation, window: null }, + { code: 'cooldown_active', where: 'installation_id=? AND repository_id=? AND actor_id=? AND created_at>?', args: [snapshot.installationId,snapshot.repositoryId,snapshot.actorId,now-q.cooldown_seconds*1000], limit: 1, window: q.cooldown_seconds*1000 } + ]; + // VALUES avoids D1's small compound-SELECT limit; all policy predicates stay in the admission transaction. + const args: (string | number | null)[] = [], choices = candidates.map((candidate,index) => { + const retry = candidate.window === null ? 'NULL' : `(SELECT created_at+? FROM requests WHERE ${candidate.where} ORDER BY created_at DESC LIMIT 1 OFFSET ?)`; + if (candidate.window !== null) args.push(candidate.window,...candidate.args,candidate.limit-1); + args.push(...candidate.args,candidate.limit); + return `(${index+1},'${candidate.code}',${candidate.limit},${candidate.window ?? 'NULL'},${retry},(SELECT count(*) FROM requests WHERE ${candidate.where})>=?)`; + }); + // Duplicate identity wins even if other limits also apply; no time-based promise can unblock it. + choices.unshift("(0,'duplicate_review',NULL,NULL,NULL,EXISTS(SELECT 1 FROM requests WHERE request_key=?))"); + args.unshift(key); + return db.prepare(`WITH checks(priority,outcome,quota,window_ms,retry_at,blocked) AS (VALUES ${choices.join(',')}) + INSERT OR IGNORE INTO review_rejections(id,request_id,installation_id,repository_id,identity,outcome,quota,window_ms,retry_at,created_at,phase) + SELECT ?,?,?,?,?,outcome,quota,window_ms,retry_at,?,? FROM checks + WHERE blocked AND NOT EXISTS(SELECT 1 FROM requests WHERE id=? OR (installation_id=? AND repository_id=? AND actor_id=? AND json_extract(snapshot,'$.commandId')=?)) ORDER BY priority LIMIT 1`) + .bind(...args,await rejectionId(snapshot),snapshot.id,snapshot.installationId,snapshot.repositoryId,JSON.stringify(rejectionIdentity(snapshot)),now,feedback ? 'pending' : 'done',snapshot.id,snapshot.installationId,snapshot.repositoryId,snapshot.actorId,snapshot.commandId ?? null); +} +export async function getRejection(db: D1Database, id: string): Promise { + return db.prepare('SELECT * FROM review_rejections WHERE id=? OR request_id=?').bind(id,id).first(); +} +export async function recordRejection(db: D1Database, identity: RejectionIdentity, outcome: RejectionCode): Promise { + const id = await rejectionId(identity); + await db.prepare('INSERT OR IGNORE INTO review_rejections(id,request_id,installation_id,repository_id,identity,outcome,created_at) VALUES(?,?,?,?,?,?,?)') + .bind(id,identity.id,identity.installationId,identity.repositoryId,JSON.stringify(rejectionIdentity(identity)),outcome,Date.now()).run(); + const row = await getRejection(db,id); requireCondition(row,'admission_outcome_missing',503); return row; +} diff --git a/src/worker/index.ts b/src/worker/index.ts index 33d5e02..b0d2036 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -13,6 +13,7 @@ import { setupPullRequest } from '../setup.js'; import { processDelivery, recover } from './recovery.js'; import { MAX_RESULT_BODY_BYTES } from '../core/limits.js'; import { repositoryCapabilities } from '../capabilities.js'; +import { recoverRejection } from './rejections.js'; import { compactWebhook } from './webhook.js'; import { credentialEnvironment, publishingEnvironment } from '../core/identity.js'; import { updateProgress, recoverProgress } from './progress.js'; @@ -310,7 +311,7 @@ export default { if (event.cron === '*/2 * * * *') { // A separate bounded invocation prevents busy dispatch/publication queues // from starving terminal comment updates or sharing their subrequest budget. - const store = new Store(env.DB); await store.cleanup(); await recoverProgress(env, store); + const store = new Store(env.DB); await store.cleanup(); await recoverRejection(env); await recoverProgress(env, store); } else await recover(env); } } satisfies ExportedHandler; diff --git a/src/worker/recovery.ts b/src/worker/recovery.ts index 1fb9149..eca8c53 100644 --- a/src/worker/recovery.ts +++ b/src/worker/recovery.ts @@ -7,15 +7,19 @@ import { invalidateMerge } from './merge.js'; import type { RequestSnapshot } from './store.js'; import { MAX_RECOVERY_ATTEMPTS } from '../core/limits.js'; import { recoverProgress } from './progress.js'; +import { getRejection, publishRejection } from './rejections.js'; +import { AdmissionError } from './admission.js'; export async function processDelivery(id: string, env: Env): Promise { const store = new Store(env.DB), delivery = await store.claimDelivery(id); if (!delivery) return 'recovering'; try { + const rejection = await getRejection(env.DB,delivery.request_id); + if (rejection) { await publishRejection(env,rejection.id); await store.finishDelivery(delivery,true); return 'admission_rejected'; } const status = await ingest(delivery.event, JSON.parse(delivery.payload) as unknown, delivery.request_id, env); await store.finishDelivery(delivery, true); return status; } catch (error) { - const terminal = error instanceof AppError && error.status < 500 && error.status !== 429; + const terminal = error instanceof AdmissionError || (error instanceof AppError && error.status < 500 && error.status !== 429); if (terminal) delivery.attempts = MAX_RECOVERY_ATTEMPTS; await store.finishDelivery(delivery, false, error instanceof AppError ? error.code : 'external_or_storage_failure'); throw error; diff --git a/src/worker/rejections.ts b/src/worker/rejections.ts new file mode 100644 index 0000000..5b720a4 --- /dev/null +++ b/src/worker/rejections.ts @@ -0,0 +1,93 @@ +import type { Octokit } from '@octokit/rest'; +import { AppError, requireCondition } from '../core/errors.js'; +import { authorize } from '../core/policy.js'; +import { MAX_RECOVERY_ATTEMPTS, RECOVERY_LEASE_MS } from '../core/limits.js'; +import { appClient, installationClient, permission, splitRepo, trustedConfig } from './github.js'; +import { retryDelay } from './store.js'; +import { getRejection, type RejectionIdentity, type RejectionRow } from './admission.js'; +import type { BackendConfig } from './webhook.js'; + +export function rejectionBody(row: RejectionRow): string { + const identity = JSON.parse(row.identity) as RejectionIdentity; + requireCondition(/^[A-Za-z0-9_-]{43}$/.test(row.id) && Number.isSafeInteger(identity.pr) && identity.pr > 0 && Number.isSafeInteger(identity.repositoryId), 'invalid_rejection_identity'); + const messages = { + rerun_limit_exceeded: 'Your explicit rerun allowance has been reached.', + user_quota_exceeded: 'Your review request quota has been reached.', + repository_quota_exceeded: 'The repository review request quota has been reached.', + installation_quota_exceeded: 'The installation review request quota has been reached.', + repository_concurrency_exceeded: 'The repository already has the maximum number of active reviews. Wait for an active review to finish.', + installation_concurrency_exceeded: 'The installation already has the maximum number of active reviews. Wait for an active review to finish.', + cooldown_active: 'The cooldown between your review requests is still active.', + duplicate_review: 'This review request was already accepted for the same commit and review context. Use an authorized --rerun for an intentional repeat, subject to its separate allowance.', + invalid_command: 'The command has an invalid, repeated or unsupported option. Check quoting and use review [--model MODEL] [--effort LEVEL] [--rerun] [--merge].', + merge_not_authorized: 'The requested merge operation is not permitted by the current repository policy or your permissions. Request a review without --merge.', + rerun_requires_write_permission: 'Explicit reruns require repository write permission. Request a normal review without --rerun.', + pr_not_reviewable: 'This pull request is closed, a draft, or outside the supported review limits.', + chatgpt_deployment_not_authorized: 'ChatGPT review execution is not enabled for this deployment. Contact the operator.' + }; + requireCondition(Object.hasOwn(messages,row.outcome),'invalid_rejection_outcome'); + const quota = row.quota === null ? '' : row.window_ms === 86_400_000 ? ` Limit: ${row.quota} requests in a rolling 24-hour window.` + : row.outcome === 'cooldown_active' ? ` Cooldown: ${row.window_ms! / 1000} seconds.` : ` Active review limit: ${row.quota}.`; + const retry = row.retry_at !== null && Number.isSafeInteger(row.retry_at) ? ` For this limit, retry no earlier than **${new Date(row.retry_at).toISOString()} (UTC)**. Other admission checks still apply.` : ''; + return `\n**CodexBot did not start this review.**\n\n${messages[row.outcome]}${quota}${retry}\n\nNo AI inference was started for this command.`; +} +export async function publishRejection(env: BackendConfig, id: string): Promise { + const lease = crypto.randomUUID(), now = Date.now(); + const row = await env.DB.prepare(`UPDATE review_rejections SET lease=?,lease_until=?,attempts=attempts+1 WHERE id=? + AND phase IN ('pending','creating') AND lease_until(); + if (!row) return; + let posting = false; + try { + const identity = JSON.parse(row.identity) as RejectionIdentity, repo = splitRepo(identity.repository); + const { data: installation } = await (await appClient(env)).rest.apps.getRepoInstallation(repo); + requireCondition(installation.id === identity.installationId && !installation.suspended_at,'rejection_installation_changed',403); + const { github } = await installationClient(env,identity.installationId,identity.repositoryId,{contents:'read',pull_requests:'write'}); + const { data: metadata } = await github.rest.repos.get(repo); + requireCondition(metadata.id === identity.repositoryId && metadata.owner.id === identity.ownerId,'rejection_repository_changed',403); + const { data: branch } = await github.rest.repos.getBranch({...repo,branch:metadata.default_branch}); + const config = await trustedConfig(github,identity.repository,branch.commit.sha); + requireCondition(authorize(identity.actor,await permission(github,identity.repository,identity.actor,identity.actorId),config.authorization),'rejection_authorization_revoked',403); + const { data: pr } = await github.rest.pulls.get({...repo,pull_number:identity.pr}); + requireCondition(pr.base.repo.id === identity.repositoryId,'rejection_pr_changed',403); + await publishComment(github,env,row,lease); + } catch (error) { + const status = error && typeof error === 'object' && 'status' in error ? Number(error.status) : 0; + // Only definitive no-write responses allow another POST. A lost response remains reconciliation-only. + const definitive = posting && [400,401,403,404,410,422,429].includes(status); + const terminal = error instanceof AppError && [400,403].includes(error.status); + await env.DB.prepare(`UPDATE review_rejections SET phase=CASE WHEN ? OR attempts>=? THEN 'failed' WHEN ? THEN 'pending' ELSE phase END, + lease_until=0,next_attempt=? WHERE id=? AND lease=? AND lease_until>?`) + .bind(terminal ? 1 : 0,MAX_RECOVERY_ATTEMPTS,definitive ? 1 : 0,Date.now()+retryDelay(row.attempts),id,lease,Date.now()).run(); + console.error(JSON.stringify({event:'rejection_publication_deferred',...(status>=400&&status<=599?{status}:{})})); + } + async function publishComment(github: Octokit, env: BackendConfig, row: RejectionRow, lease: string): Promise { + const identity = JSON.parse(row.identity) as RejectionIdentity, repo = splitRepo(identity.repository), body = rejectionBody(row), marker = body.split('\n')[0]!; + const owned = (comment: { user: {type:string;login:string} | null; performed_via_github_app?: {id:number} | null; issue_url:string;body?:string }) => + comment.user?.type === 'Bot' && comment.user.login === `${env.GITHUB_APP_SLUG}[bot]` && comment.performed_via_github_app?.id === Number(env.GITHUB_APP_ID) && + comment.issue_url === `https://api.github.com/repos/${identity.repository}/issues/${identity.pr}` && comment.body?.startsWith(marker+'\n'); + let comment; + if (row.phase === 'creating') { + // GitHub has no comment idempotency key. Ambiguous POSTs are never repeated. + for (let page=1;page<=3&&!comment;page++) { + const {data} = await github.rest.issues.listComments({...repo,issue_number:identity.pr,since:new Date(row.created_at-60_000).toISOString(),per_page:100,page}); + comment=data.find(owned);if(data.length<100)break; + } + if (!comment) throw new AppError('rejection_awaiting_reconciliation',503); + } else { + const changed=await env.DB.prepare("UPDATE review_rejections SET phase='creating' WHERE id=? AND lease=? AND lease_until>? AND phase='pending'").bind(row.id,lease,Date.now()).run(); + requireCondition(changed.meta.changes===1,'rejection_lease_lost',409); + posting=true; comment=(await github.rest.issues.createComment({...repo,issue_number:identity.pr,body})).data;posting=false; + requireCondition(owned(comment),'rejection_comment_ownership_mismatch',403); + } + requireCondition(owned(comment),'rejection_comment_ownership_mismatch',403); + const saved=await env.DB.prepare("UPDATE review_rejections SET phase='done',comment_id=?,lease_until=0 WHERE id=? AND lease=? AND lease_until>?").bind(comment.id,row.id,lease,Date.now()).run(); + requireCondition(saved.meta.changes===1,'rejection_lease_lost',409); + } +} +export async function recoverRejection(env: BackendConfig): Promise { + const row=await env.DB.prepare("SELECT id FROM review_rejections WHERE phase IN ('pending','creating') AND lease_until(); + if (!row) return false;await publishRejection(env,row.id);return true; +} +export { getRejection }; diff --git a/src/worker/store.ts b/src/worker/store.ts index b3c7b7b..b8a7b0d 100644 --- a/src/worker/store.ts +++ b/src/worker/store.ts @@ -2,6 +2,7 @@ import type { Config } from '../core/config.js'; import type { ReviewCommand } from '../core/command.js'; import type { Envelope } from '../core/crypto.js'; import { sha256 } from '../core/crypto.js'; +import { AdmissionError, admissionRejection, getRejection, rejectionId } from './admission.js'; import { requireCondition } from '../core/errors.js'; import { normalizeReview } from '../core/review.js'; import { MAX_REVIEW_BYTES, MAX_RECOVERY_ATTEMPTS, RECOVERY_LEASE_MS, REQUEST_LIFETIME_MS } from '../core/limits.js'; @@ -9,7 +10,7 @@ import { MAX_REVIEW_BYTES, MAX_RECOVERY_ATTEMPTS, RECOVERY_LEASE_MS, REQUEST_LIF export interface RequestSnapshot { id: string; installationId: number; repositoryId: number; ownerId: number; repository: string; defaultBranch: string; workflowSha: string; head: string; base: string; pr: number; actor: string; actorId: number; command: ReviewCommand; config: Config; createdAt: number; - commits?: number; changedFiles?: number; + commits?: number; changedFiles?: number; commandId?: number; } export interface RequestRow { id: string; snapshot: string; state: string; run_id: string | null; review_id: number | null; result: string | null; context_complete: number; @@ -47,15 +48,17 @@ export class Store { .bind(Date.now(), Date.now(), MAX_RECOVERY_ATTEMPTS).all<{ id: string }>(); return results.map(x => x.id); } - async create(snapshot: RequestSnapshot, trustedWorkflow = ''): Promise { - const existing = await this.db.prepare('SELECT snapshot FROM requests WHERE id=?').bind(snapshot.id).first<{ snapshot: string }>(); + async create(snapshot: RequestSnapshot, trustedWorkflow = '', feedback = false): Promise { + const existingRequest = () => this.db.prepare(`SELECT snapshot FROM requests WHERE id=? OR (installation_id=? AND repository_id=? AND actor_id=? AND json_extract(snapshot,'$.commandId')=?)`).bind(snapshot.id,snapshot.installationId,snapshot.repositoryId,snapshot.actorId,snapshot.commandId ?? null).first<{ snapshot: string }>(); + const existing = await existingRequest(); if (existing) return JSON.parse(existing.snapshot) as RequestSnapshot; + const prior = await getRejection(this.db, await rejectionId(snapshot)); if (prior) throw new AdmissionError(prior); const command = { ...snapshot.command }; delete command.rerun; const key = await sha256(JSON.stringify({ installation: snapshot.installationId, repository: snapshot.repositoryId, owner: snapshot.ownerId, actor: snapshot.actorId, pr: snapshot.pr, head: snapshot.head, base: snapshot.base, branch: snapshot.defaultBranch, - workflow: snapshot.workflowSha, trustedWorkflow, config: snapshot.config, command, rerun: snapshot.command.rerun ? snapshot.id : null })); - const q = snapshot.config.quotas, day = Date.now() - 86_400_000, cooldown = Date.now() - q.cooldown_seconds * 1000; - const result = await this.db.prepare(`INSERT OR IGNORE INTO requests(id,installation_id,repository_id,actor_id,snapshot,created_at,request_key,rerun) + workflow: snapshot.workflowSha, trustedWorkflow, config: snapshot.config, command, rerun: snapshot.command.rerun ? snapshot.commandId ?? snapshot.id : null })); + const now = Date.now(), q = snapshot.config.quotas, day = now - 86_400_000, cooldown = now - q.cooldown_seconds * 1000; + const insert = this.db.prepare(`INSERT OR IGNORE INTO requests(id,installation_id,repository_id,actor_id,snapshot,created_at,request_key,rerun) SELECT ?,?,?,?,?,?,?,? WHERE (SELECT count(*) FROM requests WHERE installation_id=? AND actor_id=? AND created_at>?)?)?) { @@ -180,10 +191,10 @@ export class Store { .bind(encoded, complete ? 1 : 0, id, runId).run(); requireCondition(result.meta.changes === 1, 'review_result_already_recorded', 409); } async removeInstallation(id: number): Promise { - await this.db.batch([this.db.prepare('DELETE FROM sessions WHERE installation_id=?').bind(id), this.db.prepare('DELETE FROM requests WHERE installation_id=?').bind(id), this.db.prepare('DELETE FROM legacy_sessions WHERE installation_id=?').bind(id), this.db.prepare('DELETE FROM web_connections WHERE installation_id=?').bind(id), this.db.prepare('DELETE FROM web_operations WHERE installation_id=?').bind(id)]); + await this.db.batch([this.db.prepare('DELETE FROM review_rejections WHERE installation_id=?').bind(id), this.db.prepare('DELETE FROM sessions WHERE installation_id=?').bind(id), this.db.prepare('DELETE FROM requests WHERE installation_id=?').bind(id), this.db.prepare('DELETE FROM legacy_sessions WHERE installation_id=?').bind(id), this.db.prepare('DELETE FROM web_connections WHERE installation_id=?').bind(id), this.db.prepare('DELETE FROM web_operations WHERE installation_id=?').bind(id)]); } async removeRepository(repositoryId: number, installationId: number): Promise { - await this.db.batch([this.db.prepare('DELETE FROM sessions WHERE repository_id=? AND installation_id=?').bind(repositoryId, installationId), + await this.db.batch([this.db.prepare('DELETE FROM review_rejections WHERE repository_id=? AND installation_id=?').bind(repositoryId, installationId), this.db.prepare('DELETE FROM sessions WHERE repository_id=? AND installation_id=?').bind(repositoryId, installationId), this.db.prepare('DELETE FROM requests WHERE repository_id=? AND installation_id=?').bind(repositoryId, installationId), this.db.prepare('DELETE FROM legacy_sessions WHERE repository_id=? AND installation_id=?').bind(repositoryId, installationId)]); await this.db.batch([this.db.prepare('DELETE FROM web_connections WHERE repository_id=? AND installation_id=?').bind(repositoryId, installationId), this.db.prepare('DELETE FROM web_operations WHERE repository_id=? AND installation_id=?').bind(repositoryId, installationId)]); @@ -267,6 +278,8 @@ export class Store { } async cleanup(): Promise { await this.db.batch([ + this.db.prepare('DELETE FROM review_rejections WHERE created_at=? AND lease_until { const { data } = await app.rest.apps.getRepoInstallation(splitRepo(repository)); @@ -79,10 +81,16 @@ export async function ingest(event: string, payload: unknown, requestId: string, const { data: branch } = await github.rest.repos.getBranch({ ...repo, branch: liveRepo.default_branch }); const config = await trustedConfig(github, fullName, branch.commit.sha); if (!config.review.enabled || (!manual && !config.review.auto_review)) return 'disabled'; - const livePermission = await permission(github, fullName, sender.login); + const livePermission = await permission(github, fullName, sender.login, Number(sender.id)); requireCondition(authorize(sender.login, livePermission, config.authorization), 'review_not_authorized', 403); let prNumber: number; let command; + const reject = async (code: RejectionCode): Promise => { + if (!manual) return 'admission_rejected'; + const comment = record(input.comment); + const identity: RejectionIdentity = {id:requestId,installationId:Number(installation.id),repositoryId:liveRepo.id,ownerId:liveRepo.owner.id,repository:liveRepo.full_name,pr:prNumber,actor:sender.login as string,actorId:Number(sender.id),...(Number.isSafeInteger(comment.id)?{commandId:Number(comment.id)}:{})}; + const row = await recordRejection(env.DB,identity,code); await publishRejection(env,row.id); return 'command_rejected'; + }; if (manual) { const comment = record(input.comment), issue = record(input.issue); if (!issue.pull_request) return 'not_a_pull_request'; @@ -91,12 +99,13 @@ export async function ingest(event: string, payload: unknown, requestId: string, requireCondition(typeof comment.created_at === 'string' && Math.abs(Date.now() - Date.parse(comment.created_at)) < 600_000, 'old_comment_replay', 403); requireCondition(typeof comment.body === 'string', 'invalid_comment'); prNumber = Number(issue.number); - try { command = [config.bot.trigger, ...config.bot.aliases].map(handle => parseCommand(comment.body as string, handle)).find(Boolean) ?? null; } - catch (error) { - const code = error instanceof Error && /^[a-z0-9_]+$/.test(error.message) ? error.message : 'invalid_command'; - await github.rest.pulls.createReview({ ...repo, pull_number: prNumber, event: 'COMMENT', body: `CodexBot command rejected: \`${code}\`. Use \`${config.bot.trigger} review [--effort LEVEL] [--merge]\`.` }); - return 'command_rejected'; - } + requireCondition(Number.isSafeInteger(prNumber) && prNumber > 0, 'invalid_pr_number'); + // Only parse an exact configured first token; malformed commands for another bot stay silent. + const first = comment.body.trimStart().split(/\s+/,1)[0]?.toLowerCase(); + const handle = [config.bot.trigger,...config.bot.aliases].find(value=>value.toLowerCase()===first); + if (!handle) return 'no_command'; + try { command = parseCommand(comment.body,handle); } + catch { return reject('invalid_command'); } if (!command) return 'no_command'; } else { const pull = record(input.pull_request); @@ -104,19 +113,25 @@ export async function ingest(event: string, payload: unknown, requestId: string, prNumber = Number(input.number); command = { merge: false, effort: null, instructions: '' }; } requireCondition(Number.isSafeInteger(prNumber) && prNumber > 0, 'invalid_pr_number'); - requireCondition(!command.merge || (config.merge.enabled && authorize(sender.login, livePermission, config.merge.authorization, true)), 'merge_not_authorized', 403); - requireCondition(!command.rerun || ['write', 'maintain', 'admin'].includes(livePermission), 'rerun_requires_write_permission', 403); - requireCondition(config.credentials.mode !== 'chatgpt' || env.CHATGPT_DEPLOYMENT_AUTHORIZED === 'true', 'chatgpt_deployment_not_authorized', 403); + if (command.merge && !(config.merge.enabled && authorize(sender.login, livePermission, config.merge.authorization, true))) return reject('merge_not_authorized'); + if (command.rerun && !['write','maintain','admin'].includes(livePermission)) return reject('rerun_requires_write_permission'); + if (config.credentials.mode === 'chatgpt' && env.CHATGPT_DEPLOYMENT_AUTHORIZED !== 'true') return reject('chatgpt_deployment_not_authorized'); const { data: pr } = await github.rest.pulls.get({ ...repo, pull_number: prNumber }); if (automatic) requireCondition(record(record(input.pull_request).head).sha === pr.head.sha, 'stale_pr_event_head', 409); - requireCondition(pr.state === 'open' && !pr.draft && pr.base.repo.id === liveRepo.id && pr.changed_files <= 200, 'pr_not_reviewable'); + requireCondition(pr.base.repo.id === liveRepo.id,'pr_repository_mismatch',403); + if (pr.state !== 'open' || pr.draft || pr.changed_files > 200) return reject('pr_not_reviewable'); requireCondition(Number.isSafeInteger(pr.commits) && pr.commits > 0 && Number.isSafeInteger(pr.changed_files) && pr.changed_files >= 0, 'invalid_pr_counts'); let snapshot: RequestSnapshot = { id: requestId, installationId: Number(installation.id), repositoryId: liveRepo.id, ownerId: liveRepo.owner.id, repository: liveRepo.full_name, defaultBranch: liveRepo.default_branch, workflowSha: branch.commit.sha, - head: pr.head.sha, base: pr.base.sha, pr: prNumber, actor: sender.login, actorId: Number(sender.id), command, config, createdAt: Date.now(), commits: pr.commits, changedFiles: pr.changed_files + head: pr.head.sha, base: pr.base.sha, pr: prNumber, actor: sender.login, actorId: Number(sender.id), command, config, createdAt: Date.now(), ...(manual && Number.isSafeInteger(record(input.comment).id) ? {commandId:Number(record(input.comment).id)} : {}), commits: pr.commits, changedFiles: pr.changed_files }; - snapshot = await store.create(snapshot, env.TRUSTED_WORKFLOW); + try { snapshot = await store.create(snapshot, env.TRUSTED_WORKFLOW, manual); } + catch (error) { + if (!(error instanceof AdmissionError)) throw error; + if (manual) await publishRejection(env,error.rejection.id); + return 'admission_rejected'; + } requireCondition(snapshot.head === pr.head.sha && snapshot.base === pr.base.sha && snapshot.workflowSha === branch.commit.sha, 'request_context_changed', 409); try { await updateProgress(github, store, snapshot, env.GITHUB_APP_SLUG, Number(env.GITHUB_APP_ID)); } catch { console.error(JSON.stringify({ event: 'progress_update_deferred', request_id: snapshot.id })); } diff --git a/tests/admission-runtime-worker.ts b/tests/admission-runtime-worker.ts new file mode 100644 index 0000000..4dc8734 --- /dev/null +++ b/tests/admission-runtime-worker.ts @@ -0,0 +1,17 @@ +// Local workerd harness only; this module is never a production entry point. +import worker from '../src/worker/index.js'; +import { Store } from '../src/worker/store.js'; +import { AdmissionError } from '../src/worker/admission.js'; +import { snapshot } from './fixtures.js'; +export default { + ...worker, + async fetch(request: Request,env: Env): Promise { + if(new URL(request.url).pathname!=='/__admission-contract')return worker.fetch(request,env); + const candidate=snapshot();candidate.config.quotas.cooldown_seconds=0; + const attempts=await Promise.allSettled([new Store(env.DB).create(candidate,'',true),new Store(env.DB).create({...candidate,id:crypto.randomUUID()},'',true)]); + const accepted=attempts.filter(item=>item.status==='fulfilled').length; + const rejected=attempts.find(item=>item.status==='rejected'); + const {results}=await env.DB.prepare('SELECT outcome,phase FROM review_rejections').all(); + return Response.json({accepted,rejected:rejected?.status==='rejected'&&rejected.reason instanceof AdmissionError,outcomes:results}); + } +} satisfies ExportedHandler; diff --git a/tests/admission.test.ts b/tests/admission.test.ts new file mode 100644 index 0000000..18f15e5 --- /dev/null +++ b/tests/admission.test.ts @@ -0,0 +1,131 @@ +import { test, mock } from 'node:test'; +import assert from 'node:assert/strict'; +import { generateKeyPair, exportPKCS8 } from 'jose'; +import { database } from './sqlite.js'; +import { snapshot } from './fixtures.js'; +import { githubFixture } from './github-fixture.js'; +import { Store } from '../src/worker/store.js'; +import { AdmissionError, getRejection, recordRejection, rejectionId, QUOTA_WINDOW_MS } from '../src/worker/admission.js'; +import { publishRejection, rejectionBody } from '../src/worker/rejections.js'; +import { processDelivery } from '../src/worker/recovery.js'; +import type { BackendConfig } from '../src/worker/webhook.js'; + +await test('atomic admissions classify duplicate, every quota, cooldown and concurrency without weakening limits',async()=>{ + for(const [field,code] of [['user_per_day','user_quota_exceeded'],['repository_per_day','repository_quota_exceeded'],['installation_per_day','installation_quota_exceeded'],['concurrent_repository','repository_concurrency_exceeded'],['concurrent_installation','installation_concurrency_exceeded'],['cooldown_seconds','cooldown_active'],['reruns_per_day','rerun_limit_exceeded'],['duplicate','duplicate_review']] as const){ + const {db,sqlite}=database(),store=new Store(db),first=snapshot(); + Object.assign(first.config.quotas,{cooldown_seconds:0,concurrent_repository:10,concurrent_installation:10}); + if(field!=='duplicate') first.config.quotas[field]=field==='cooldown_seconds'?60:1; + if(field==='reruns_per_day')first.command.rerun=true; + try{ + await store.create(first); + if(!field.startsWith('concurrent'))await store.transition(first.id,'created','failed'); + const second={...first,id:crypto.randomUUID(),command:{...first.command,instructions:field==='duplicate'?'':'Different context'}}; + await assert.rejects(store.create(second,'',true),error=>{assert.ok(error instanceof AdmissionError);assert.equal(error.code,code);return true;}); + assert.equal(sqlite.prepare('SELECT count(*) AS n FROM requests').get()!.n,1); + const row=await getRejection(db,second.id);assert.ok(row);assert.equal(row.phase,'pending'); + assert.match(rejectionBody(row),/No AI inference was started/); + if(field.startsWith('concurrent')||field==='duplicate')assert.equal(row.retry_at,null); + const original=JSON.stringify(row); + await store.transition(first.id,'created','failed'); + await assert.rejects(store.create(second,'',true),AdmissionError);assert.equal(JSON.stringify(await getRejection(db,second.id)),original); + }finally{sqlite.close();} + } +}); +await test('rolling-window retry uses the limiting newest timestamp, exact boundary and persisted scope',async()=>{ + const {db,sqlite}=database(),store=new Store(db),now=1_900_000_000_000,clock=mock.method(Date,'now',()=>now),first=snapshot(); + Object.assign(first.config.quotas,{cooldown_seconds:0,concurrent_repository:10,reruns_per_day:3});first.command.rerun=true; + try{ + const offsets=[-QUOTA_WINDOW_MS+1000,-QUOTA_WINDOW_MS+2000,-QUOTA_WINDOW_MS+3000]; + for(const offset of offsets){const request={...first,id:crypto.randomUUID(),createdAt:now+offset};await store.create(request);await store.transition(request.id,'created','failed');} + first.config.quotas.reruns_per_day=2;const rejected={...first,id:crypto.randomUUID()}; + await assert.rejects(store.create(rejected,'',true),AdmissionError); + const row=await getRejection(db,rejected.id);assert.equal(row!.retry_at,now+2000);assert.equal(row!.window_ms,QUOTA_WINDOW_MS);assert.equal(row!.quota,2); + clock.mock.mockImplementation(()=>now+2000); + const next={...first,id:crypto.randomUUID(),createdAt:now+2000};await store.create(next); + await store.create({...first,id:crypto.randomUUID(),installationId:9}); + assert.ok(!rejectionBody(row!).includes('1900000000000')); + }finally{clock.mock.restore();sqlite.close();} +}); +await test('two Store instances atomically admit exactly one and retain the rejected outcome; rollback never fabricates a denial',async()=>{ + const {db,sqlite}=database(),request=snapshot();request.config.quotas.cooldown_seconds=0; + try{ + const calls=await Promise.allSettled([new Store(db).create(request,'',true),new Store(db).create({...request,id:crypto.randomUUID()},'',true)]); + assert.equal(calls.filter(result=>result.status==='fulfilled').length,1); + const rejected=calls.find(result=>result.status==='rejected');assert.ok(rejected&&rejected.status==='rejected');assert.ok(rejected.reason instanceof AdmissionError); + assert.equal(sqlite.prepare('SELECT count(*) AS n FROM requests').get()!.n,1); + assert.equal(sqlite.prepare('SELECT count(*) AS n FROM review_rejections').get()!.n,1); + const batch=mock.method(db,'batch',async()=>{throw new Error('synthetic D1 failure');}); + await assert.rejects(new Store(db).create({...request,id:crypto.randomUUID(),installationId:8}),/synthetic D1 failure/);batch.mock.restore(); + assert.equal(sqlite.prepare('SELECT count(*) AS n FROM review_rejections').get()!.n,1); + }finally{sqlite.close();} +}); +const pem=await exportPKCS8((await generateKeyPair('RS256',{extractable:true})).privateKey); +await test('feedback leases, definite failures, lost responses, ownership checks and terminal delivery recovery never duplicate posts',async()=>{ + for(const failure of ['none','network','429','503','save','spoof','revoked','expired-lease'] as const){ + const {db,sqlite}=database(),f=githubFixture(),identity={...snapshot(),commandId:777},row=await recordRejection(db,identity,'invalid_command'); + const env:BackendConfig={DB:db,PUBLIC_URL:'https://broker.invalid',GITHUB_APP_ID:'42',GITHUB_PRIVATE_KEY:pem,GITHUB_APP_SLUG:'codexbot',TRUSTED_WORKFLOW:'unused',CHATGPT_DEPLOYMENT_AUTHORIZED:'false'}; + let attempted=0;const base=f.fetcher; + const fetcher=mock.method(globalThis,'fetch',async(input:Parameters[0],init?:Parameters[1])=>{ + if(init?.method==='POST'&&String(input).endsWith('/issues/4/comments')){ + attempted++; + if(attempted===1&&failure==='429')return Response.json({message:'sensitive response'},{status:429}); + const response=await base(input,init); + if(attempted===1&&(failure==='network'||failure==='503')){ + if(failure==='network')throw new Error('sensitive transport text');return Response.json({message:'sensitive response'},{status:503}); + } + if(failure==='spoof')f.state.comments[0]!.performed_via_github_app={id:999}; + if(failure==='expired-lease')sqlite.prepare('UPDATE review_rejections SET lease_until=0').run(); + return failure==='spoof'?Response.json(f.state.comments[0]):response; + } + return base(input,init); + }); + const logs:unknown[]=[];const logger=mock.method(console,'error',(...args:unknown[])=>logs.push(args)); + const prepare=db.prepare.bind(db);let saveFailed=false; + const storage=mock.method(db,'prepare',(sql:string)=>{ + if(failure==='save'&&!saveFailed&&sql.startsWith("UPDATE review_rejections SET phase='done'")){saveFailed=true;throw new Error('synthetic lost D1 response');} + return prepare(sql); + }); + try{ + if(failure==='revoked')f.config.authorization.whitelist=''; + await Promise.all([publishRejection(env,row.id),publishRejection(env,row.id)]); + assert.ok(f.state.comments.length<=1); + if(failure==='spoof'||failure==='revoked')assert.equal((await getRejection(db,row.id))!.phase,'failed'); + else{ + sqlite.prepare('UPDATE review_rejections SET next_attempt=0,lease_until=0').run(); + await publishRejection(env,row.id); + assert.equal((await getRejection(db,row.id))!.phase,'done');assert.equal(f.state.comments.length,1); + await publishRejection(env,row.id);assert.equal(f.state.comments.length,1); + } + assert.ok(!JSON.stringify(logs).includes('sensitive')); + // Already-recorded denial recovery never re-enters admission, even with an expired original payload. + await new Store(db).delivery('fixture-delivery','digest','issue_comment',JSON.stringify({comment:{created_at:'2000-01-01'}})); + sqlite.prepare("UPDATE deliveries SET request_id=? WHERE id='fixture-delivery'").run(identity.id); + await processDelivery('fixture-delivery',env as Env); + assert.equal(sqlite.prepare('SELECT phase FROM deliveries').get()!.phase,'done');assert.equal(f.state.reviews.length,0); + assert.equal(f.calls.filter(call=>call.path.endsWith('/dispatches')).length,0); + const replay=await recordRejection(db,{...identity,id:crypto.randomUUID()},'invalid_command');assert.equal(replay.id,row.id);assert.equal(await rejectionId({...identity,installationId:9})===row.id,false); + }finally{storage.mock.restore();logger.mock.restore();fetcher.mock.restore();sqlite.close();} + } +}); +await test('ambiguous POST with no App-owned match exhausts bounded reconciliation without another POST',async()=>{ + const {db,sqlite}=database(),f=githubFixture(),row=await recordRejection(db,snapshot(),'invalid_command'); + const env:BackendConfig={DB:db,PUBLIC_URL:'https://broker.invalid',GITHUB_APP_ID:'42',GITHUB_PRIVATE_KEY:pem,GITHUB_APP_SLUG:'codexbot',TRUSTED_WORKFLOW:'unused',CHATGPT_DEPLOYMENT_AUTHORIZED:'false'}; + const fetcher=mock.method(globalThis,'fetch',f.fetcher),logger=mock.method(console,'error',()=>{}); + try{ + sqlite.prepare("UPDATE review_rejections SET phase='creating'").run(); + f.state.comments.push({id:100,user:{type:'User',login:'alice'},performed_via_github_app:{id:42},issue_url:'https://api.github.com/repos/owner/repo/issues/4',body:rejectionBody(row)}); + for(let n=0;n<7;n++){sqlite.prepare('UPDATE review_rejections SET next_attempt=0').run();await publishRejection(env,row.id);} + assert.equal((await getRejection(db,row.id))!.phase,'failed');assert.equal((await getRejection(db,row.id))!.attempts,5); + assert.equal(f.calls.filter(call=>call.method==='POST'&&call.path.endsWith('/comments')).length,0); + }finally{logger.mock.restore();fetcher.mock.restore();sqlite.close();} +}); + +await test('concurrent redeliveries of an accepted command reuse its admission, never publish a misleading denial or spend a second rerun',async()=>{ + const {db,sqlite}=database(),first={...snapshot(),commandId:123};first.command.rerun=true;first.config.quotas.cooldown_seconds=0; + try{ + const outcomes=await Promise.all([new Store(db).create(first,'',true),new Store(db).create({...first,id:crypto.randomUUID()},'',true)]); + assert.equal(outcomes[0].id,outcomes[1].id); + assert.equal(sqlite.prepare('SELECT count(*) AS n FROM requests').get()!.n,1); + assert.equal(sqlite.prepare('SELECT count(*) AS n FROM review_rejections').get()!.n,0); + }finally{sqlite.close();} +}); diff --git a/tests/security-regressions.test.ts b/tests/security-regressions.test.ts index cf94111..8373260 100644 --- a/tests/security-regressions.test.ts +++ b/tests/security-regressions.test.ts @@ -28,7 +28,7 @@ await test('per-review model is persisted and included in same-head idempotency await store.create(other); assert.equal((await store.get(other.id)).snapshot.command.model, 'codex-other'); await store.transition(first.id, 'created', 'failed'); - await assert.rejects(store.create({ ...first, id: crypto.randomUUID() }), /duplicate_review_or_quota_exceeded/); + await assert.rejects(store.create({ ...first, id: crypto.randomUUID() }), /duplicate_review/); assert.equal(sqlite.prepare('SELECT count(*) AS n FROM requests').get()!.n, 2); } finally { sqlite.close(); } }); @@ -88,11 +88,11 @@ await test('atomic quotas, same-head idempotency, bounded rerun and tenant isola const attempts = await Promise.allSettled([store.create(request), store.create({ ...request, id: crypto.randomUUID() })]); assert.equal(attempts.filter(x => x.status === 'fulfilled').length, 1); const admitted = attempts.find(x => x.status === 'fulfilled'); assert.ok(admitted); assert.equal(await store.transition(admitted.value.id, 'created', 'failed'), true); - await assert.rejects(store.create({ ...request, id: crypto.randomUUID() }), /duplicate_review_or_quota_exceeded/); + await assert.rejects(store.create({ ...request, id: crypto.randomUUID() }), /duplicate_review/); const rerun = { ...request, id: crypto.randomUUID(), command: { ...request.command, rerun: true } }; await store.create(rerun); await store.transition(rerun.id, 'created', 'failed'); const twice = { ...rerun, id: crypto.randomUUID() }; await store.create(twice); await store.transition(twice.id, 'created', 'failed'); - await assert.rejects(store.create({ ...rerun, id: crypto.randomUUID() }), /quota/); + await assert.rejects(store.create({ ...rerun, id: crypto.randomUUID() }), /rerun_limit_exceeded/); await store.create({ ...request, id: crypto.randomUUID(), installationId: 2 }); const other = { ...request, id: crypto.randomUUID(), command: { ...request.command, instructions: 'Different task context' } }; await store.create(other); diff --git a/tests/sqlite.ts b/tests/sqlite.ts index 8a933c8..b0c4fc0 100644 --- a/tests/sqlite.ts +++ b/tests/sqlite.ts @@ -21,6 +21,12 @@ export function database(): { db: D1Database; sqlite: DatabaseSync } { raw } as D1PreparedStatement; } - const db = { prepare: prepared, async batch(statements: D1PreparedStatement[]) { sqlite.exec('BEGIN'); try { const results = await Promise.all(statements.map(x => x.run())); sqlite.exec('COMMIT'); return results; } catch (error) { sqlite.exec('ROLLBACK'); throw error; } } } as D1Database; + let transaction: Promise = Promise.resolve(); + const db = { prepare: prepared, batch(statements: D1PreparedStatement[]) { + const operation = transaction.then(async () => { sqlite.exec('BEGIN'); try { + const results = []; for (const statement of statements) results.push(await statement.run()); sqlite.exec('COMMIT'); return results; + } catch (error) { sqlite.exec('ROLLBACK'); throw error; } }); + transaction = operation.catch(() => {}); return operation; + } } as D1Database; return { db, sqlite }; } diff --git a/tests/worker.test.ts b/tests/worker.test.ts index cd8d5ea..514c7b4 100644 --- a/tests/worker.test.ts +++ b/tests/worker.test.ts @@ -216,7 +216,9 @@ await test('invalid signature, replay, spoofed user, forged installation and una assert.equal((await worker.fetch(signed(valid, env.GITHUB_WEBHOOK_SECRET), env)).status, 200); const spoofed = payload(); spoofed.comment.user.id = 6; assert.equal((await worker.fetch(signed(spoofed, env.GITHUB_WEBHOOK_SECRET), env)).status, 403); const forged = payload(); forged.installation.id = 99; assert.equal((await worker.fetch(signed(forged, env.GITHUB_WEBHOOK_SECRET), env)).status, 403); - assert.equal((await worker.fetch(signed(payload('@codexbot review --merge'), env.GITHUB_WEBHOOK_SECRET), env)).status, 403); + assert.equal((await worker.fetch(signed(payload('@codexbot review --merge'), env.GITHUB_WEBHOOK_SECRET), env)).status, 200); + assert.equal((await env.DB.prepare("SELECT phase FROM review_rejections WHERE outcome='merge_not_authorized'").first<{phase:string}>())!.phase,'done'); + assert.ok(String(f.state.comments.at(-1)!.body).includes('not permitted')); f.config.authorization.whitelist = ''; assert.equal((await worker.fetch(signed(payload('@codexbot review --effort low'), env.GITHUB_WEBHOOK_SECRET), env)).status, 403); assert.equal(f.calls.filter(x => x.path.endsWith('/dispatches')).length, 1); })); @@ -226,7 +228,9 @@ await test('configured handle works at webhook boundary and hosted ChatGPT canno assert.equal(f.calls.filter(x => x.path.endsWith('/dispatches')).length, 0); assert.equal((await worker.fetch(signed(payload('@CODEXBOTREVIEWTEST review --effort high'), env.GITHUB_WEBHOOK_SECRET), env)).status, 202); f.config.credentials.mode = 'chatgpt'; - assert.equal((await worker.fetch(signed(payload('@CodexBotReviewTest review'), env.GITHUB_WEBHOOK_SECRET), env)).status, 403); + assert.equal((await worker.fetch(signed(payload('@CodexBotReviewTest review'), env.GITHUB_WEBHOOK_SECRET), env)).status, 200); + assert.equal((await env.DB.prepare("SELECT phase FROM review_rejections WHERE outcome='chatgpt_deployment_not_authorized'").first<{phase:string}>())!.phase,'done'); + assert.equal(f.calls.filter(x=>x.path.endsWith('/dispatches')).length,1); })); await test('ChatGPT handoff is encrypted, lease-protected and disconnected sessions cannot publish', async () => fixture(async (env, f) => { env.CHATGPT_DEPLOYMENT_AUTHORIZED = 'true'; f.config.credentials.mode = 'chatgpt'; @@ -319,3 +323,34 @@ await test('revoked ChatGPT hosting authorization denies an already-dispatched p const denied = await post('codexbot-1-5'); assert.equal(denied.status, 403); assert.equal((await denied.json() as { error: string }).error, 'chatgpt_deployment_authorization_revoked'); assert.equal((await new Store(env.DB).get(id!)).row.state, 'dispatched'); })); + +await test('exhausted reruns produce one terminal explanation and no workflow under redelivery and simultaneous processing', async () => fixture(async (env,f) => { + const store=new Store(env.DB); f.config.quotas.cooldown_seconds=0; + for(let n=0;n<2;n++) { + const request={...snapshot(),id:crypto.randomUUID(),config:f.config,command:{...snapshot().command,rerun:true}}; + await store.create(request);await store.transition(request.id,'created','failed'); + } + const input={...payload('@codexbot review --effort high --rerun'),comment:{...payload().comment,id:777,body:'@codexbot review --effort high --rerun'}}; + const delivery=crypto.randomUUID(); + const responses=await Promise.all([worker.fetch(signed(input,env.GITHUB_WEBHOOK_SECRET,delivery),env),worker.fetch(signed(input,env.GITHUB_WEBHOOK_SECRET,delivery),env)]); + assert.ok(responses.every(response=>response.status===200)); + assert.equal((await worker.fetch(signed(input,env.GITHUB_WEBHOOK_SECRET),env)).status,200); + assert.equal(f.calls.filter(x=>x.path.endsWith('/dispatches')).length,0); + assert.equal(f.state.comments.length,1); + assert.match(String(f.state.comments[0]!.body),/rerun allowance.*2 requests.*rolling 24-hour.*UTC.*No AI inference/s); + const row=await env.DB.prepare('SELECT outcome,phase,comment_id FROM review_rejections').first<{outcome:string;phase:string;comment_id:number}>(); + assert.equal(row!.outcome,'rerun_limit_exceeded');assert.equal(row!.phase,'done');assert.equal(row!.comment_id,1000); + const {results}=await env.DB.prepare('SELECT phase FROM deliveries').all<{phase:string}>();assert.ok(results.every(row=>row.phase==='done')); +})); +await test('fresh invalid commands get fixed feedback; unauthorized, other-bot, quoted and expired comments disclose nothing',async()=>fixture(async(env,f)=>{ + for(const body of ['discussion','@otherbot review --bad','> @codexbot review','```\n@codexbot review --bad\n```',' @codexbot review --bad']) { + assert.equal((await worker.fetch(signed(payload(body),env.GITHUB_WEBHOOK_SECRET),env)).status,200); + } + const old=payload('@codexbot review --bad');old.comment.created_at=new Date(Date.now()-700_000).toISOString(); + assert.equal((await worker.fetch(signed(old,env.GITHUB_WEBHOOK_SECRET),env)).status,403); + f.config.authorization.whitelist='';assert.equal((await worker.fetch(signed(payload('@codexbot review --bad'),env.GITHUB_WEBHOOK_SECRET),env)).status,403); + assert.equal(f.state.comments.length,0); + f.config.authorization.whitelist='alice'; + assert.equal((await worker.fetch(signed(payload('@codexbot review --bad sensitive-fixture'),env.GITHUB_WEBHOOK_SECRET),env)).status,200); + assert.equal(f.state.comments.length,1);assert.ok(!String(f.state.comments[0]!.body).includes('sensitive-fixture'));assert.equal(f.state.reviews.length,0); +})); diff --git a/tests/workers-runtime.mjs b/tests/workers-runtime.mjs index 0e6c0b4..7f389db 100644 --- a/tests/workers-runtime.mjs +++ b/tests/workers-runtime.mjs @@ -19,7 +19,7 @@ const env = { ...process.env, WRANGLER_SEND_METRICS: 'false' }; const cli = args => execFileSync(process.execPath, [wrangler, ...args, '--config', config], { encoding: 'utf8', env, stdio: 'pipe', timeout: 60_000 }); let child; try { - await writeFile(config, JSON.stringify({ name: 'codexbot-runtime-test', main: resolve('src/worker/index.ts'), compatibility_date: '2026-11-13', compatibility_flags: ['nodejs_compat'], + await writeFile(config, JSON.stringify({ name: 'codexbot-runtime-test', main: resolve('tests/admission-runtime-worker.ts'), compatibility_date: '2026-11-13', compatibility_flags: ['nodejs_compat'], vars: { PUBLIC_URL: `http://127.0.0.1:${port}`, GITHUB_WEBHOOK_SECRET: secret, GITHUB_PRIVATE_KEY: 'not-used', GITHUB_APP_ID: '42', GITHUB_APP_SLUG: 'codexbot', TRUSTED_WORKFLOW: '', CHATGPT_DEPLOYMENT_AUTHORIZED: 'false', MERGE_DEPLOYMENT_AUTHORIZED: 'false' }, d1_databases: [{ binding: 'DB', database_name: 'codexbot-runtime-test', database_id: randomUUID(), migrations_dir: resolve('migrations') }] })); await writeFile(join(directory, 'empty.env'), ''); @@ -51,7 +51,9 @@ try { assert.deepEqual(terminal, [{ state: 'failed', failure_code: 'request_expired' }]); const leases = JSON.parse(cli(['d1', 'execute', 'DB', '--local', '--persist-to', state, '--json', '--command', 'SELECT installation_id,lease_request FROM sessions ORDER BY installation_id']))[0].results; assert.deepEqual(leases, [{ installation_id: 1, lease_request: null }, { installation_id: 9, lease_request: 'other-installation' }]); - process.stdout.write('Real workerd + D1: migrations, HMAC, receipts, cron, expired requests and cross-installation lease cleanup passed.\n'); + const admission=await (await globalThis.fetch(`${origin}/__admission-contract`)).json(); + assert.deepEqual(admission,{accepted:1,rejected:true,outcomes:[{outcome:'duplicate_review',phase:'pending'}]}); + process.stdout.write('Real workerd + D1: atomic admission/outbox, migrations, HMAC, receipts, cron, expired requests and cross-installation lease cleanup passed.\n'); } finally { if (child?.pid) { if (process.platform === 'win32') execFileSync('taskkill', ['/PID', String(child.pid), '/T', '/F'], { stdio: 'ignore' });