File size: 985 Bytes
ec17af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from collections.abc import Callable

from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response

from app.config import settings


class SecurityHeadersMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next: Callable) -> Response:
        response = await call_next(request)
        response.headers["X-Content-Type-Options"] = "nosniff"
        response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
        if settings.environment.lower() == "production":
            response.headers["X-Frame-Options"] = "SAMEORIGIN"
            response.headers["Permissions-Policy"] = "camera=(self), microphone=(self), geolocation=()"
            response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
        else:
            response.headers["Permissions-Policy"] = "camera=*, microphone=*, geolocation=()"
        return response