Skip to content

Commit d85803b

Browse files
authored
♻️ Simplify CORS configuration (#2429)
## AI Disclaimer Using Codex with gpt-5.6-sol. Reviewed manually.
1 parent 781b283 commit d85803b

4 files changed

Lines changed: 8 additions & 33 deletions

File tree

backend/app/core/config.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import warnings
2-
from typing import Annotated, Any, Literal, Self
2+
from typing import Literal, Self
33

44
from pydantic import (
5-
AnyUrl,
6-
BeforeValidator,
75
EmailStr,
86
HttpUrl,
97
PostgresDsn,
@@ -13,14 +11,6 @@
1311
from pydantic_settings import BaseSettings, SettingsConfigDict
1412

1513

16-
def parse_cors(v: Any) -> list[str] | str:
17-
if isinstance(v, str) and not v.startswith("["):
18-
return [i.strip() for i in v.split(",") if i.strip()]
19-
elif isinstance(v, list | str):
20-
return v
21-
raise ValueError(v)
22-
23-
2414
class Settings(BaseSettings):
2515
model_config = SettingsConfigDict(
2616
# Use top level .env file (one level above ./backend/)
@@ -35,17 +25,6 @@ class Settings(BaseSettings):
3525
FRONTEND_HOST: str = "http://localhost:5173"
3626
FASTAPI_ENV: Literal["development"] | None = None
3727

38-
BACKEND_CORS_ORIGINS: Annotated[
39-
list[AnyUrl] | str, BeforeValidator(parse_cors)
40-
] = []
41-
42-
@computed_field # type: ignore[prop-decorator]
43-
@property
44-
def all_cors_origins(self) -> list[str]:
45-
return [str(origin).rstrip("/") for origin in self.BACKEND_CORS_ORIGINS] + [
46-
self.FRONTEND_HOST
47-
]
48-
4928
PROJECT_NAME: str
5029
SENTRY_DSN: HttpUrl | None = None
5130
POSTGRES_SERVER: str

backend/app/main.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ def custom_generate_unique_id(route: APIRoute) -> str:
2424
generate_unique_id_function=custom_generate_unique_id,
2525
)
2626

27-
# Set all CORS enabled origins
28-
if settings.all_cors_origins:
29-
app.add_middleware(
30-
CORSMiddleware,
31-
allow_origins=settings.all_cors_origins,
32-
allow_credentials=True,
33-
allow_methods=["*"],
34-
allow_headers=["*"],
35-
)
27+
app.add_middleware(
28+
CORSMiddleware,
29+
allow_origins=[settings.FRONTEND_HOST],
30+
allow_credentials=True,
31+
allow_methods=["*"],
32+
allow_headers=["*"],
33+
)
3634

3735
app.include_router(api_router, prefix=settings.API_V1_STR)
3836
app.frontend("/", directory=FRONTEND_DIR)

compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ services:
5555
restart: true
5656
environment:
5757
PROJECT_NAME: ${PROJECT_NAME:?Variable not set}
58-
BACKEND_CORS_ORIGINS: ${BACKEND_CORS_ORIGINS:-}
5958
SECRET_KEY: ${SECRET_KEY:?Variable not set}
6059
FIRST_SUPERUSER: ${FIRST_SUPERUSER:?Variable not set}
6160
FIRST_SUPERUSER_PASSWORD: ${FIRST_SUPERUSER_PASSWORD:?Variable not set}

deployment.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ export FIRST_SUPERUSER_PASSWORD="$(python -c 'import secrets; print(secrets.toke
7575
You can set several other environment variables:
7676

7777
* `PROJECT_NAME`: The name of the project, used in the API for the docs and emails.
78-
* `BACKEND_CORS_ORIGINS`: A list of additional allowed CORS origins separated by commas. The frontend served by FastAPI uses the same origin and doesn't need to be added.
7978
* `FIRST_SUPERUSER`: The email of the first superuser, this superuser will be the one that can create new users.
8079
* `SMTP_HOST`: The SMTP server host to send emails, this would come from your email provider (E.g. Mailgun, Sparkpost, Sendgrid, etc).
8180
* `SMTP_USER`: The SMTP server user to send emails.

0 commit comments

Comments
 (0)