diff --git a/backend/server.js b/backend/server.js index 95a9d48..01f6f93 100644 --- a/backend/server.js +++ b/backend/server.js @@ -14,13 +14,29 @@ const rateLimit = require('express-rate-limit'); const app = express(); const port = Number(process.env.PORT) || 3001; -// CORS: при необходимости сузьте до списка доменов (пример ниже) -// const allowed = (process.env.CORS_ORIGINS || '').split(',').map(s => s.trim()).filter(Boolean); -// app.use(cors({ origin: (origin, cb) => { if (!origin || allowed.length===0 || allowed.includes(origin)) return cb(null, true); cb(new Error('CORS blocked')); }, credentials: true })); -app.use(cors()); +// CORS: по умолчанию максимально разрешаем, можно сузить через CORS_ORIGINS +const allowed = (process.env.CORS_ORIGINS || '').split(',').map(s => s.trim()).filter(Boolean); +if (allowed.length > 0) { + app.use(cors({ + origin: (origin, cb) => { + if (!origin || allowed.includes(origin)) return cb(null, true); + return cb(new Error('CORS blocked')); + }, + credentials: true, + })); +} else { + app.use(cors({ origin: true, credentials: true })); +} +// Разрешаем preflight для всех путей +app.options('*', cors()); app.use(helmet({ + contentSecurityPolicy: false, + crossOriginEmbedderPolicy: false, + crossOriginOpenerPolicy: { policy: 'same-origin-allow-popups' }, crossOriginResourcePolicy: { policy: 'cross-origin' }, })); +// Если приложение работает за прокси/ингрессом (Docker/NGINX), доверяем первому прокси для корректной работы rate-limit +app.set('trust proxy', 1); app.disable('x-powered-by'); const limiter = rateLimit({ windowMs: 15 * 60 * 1000,