Skip to content

fix: implement dynamic CORS headers based on request origins (Fixes #1049)#1143

Closed
zeroknowledge0x wants to merge 1 commit into
sublime247:mainfrom
zeroknowledge0x:fix/dynamic-cors-headers-1049
Closed

fix: implement dynamic CORS headers based on request origins (Fixes #1049)#1143
zeroknowledge0x wants to merge 1 commit into
sublime247:mainfrom
zeroknowledge0x:fix/dynamic-cors-headers-1049

Conversation

@zeroknowledge0x
Copy link
Copy Markdown

Summary

Replaces hardcoded Access-Control-Allow-Origin: * with dynamic origin validation in the well-known cache worker, ensuring CORS headers are only sent for explicitly allowed origins.

Changes

workers/well-known-cache/src/index.ts

  • Add ALLOWED_ORIGINS to the Env interface
  • Add parseAllowedOrigins() function to parse comma-separated env var
  • Add getCorsHeaders() function that validates request origin against allowlist
  • Only sets Access-Control-Allow-Origin for requests from allowed origins
  • Adds Vary: Origin header for proper cache key differentiation
  • Update errorResponse() to accept and pass through CORS headers
  • Replace all CORS_HEADERS constant usage with dynamic corsHeaders

wrangler.toml

  • Add ALLOWED_ORIGINS environment variable with placeholder domain

Security Improvements

  • Before: Any origin could make cross-origin requests (wildcard *)
  • After: Only explicitly listed origins receive CORS headers
  • Prevents unauthorized cross-origin access to cached .well-known resources
  • Vary: Origin ensures proper cache segregation per origin

Configuration

Set ALLOWED_ORIGINS in wrangler.toml or Cloudflare dashboard:

ALLOWED_ORIGINS = "https://app.yourdomain.com,https://admin.yourdomain.com"

Related Issues

Fixes #1049

- Replace hardcoded Access-Control-Allow-Origin: * with origin validation
- Add ALLOWED_ORIGINS environment variable for configurable allowlist
- Validate request Origin header against allowed origins
- Only set CORS headers for requests from allowed origins
- Add Vary: Origin header for proper cache key differentiation
- Update errorResponse to pass CORS headers through

Fixes sublime247#1049
@sublime247 sublime247 closed this May 30, 2026
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.

[MEDIUM] Implement dynamic cors headers injection in Edge Workers

3 participants