Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
16 changes: 16 additions & 0 deletions docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 13 additions & 0 deletions migrations/0005_review_admission_feedback.sql
Original file line number Diff line number Diff line change
@@ -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);
63 changes: 63 additions & 0 deletions src/worker/admission.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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<D1PreparedStatement> {
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<RejectionRow | null> {
return db.prepare('SELECT * FROM review_rejections WHERE id=? OR request_id=?').bind(id,id).first<RejectionRow>();
}
export async function recordRejection(db: D1Database, identity: RejectionIdentity, outcome: RejectionCode): Promise<RejectionRow> {
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;
}
3 changes: 2 additions & 1 deletion src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<Env>;
6 changes: 5 additions & 1 deletion src/worker/recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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;
Expand Down
Loading