Skip to content

Commit 5a9c9b4

Browse files
cheddarfoxclaude
andcommitted
fix(security): restrict CORS and add security response headers [REN-74]
Replace wildcard CORS allow_methods/allow_headers with explicit lists to reduce attack surface (OWASP A05). Add SecurityHeadersMiddleware with X-Content-Type-Options, X-Frame-Options, X-XSS-Protection, Referrer-Policy, and Permissions-Policy on all responses. HSTS is enabled only in production. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ce11c90 commit 5a9c9b4

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎core/config.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class AppSettings(BaseSettings):
7171

7272
# CORS
7373
cors_origins: list[str] = ["http://localhost:3000", "http://localhost:8000"]
74+
cors_allow_methods: list[str] = ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
75+
cors_allow_headers: list[str] = ["Authorization", "Content-Type", "X-Request-ID"]
7476

7577
@property
7678
def is_production(self) -> bool:

‎core/main.py‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
from contextlib import asynccontextmanager
2323

2424
import structlog
25-
from fastapi import FastAPI
25+
from fastapi import FastAPI, Request, Response
2626
from fastapi.middleware.cors import CORSMiddleware
27+
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
2728

2829
from core.api.v1.router import api_v1_router
2930
from core.config import get_settings
@@ -68,6 +69,26 @@ async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
6869
await engine.dispose()
6970

7071

72+
class SecurityHeadersMiddleware(BaseHTTPMiddleware):
73+
"""Add OWASP-recommended security headers to all responses."""
74+
75+
async def dispatch(
76+
self, request: Request, call_next: RequestResponseEndpoint
77+
) -> Response:
78+
response = await call_next(request)
79+
response.headers["X-Content-Type-Options"] = "nosniff"
80+
response.headers["X-Frame-Options"] = "DENY"
81+
response.headers["X-XSS-Protection"] = "1; mode=block"
82+
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
83+
response.headers["Permissions-Policy"] = "camera=(), microphone=(), geolocation=()"
84+
# HSTS only in production (requires HTTPS)
85+
if get_settings().is_production:
86+
response.headers["Strict-Transport-Security"] = (
87+
"max-age=31536000; includeSubDomains"
88+
)
89+
return response
90+
91+
7192
def create_app() -> FastAPI:
7293
"""Create and configure the FastAPI application.
7394
@@ -90,10 +111,13 @@ def create_app() -> FastAPI:
90111
CORSMiddleware,
91112
allow_origins=settings.cors_origins,
92113
allow_credentials=True,
93-
allow_methods=["*"],
94-
allow_headers=["*"],
114+
allow_methods=settings.cors_allow_methods,
115+
allow_headers=settings.cors_allow_headers,
95116
)
96117

118+
# Security headers middleware (wraps CORS so it runs on all responses)
119+
application.add_middleware(SecurityHeadersMiddleware)
120+
97121
# Include API v1 routes
98122
application.include_router(api_v1_router)
99123

0 commit comments

Comments
 (0)