Skip to content

Commit 14f7e3e

Browse files
lesnik512claude
andauthored
fix: correct CORS allowed-methods/headers defaults (#34)
cors_allowed_methods and cors_allowed_headers defaulted to [""], a list holding one empty string. That is not "allow all" (which needs "*") nor empty — it made litestar's CORSConfig emit an empty Access-Control-Allow-Methods on preflight, so a browser at the configured dev origin (localhost:5173) had every preflighted write (POST/PUT with JSON) blocked. allowed-headers likewise emitted a stray leading-comma token. Default both to ["*"] (permissive dev defaults; tighten via env in prod). Add a preflight integration test asserting POST/PUT appear in Access-Control-Allow-Methods through the real settings -> bootstrap -> middleware path; it fails on the old [""] default. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent c991b8a commit 14f7e3e

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎app/settings.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class Settings(pydantic_settings.BaseSettings):
2424
swagger_offline_docs: bool = True
2525

2626
cors_allowed_origins: list[str] = ["http://localhost:5173"]
27-
cors_allowed_methods: list[str] = [""]
28-
cors_allowed_headers: list[str] = [""]
27+
cors_allowed_methods: list[str] = ["*"]
28+
cors_allowed_headers: list[str] = ["*"]
2929
cors_exposed_headers: list[str] = []
3030

3131
request_max_body_size: int = 1024 * 1024 # 1MB limit

‎tests/test_cors.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import TYPE_CHECKING
2+
3+
4+
if TYPE_CHECKING:
5+
from httpx import AsyncClient
6+
7+
8+
async def test_cors_preflight_allows_write_methods(client: AsyncClient) -> None:
9+
response = await client.options(
10+
"/api/decks/",
11+
headers={
12+
"Origin": "http://localhost:5173",
13+
"Access-Control-Request-Method": "POST",
14+
},
15+
)
16+
17+
allow_methods = response.headers.get("access-control-allow-methods", "")
18+
assert "POST" in allow_methods
19+
assert "PUT" in allow_methods

0 commit comments

Comments
 (0)