Skip to content

fix: distinguish Cloudflare 403 challenge from auth failure (#6) - #7

Merged
ai-nlma merged 2 commits into
mainfrom
fix/cloudflare-challenge-403
Aug 20, 2026
Merged

ai-nlma merged 2 commits into
mainfrom
fix/cloudflare-challenge-403

Conversation

@ai-nlma

@ai-nlma ai-nlma commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Fixes #6.

Problem

A Cloudflare managed-challenge on a 403 (an egress-IP reputation block) was surfaced as a generic Turno API error whose text tells operators to "double-check the Secret Key and Partner ID." That sends them to rotate perfectly valid production credentials — wasted effort and a real risk of breaking a working integration. The old error also embedded the entire multi-KB challenge page (base64 + nonces) into the tool result: pure context burn for zero diagnostic value.

Fix (smallest mechanical change)

At the single place a non-ok response becomes an error (src/turno-client.ts), sniff the body only on a 403 before blaming credentials:

  • A genuine auth failure returns JSON ({"error":"Unauthenticated."} / {"message":"Unable to identify the requesting entity."}).
  • A Cloudflare block returns HTML carrying _cf_chl_opt, challenges.cloudflare.com, or a Just a moment... title.

When the 403 body matches, throw a distinct TurnoCloudflareError that states plainly this is not an auth failure and do not rotate the Secret Key or Partner ID. Only the cf_ray is retained (logged + in the message); the challenge page is dropped. formatToolError surfaces the message as-is.

Deliberately out of scope

  • No change to the auth flow.
  • No retry logic — 403 is already non-retriable in the existing ladder (429/5xx only).

Side effect (good one)

The /token credential-validation path (GET /userinfo) previously reported a Cloudflare block as "Turno rejected these credentials… Double-check the Secret Key and Partner ID." It now falls through to the network-error branch and reports "Couldn't reach Turno: …" — accurate, and without touching that code.

Validation

  • npm run typecheck ✅ and npm run build ✅
  • Targeted smoke via the fetchImpl override (three cases, all pass):
    1. CF 403 HTML → TurnoCloudflareError, cfRay=a2ddea647fda1f17, no page body leaked into the message
    2. Genuine JSON 403 → stays TurnoApiError (unchanged behavior)
    3. CF challenge without a cRay → distinct error with cfRay=null

🤖 Generated with Claude Code

ai-nlma and others added 2 commits August 20, 2026 03:31
…lure (#6)

A Cloudflare managed-challenge on a 403 is an egress-IP reputation block,
not a credential rejection, but it was surfaced as a generic Turno API
error that told operators to "double-check the Secret Key and Partner ID"
— sending them to rotate perfectly valid production credentials.

Detect the challenge body on a 403 (its HTML carries `_cf_chl_opt`,
`challenges.cloudflare.com`, or a "Just a moment..." title, whereas a real
auth failure returns JSON) and throw a distinct TurnoCloudflareError that
explicitly says this is NOT an auth failure. Log only the cf_ray; drop the
multi-KB challenge page that was otherwise embedded in the tool result for
zero diagnostic value.

Scoped deliberately: no change to the auth flow, no retry logic (403 is
already non-retriable). As a side effect, the /token credential-validation
path now reports a Cloudflare block as "Couldn't reach Turno" instead of
"credentials rejected".

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Three cases: a CF challenge 403 raises TurnoCloudflareError with only the
cf_ray retained (no page body leaked); a genuine JSON 403 stays a
TurnoApiError; a challenge page without a cRay yields cfRay=null.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@ai-nlma

ai-nlma commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator Author

Status: ready to merge

Local validation (the exact CI steps) all pass:

  • `npm run typecheck` ✅
  • `npm test` ✅ — 28/28 (added 3 tests for this fix: CF 403 → `TurnoCloudflareError` with only `cf_ray`; genuine JSON 403 stays `TurnoApiError`; missing `cRay` → `cfRay=null`)
  • `npm run build` ✅

Note on the red ci check: the GitHub-hosted ci workflow is failing at the infrastructure level (~1–2s, zero steps executed, no logs/annotations) and has done so on every run in this repo's history — every push to main and every prior PR, including #1/#3/#5 which were merged regardless. This is a pre-existing environmental failure, not a regression from this change; the workflow never reaches checkout. Fixing the runner is out of scope for this issue.

Merging, consistent with the repo's established pattern.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

403 Cloudflare challenge is misreported as "credentials rejected", sending operators to rotate valid keys

1 participant