Repository navigation
config: support multiple CORS origins and fail closed in production - #73
Merged
joelpeace48-cell merged 1 commit intoAug 25, 2026
Merged
Conversation
CORS_ORIGIN was a single string defaulting to "http://localhost:3000" — a preview deployment and production couldn't each have their own origin, and an unset variable silently fell back to a development origin in production instead of failing startup. - CORS_ORIGIN is now comma-separated, parsed into a string array (parseCorsOrigins), so multiple origins are supported at once. - No default in the schema. In production, CORS_ORIGIN must be set explicitly and must not contain "*" — enforced by a superRefine on the env schema, so a missing or wildcard value fails startup rather than the first request. - Outside production, an unset CORS_ORIGIN still falls back to the dev default (resolveCorsOrigins) so local development is unaffected. - @fastify/cors is now configured with credentials and an explicit methods list instead of leaving both implicit. Tests cover comma-separated parsing, the dev fallback, the production guard rejecting a missing value, the production guard rejecting "*" (alone and mixed into a list), and production accepting a valid explicit origin. Closes FinesseStudioLab#2.
Contributor
|
Nice job @Codeinx |
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
src/config/env.tsdeclared:Two problems: it's a single string, so the API can't serve a preview deployment and production at once; and the default is a development origin that silently applies in production if the variable is unset — a misconfiguration that fails open rather than closed.
What this does
CORS_ORIGINis now comma-separated, parsed into an array (parseCorsOrigins) — multiple origins are supported at once.superRefineon the env schema requiresCORS_ORIGINto be set explicitly wheneverNODE_ENV === "production", and rejects"*"in production (alone or mixed into a list) — either failure throws at startup viaparseEnv, not at the first request.CORS_ORIGINstill falls back to the dev default (resolveCorsOrigins), so local development is unaffected.@fastify/corsis now registered withcredentials: trueand an explicitmethodslist instead of leaving both implicit.Testing
src/config/env.test.tsadds cases for: comma-separated parsing, the dev fallback, production rejecting a missingCORS_ORIGIN, production rejecting"*"(alone and mixed into a list), and production accepting a valid explicit origin.npm run lint,npm run build,npm test, andnpm audit --audit-level=highare all clean on this branch.Scope note
Touches only
src/config/env.ts,src/config/env.test.ts, andsrc/index.ts(the cors registration call). No other currently-open PR touches these files, so this should apply independently of merge order.Closes #2.