Web Development
VERIFIED FIXES

Next.js API routes return 401 when Authorization header is stripped behind Cloudflare Access

Environment

Environment

Next.js 13 app (app router) running on Vercel, Node 18 runtime; Cloudflare Access + Cloudflare Worker in front, production

Symptoms

Symptoms

Intermittent 401 Unauthorized on POST /api/* endpoints that require Bearer token. Failures affect ~10% of requests, no client-side errors.

Observations

Observations

Origin logs show missing Authorization header for failing requests. Requests succeed when sent directly to origin hostname bypassing Cloudflare. Cloudflare Access shows normal allow decisions, Worker modifies headers for other routes.

Already attempted

Already attempted

Disabled a recently added Transform Rule in Cloudflare — no change. Temporarily removed Worker but left Access enabled; still saw some failures. Replayed failing request via curl to Cloudflare edge and to origin.

Question

Question

What Cloudflare configuration or proxy behavior can strip Authorization on some requests, and how to ensure Authorization reaches origin reliably?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
simonsupport Answer
user
Posted 1mo ago • 0 confirmations

First check any Cloudflare Worker or Transform Rules — they frequently remove Authorization. Re-enable the Worker with temporary logging (mask tokens) to confirm whether Authorization arrives at the edge and whether the Worker forwards it explicitly. If Cloudflare Access is in use, switch to sending the Access JWT assertion to the origin or enable the option that forwards the original Authorization header. Test by curling the Cloudflare edge (host header set) and verifying the origin receives Authorization. Roll back Worker changes if they cause regression; use the Access assertion header as a safe fallback.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Cloudflare Worker or Transform Rule unintentionally deleting Authorization header 85%
Cloudflare Access configured to exchange user token and not forward client Authorization to origin 65%
Origin request policy / HTTP/2 header conversion dropping sensitive headers between edge and origin 40%

Summary

Requests that arrive at the origin without Authorization commonly indicate an edge component (Cloudflare Access, Transform Rules, Workers, or HTTP/2 origin request policy) removing or not forwarding that header. Cloudflare Access may present a JWT to origin instead of forwarding the original header; Cloudflare Workers or Transform Rules can explicitly delete or rewrite headers. Also some origin-request settings used by Cloudflare (or the hosting platform) may normalise or omit sensitive headers when using HTTP/2 or when credentials are not allowed to be forwarded to third-party origins.

Suggested Fixes

Diagnostic step 1
Re-enable Cloudflare Worker but instrument it to log incoming headers (mask tokens) and explicitly forward Authorization to origin (add request.headers.set('authorization', incomingAuth)) for the API pathname.
Diagnostic step 2
Check Cloudflare Transform Rules and Page Rules for any header removal; disable or adjust rules affecting /api/*.
Diagnostic step 3
In Cloudflare Access application settings, enable 'JWT assertion' forwarding or configure 'Forward original Authorization' to origin if available; alternatively accept Cloudflare's assertion header and update origin to use it.

Continue troubleshooting

Matched from the title, description and tags