Skip to content

[WRONG BRANCH] fix: validate manual Codex import identity - #228

Draft
luvs01 wants to merge 1 commit into
mainfrom
codex/fix-manual-codex-import-email-collision-check
Draft

[WRONG BRANCH] fix: validate manual Codex import identity#228
luvs01 wants to merge 1 commit into
mainfrom
codex/fix-manual-codex-import-email-collision-check

Conversation

@luvs01

@luvs01 luvs01 commented Aug 9, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Prevent request-controlled email from bypassing account-id collision checks during the manual /api/codex-auth/accounts import path.
  • Ensure imported account identity is anchored to provider-verified token claims so a caller cannot import the same real ChatGPT account repeatedly by changing the submitted email.

Description

  • Add checkManualImportCollision which only treats a matching chatgptAccountId as a collision for manual imports, avoiding untrusted request metadata.
  • Require the import accessToken to contain an email claim and derive the canonical email via extractEmail(...), rejecting imports when the token lacks an email.
  • Persist the token-derived email into the runtime codexAccounts entry instead of the JSON-supplied body.email to ensure future collision checks use provider-verified identity evidence.
  • Update tests and fixtures to use JWT-shaped tokens with email and ChatGPT account-id claims and add a regression test that proves changing the submitted email/plan cannot re-import the same token/account.

Testing

  • Ran the focused suites with bun test tests/codex-auth-api.test.ts tests/codex-auth-collision.test.ts (using the repository Bun binary), and the modified tests passed; overall 182 tests in those files passed.
  • Ran bun run typecheck and bun run privacy:scan, both of which succeeded.
  • Ran the full bun run test in this environment; several unrelated integration tests timed out in this run and were not caused by these changes, but the focused auth tests above (and the collision coverage) exercised and validated the fix.

Codex Task

Summary by CodeRabbit

  • Bug Fixes
    • Improved manual Codex account imports by securely deriving account email and identity from the access token.
    • Tokens without an email claim are now rejected.
    • Duplicate accounts are detected more reliably, even when submitted account details differ from token information.
    • Rejected duplicate imports no longer alter existing configuration or save credentials.

@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Aug 9, 2026
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

⏳ DRAFT

  • wrong target branch (main); retarget to dev.

What to do

  • Retarget this PR to dev — all contributions go to dev.

Its title has been prefixed with [WRONG BRANCH].
This pull request was already a draft. Its draft status will be preserved after every issue above is resolved.

@github-actions github-actions Bot changed the title fix: validate manual Codex import identity [WRONG BRANCH] fix: validate manual Codex import identity Aug 9, 2026
@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Manual Codex imports now derive email and account identity from access-token claims. Imports reject tokens without email claims, detect duplicate ChatGPT account IDs, and persist the derived email. Tests cover token-based duplicates and unchanged configuration.

Changes

Manual import identity validation

Layer / File(s) Summary
Account ID collision checker
src/codex/auth-collision.ts
Adds checkManualImportCollision, which detects conflicts by stored ChatGPT account ID in selectable pool accounts.
Token-derived import flow
src/codex/auth-api.ts
Extracts and validates the access-token email, uses token identity for collision checks, and persists the derived email.
Import identity test coverage
tests/codex-auth-api.test.ts
Generates JWT-like test tokens and verifies token-derived persistence, main-account matching, and duplicate rejection without configuration or credential writes.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ManualImportAPI
  participant AccessToken
  participant extractEmail
  participant checkManualImportCollision
  participant PoolAccountStorage
  ManualImportAPI->>AccessToken: read access-token claims
  ManualImportAPI->>extractEmail: extract email claim
  extractEmail-->>ManualImportAPI: token-derived email
  ManualImportAPI->>checkManualImportCollision: ChatGPT account ID
  checkManualImportCollision-->>ManualImportAPI: collision result
  ManualImportAPI->>PoolAccountStorage: persist token-derived email
Loading

Suggested reviewers: ingwannu, chrisae9, lidge-jun

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: validating identity during manual Codex account imports.
✨ Finishing Touches 💡 2
⚔️ Resolve merge conflicts 💡
  • Resolve merge conflict in branch codex/fix-manual-codex-import-email-collision-check
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-manual-codex-import-email-collision-check

Comment @coderabbitai help to get the list of available commands.

@github-actions
github-actions Bot marked this pull request as draft August 9, 2026 07:33

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/codex/auth-api.ts (1)

1241-1252: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Repeat the account-ID collision check immediately before persistence.

The check at Line 1241 runs before await verifyCodexAccountWarmup(...). Two requests with different pool IDs and the same token account ID can both pass this check while warmup is pending. commitConflict then checks only body.id, so both credentials and pool rows can be persisted.

After Line 1250, run checkManualImportCollision(derivedAccountId) again and reject a conflict before saveCodexAccountCredential. Add a concurrent-import regression test with a warmup barrier.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/codex/auth-api.ts` around lines 1241 - 1252, Repeat the derivedAccountId
collision check immediately after verifyCodexAccountWarmup and before
saveCodexAccountCredential, returning the same 400 error response when a
conflict exists. Add a regression test that uses a warmup barrier to simulate
concurrent imports and verifies only one request persists credentials and pool
rows.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/codex/auth-api.ts`:
- Around line 1235-1241: Require extractAccountId(undefined, body.accessToken)
to return a value in the manual-import flow before calling
checkManualImportCollision; remove the fallback to body.chatgptAccountId and
reject tokens without an account-ID claim. Add a regression test covering a
token with an email but no supported account-ID claim plus a supplied
chatgptAccountId, asserting that neither credential nor account rows are
created.

---

Outside diff comments:
In `@src/codex/auth-api.ts`:
- Around line 1241-1252: Repeat the derivedAccountId collision check immediately
after verifyCodexAccountWarmup and before saveCodexAccountCredential, returning
the same 400 error response when a conflict exists. Add a regression test that
uses a warmup barrier to simulate concurrent imports and verifies only one
request persists credentials and pool rows.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 58cbc81f-96f4-427d-9144-64bd11f4ee7d

📥 Commits

Reviewing files that changed from the base of the PR and between 121f1ad and 3efca17.

📒 Files selected for processing (3)
  • src/codex/auth-api.ts
  • src/codex/auth-collision.ts
  • tests/codex-auth-api.test.ts

Comment thread src/codex/auth-api.ts
Comment on lines +1235 to +1241
// Manual-import identity must come from the token, not request-controlled metadata.
const derivedAccountId = extractAccountId(undefined, body.accessToken) ?? body.chatgptAccountId;
const collision = checkAccountIdCollision(derivedAccountId, body.email, body.plan);
const derivedEmail = extractEmail(undefined, body.accessToken);
if (!derivedEmail) {
return jsonResponse({ error: "Access token does not contain an email claim" }, 400);
}
const collision = checkManualImportCollision(derivedAccountId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Require a ChatGPT account-ID claim from the access token.

Line 1236 falls back to request-controlled body.chatgptAccountId. A token with an email claim but no supported account-ID claim can therefore select a different collision identity and bypass checkManualImportCollision.

Reject the import when extractAccountId(undefined, body.accessToken) returns undefined. Add a regression test that submits such a token with a supplied chatgptAccountId and expects no credential or account row.

Proposed fix
-    const derivedAccountId = extractAccountId(undefined, body.accessToken) ?? body.chatgptAccountId;
+    const derivedAccountId = extractAccountId(undefined, body.accessToken);
+    if (!derivedAccountId) {
+      return jsonResponse({ error: "Access token does not contain a ChatGPT account ID claim" }, 400);
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Manual-import identity must come from the token, not request-controlled metadata.
const derivedAccountId = extractAccountId(undefined, body.accessToken) ?? body.chatgptAccountId;
const collision = checkAccountIdCollision(derivedAccountId, body.email, body.plan);
const derivedEmail = extractEmail(undefined, body.accessToken);
if (!derivedEmail) {
return jsonResponse({ error: "Access token does not contain an email claim" }, 400);
}
const collision = checkManualImportCollision(derivedAccountId);
// Manual-import identity must come from the token, not request-controlled metadata.
const derivedAccountId = extractAccountId(undefined, body.accessToken);
if (!derivedAccountId) {
return jsonResponse({ error: "Access token does not contain a ChatGPT account ID claim" }, 400);
}
const derivedEmail = extractEmail(undefined, body.accessToken);
if (!derivedEmail) {
return jsonResponse({ error: "Access token does not contain an email claim" }, 400);
}
const collision = checkManualImportCollision(derivedAccountId);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/codex/auth-api.ts` around lines 1235 - 1241, Require
extractAccountId(undefined, body.accessToken) to return a value in the
manual-import flow before calling checkManualImportCollision; remove the
fallback to body.chatgptAccountId and reject tokens without an account-ID claim.
Add a regression test covering a token with an email but no supported account-ID
claim plus a supplied chatgptAccountId, asserting that neither credential nor
account rows are created.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3efca17e3b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/codex/auth-api.ts
if (!derivedEmail) {
return jsonResponse({ error: "Access token does not contain an email claim" }, 400);
}
const collision = checkManualImportCollision(derivedAccountId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject tokens without a verified account ID

When a warmup-valid access token has an email but no account-id claim recognized by extractAccountId, derivedAccountId still falls back to request-controlled body.chatgptAccountId; this new collision check therefore compares and later persists an unverified identity, allowing the same token to bypass duplicate detection by submitting another account ID that the upstream warmup accepts. Reject tokens without a token-derived account ID, as is already done for email, or obtain the ID from a provider-verified response before checking the collision.

AGENTS.md reference: src/AGENTS.md:L20-L20

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aardvark bug Something isn't working codex

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant