Repository navigation
refactor(auth): centralize codex oauth ownership in OpenClaw - #68284
Conversation
🔒 Aisle Security AnalysisWe found 3 potential security issue(s) in this PR:
1. 🟠 OAuth credential confusion: identity-less profiles can adopt/mirror another account's tokens via main-store sharing
DescriptionThe new OAuth identity adoption/mirroring logic allows cross-account credential adoption when the existing credential has no identity metadata ( In
This is a broken authorization/identity-binding boundary if multiple tenants/users share the same OS account or otherwise share the same main auth store but are expected to be isolated by Vulnerable logic:
if (!hasOAuthIdentity(existing)) {
return true;
}Impact:
RecommendationRequire positive identity binding (accountId/email) before adopting from or mirroring into the main store. Safer options:
export function isSafeToAdoptMainStoreOAuthIdentity(existing: OAuthCredential | undefined, incoming: OAuthCredential): boolean {
if (!existing || existing.type !== "oauth") return false;
if (existing.provider !== incoming.provider) return false;
if (!hasOAuthIdentity(existing) || !hasOAuthIdentity(incoming)) return false; // require identity on both
return hasMatchingOAuthIdentity(existing, incoming);
}
Also apply the same stricter rule in 2. 🟡 Auth profile mismatch not enforced when resuming Codex thread binding (potential cross-account thread access)
DescriptionWhen resuming an existing Codex app-server thread, the code allows a caller-provided
If an attacker can influence Vulnerable code: const boundAuthProfileId = params.params.authProfileId ?? binding.authProfileId;
await writeCodexAppServerBinding(params.params.sessionFile, {
threadId: response.thread.id,
cwd: params.cwd,
authProfileId: boundAuthProfileId,
...
});RecommendationEnforce binding/profile consistency when a binding already exists.
Example fix: const requested = params.params.authProfileId?.trim() || undefined;
const bound = binding.authProfileId?.trim() || undefined;
if (requested && bound && requested !== bound) {
throw new Error("auth profile mismatch for session binding");
}
const effectiveAuthProfileId = requested ?? bound;
// proceed to resume and write effectiveAuthProfileIdAlso consider applying the same check earlier in 3. 🟡 Credential mix-up risk: CODEX_HOME isolation uses truncated SHA-256 of authProfileId (64-bit)
Description
Impact:
This affects both:
Vulnerable code: const digest = crypto.createHash("sha256").update(profileId).digest("hex").slice(0, 16);
return path.join(agentDir, "harness-auth", "codex", digest);Although accidental collisions are unlikely, intentional collisions against a 64-bit truncated hash are within reach of a determined attacker (~2^32 work). If RecommendationAvoid truncating the digest used for isolation, or include additional namespace and verification. Preferred: use the full SHA-256 hex (256-bit) as the directory name: function resolveCodexBridgeHome(agentDir: string, profileId: string): string {
const digest = crypto.createHash("sha256").update(profileId).digest("hex");
return path.join(agentDir, "harness-auth", "codex", digest);
}Additional hardening options:
Analyzed PR: #68284 at commit Last updated on: 2026-04-18T20:31:17Z |
Greptile SummaryThis PR extracts a central
Confidence Score: 3/5Hold for the two logic gaps in oauth-manager.ts before merging. The P1 (uncaught retry error bypassing main-store fallback) and P2 (expired credential adoption inconsistency) are both present defects in the new centralized path. The P1 silently skips a recovery step that the original catch block logic intends to always execute, and the inconsistency between the pre-lock and inside-lock adoption paths creates a correctness gap that could cause unnecessary store writes and future confusion. src/agents/auth-profiles/oauth-manager.ts — both findings are in Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/agents/auth-profiles/oauth-manager.ts
Line: 218-224
Comment:
**Pre-lock adoption missing usability check**
`adoptNewerMainOAuthCredential` adopts the main-store credential when its `expires` timestamp is finite and newer than the local one, but never checks `hasUsableOAuthCredential(mainCred)`. The inside-lock adoption path in `doRefreshOAuthTokenWithLock` (lines 347–351) does check `hasUsableOAuthCredential(mainCred)`, creating an inconsistency.
Concretely: if the local credential has `expires = undefined` (or any non-finite value), the condition `!Number.isFinite(params.credential.expires)` is `true`, so any main-store credential with a finite `expires` (even one already expired) will be adopted and written to the local store. The local credential is then overwritten with an unusable one right before the refresh path.
```suggestion
if (
mainCred?.type === "oauth" &&
mainCred.provider === params.credential.provider &&
hasUsableOAuthCredential(mainCred) &&
Number.isFinite(mainCred.expires) &&
(!Number.isFinite(params.credential.expires) ||
mainCred.expires > params.credential.expires) &&
adapter.isSafeToCopyOAuthIdentity(params.credential, mainCred)
) {
```
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: src/agents/auth-profiles/oauth-manager.ts
Line: 519-531
Comment:
**Retry throw bypasses main-store fallback and `OAuthManagerRefreshError` wrapping**
When `refreshOAuthTokenWithLock` (the retry on line 523) throws, the exception exits the surrounding `catch` block immediately. This means:
1. The main-store credential inheritance path (lines 532–557) is silently skipped.
2. The error is never wrapped in `OAuthManagerRefreshError`, so callers that check `error instanceof OAuthManagerRefreshError` (e.g., `oauth.ts` line 471) fall to the branch that reloads the store fresh, missing the chance to inherit a valid credential from the main agent store.
The retry should be wrapped so an uncaught throw doesn't escape the recovery block:
```typescript
try {
const retried = await refreshOAuthTokenWithLock({
profileId: params.profileId,
provider: params.credential.provider,
agentDir: params.agentDir,
});
if (retried) {
return retried;
}
} catch {
// retry also failed; fall through to main-store fallback below
}
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "Merge branch 'main' into oauth-central-m..." | Re-trigger Greptile |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 97daa41683
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d2bf852569
ℹ️ 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".
d2bf852 to
4d87f4b
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4d87f4b26c
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: df4c2aa112
ℹ️ 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".
|
follow-up fix pass is up in 4edc9be. addressed in this push:
verification run on this branch:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3505c64ace
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 27d0028175
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6c771bb9a4
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5c6d4ab9fc
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 12f1aa9b17
ℹ️ 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".
| await writeCodexAppServerBinding(params.params.sessionFile, { | ||
| threadId: response.thread.id, | ||
| cwd: params.cwd, | ||
| authProfileId: params.params.authProfileId, |
There was a problem hiding this comment.
Preserve inherited auth profile on replacement thread start
When a run request omits authProfileId, runCodexAppServerAttempt can still launch the app-server using the previously bound profile, but if startOrResumeThread falls back to thread/start (for example after a stale thread resume failure or dynamic-tools fingerprint reset) this path writes authProfileId from params.params.authProfileId directly. In that case the new binding is persisted without the inherited profile, so later resume/compaction calls can silently fall back to openai-codex:default and run the same session under a different auth context.
Useful? React with 👍 / 👎.
Summary
.codexbootstrap/overlay paths, and Codex ambient host auth incodex exec/ app-server harness flows..codexor host env.openai-codexauth,.codexis import-only for onboarding/recovery, and bothcodex-cliplus Codex app-server can run against a generated privateCODEX_HOMEderived from canonical OpenClaw auth.Change Type
Scope
Linked Issue/PR
Root Cause
openai-codexlogin wrote canonical auth,.codexcould still influence runtime selection later, and Codex harnesses used ambient host auth instead of the selected OpenClaw profile.Regression Test Plan
extensions/openai/openai-codex-provider.test.tsextensions/openai/openai-codex-cli-bridge.test.tsextensions/codex/src/app-server/auth-bridge.test.tsextensions/codex/src/app-server/shared-client.test.tsextensions/codex/src/app-server/run-attempt.test.tsextensions/codex/src/app-server/compact.test.tssrc/agents/cli-auth-epoch.test.tssrc/agents/cli-backends.test.tssrc/agents/auth-profiles.external-cli-sync.test.tssrc/agents/auth-health.test.tssrc/agents/auth-profiles/oauth.openai-codex-refresh-fallback.test.ts.codexis not a runtime overlay source foropenai-codexcodex-cliuses canonicalopenai-codex:defaultvia generatedCODEX_HOMECODEX_HOMEauthProfileIdfor resume/compaction.codexdriftUser-visible / Behavior Changes
.codexis no longer a normal runtime auth source foropenai-codex; it is a one-time import path.openai-codexlogin now exposes an explicitImport Codex CLI loginauth method for onboarding/recovery.codex-clinow defaults to the canonicalopenai-codex:defaultprofile and runs with an isolated generatedCODEX_HOMEinstead of the user's real.codex..codexis still never written by OpenClaw.Diagram
Security Impact
.codexremains read-only import input and is never written back.Repro + Verification
Environment
v24.13.0, pnpm10.11.1openai-codexOAuth and Codex harness pathscodex-cli, Codex app-server, local.codexVerified
OPENCLAW_LOCAL_CHECK=0 pnpm test extensions/openai/openai-codex-provider.test.ts extensions/openai/openai-codex-cli-bridge.test.ts extensions/codex/src/app-server/auth-bridge.test.ts extensions/codex/src/app-server/shared-client.test.ts extensions/codex/src/app-server/run-attempt.test.ts extensions/codex/src/app-server/compact.test.ts src/agents/cli-auth-epoch.test.ts src/agents/cli-backends.test.ts src/agents/auth-profiles.external-cli-sync.test.ts src/agents/auth-health.test.ts src/agents/auth-profiles/oauth.openai-codex-refresh-fallback.test.tspnpm plugin-sdk:api:genOPENCLAW_LOCAL_CHECK=0 pnpm buildpnpm exec oxfmt --check ...on the touched filesNotes
scripts/committer/repo-widepnpm checkis still blocked by unrelated generated-file drift inapps/macos/Sources/OpenClaw/HostEnvSecurityPolicy.generated.swift, so I used scoped verification plusFAST_COMMIT=1for the final commit.ui/; I expanded the sparse set to includeui/and reran the full build successfully.Human Verification
.codex..codexoverlay behavior foropenai-codexis gone and health/runtime now reflect canonical auth only.Compatibility / Migration
.codexlogin now need to explicitly import it into OpenClaw once if they want OpenClaw to own that session.Risks and Mitigations
.codexexpectations from earlier code paths could confuse reviewers/users.openai-codex.docs/.generated/plugin-sdk-api-baseline.sha256and passed the d.ts build.