Web Development

React SPA loses session cookie intermittently when routed through Cloudflare Worker edge scripts

Environment

Environment

Single-page React app (production build, served from an S3 bucket behind Cloudflare CDN). Authentication is cookie-based (HTTP only, SameSite=Lax, Domain=example.com). Cloudflare account uses Workers to rewrite URLs and inject headers at the edge. Origin runs Node.js Express for API at api.example.com. Incidents seen in Chrome 120 and Firefox 119 on desktop and mobile browsers.

Symptoms

Symptoms

Users intermittently appear logged out while the browser still shows session cookie present in DevTools; subsequent API calls receive 401 from origin. Issue is transient and resolves after page refresh or after 10–30 seconds. Affected requests are often the first POST after page load or after navigation handled by the Worker.

Observations

Observations

Cloudflare Worker logs show that some incoming requests lack the Cookie header when forwarded to origin; Worker sometimes sets Set-Cookie in response but the browser does not persist it. When tracing with curl against Cloudflare edge, responses to the browser include Set-Cookie but the header is dropped on subsequent requests. Reproduces more frequently when the Worker rewrites hostnames (proxying api.example.com to origin-internal.example) or when Worker returns an early 302 redirect. No issues when bypassing Cloudflare (direct DNS to origin) and requests include expected cookies. Browser console shows no JS errors; cookies are marked Secure and HttpOnly.

Already attempted

Already attempted

Disabled Worker and confirmed cookie behavior restored (no more intermittent loss). Reduced Worker script to a simple header passthrough — issue absent. Reintroduced URL rewrite logic gradually; issue reappeared when rewriting to an internal host and setting a custom Host header in fetch(). Tried using fetch(event.request) vs. fetch(modifiedRequest) in Worker; same pattern. Verified cookie domain and path settings; tested with SameSite=None; issue persists only when Worker alters request host header or emits a redirect.

Question

Question

Why are session cookies intermittently missing when requests are forwarded by a Cloudflare Worker that rewrites host/redirects, and what changes to Worker logic or cookie configuration will reliably preserve cookies without weakening security?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Worker rewrites the request URL or Host header and creates a new Request object; this new Request lacks the original Cookie header or uses a different host, causing browser cookies not to be sent/forwarded 75%
Edge-issued redirects (302) from the Worker change the effective domain/path of the following request so the browser does not attach the cookie on the redirected request, particularly with SameSite=Lax/potential cross-site redirect semantics 50%
Cloudflare caching or custom cache logic at the Worker level returns an older response without Set-Cookie, making clients briefly appear unauthenticated until they refresh 25%

Summary

Cloudflare Workers run at the edge and can modify request/response headers. Browsers send cookies based on the request URL (domain + scheme + path + SameSite rules); if the edge script issues redirects, rewrites hostnames, or constructs a new Request with different URL/host, the Cookie header forwarded to origin may differ from the browser's original header. Additionally, Set-Cookie semantics combined with proxying and redirects can cause the browser not to persist cookies when domain/secure/sameSite constraints are not met. The correlation between Worker host rewrite and missing Cookie suggests the Worker is reconstructing requests or issuing cross-host redirects that break cookie scoping.

Suggested Fixes

Diagnostic step 1
Modify Worker to avoid creating a fresh Request with a new URL/Host. Instead, use event.fetch(event.request) with minimal alterations or explicitly copy event.request.headers and ensure Cookie is preserved when constructing a new Request (e.g., new Request(url, { headers: event.request.headers })).
Diagnostic step 2
Avoid returning edge-side redirects that change site origin during authentication flows. If redirect is required, ensure Set-Cookie attributes (Domain, SameSite) match the redirect target and use SameSite=None with Secure for third-party contexts only if acceptable.
Diagnostic step 3
Temporarily disable the Worker and confirm production traffic behaves correctly (you already tested this). Use that as a control while iteratively enabling minimal parts of the Worker to identify the exact instruction that strips or changes Cookie.

Continue troubleshooting

Matched from the title, description and tags