security: Härtung der CMS-API + Deployment
App-Level: - Security-Header (secureHeaders) global; /images/* mit strikter CSP+sandbox → bösartiges SVG kann kein JS im Origin ausführen - Body-Limit 256 KB auf /api/*; Login-Rate-Limit (10/5min) gegen Brute-Force - Upload: 8-MB-Limit + Format-Verifikation (sharp-Metadaten, SVG/GIF-Signatur) - Comment-Längenlimit (10k) gegen DB-Bloat - DB-Fehler nicht mehr roh ausliefern (serverError-Helper) - Profil-PUT koalesziert Hugo-Builds (kein Build-Sturm) Infra: - Container läuft non-root (USER node, uid 1000) + Proxmox-Repo-chown - Ports binden per Default auf 127.0.0.1 (BIND_ADDR-Escape-Hatch) - Kong-CORS auf SITE_URL beschränkt statt "*" - README: Härtungs- + Migrationshinweise Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -6,8 +6,14 @@ import sharp from 'sharp';
|
||||
// Bild-Upload → static/images/. Raster-Bilder werden zu WebP konvertiert
|
||||
// (kleiner, web-optimiert), auf max. 2000px begrenzt, EXIF-Rotation korrigiert.
|
||||
// SVG/GIF bleiben unangetastet (Vektor/Animation erhalten).
|
||||
//
|
||||
// Sicherheit: hartes Größenlimit (DoS / Decompression-Bombs), Raster wird über
|
||||
// sharp-Metadaten als echtes Bild verifiziert, SVG nur wenn es wie SVG aussieht.
|
||||
// Hochgeladene Dateien werden zudem mit strikter CSP (sandbox) ausgeliefert
|
||||
// (siehe index.js, /images/*) → ein bösartiges SVG kann kein JS im Origin starten.
|
||||
const SITE_DIR = process.env.SITE_DIR || '/site';
|
||||
const PASSTHROUGH = new Set(['.svg', '.gif']);
|
||||
const MAX_UPLOAD = 8 * 1024 * 1024; // 8 MB Rohdatei
|
||||
const ALLOWED_RASTER = new Set(['jpeg', 'png', 'webp', 'avif', 'tiff']);
|
||||
|
||||
const upload = new Hono();
|
||||
|
||||
@@ -15,19 +21,42 @@ upload.post('/', async (c) => {
|
||||
const body = await c.req.parseBody();
|
||||
const file = body['file'];
|
||||
if (!file || typeof file === 'string') return c.json({ error: 'Keine Datei' }, 400);
|
||||
if (typeof file.size === 'number' && file.size > MAX_UPLOAD) {
|
||||
return c.json({ error: 'Datei zu groß (max. 8 MB)' }, 413);
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
if (buffer.length > MAX_UPLOAD) return c.json({ error: 'Datei zu groß (max. 8 MB)' }, 413);
|
||||
if (!buffer.length) return c.json({ error: 'Leere Datei' }, 400);
|
||||
|
||||
const dir = path.join(SITE_DIR, 'static', 'images');
|
||||
await mkdir(dir, { recursive: true });
|
||||
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
const ext = path.extname(file.name || '').toLowerCase();
|
||||
const base = `${safeBase(file.name)}-${uid()}`;
|
||||
|
||||
let outName, outBuf;
|
||||
if (PASSTHROUGH.has(ext)) {
|
||||
outName = `${base}${ext}`;
|
||||
if (ext === '.svg') {
|
||||
// Muss wie SVG aussehen (Magie/Marker), sonst ablehnen.
|
||||
const head = buffer.subarray(0, 512).toString('utf8').trimStart().toLowerCase();
|
||||
if (!head.startsWith('<?xml') && !head.startsWith('<svg')) {
|
||||
return c.json({ error: 'Keine gültige SVG-Datei' }, 400);
|
||||
}
|
||||
outName = `${base}.svg`;
|
||||
outBuf = buffer;
|
||||
} else if (ext === '.gif') {
|
||||
// GIF-Magie prüfen (kann kein Skript ausführen → Passthrough ok).
|
||||
const sig = buffer.subarray(0, 6).toString('latin1');
|
||||
if (sig !== 'GIF87a' && sig !== 'GIF89a') return c.json({ error: 'Keine gültige GIF-Datei' }, 400);
|
||||
outName = `${base}.gif`;
|
||||
outBuf = buffer;
|
||||
} else {
|
||||
// Raster: über sharp als echtes Bild verifizieren, dann zu WebP.
|
||||
let meta;
|
||||
try { meta = await sharp(buffer).metadata(); } catch { meta = null; }
|
||||
if (!meta || !ALLOWED_RASTER.has(meta.format)) {
|
||||
return c.json({ error: 'Kein unterstütztes Bildformat' }, 400);
|
||||
}
|
||||
outName = `${base}.webp`;
|
||||
outBuf = await sharp(buffer)
|
||||
.rotate()
|
||||
|
||||
Reference in New Issue
Block a user