Files
harheimertc/server/middleware/security-headers.js
Torsten Schulz (local) 3586704cce
Some checks failed
Code Analysis and Production Deploy / analyze (push) Failing after 10m29s
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Has been skipped
Update file permissions for multiple server and test files to executable mode
2026-07-15 08:45:12 +02:00

57 lines
2.1 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Globale Security-Header für Nitro (Nuxt 3).
*
* Apache setzt ggf. bereits Header diese Middleware dient als "Default",
* damit die App auch ohne Reverse-Proxy sauber gehärtet ist.
*
* CSP ist optional und sollte zuerst im Report-Only Modus getestet werden.
* Siehe ENV:
* - CSP_ENABLED=true|false
* - CSP_REPORT_ONLY=true|false
* - CSP_VALUE="..."
*/
export default defineEventHandler((event) => {
// Grundsätzlich sinnvolle Header
setHeader(event, 'X-Content-Type-Options', 'nosniff')
setHeader(event, 'Referrer-Policy', 'strict-origin-when-cross-origin')
setHeader(event, 'Permissions-Policy', 'geolocation=(), microphone=(), camera=()')
// X-Frame-Options entfernt - verwenden CSP frame-ancestors stattdessen
// CSP frame-ancestors ist moderner und unterstützt mehrere Domains
// Legacy-Header (optional; moderne Browser verlassen sich primär auf CSP)
setHeader(event, 'X-XSS-Protection', '0')
// Frame-Ancestors (für Einbettung von harheimertc.de erlauben)
const allowedFrameAncestors = process.env.FRAME_ANCESTORS ||
"'self' https://harheimertc.de https://www.harheimertc.de"
// Optional: Vollständige CSP
const cspEnabled = (process.env.CSP_ENABLED || '').toLowerCase() === 'true'
if (cspEnabled) {
const reportOnly = (process.env.CSP_REPORT_ONLY || 'true').toLowerCase() !== 'false'
const cspValue =
process.env.CSP_VALUE ||
[
"default-src 'self'",
"base-uri 'self'",
"object-src 'none'",
`frame-ancestors ${allowedFrameAncestors}`,
"font-src 'self' data:",
"style-src 'self' 'unsafe-inline'",
// Script: Nuxt kann in Dev eval nutzen; diese CSP ist primär für Produktion gedacht.
"script-src 'self'",
"img-src 'self' data: blob:",
"connect-src 'self'"
].join('; ')
setHeader(event, reportOnly ? 'Content-Security-Policy-Report-Only' : 'Content-Security-Policy', cspValue)
} else {
// Wenn CSP nicht aktiviert ist, setze nur frame-ancestors
setHeader(event, 'Content-Security-Policy', `frame-ancestors ${allowedFrameAncestors}`)
}
})