Files
webhook/server/app.js
T
2026-07-07 19:57:02 +02:00

96 lines
2.9 KiB
JavaScript

// Express alkalmazás konfiguráció
import express from 'express';
import helmet from 'helmet';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { createRouter } from './routes/index.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
// Nginx proxy mögött a valódi kliens IP-t az X-Forwarded-For fejlécből veszi
app.set('trust proxy', 1);
// Express verzió fejléc elrejtése (ne áruljuk el a stacket)
app.disable('x-powered-by');
// Biztonsági fejlécek (CSP, nosniff, frame védelem stb.)
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
// A Tailwind build inline stílusokat is használhat
styleSrc: ["'self'", "'unsafe-inline'"],
scriptSrc: ["'self'"],
// Socket.io WebSocket ugyanarról a hostról
connectSrc: ["'self'"],
imgSrc: ["'self'", 'data:'],
objectSrc: ["'none'"],
frameAncestors: ["'none'"],
baseUri: ["'self'"]
}
},
// A webhook API-t más domainről is hívhatják, ne blokkolja a COEP
crossOriginEmbedderPolicy: false,
crossOriginResourcePolicy: { policy: 'cross-origin' }
}));
// JSON és form body parser (webhook payloadokhoz)
app.use(express.json({ limit: '1mb' }));
app.use(express.urlencoded({ extended: true, limit: '1mb' }));
// Nyers body maximum mérete DoS ellen (1MB) - a json/urlencoded parserrel egyezik
const RAW_BODY_LIMIT = 1024 * 1024;
// Nyers body mentése más content-type esetén (pl. text/plain, application/xml)
app.use((req, res, next) => {
const ct = req.headers['content-type'] || '';
// Ha már parseolta az express (json/urlencoded), nem kell külön
if (ct.includes('application/json') || ct.includes('application/x-www-form-urlencoded')) {
return next();
}
let raw = '';
let size = 0;
let aborted = false;
req.setEncoding('utf8');
req.on('data', chunk => {
if (aborted) return;
size += Buffer.byteLength(chunk);
// Méretkorlát túllépése: elvágjuk a kapcsolatot, hogy ne fogyjon a memória
if (size > RAW_BODY_LIMIT) {
aborted = true;
res.status(413).json({ error: 'A kérés törzse túl nagy (max 1MB).' });
req.destroy();
return;
}
raw += chunk;
});
req.on('end', () => {
if (aborted) return;
req.rawBody = raw;
next();
});
req.on('error', () => {
if (!aborted) next();
});
});
// Statikus fájlok kiszolgálása (Vite build output)
app.use(express.static(join(__dirname, '../public')));
// API és webhook route-ok
app.use('/', createRouter());
// SPA fallback - minden egyéb kérésre az index.html-t küldi vissza
app.get('*', (_req, res) => {
const indexPath = join(__dirname, '../public/index.html');
res.sendFile(indexPath, (err) => {
if (err) {
res.status(503).send('A frontend még nincs buildeve. Futtasd: npm run build');
}
});
});
export default app;