syncova-ticket/server/runtime.js
Weapie 0e8be73fec
All checks were successful
Tests und Docker-Image / Tests und Frontend-Build (push) Successful in 44s
Tests und Docker-Image / Leeres Produktionsimage bauen und veröffentlichen (push) Successful in 2m0s
Document Syncova with demo screenshots and harden production deployment
2026-11-03 18:10:13 +01:00

51 lines
1.9 KiB
JavaScript

import { readFileSync } from 'node:fs'
export function loadRuntimeSecrets(env = process.env) {
for (const name of ['ADMIN_PASSWORD', 'SMTP_ENCRYPTION_KEY']) {
const file = env[`${name}_FILE`]
if (!file) continue
if (env[name]) throw Error(`${name} und ${name}_FILE dürfen nicht gleichzeitig gesetzt sein.`)
let value
try {
value = readFileSync(file, 'utf8').replace(/\r?\n$/, '')
} catch {
throw Error(`${name}_FILE kann nicht gelesen werden.`)
}
if (!value || value.length > 4096) throw Error(`${name}_FILE ist leer oder zu groß.`)
env[name] = value
}
}
export function proxyHops(env = process.env) {
const raw = env.TRUST_PROXY ?? (env.NODE_ENV === 'production' ? '1' : '0')
if (!/^[0-3]$/.test(raw))
throw Error('TRUST_PROXY muss die Anzahl vertrauenswürdiger Proxy-Hops sein (0 bis 3).')
return Number(raw)
}
export function validateRuntime(env = process.env) {
proxyHops(env)
if (env.SMTP_ENCRYPTION_KEY && !/^[a-f0-9]{64}$/i.test(env.SMTP_ENCRYPTION_KEY))
throw Error('SMTP_ENCRYPTION_KEY muss 32 Byte in Hex enthalten.')
if (env.NODE_ENV !== 'production') return
let origin
try {
origin = new URL(env.APP_ORIGIN)
} catch {
throw Error('APP_ORIGIN muss die öffentliche HTTPS-Adresse sein.')
}
if (
origin.protocol !== 'https:' ||
origin.username ||
origin.password ||
origin.pathname !== '/' ||
origin.search ||
origin.hash
)
throw Error('APP_ORIGIN muss eine HTTPS-Origin ohne Pfad, Anmeldedaten oder Query sein.')
if (env.APP_ORIGIN !== origin.origin)
throw Error('APP_ORIGIN ohne abschließenden Schrägstrich angeben.')
if (env.COOKIE_SECURE !== 'true')
throw Error('COOKIE_SECURE=true ist im Produktionsmodus erforderlich.')
if (env.MAIL_MODE && !['disabled', 'smtp'].includes(env.MAIL_MODE))
throw Error('In Produktion ist nur MAIL_MODE=disabled oder smtp erlaubt.')
}