feat: Improve CORS configuration and security headers in server setup, allowing for customizable origins and enhanced preflight handling; trust first proxy for rate limiting functionality
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 4m57s
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 4m57s
This commit is contained in:
+20
-4
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user