Skip to content

Commit a844db9

Browse files
Add CORS and TrustedHost Middleware to FastAPI app
- Configure `TrustedHostMiddleware` with `ALLOWED_HOSTS`. - Configure `CORSMiddleware` with `ALLOWED_ORIGINS`. - Ensure `allow_credentials=False` if `ALLOWED_ORIGINS` is `*`. - Add test case verifying middleware presence. Co-authored-by: lgcorzo <46710567+lgcorzo@users.noreply.github.com>
1 parent ab9e880 commit a844db9

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

.jules/sentinel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@
1212
**Vulnerability:** The application was catching exceptions in logic callbacks and Kafka consumers, then assigning the raw exception string to a JSON `error` field in the successful response object. This leaked internal details even when the HTTP status code was 200 OK or when processing asynchronously via Kafka.
1313
**Learning:** Checking for HTTP 500 handlers is not enough. Review application-level error handling where business logic manually constructs error objects.
1414
**Prevention:** Ensure that any `result["error"]` or similar fields populated in catch blocks use generic messages, while the real exception is logged server-side.
15+
16+
## 2026-06-18 - Insecure Default CORS Configuration
17+
**Vulnerability:** The application was missing explicit CORS and TrustedHost middleware, potentially exposing it to Host Header Injection and unauthorized cross-origin access.
18+
**Learning:** Defaulting to `allow_credentials=True` with `allow_origins=["*"]` is a common mistake that browsers reject. Safe defaults must ensure credentials are disabled if wildcard origins are used.
19+
**Prevention:** Implement conditional logic to force `allow_credentials=False` when `ALLOWED_ORIGINS` contains `*`, ensuring the application remains functional and spec-compliant by default.

src/regression_model_template/controller/kafka_app.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import uvicorn
1313
import pandas as pd
1414
from fastapi import FastAPI, HTTPException
15+
from fastapi.middleware.cors import CORSMiddleware
16+
from fastapi.middleware.trustedhost import TrustedHostMiddleware
1517
from pydantic import BaseModel
1618

1719
from confluent_kafka import Producer, Consumer, KafkaError, Message
@@ -43,6 +45,27 @@
4345
version="1.0.0",
4446
)
4547

48+
# Security Enhancements
49+
ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "*").split(",")
50+
ALLOWED_ORIGINS = os.getenv("ALLOWED_ORIGINS", "*").split(",")
51+
52+
app.add_middleware(
53+
TrustedHostMiddleware, allowed_hosts=ALLOWED_HOSTS
54+
)
55+
56+
# Browsers reject CORS if origins is '*' and credentials=True
57+
allow_credentials = True
58+
if "*" in ALLOWED_ORIGINS:
59+
allow_credentials = False
60+
61+
app.add_middleware(
62+
CORSMiddleware,
63+
allow_origins=ALLOWED_ORIGINS,
64+
allow_credentials=allow_credentials,
65+
allow_methods=["*"],
66+
allow_headers=["*"],
67+
)
68+
4669

4770
# Data Models
4871
class PredictionRequest(BaseModel):
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from fastapi.middleware.cors import CORSMiddleware
2+
from fastapi.middleware.trustedhost import TrustedHostMiddleware
3+
from regression_model_template.controller.kafka_app import app
4+
5+
def test_middleware_configuration():
6+
middleware_types = [m.cls for m in app.user_middleware]
7+
assert TrustedHostMiddleware in middleware_types, "TrustedHostMiddleware missing"
8+
assert CORSMiddleware in middleware_types, "CORSMiddleware missing"

0 commit comments

Comments
 (0)