Skip to content

Fix CORS origin handling for deployed frontend origins#7

Merged
oshankkkk merged 1 commit into
corsbugfixfrom
codex/fix-cors-policy-for-fetch-requests
Apr 6, 2026
Merged

Fix CORS origin handling for deployed frontend origins#7
oshankkkk merged 1 commit into
corsbugfixfrom
codex/fix-cors-policy-for-fetch-requests

Conversation

@oshankkkk
Copy link
Copy Markdown
Owner

Motivation

  • The middleware previously returned a hardcoded Access-Control-Allow-Origin, causing preflight failures when the frontend was served from a different Railway domain.
  • Browsers require the Access-Control-Allow-Origin to either exactly match the request Origin or be a wildcard, so dynamic reflection must be gated by an allowlist.
  • Allowing origins via environment variables and a safe local default prevents deployment mismatches while keeping local development working.

Description

  • Replaced the fixed origin with an allowlist-based approach that reads CLIENT_URL and CORS_ALLOWED_ORIGINS (comma-separated) from the environment and normalizes entries by trimming trailing slashes.
  • The middleware now echoes the request Origin only when it is present in the allowlist and adds Vary: Origin when reflecting the origin.
  • Added a sensible default of http://localhost:5173 when no CORS env vars are provided and preserved preflight OPTIONS handling returning 200 OK.
  • Implemented helper functions allowedOriginsFromEnv and isAllowedOrigin in backend/internal/middleware/cors/cors.go to centralize origin parsing and validation.

Testing

  • Ran the backend test suite with cd backend && go test ./... and the tests completed successfully.
  • Test output included successful packages such as backend/internal/api/handler, backend/internal/api/request, backend/internal/api/response, and backend/internal/api/validator reporting ok.
  • No middleware-specific unit tests were present, and no test failures occurred in the run.

Codex Task

@oshankkkk oshankkkk merged commit ccbade2 into corsbugfix Apr 6, 2026
1 of 2 checks passed
@oshankkkk oshankkkk deleted the codex/fix-cors-policy-for-fetch-requests branch April 7, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant