bharatgraph / api /middleware /security_headers.py
abinazebinoy's picture
feat(security):complete — security hardening and provenance
b1a489c
Raw
History Blame Contribute Delete
1.33 kB
import os, sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
CSP = (
"default-src 'self'; "
"script-src 'self' https://cdnjs.cloudflare.com https://fonts.googleapis.com 'unsafe-inline'; "
"style-src 'self' https://fonts.googleapis.com 'unsafe-inline'; "
"font-src 'self' https://fonts.gstatic.com; "
"img-src 'self' data: https:; "
"connect-src 'self' https://*.hf.space wss://*.hf.space https://fonts.googleapis.com; "
"frame-ancestors 'none'; "
"base-uri 'self';"
)
HEADERS = {
"Content-Security-Policy": CSP,
"Strict-Transport-Security": "max-age=31536000; includeSubDomains",
"X-Frame-Options": "DENY",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "strict-origin-when-cross-origin",
"Permissions-Policy": "geolocation=(), microphone=(), camera=()",
"X-XSS-Protection": "1; mode=block",
}
class SecurityHeadersMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
response = await call_next(request)
for header, value in HEADERS.items():
response.headers[header] = value
return response