From e0e06eefa4cd5c4ea3d8f207275721914b7d8b4f Mon Sep 17 00:00:00 2001 From: nobswebdev Date: Wed, 25 Nov 2026 19:20:02 +0000 Subject: [PATCH] Enable CORS only in development. Production serves the CMS and API on one origin, so the allowlist is only needed while they run on separate ports. --- backend/src/config/validate.ts | 13 ++++++++++++- backend/src/main.ts | 10 ++++++---- deploy/DEPLOYMENT_GUIDE.md | 1 - 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/backend/src/config/validate.ts b/backend/src/config/validate.ts index c92bc23..7c4476c 100644 --- a/backend/src/config/validate.ts +++ b/backend/src/config/validate.ts @@ -1,5 +1,15 @@ import { plainToClass } from 'class-transformer'; -import { IsBoolean, IsEnum, IsNotEmpty, IsNumber, IsString, Max, Min, validateSync } from 'class-validator'; +import { + IsBoolean, + IsEnum, + IsNotEmpty, + IsNumber, + IsString, + Max, + Min, + ValidateIf, + validateSync +} from 'class-validator'; import { NodeEnv } from '../types/NodeEnv'; import { ShopFiatCurrency } from '../types/ShopFiatCurrency'; import { IsBase64 } from '../validation/decorators/isBase64'; @@ -41,6 +51,7 @@ class EnvironmentVariables { @IsBoolean() POSTGRES_MIGRATIONS_RUN: boolean; + @ValidateIf((config: EnvironmentVariables) => config.NODE_ENV === NodeEnv.Development) @IsNotEmpty() @IsString() CORS_ORIGINS: string; diff --git a/backend/src/main.ts b/backend/src/main.ts index 3b3f0e7..1bf7440 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -106,10 +106,12 @@ async function bootstrap() { ] }); - app.enableCors({ - origin: corsOrigins, - credentials: true - }); + if (nodeEnv === NodeEnv.Development) { + app.enableCors({ + origin: corsOrigins, + credentials: true + }); + } await app.listen(port); } diff --git a/deploy/DEPLOYMENT_GUIDE.md b/deploy/DEPLOYMENT_GUIDE.md index 6500b5e..0db6152 100644 --- a/deploy/DEPLOYMENT_GUIDE.md +++ b/deploy/DEPLOYMENT_GUIDE.md @@ -40,7 +40,6 @@ Edit `.env.prod`. Mandatory configuration: | `POSTGRES_MIGRATIONS_RUN` | `true` | | `PGADMIN_DEFAULT_PASSWORD` | strong random password | | `NODE_ENV` | `production` | -| `CORS_ORIGINS` | `https://your-domain.com` | | `CLEARNET_DOMAIN` | `your-domain.com` | | `JWT_SECRET` | strong random secret | | `CMS_PASSWORD` | strong admin password |