Files
harheimertc/server/middleware/security-headers.js
Torsten Schulz (local) 12ae192b37
Some checks failed
Code Analysis (JS/Vue) / analyze (push) Failing after 47s
Update security headers in Apache configuration to enhance protection
This commit removes the X-Frame-Options header in favor of using Content Security Policy (CSP) with frame-ancestors for better flexibility and modern security practices. It also adds a fallback for frame-ancestors in case CSP is not enabled. Additionally, the JavaScript middleware is updated to reflect these changes, ensuring consistent security header management across the application.
2026-01-11 20:59:42 +01:00

58 lines
2.2 KiB
JavaScript
Raw Permalink 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}`,
// Nuxt lädt Fonts ggf. von Google (siehe nuxt.config.js)
"font-src 'self' https://fonts.gstatic.com data:",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
// 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}`)
}
})