Spaces:
Configuration error
Configuration error
Niranjan-Murugarasu024
feat: TradeSense AI Chairman Edition production release ready for Hugging Face Spaces
0125724 Download backend/security/headers.py from BlueMen/tradesenseAi: direct link, hf CLI and curl.
- Browser
- Download file 1.54 kB
-
https://huggingface.co/spaces/BlueMen/tradesenseAi/resolve/main/backend/security/headers.py
- Command line
-
hf download hf://spaces/BlueMen/tradesenseAi/backend/security/headers.py
-
curl -L -o headers.py https://huggingface.co/spaces/BlueMen/tradesenseAi/resolve/main/backend/security/headers.py
1.54 kB
| """ | |
| TradeSense AI — Enterprise Security Headers & Sanitization Middleware | |
| """ | |
| from starlette.middleware.base import BaseHTTPMiddleware | |
| from starlette.requests import Request | |
| from starlette.responses import Response | |
| class SecurityHeadersMiddleware(BaseHTTPMiddleware): | |
| """ | |
| Appends hardened HTTP security headers to every response. | |
| """ | |
| async def dispatch(self, request: Request, call_next): | |
| response: Response = await call_next(request) | |
| # 1. Content Security Policy | |
| response.headers["Content-Security-Policy"] = ( | |
| "default-src 'self'; " | |
| "script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net https://cdnjs.cloudflare.com; " | |
| "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://cdn.jsdelivr.net; " | |
| "font-src 'self' https://fonts.gstatic.com; " | |
| "img-src 'self' data:; " | |
| "connect-src 'self';" | |
| ) | |
| # 2. X-Content-Type-Options (prevent MIME sniffing) | |
| response.headers["X-Content-Type-Options"] = "nosniff" | |
| # 3. X-Frame-Options (prevent clickjacking) | |
| response.headers["X-Frame-Options"] = "DENY" | |
| # 4. X-XSS-Protection | |
| response.headers["X-XSS-Protection"] = "1; mode=block" | |
| # 5. Strict-Transport-Security (HSTS) | |
| response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains" | |
| # 6. Referrer-Policy | |
| response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin" | |
| return response | |