docs(#414): add TRUSTED_ORIGINS and COOKIE_DOMAIN to .env.example
All checks were successful
ci/woodpecker/push/api Pipeline was successful

Refs #414

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jason Woltje
2026-02-16 11:27:26 -06:00
parent 7ebbcbf958
commit f37c83e280
2 changed files with 11 additions and 32 deletions

View File

@@ -2,6 +2,7 @@ import { NestFactory } from "@nestjs/core";
import { ValidationPipe } from "@nestjs/common";
import cookieParser from "cookie-parser";
import { AppModule } from "./app.module";
import { getTrustedOrigins } from "./auth/auth.config";
import { GlobalExceptionFilter } from "./filters/global-exception.filter";
function getPort(): number {
@@ -47,39 +48,9 @@ async function bootstrap() {
app.useGlobalFilters(new GlobalExceptionFilter());
// Configure CORS for cookie-based authentication
// SECURITY: Cannot use wildcard (*) with credentials: true
const isDevelopment = process.env.NODE_ENV !== "production";
const allowedOrigins = [
process.env.NEXT_PUBLIC_APP_URL ?? "http://localhost:3000",
"https://app.mosaicstack.dev", // Production web
"https://api.mosaicstack.dev", // Production API
];
// Development-only origins (not allowed in production)
if (isDevelopment) {
allowedOrigins.push("http://localhost:3001"); // API origin (dev)
}
// Origin list is shared with BetterAuth trustedOrigins via getTrustedOrigins()
app.enableCors({
origin: (
origin: string | undefined,
callback: (err: Error | null, allow?: boolean) => void
): void => {
// Allow requests with no Origin header (health checks, server-to-server,
// load balancer probes). These are not cross-origin requests per the CORS spec.
if (!origin) {
callback(null, true);
return;
}
// Check if origin is in allowed list
if (allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error(`Origin ${origin} not allowed by CORS`));
}
},
origin: getTrustedOrigins(),
credentials: true, // Required for cookie-based authentication
methods: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
allowedHeaders: ["Content-Type", "Authorization", "Cookie", "X-CSRF-Token", "X-Workspace-Id"],