Add CSP for HTML5 responses
CI / python (push) Has been cancelled
CI / rust (push) Has been cancelled

This commit is contained in:
2026-05-17 12:45:54 +03:00
parent 2e6fee5fc7
commit faf1bbd10a
2 changed files with 23 additions and 1 deletions
@@ -6,6 +6,15 @@ from fastapi.responses import Response, StreamingResponse
from fastapi.staticfiles import StaticFiles
HTML5_SECURITY_HEADERS = {"X-Content-Type-Options": "nosniff"}
HTML5_CONTENT_SECURITY_POLICY = (
"default-src 'self'; "
"script-src 'self'; "
"style-src 'self'; "
"connect-src 'self'; "
"img-src 'self' data:; "
"base-uri 'self'; "
"form-action 'self'"
)
class Html5StaticFiles(StaticFiles):
@@ -29,7 +38,11 @@ def html5_response(fragment: str) -> Response:
return Response(
fragment,
media_type="text/html; charset=utf-8",
headers={"Cache-Control": "no-cache, no-transform", **HTML5_SECURITY_HEADERS},
headers={
"Cache-Control": "no-cache, no-transform",
"Content-Security-Policy": HTML5_CONTENT_SECURITY_POLICY,
**HTML5_SECURITY_HEADERS,
},
)