"""Security headers middleware.""" from starlette.middleware.base import BaseHTTPMiddleware from starlette.requests import Request from starlette.responses import Response class SecurityHeadersMiddleware(BaseHTTPMiddleware): """Add standard security headers to every response.""" async def dispatch(self, request: Request, call_next) -> Response: # type: ignore[no-untyped-def] response = await call_next(request) response.headers["X-Content-Type-Options"] = "nosniff" response.headers["X-Frame-Options"] = "DENY" response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin" response.headers["Content-Security-Policy"] = ( "default-src 'self'; " "script-src 'self'; " "style-src 'self' 'unsafe-inline'; " "connect-src 'self' https://*.mapbox.com; " "img-src 'self' data: https://*.mapbox.com https://media.rightmove.co.uk; " "frame-ancestors 'none'" ) # Only add HSTS when behind TLS-terminating proxy if request.headers.get("x-forwarded-proto") == "https": response.headers["Strict-Transport-Security"] = ( "max-age=63072000; includeSubDomains" ) return response