Files
ServerKit/backend/app/middleware/security.py
T
Juan Denis f677d760dd Add backup, monitoring, and deployment APIs
Introduces new API endpoints and services for backups, monitoring, and Git deployment. Adds security middleware, account lockout on failed logins, and improves domain/SSL management. Updates Dockerfiles, deployment configs, and frontend to support new features and UI components.
2026-01-17 12:08:02 -05:00

43 lines
1.5 KiB
Python

"""Security middleware for adding security headers to responses."""
from flask import Flask
def register_security_headers(app: Flask):
"""Register security headers as an after_request handler."""
@app.after_request
def add_security_headers(response):
# Prevent clickjacking
response.headers['X-Frame-Options'] = 'DENY'
# Prevent MIME type sniffing
response.headers['X-Content-Type-Options'] = 'nosniff'
# XSS Protection (legacy but still useful for older browsers)
response.headers['X-XSS-Protection'] = '1; mode=block'
# Referrer Policy
response.headers['Referrer-Policy'] = 'strict-origin-when-cross-origin'
# Content Security Policy
csp_directives = [
"default-src 'self'",
"script-src 'self' 'unsafe-inline' 'unsafe-eval'",
"style-src 'self' 'unsafe-inline'",
"img-src 'self' data: https:",
"font-src 'self'",
"connect-src 'self' ws: wss:",
"frame-ancestors 'none'",
]
response.headers['Content-Security-Policy'] = '; '.join(csp_directives)
# Permissions Policy (formerly Feature-Policy)
response.headers['Permissions-Policy'] = 'geolocation=(), microphone=(), camera=()'
# HSTS - only in production and on HTTPS
if not app.debug:
response.headers['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'
return response