-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·356 lines (314 loc) · 19.5 KB
/
server.js
File metadata and controls
executable file
·356 lines (314 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// ─── server.js ───────────────────────────────────────────────────────────────
// Security-gehärteter Express 5 Server
// ──────────────────────────────────────────────────────────────────────────────
require('dotenv').config();
const express = require('express');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const helmet = require('helmet');
const compression = require('compression');
const rateLimit = require('express-rate-limit');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const IS_PROD = process.env.NODE_ENV === 'production';
// ─── Globale Fehlerbehandlung ───────────────────────────────────────────────
process.on('uncaughtException', (err) => {
console.error('[FATAL] Uncaught Exception:', err.message);
if (!IS_PROD) console.error(err.stack);
});
process.on('unhandledRejection', (err) => {
console.error('[FATAL] Unhandled Rejection:', err);
});
// ─── Verzeichnisse ──────────────────────────────────────────────────────────
const dataDir = path.join(__dirname, 'data');
const uploadDir = process.env.UPLOAD_DIR || path.join(__dirname, 'uploads');
if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir, { recursive: true });
if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir, { recursive: true });
// ─── Session-Secret validieren ──────────────────────────────────────────────
const SESSION_SECRET = process.env.SESSION_SECRET;
if (!SESSION_SECRET || SESSION_SECRET === 'dev-secret-change-me') {
if (IS_PROD) {
console.error('[SECURITY] FATAL: Kein sicheres SESSION_SECRET gesetzt!');
console.error(' Bitte in .env setzen: SESSION_SECRET=$(openssl rand -hex 48)');
process.exit(1);
} else {
console.warn('[SECURITY] WARNUNG: Kein sicheres SESSION_SECRET – nur in Entwicklung akzeptabel!');
}
}
// ─── Datenbank ──────────────────────────────────────────────────────────────
const { initialize } = require('./database');
initialize();
const app = express();
const PORT = process.env.PORT || 3000;
// ═══════════════════════════════════════════════════════════════════════════
// SECURITY: Trust Proxy (für Reverse Proxy / Caddy / nginx)
// ═══════════════════════════════════════════════════════════════════════════
// Ermöglicht korrekte IP-Erkennung und secure cookies hinter einem Proxy
app.set('trust proxy', 1);
// ═══════════════════════════════════════════════════════════════════════════
// SECURITY: Request-Logging (nicht-verbose in Produktion)
// ═══════════════════════════════════════════════════════════════════════════
app.use((req, res, next) => {
if (/^\/(css|js|img|fonts|vendor|favicon)/.test(req.path)) return next();
const start = Date.now();
res.on('finish', () => {
const ms = Date.now() - start;
const ts = new Date().toLocaleTimeString('de-DE');
// In Produktion: nur Warnungen und Fehler
if (IS_PROD && res.statusCode < 400) return;
console.log(`[${ts}] ${req.method} ${req.originalUrl} → ${res.statusCode} (${ms}ms)`);
});
next();
});
// ═══════════════════════════════════════════════════════════════════════════
// SECURITY: Helmet (HTTP Security Headers)
// ═══════════════════════════════════════════════════════════════════════════
app.use(helmet({
contentSecurityPolicy: {
useDefaults: false, // WICHTIG: Defaults enthalten upgrade-insecure-requests
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", "data:", "blob:"],
fontSrc: ["'self'"],
connectSrc: ["'self'"],
frameSrc: ["'self'"],
objectSrc: ["'none'"],
baseUri: ["'self'"],
formAction: ["'self'"],
// KEIN upgrade-insecure-requests! Sonst geht HTTP nicht.
}
},
hsts: false, // Caddy/nginx setzt HSTS, nicht die App
crossOriginEmbedderPolicy: false,
crossOriginOpenerPolicy: false, // Verursacht Warnungen bei HTTP
crossOriginResourcePolicy: false,
originAgentCluster: false, // Verhindert Agent-Cluster-Warnung bei HTTP
}));
app.use(compression());
// ═══════════════════════════════════════════════════════════════════════════
// SECURITY: Body Parser mit strikten Limits
// ═══════════════════════════════════════════════════════════════════════════
app.use(express.json({ limit: '5mb' }));
app.use(express.urlencoded({ extended: true, limit: '5mb', parameterLimit: 50 }));
app.use(cookieParser());
// ═══════════════════════════════════════════════════════════════════════════
// SECURITY: Rate Limiting
// ═══════════════════════════════════════════════════════════════════════════
// Login: max 10 Versuche pro 15 Minuten
const loginLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 10,
message: 'Zu viele Anmeldeversuche. Bitte 15 Minuten warten.',
standardHeaders: true,
legacyHeaders: false,
skipSuccessfulRequests: true,
});
// Kunden-Freigabeseite: max 30 Requests pro 15 Minuten
const approveLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 30,
message: 'Zu viele Anfragen. Bitte später erneut versuchen.',
standardHeaders: true,
legacyHeaders: false,
});
// API: max 200 Requests pro 15 Minuten
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 200,
standardHeaders: true,
legacyHeaders: false,
});
app.use('/api/', apiLimiter);
// ═══════════════════════════════════════════════════════════════════════════
// SECURITY: Sessions
// ═══════════════════════════════════════════════════════════════════════════
let sessionConfig = {
secret: SESSION_SECRET || crypto.randomBytes(48).toString('hex'),
name: 'sid', // Nicht den default 'connect.sid' verwenden (Fingerprinting)
resave: false,
saveUninitialized: true, // Nötig damit Login-Seite ein CSRF-Token bekommt
cookie: {
secure: false, // Auf false lassen! Bei HTTPS hinter Proxy: trust proxy + sameSite reichen
httpOnly: true, // Kein JS-Zugriff auf Session-Cookie
maxAge: 8 * 60 * 60 * 1000, // 8 Stunden
sameSite: 'lax', // CSRF-Schutz auf Cookie-Ebene
}
};
try {
const SQLiteStore = require('connect-sqlite3')(session);
sessionConfig.store = new SQLiteStore({ db: 'sessions.sqlite', dir: dataDir });
console.log('[INIT] Session-Store: SQLite');
} catch (err) {
console.warn('[INIT] SQLite-Store fehlgeschlagen:', err.message);
}
app.use(session(sessionConfig));
// ═══════════════════════════════════════════════════════════════════════════
// SECURITY: CSRF-Schutz
// Strategie:
// 1. Token in Session gespeichert + als Cookie gesetzt (JS kann es lesen)
// 2. Formulare senden Token als _csrf hidden field
// 3. AJAX sendet Token als X-CSRF-Token header
// 4. Multipart-Routen: geschützt durch SameSite=lax + requireLogin
// (Multer parst den Body erst nach CSRF-Middleware, daher _csrf
// nicht lesbar → SameSite=lax verhindert cross-site POSTs)
// ═══════════════════════════════════════════════════════════════════════════
app.use((req, res, next) => {
if (!req.session) return next();
// Kein CSRF-Token für öffentliche Approve-Seiten (vermeidet unnötige Sessions)
if (req.path.startsWith('/approve/') && req.method === 'GET') return next();
if (!req.session.csrfToken) {
req.session.csrfToken = crypto.randomBytes(32).toString('hex');
}
res.locals.csrfToken = req.session.csrfToken;
// Cookie setzen damit JS den Token für AJAX-Requests lesen kann
res.cookie('csrf-token', req.session.csrfToken, {
httpOnly: false,
sameSite: 'strict',
secure: req.secure || req.headers['x-forwarded-proto'] === 'https',
path: '/',
});
// Nur POST/PUT/DELETE prüfen
if (['GET', 'HEAD', 'OPTIONS'].includes(req.method)) return next();
// Öffentliche Routen ohne CSRF
if (req.path.startsWith('/approve/')) return next();
// Multipart-Formulare: Body ist vor Multer nicht lesbar → kein _csrf verfügbar.
// Schutz erfolgt durch SameSite=lax Session-Cookie (cross-site POSTs werden geblockt).
if (req.is('multipart/form-data')) return next();
// Token aus Header (für AJAX) oder Body (für reguläre Formulare).
const token = req.headers['x-csrf-token'] || req.body?._csrf;
if (!token || token !== req.session.csrfToken) {
console.warn(`[CSRF] Abgelehnt: ${req.method} ${req.path}`);
const contentType = req.headers['content-type'] || '';
if (req.xhr || contentType.includes('application/json')) {
return res.status(403).json({ error: 'CSRF-Token ungültig. Bitte Seite neu laden.' });
}
req.session.flash = { type: 'error', text: 'Sitzung abgelaufen – bitte erneut versuchen.' };
return res.redirect(req.get('referer') || '/');
}
next();
});
// ═══════════════════════════════════════════════════════════════════════════
// View Engine & Static Files
// ═══════════════════════════════════════════════════════════════════════════
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Statische Dateien mit Caching
app.use(express.static(path.join(__dirname, 'public'), {
maxAge: IS_PROD ? '7d' : 0,
etag: true,
lastModified: true,
}));
// ═══════════════════════════════════════════════════════════════════════════
// Globale Template-Variablen
// ═══════════════════════════════════════════════════════════════════════════
const APP_VERSION = require('./package.json').version || '1';
app.use((req, res, next) => {
res.locals.user = req.session?.user || null;
res.locals.path = req.path;
res.locals.v = APP_VERSION; // Cache-Buster für CSS/JS
res.locals.flash = req.session?.flash || null;
if (req.session?.flash) delete req.session.flash;
// BaseURL: DB-Setting > .env > auto-detect
try {
const { getDb: gdb } = require('./database');
const db = gdb();
const row = db.prepare("SELECT value FROM settings WHERE key='base_url'").get();
res.locals.baseUrl = (row && row.value) ? row.value : (process.env.BASE_URL || `${req.protocol}://${req.get('host')}`);
res.locals.companyLogo = db.prepare("SELECT value FROM settings WHERE key='company_logo'").get()?.value || null;
res.locals.companyName = db.prepare("SELECT value FROM settings WHERE key='company_name'").get()?.value || 'PDF-Freigabe';
} catch {
res.locals.baseUrl = process.env.BASE_URL || `${req.protocol}://${req.get('host')}`;
res.locals.companyLogo = null;
res.locals.companyName = 'PDF-Freigabe';
}
if (req.session?.user) {
try {
const { getDb } = require('./database');
res.locals.notificationCount = getDb().prepare(
'SELECT COUNT(*) AS c FROM notifications WHERE user_id = ? AND read = 0'
).get(req.session.user.id)?.c || 0;
} catch { res.locals.notificationCount = 0; }
} else {
res.locals.notificationCount = 0;
}
next();
});
// ═══════════════════════════════════════════════════════════════════════════
// Routen (mit Rate Limiters)
// ═══════════════════════════════════════════════════════════════════════════
// Datenschutz (öffentlich, kein Login)
app.get('/datenschutz', (req, res) => {
try {
const { getDb: gdb } = require('./database');
const db = gdb();
const text = db.prepare("SELECT value FROM settings WHERE key='privacy_policy'").get()?.value || '';
const companyName = db.prepare("SELECT value FROM settings WHERE key='company_name'").get()?.value || 'Unternehmen';
const companyLogo = db.prepare("SELECT value FROM settings WHERE key='company_logo'").get()?.value || null;
const primaryColor = db.prepare("SELECT value FROM settings WHERE key='primary_color'").get()?.value || '#4361ee';
res.render('datenschutz', { title: 'Datenschutz', privacyText: text, companyName, companyLogo, primaryColor });
} catch {
res.render('datenschutz', { title: 'Datenschutz', privacyText: '', companyName: 'Unternehmen', companyLogo: null, primaryColor: '#4361ee' });
}
});
// Impressum (öffentlich, kein Login)
app.get('/impressum', (req, res) => {
try {
const { getDb: gdb } = require('./database');
const db = gdb();
const text = db.prepare("SELECT value FROM settings WHERE key='imprint'").get()?.value || '';
const companyName = db.prepare("SELECT value FROM settings WHERE key='company_name'").get()?.value || 'Unternehmen';
const companyLogo = db.prepare("SELECT value FROM settings WHERE key='company_logo'").get()?.value || null;
const primaryColor = db.prepare("SELECT value FROM settings WHERE key='primary_color'").get()?.value || '#4361ee';
res.render('impressum', { title: 'Impressum', imprintText: text, companyName, companyLogo, primaryColor });
} catch {
res.render('impressum', { title: 'Impressum', imprintText: '', companyName: 'Unternehmen', companyLogo: null, primaryColor: '#4361ee' });
}
});
app.post('/login', loginLimiter); // Rate Limit VOR dem Auth-Router
app.use('/', require('./routes/auth'));
app.use('/dashboard', require('./routes/dashboard'));
app.use('/jobs', require('./routes/jobs'));
app.use('/approve', approveLimiter, require('./routes/approve'));
app.use('/admin', require('./routes/admin'));
app.use('/api', require('./routes/api'));
app.use('/webhooks', require('./routes/webhooks'));
app.use('/portal', require('./routes/customer_portal'));
// ═══════════════════════════════════════════════════════════════════════════
// SECURITY: 404 – keine Pfad-Informationen leaken
// ═══════════════════════════════════════════════════════════════════════════
app.use((req, res) => {
res.status(404).render('error', {
title: 'Nicht gefunden',
message: 'Die angeforderte Seite existiert nicht.',
code: 404,
});
});
// ═══════════════════════════════════════════════════════════════════════════
// SECURITY: Error Handler – keine Stack Traces in Produktion
// ═══════════════════════════════════════════════════════════════════════════
app.use((err, req, res, _next) => {
console.error('[ERROR]', IS_PROD ? err.message : err.stack);
res.status(500).render('error', {
title: 'Serverfehler',
message: IS_PROD ? 'Ein interner Fehler ist aufgetreten.' : err.message,
code: 500,
});
});
// ─── Start ──────────────────────────────────────────────────────────────────
app.listen(PORT, '0.0.0.0', () => {
console.log('');
console.log(' ╔═══════════════════════════════════════════════════╗');
console.log(` ║ PDF-Freigabetool v1.0 (${IS_PROD ? 'PRODUKTION' : 'ENTWICKLUNG'}) ║`);
console.log(` ║ Port: ${PORT} | http://0.0.0.0:${PORT} ║`);
console.log(' ╠═══════════════════════════════════════════════════╣');
console.log(` ║ Session: ${SESSION_SECRET ? '✓ Eigenes Secret' : '⚠ Standard-Secret'} ║`);
console.log(` ║ CSP: ✓ Aktiv ║`);
console.log(` ║ CSRF: ✓ Double Submit Cookie ║`);
console.log(` ║ Rate-Limit: ✓ Login, Approve, API ║`);
console.log(' ╚═══════════════════════════════════════════════════╝');
console.log('');
});
module.exports = app;