Cloudflare Worker strips Authorization header when X-Forwarded-For present causing 401s on API POSTs
Environment
Cloudflare Workers (production), origin API behind Cloudflare Spectrum, Worker deployed at edge with fetch() rewriting; clients include mobile apps and curl. Worker runtime JS v8, no KV usage.
Symptoms
Intermittent 401s from origin for authenticated POST requests. Some clients succeed; others fail. Failures correlate to client IPs being proxied (X-Forwarded-For header present).
Observations
Origin logs show missing Authorization header on failed requests though clients include it. Cloudflare request inspector shows Worker received Authorization but origin request lacks it. Failures started after Worker change that adds X-Forwarded-For rewrite and conditional header sanitization.
Already attempted
Rolled back recent Worker commit to previous version — failures reduced but not eliminated. Added explicit Authorization to fetch() init headers in Worker, no effect. Temporarily bypassed Worker via page rule — origin now receives Authorization reliably.
Question
What in the Worker or Cloudflare path can remove Authorization when X-Forwarded-For is set, and how to ensure origin always receives Authorization for proxied POSTs without disabling X-Forwarded-For?
Verified community troubleshooting
First, change the Worker to explicitly forward the Authorization header instead of relying on the fetch to inherit it. In code, build a headers object and set headers['authorization']=request.headers.get('authorization') before calling fetch(new Request(url,{method,headers,body})). Verify by enabling a non-sensitive debug flag that confirms presence (true/false) of Authorization at edge and origin logs. If that fixes it, revert any broad header rewrites. If problems persist, temporarily bypass the Worker or disable WAF rules to isolate Cloudflare policy as the cause; rollback is safe by restoring the prior Worker version or removing the page rule.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Web Development27% match
React SPA loses session cookie intermittently when routed through Cloudflare Worker edge scripts
Open0 answers - Web Development23% match
Next.js API routes return 401 when Authorization header is stripped behind Cloudflare Access
Solved1 answer - Web Development20% match
Next.js API behind Vercel Edge intermittently returns 403 due to cached edge responses dropping Cookie headers
Open0 answers