Skip to content

fix: handle Cloudflare challenge errors for Codex OAuth calls - #95150

Closed
sunlit-deng wants to merge 4 commits into
openclaw:mainfrom
sunlit-deng:fix/issue-94432
Closed

sunlit-deng wants to merge 4 commits into
openclaw:mainfrom
sunlit-deng:fix/issue-94432

Conversation

@sunlit-deng

@sunlit-deng sunlit-deng commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

OpenAI/Codex OAuth users can log in successfully but model calls fail with a misleading "Authentication failed" error. The real cause: chatgpt.com/backend-api returns an HTML Cloudflare browser-challenge page (HTTP 403) instead of an API response. The openclaw/(os release; arch) User-Agent and missing browser-standard headers can contribute to Cloudflare bot detection.

Related: #94432

Why This Change Was Made

Three coordinated changes across the chatgpt.com/backend-api code paths:

  1. Cloudflare challenge classification. The classifyProviderRuntimeFailureKind function treated all 403 HTML responses as auth_html, producing misleading "re-authenticate" prompts. Cloudflare browser-challenge pages are CDN/gateway blocks, not auth failures, so they now return upstream_html with the accurate "CDN or gateway blocked" message. Generic 403 HTML still returns auth_html.

  2. Browser-like HTTP headers for Codex model calls. Chrome-like User-Agent plus standard browser headers (Accept, Accept-Language) replace the openclaw/... fingerprint on Codex model calls. buildBaseCodexHeaders applies these to SSE, WebSocket, and regular HTTP paths, with Accept still overridden by buildSSEHeaders / buildWebSocketHeaders where those transports require it.

  3. Browser-like User-Agent for WHAM usage probes. The Codex usage fetch and WHAM cooldown probe both call chatgpt.com/backend-api/wham/usage. The User-Agent override is applied after resolveProviderRequestHeaders() so provider attribution resolution does not reintroduce the openclaw/... User-Agent.

User Impact

  • Codex OAuth users see accurate error messages for Cloudflare challenge pages instead of misleading auth-failure prompts.
  • Browser-like headers may reduce Cloudflare blocks on chatgpt.com/backend-api Codex model calls.
  • WHAM usage probes use the same browser-like User-Agent direction as Codex model calls.
  • Generic 403 HTML still classifies as auth_html, preserving existing behavior for non-Cloudflare/auth-like 403 pages.

Evidence

Focused test run:

node scripts/run-vitest.mjs run src/agents/embedded-agent-helpers/provider-error-patterns.test.ts src/llm/providers/openai-chatgpt-responses.test.ts src/infra/provider-usage.fetch.codex.test.ts src/agents/auth-profiles/usage.test.ts

Result: passed.

Coverage includes:

  • Cloudflare challenge HTML + 403 -> upstream_html
  • Generic 403 HTML -> auth_html
  • Codex model-call base headers include the browser-like User-Agent, Accept, and Accept-Language
  • SSE and WebSocket builders preserve their transport-specific Accept behavior
  • WHAM usage fetch and cooldown probe override User-Agent after provider attribution resolution

Live Behavior Proof

Real WHAM usage request tested locally on June 27, 2026. The request used the same browser-like User-Agent direction added by this PR and redacts the bearer token, ChatGPT account id, and response body data.

{
  "started": "2026-06-27T11:08:47.752Z",
  "endpoint": "https://chatgpt.com/backend-api/wham/usage",
  "method": "GET",
  "requestHeaders": {
    "Accept": "application/json",
    "Authorization": "Bearer <redacted>",
    "originator": "openclaw",
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
    "ChatGPT-Account-Id": "<redacted>"
  },
  "status": 200,
  "ok": true,
  "contentType": "application/json",
  "parsedJson": true,
  "jsonTopLevelKeys": [
    "user_id",
    "account_id",
    "email",
    "plan_type",
    "rate_limit",
    "code_review_rate_limit",
    "additional_rate_limits",
    "credits",
    "spend_control",
    "rate_limit_reached_type",
    "promo",
    "referral_beacon"
  ],
  "bodyPrefix": "<json redacted>"
}

Risk checklist

  • Backward-compatible: generic 403 HTML still returns auth_html
  • Accept is overridden by SSE/WebSocket builders where required
  • Accept-Language is additive
  • WHAM probe + usage fetch override User-Agent after attribution policy resolution
  • No new dependencies

AI-assisted: built with Codex

…openclaw#94432)

- Use Chrome-like User-Agent for Codex chatgpt.com/backend-api requests
  and add browser-standard Accept/Accept-Language headers. Non-browser
  User-Agent triggers Cloudflare HTML challenge pages.
- Classify Cloudflare browser-challenge 403 HTML as upstream_html instead
  of auth_html, surfacing the accurate "CDN or gateway blocked the
  request" message instead of the misleading "authentication failed" one.
- Add isCloudflareChallengeResponse() with regex matching common
  Cloudflare challenge page patterns.

src/llm/providers/openai-chatgpt-responses.ts — browser-like headers
src/infra/provider-usage.fetch.codex.ts — browser-like User-Agent
src/agents/embedded-agent-helpers/errors.ts — challenge detection + reclassification
src/agents/embedded-agent-helpers/provider-error-patterns.test.ts — tests
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Jun 20, 2026
@clawsweeper

clawsweeper Bot commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 27, 2026, 9:40 AM ET / 13:40 UTC.

Summary
The PR changes Codex OAuth and WHAM request headers to a fixed browser-like User-Agent and classifies matching 403 Cloudflare challenge HTML as upstream_html instead of auth_html.

PR surface: Source +33, Tests +11. Total +44 across 7 files.

Reproducibility: Partly: source inspection confirms current main maps 403 HTML to auth_html, but this review did not establish a live current-main Codex OAuth model-call reproduction of the Cloudflare challenge.

Review metrics: 1 noteworthy metric.

  • Codex request header surfaces: 3 changed, 0 added, 0 removed. The PR changes User-Agent behavior in the model-call base headers, the WHAM usage fetch, and the WHAM cooldown probe, which are provider/auth-sensitive request paths.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🦐 gold shrimp
Result: blocked until stronger real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Add redacted live proof of a real Codex OAuth model request before and after the change, including the observed Cloudflare failure being avoided or correctly classified.
  • [P1] Get maintainer approval for any browser-like User-Agent direction, or remove the header changes and keep the PR limited to the classifier fix.

Proof guidance:

  • [P1] Needs stronger real behavior proof before merge: The PR body includes live WHAM usage output, but it does not prove the central Codex OAuth model-call path or show an observed before/after Cloudflare 403 fix. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] Merging as-is would silently replace the documented OpenClaw/Codex attribution User-Agent on model calls and WHAM probes with a fixed browser-like value, which can affect provider auth, attribution, or future compatibility.
  • [P1] The supplied live proof only shows a WHAM usage request returning 200; it does not show the central Codex OAuth model-call path succeeding or a before/after Cloudflare 403 challenge being fixed.
  • [P1] The classifier regex is broader than the minimal challenge markers and now treats generic Cloudflare/captcha/attention-required 403 HTML as upstream_html; that may be acceptable, but the branch proof does not exercise the wider cases.

Maintainer options:

  1. Split and preserve attribution first (recommended)
    Remove the browser-like User-Agent changes from this PR, keep or defer the classifier-only change, and handle any provider header direction in a maintainer-approved follow-up with live model-call proof.
  2. Approve a new Codex header contract
    Maintainers can intentionally accept the browser-like header direction, but should first update the attribution policy/docs and require proof that real Codex OAuth model calls work without regressing WHAM or transport-specific Accept behavior.
  3. Use the narrower canonical PR
    If maintainers only want the error-message fix, close or pause this branch and continue review on the open classifier-only PR fix: #94432 classify Cloudflare challenge 403 as upstream_html instead of auth_html #94440.

Next step before merge

  • [P1] Needs maintainer decision on the Codex attribution/header contract and contributor live model-call proof; this is not a safe automated repair lane.

Security
Cleared: Security review cleared: the diff does not add dependencies, workflows, scripts, permissions, secrets handling, or new code-execution supply-chain surface.

Review findings

  • [P1] Keep Codex requests on the attribution contract — src/llm/providers/openai-chatgpt-responses.ts:1603-1607
  • [P1] Do not override protected attribution after resolution — src/agents/auth-profiles/usage.ts:253-254
Review details

Best possible solution:

Keep the narrow diagnostic classifier improvement only with maintainer agreement, and require an approved provider-attribution direction plus redacted live Codex OAuth model-call proof before changing request headers.

Do we have a high-confidence way to reproduce the issue?

Partly: source inspection confirms current main maps 403 HTML to auth_html, but this review did not establish a live current-main Codex OAuth model-call reproduction of the Cloudflare challenge.

Is this the best way to solve the issue?

No as proposed: the classifier direction is plausible, but replacing Codex request attribution headers is not the best fix without maintainer approval and live model-call proof.

Full review comments:

  • [P1] Keep Codex requests on the attribution contract — src/llm/providers/openai-chatgpt-responses.ts:1603-1607
    This replaces the documented OpenClaw/Codex User-Agent on every ChatGPT backend model request with a fixed Chrome UA. Current docs and provider-attribution policy treat originator, version, and User-Agent as Codex attribution, and upstream Codex still sends an originator/version UA, so this provider-auth header change needs a maintainer-approved contract and live model-call proof before replacing the existing path.
    Confidence: 0.84
  • [P1] Do not override protected attribution after resolution — src/agents/auth-profiles/usage.ts:253-254
    The WHAM probe resolves OpenAI request headers through the provider attribution policy and then overwrites User-Agent afterward. That bypasses the protected-header merge point, so WHAM traffic can diverge from the same attribution contract the resolver is meant to enforce; route this through the policy only if maintainers approve the new header value.
    Confidence: 0.82

Overall correctness: patch is incorrect
Overall confidence: 0.84

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against c5d34c8376f8.

Label changes

Label changes:

  • add merge-risk: 🚨 compatibility: The PR changes documented native Codex attribution/header behavior for existing users without an upgrade or maintainer-approved contract.
  • add merge-risk: 🚨 auth-provider: The changed headers are sent on OpenAI/Codex OAuth model and WHAM requests, so they can affect provider auth, account routing, or Cloudflare/provider handling.

Label justifications:

  • P2: This is a normal provider bugfix PR with limited file scope, but it is not a proven urgent runtime repair in its current form.
  • merge-risk: 🚨 compatibility: The PR changes documented native Codex attribution/header behavior for existing users without an upgrade or maintainer-approved contract.
  • merge-risk: 🚨 auth-provider: The changed headers are sent on OpenAI/Codex OAuth model and WHAM requests, so they can affect provider auth, account routing, or Cloudflare/provider handling.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The PR body includes live WHAM usage output, but it does not prove the central Codex OAuth model-call path or show an observed before/after Cloudflare 403 fix. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +33, Tests +11. Total +44 across 7 files.

View PR surface stats
Area Files Added Removed Net
Source 4 37 4 +33
Tests 3 14 3 +11
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 51 7 +44

Acceptance criteria:

  • [P1] Require redacted live Codex OAuth model-call proof for the changed header path, not only WHAM usage output.
  • [P1] If the header direction is approved, validate focused tests for provider-error patterns, OpenAI ChatGPT Responses headers, Codex usage fetch, and auth-profile WHAM cooldown probing.
  • [P1] Confirm docs and provider-attribution policy match the final header contract.

What I checked:

Likely related people:

  • stainlu: Authored merged PR fix(agents): classify Cloudflare/CDN HTML error pages as transport failures #67642, which established the Cloudflare/CDN HTML classification boundary this PR changes. (role: HTML provider classification contributor; confidence: high; commits: e588e904a744, d13aded0269b, c926890b7ee6; files: src/agents/pi-embedded-helpers/errors.ts, src/agents/pi-embedded-helpers/provider-error-patterns.test.ts)
  • Chris Yau: Added and refined the shared standalone Cloudflare HTML challenge detector that the PR explicitly aligns with. (role: adjacent Cloudflare challenge detector contributor; confidence: high; commits: 36dd58ac2a5d, 59caf03d6783; files: src/shared/assistant-error-format.ts)
  • Peter Steinberger: Recent history shows repeated ownership of the Codex provider catalog, OpenAI provider identity unification, and OpenAI attribution policy surfaces touched by this PR. (role: OpenAI/Codex provider identity and attribution contributor; confidence: high; commits: 4c33aaa86c16, 20b05f220e67, d1b2d81752b8; files: extensions/codex/provider-catalog.ts, src/agents/provider-attribution.ts, src/infra/provider-usage.fetch.codex.ts)
  • Vincent Koc: Recent commits touched provider usage and auth-profile usage helpers adjacent to the WHAM paths changed here. (role: recent usage/auth helper contributor; confidence: medium; commits: 0c565f3b0ef9, 5d48a2ec547e, 8aaf937bc045; files: src/infra/provider-usage.fetch.codex.ts, src/agents/auth-profiles/usage.ts, src/agents/provider-attribution.ts)
  • Sarah Fortune: Recent current-main history includes provider authentication failure surfacing in the same embedded-agent error helper path. (role: recent embedded-agent error contributor; confidence: medium; commits: 113d6f3c6483; files: src/agents/embedded-agent-helpers/errors.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. labels Jun 20, 2026
@sunlit-deng
sunlit-deng requested a review from a team as a code owner June 26, 2026 08:59
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. and removed P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. labels Jun 27, 2026
@sunlit-deng sunlit-deng changed the title fix: browser-like Codex headers + Cloudflare challenge classification (#94432) fix: show CDN-blocked error for Cloudflare challenge 403 Jun 27, 2026
@sunlit-deng sunlit-deng changed the title fix: show CDN-blocked error for Cloudflare challenge 403 fix: handle Cloudflare challenge errors for Codex OAuth calls Jun 27, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. and removed merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. labels Jun 27, 2026
@clawsweeper

clawsweeper Bot commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. and removed merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. labels Jun 27, 2026
@sunlit-deng

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper clawsweeper Bot added merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. labels Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant