diff --git a/backend/src/index.ts b/backend/src/index.ts index a449d42..b752b8a 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -36,9 +36,28 @@ const discoveryLimiter = rateLimit({ }); // Middleware -app.use(cors()); -app.use(express.json()); -app.use(express.urlencoded({ limit: '10mb', extended: true })); +const allowedOrigins = (process.env.CORS_ORIGIN || '') + .split(',') + .map((s) => s.trim()) + .filter(Boolean); + +app.use( + cors({ + origin: (origin, callback) => { + // Allow same-origin (no Origin header) and whitelisted origins + if (!origin) return callback(null, true); + if (allowedOrigins.length === 0 || allowedOrigins.includes(origin)) { + return callback(null, true); + } + return callback(new Error('Not allowed by CORS')); + }, + credentials: true, + methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], + allowedHeaders: ['Content-Type', 'Authorization'], + }) +); +app.use(express.json({ limit: '1mb' })); +app.use(express.urlencoded({ limit: '1mb', extended: true })); // Initialize databases on startup let isInitialized = false;