diff --git a/.github/workflows/review.yml b/.github/workflows/review.yml index 16fe330..e2be1ee 100644 --- a/.github/workflows/review.yml +++ b/.github/workflows/review.yml @@ -42,7 +42,7 @@ jobs: review: needs: source runs-on: ubuntu-24.04 - timeout-minutes: 35 + timeout-minutes: 50 # Provider refresh eligibility can require up to 25 minutes before bounded inference. environment: codexbot-${{ inputs.installation_id }}-${{ inputs.credential_owner }} permissions: contents: read diff --git a/CHANGELOG.md b/CHANGELOG.md index d5c4014..26ad57f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Security and behavior changes +- Respect the provider's earliest refresh timestamp before runtime token renewal. Bound the credential wait and workflow lifetime, preserve exclusive leases/CAS and persist rotation before inference; classify premature/unavailable refresh windows without raw credential diagnostics. + - Accept GitHub's documented immutable default OIDC subjects for new/renamed repositories, exactly binding owner ID, repository ID and Environment while retaining the legacy subject and all signed identity/workflow checks. - Add primary Fully Autonomous merging (0 humans), optional Human-in-the-Loop (1–6), owner-opted-in merge-on-approval and a repository/installation-scoped live acceptance gate. Preserve GitHub protections, native approval and exact-head checks. diff --git a/docs/authentication.md b/docs/authentication.md index 2516614..c19e1cd 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -16,7 +16,7 @@ The selected session is AES-256-GCM encrypted locally with a per-profile 256-bit Actions authenticates by OIDC and can retrieve only the requesting user's ciphertext. D1 leases serialize the session for an hour, in addition to repository workflow concurrency. A rotating refresh token and its expiry/scopes are encrypted and durably version-checked **before inference starts**. A failed/uncertain exchange or persistence conflict requires reconnection; there is no fallback to paid API billing. A cancelled workflow can leave a lease until its one-hour expiry; do not break it while a runner may still be active. Loss of the rotated token after the provider has consumed the old one cannot be made atomic across providers: reconnect using the saved registration. -The app-server uses the official Responses provider contract. A separate credential proxy performs bearer injection so app-server's model/tool process never receives access/refresh tokens. Streaming and `store:false` are enforced. The proxy restarts for every review; tokens are renewed beforehand for the maximum review duration. [Preview constraints](https://developers.openai.com/siwc/token-sharing-open-source/preview-limitations) apply. +The app-server uses the official Responses provider contract. A separate credential proxy performs bearer injection so app-server's model/tool process never receives access/refresh tokens. Streaming and `store:false` are enforced. The proxy restarts for every review; tokens are renewed beforehand for the maximum review duration. If OpenAI has not reached its signed `earliest_refresh_at`, the trusted runner waits for that eligibility timestamp under the existing exclusive session lease (at most 25 minutes), then rotates and durably persists before starting inference. No premature token call or CLI reconnect is required. The review job has a 50-minute cap to include this provider-mandated wait and bounded inference; Actions compute may be idle during this window. [Preview constraints](https://developers.openai.com/siwc/token-sharing-open-source/preview-limitations) apply. ### Hosting eligibility diff --git a/src/core/diagnostics.ts b/src/core/diagnostics.ts index 3694297..7084d31 100644 --- a/src/core/diagnostics.ts +++ b/src/core/diagnostics.ts @@ -5,6 +5,7 @@ export const REVIEW_STAGES = ['environment','prepare','context','credentials','a export const FAILURE_CODES = new Set(['missing_environment_credentials','ambiguous_environment_credentials','credential_owner_mismatch','missing_read_token', 'api_key_missing_or_ambiguous_credentials','vault_key_missing_or_ambiguous_credentials','invalid_envelope','invalid_vault_key','invalid_saved_session', 'review_incomplete','review_context_incomplete','review_output_invalid','read_token_cleanup_failed', + 'oauth_refresh_too_early','oauth_refresh_window_unavailable', 'session_too_short_reconnect_required','review_image_missing','isolated_review_failed','proxy_start_failed','docker_unavailable','docker_cleanup_failed_check_runner', 'oauth_grant_invalid_reconnect_required','chatgpt_plan_usage_permission_missing','oauth_token_endpoint_failed','oauth_token_exchange_uncertain_reconnect_required', 'invalid_saved_registration','invalid_saved_expiry','oauth_invalid_credential','oauth_invalid_expiry','oauth_invalid_refresh_time', diff --git a/src/runner/main.ts b/src/runner/main.ts index 6677789..f4221a7 100644 --- a/src/runner/main.ts +++ b/src/runner/main.ts @@ -15,6 +15,13 @@ import type { ContextLookup } from './context.js'; import { isolatedReview } from './docker.js'; import { failedReview, normalizeReview, validateReview, type ChangedFile } from '../core/review.js'; +const REFRESH_WINDOW_MS = 25 * 60_000; +export async function waitForRefresh(earliest: number): Promise { + requireCondition(Number.isFinite(earliest) && earliest >= 0 && earliest - Date.now() <= REFRESH_WINDOW_MS, 'oauth_refresh_window_unavailable'); + // Provider eligibility is a timestamp, not permission to send a premature refresh request. + while (Date.now() < earliest) await new Promise(resolve => setTimeout(resolve, earliest - Date.now())); +} + export async function reviewJob(): Promise { const id = process.env.CODEXBOT_REQUEST_ID ?? ''; let backend = '', temporary: string | undefined; @@ -50,7 +57,8 @@ export async function reviewJob(): Promise { const saved = record(prepared.session); const binding = sessionBinding(backend, snapshot.installationId, snapshot.repositoryId, snapshot.actorId); let session = validateSession(await unseal(saved.envelope as Envelope, process.env.CODEXBOT_VAULT_KEY, binding)); - if (session.expires_at - Date.now() < 25 * 60_000) { + if (session.expires_at - Date.now() < REFRESH_WINDOW_MS) { + await waitForRefresh(session.earliest_refresh_at); if (session.hosted) { const renewed = record(await broker(backend, id, 'renew')); session = validateSession(await unseal(renewed.envelope as Envelope, process.env.CODEXBOT_VAULT_KEY, binding)); diff --git a/tests/execution.test.ts b/tests/execution.test.ts index 7661d57..fb7e3ce 100644 --- a/tests/execution.test.ts +++ b/tests/execution.test.ts @@ -10,7 +10,7 @@ import { ExecutionDiagnostics, ExecutionError, executionEnvelope, executionFailu import { docker } from '../src/runner/docker.js'; import { runCodex } from '../src/runner/engine.js'; import { Rpc } from '../src/runner/rpc.js'; -import { reviewJob } from '../src/runner/main.js'; +import { reviewJob, waitForRefresh } from '../src/runner/main.js'; import { snapshot, clean } from './fixtures.js'; const sensitive = 'Bearer fixture-private-credential OAuth-code private-source prompt-provider-message'; @@ -204,3 +204,14 @@ await test('actual review job records incomplete fallback, releases acquired lea for (const key of Object.keys(process.env)) if (!(key in saved)) delete process.env[key]; Object.assign(process.env, saved); } }); + +await test('runner waits for the provider refresh window without consuming a token early and bounds future waits',async t=>{ + const now=1700000000000;t.mock.timers.enable({apis:['Date','setTimeout'],now}); + try{ + let allowed=false;const waiting=waitForRefresh(now+5*60000).then(()=>{allowed=true;}); + t.mock.timers.tick(5*60000-1);await Promise.resolve();assert.equal(allowed,false); + t.mock.timers.tick(1);await waiting;assert.equal(allowed,true);assert.equal(Date.now(),now+5*60000); + await waitForRefresh(now-1); + for(const earliest of [NaN,Infinity,-1,Date.now()+25*60000+1])await assert.rejects(waitForRefresh(earliest),{code:'oauth_refresh_window_unavailable'}); + }finally{t.mock.timers.reset();} +}); diff --git a/tests/workflows.test.ts b/tests/workflows.test.ts index 46856ac..cd0a270 100644 --- a/tests/workflows.test.ts +++ b/tests/workflows.test.ts @@ -10,6 +10,7 @@ await test('reusable workflow has no GitHub write permissions, shared secrets or const text = await readFile('.github/workflows/review.yml', 'utf8'), workflow = record(parse(text)); assert.deepEqual(workflow.permissions, {}); const jobs = record(workflow.jobs); + assert.equal(record(jobs.review)['timeout-minutes'],50); for (const [name, value] of Object.entries(jobs)) { const job = record(value), permissions = record(job.permissions); assert.equal(job['runs-on'], 'ubuntu-24.04');