Web Development
VERIFIED FIXES

Cloudflare Worker strips Authorization header when X-Forwarded-For present causing 401s on API POSTs

pixelpelle
1mo ago
Environment

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

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

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

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

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?

COMMUNITY RESPONSES

Verified community troubleshooting

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

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.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Worker fetch() omitted 'Authorization' in forwarded headers or used Request.clone() incorrectly, causing header to be dropped 85%
Cloudflare edge security or origin rules block Authorization when unexpected X-Forwarded-For value present (e.g., header length/format triggers WAF) 40%
Origin server rejects requests with multiple or malformed X-Forwarded-For headers and treats as unauthenticated 30%

Summary

Evidence points to header normalization or unsafe header merging in the Worker fetch() call when forwarding requests. Cloudflare edge will drop certain sensitive headers unless explicitly forwarded, and adding X-Forwarded-For via Worker can trigger header list merging that inadvertently removes Authorization. Other possibilities include Cloudflare's origin header rules, transform-streams in Worker code, or downstream origin rejecting requests with unexpected proxy headers.

Suggested Fixes

Diagnostic step 1
Inspect Worker code: ensure fetch(request, { headers }) explicitly sets Authorization from original request (use request.headers.get('authorization')) before calling fetch.
Diagnostic step 2
Use Request constructor to preserve body and method: new Request(originUrl, { method: req.method, headers: forwardedHeaders, body: req.body }).
Diagnostic step 3
Enable debug logs in Workers (console.log) to assert header values before and after modifications but avoid logging secrets; log boolean presence only.

Continue troubleshooting

Matched from the title, description and tags