Secure Configuration
Security Headers
Section titled “Security Headers”Enable secure headers explicitly (not automatic):
app.enable_secure_headers(with_hsts=True)Headers added:
Strict-Transport-Security: max-age=31536000(withincludeSubDomains)X-Content-Type-Options: nosniffX-Frame-Options: DENYContent-Security-Policy: default-src 'self'X-XSS-Protection: 0Referrer-Policy: no-referrer
CORS Configuration
Section titled “CORS Configuration”CORS is deny-by-default. Configure explicitly:
app.add_cors( allow_origins=["https://app.example.com"], # Never use ["*"] in production allow_methods=["GET", "POST", "PUT", "DELETE"], allow_headers=["Authorization", "Content-Type"], allow_credentials=True,)Rate Limiting
Section titled “Rate Limiting”# Global rate limitapp.enable_rate_limiter(max_requests=1000, window_seconds=60)
# Per-IP rate limitapp.enable_ip_rate_limiter(max_requests=100, window_seconds=60)Request Body Size
Section titled “Request Body Size”app.run("0.0.0.0:8000", max_body_size=1024 * 1024) # 1 MiBSecrets Management
Section titled “Secrets Management”# Never hardcode secrets# Use environment variables:import osDATABASE_URL = os.environ["DATABASE_URL"]JWT_SECRET = os.environ["JWT_SECRET"]TLS Configuration
Section titled “TLS Configuration”Recommended: terminate TLS at the edge proxy. If using native TLS:
# Server configured with rustls (TLS 1.2/1.3 only)app.run_tls( "0.0.0.0:8443", cert_path="/etc/certs/cert.pem", key_path="/etc/certs/key.pem",)Security Checklist
Section titled “Security Checklist”- Secure headers enabled
- CORS restricted to specific origins
- Rate limiting configured
- Body size limits set
- TLS termination configured (proxy or native)
- Secrets in environment variables, not code
- CI/CD: cargo audit, cargo deny passing
- Logging: no PII in request bodies
- Non-root user in Docker
- Regular dependency updates via Dependabot
See Also
Section titled “See Also”- Security Policy — Vulnerability reporting
- OWASP Compliance — Compliance checklist
- Penetration Testing — Pentest guidelines