28ee44bcf1
Punto 12 del documento modifiche del 28/07. L'installazione a Trafilo e' on-premise su rete di produzione isolata: finche' Alpine.js, Plotly, PDF.js, Fabric.js e i Google Fonts venivano scaricati da CDN a ogni apertura di pagina, senza internet l'interfaccia non partiva affatto (Alpine governa tutta la UI). Le librerie passano in static/vendor/ con la versione nel nome del file, e la CSP del backend si chiude a solo-origine. Il worker di PDF.js e' ripuntato in tutti e quattro i template che lo usano: ripuntando solo lo script principale la libreria si carica in locale ma il worker continua a cercare internet, e sembra funzionare finche' non si apre un disegno. Alpine e' pinnato a 3.15.12, cioe' la versione a cui "3.x.x" risolveva oggi: il congelamento non cambia il comportamento di quanto e' gia' in esercizio, e toglie il fatto che l'applicazione cambiasse da sola a ogni rilascio degli autori - rilevante per le evidenze ISO 9001 / IATF 16949. Inter e JetBrains Mono sono font variabili: un solo woff2 per subset copre tutti i pesi, dove Google serviva lo stesso file sotto otto URL. Tenuti i soli subset latin e latin-ext, che coprono per intero italiano e inglese. Provenienza, versioni e impronte SHA-256 in static/vendor/VERSIONS.md. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
"""Security headers middleware for FastAPI.
|
|
|
|
Adds standard security headers to every HTTP response to mitigate
|
|
common web vulnerabilities (clickjacking, XSS, MIME sniffing, etc.).
|
|
"""
|
|
from typing import Callable
|
|
|
|
from fastapi import Request, Response
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
|
|
from src.backend.config import settings
|
|
|
|
# Content Security Policy - same-origin only: every third-party library now ships with
|
|
# the app (see frontend static/vendor/), so no CDN host needs allowing. The install runs
|
|
# on an isolated shop-floor network where an outbound fetch would simply fail.
|
|
# Note: 'unsafe-eval' required for Plotly.js runtime evaluation in SPC charts
|
|
CSP = (
|
|
"default-src 'self'; "
|
|
"script-src 'self' 'unsafe-inline' 'unsafe-eval'; "
|
|
"style-src 'self' 'unsafe-inline'; "
|
|
"font-src 'self'; "
|
|
"img-src 'self' data: blob:; "
|
|
"connect-src 'self'; "
|
|
"worker-src 'self' blob:"
|
|
)
|
|
|
|
|
|
class SecurityHeadersMiddleware(BaseHTTPMiddleware):
|
|
"""Middleware that injects security headers into every response."""
|
|
|
|
async def dispatch(self, request: Request, call_next: Callable) -> Response:
|
|
response = await call_next(request)
|
|
|
|
response.headers["X-Content-Type-Options"] = "nosniff"
|
|
response.headers["X-Frame-Options"] = "DENY"
|
|
response.headers["X-XSS-Protection"] = "1; mode=block"
|
|
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
|
|
response.headers["Content-Security-Policy"] = CSP
|
|
|
|
# Add HSTS header only when running with HTTPS (SSL configured)
|
|
if settings.ssl_certfile and settings.ssl_keyfile:
|
|
response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
|
|
|
|
return response
|