Upload app.py
Browse files
app.py
CHANGED
|
@@ -149,6 +149,27 @@ app.add_middleware(
|
|
| 149 |
|
| 150 |
# ββ Operational Guardrails Middleware ββββββββββββββββββββββββββββββββββββββββββ
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
@app.middleware("http")
|
| 153 |
async def operational_guardrails(request: Request, call_next):
|
| 154 |
settings = get_settings()
|
|
|
|
| 149 |
|
| 150 |
# ββ Operational Guardrails Middleware ββββββββββββββββββββββββββββββββββββββββββ
|
| 151 |
|
| 152 |
+
@app.middleware("http")
|
| 153 |
+
async def security_headers(request: Request, call_next):
|
| 154 |
+
"""
|
| 155 |
+
Add security headers to every response.
|
| 156 |
+
X-Content-Type-Options β prevents MIME sniffing
|
| 157 |
+
X-Frame-Options β prevents clickjacking
|
| 158 |
+
Referrer-Policy β limits referrer leakage
|
| 159 |
+
X-XSS-Protection β legacy XSS filter (belt-and-braces)
|
| 160 |
+
Permissions-Policy β disables unused browser features
|
| 161 |
+
"""
|
| 162 |
+
response = await call_next(request)
|
| 163 |
+
response.headers["X-Content-Type-Options"] = "nosniff"
|
| 164 |
+
response.headers["X-Frame-Options"] = "DENY"
|
| 165 |
+
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
|
| 166 |
+
response.headers["X-XSS-Protection"] = "1; mode=block"
|
| 167 |
+
response.headers["Permissions-Policy"] = (
|
| 168 |
+
"geolocation=(), microphone=(), camera=(), payment=()"
|
| 169 |
+
)
|
| 170 |
+
return response
|
| 171 |
+
|
| 172 |
+
|
| 173 |
@app.middleware("http")
|
| 174 |
async def operational_guardrails(request: Request, call_next):
|
| 175 |
settings = get_settings()
|