fix: correct CORS allowed-methods/headers defaults#34
Merged
Conversation
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>
lesnik512
added a commit
to modern-python/fastapi-sqlalchemy-template
that referenced
this pull request
Jun 26, 2026
The CORS defaults already ship correct (`["*"]`), but nothing guarded the preflight contract. Add a regression test that sends an `OPTIONS /api/decks/` preflight from the shipped dev origin (`http://localhost:5173`) and asserts `POST`/`PUT` appear in `Access-Control-Allow-Methods`, exercising the real settings → lite-bootstrap → Starlette CORS middleware wiring. Ported from modern-python/litestar-sqlalchemy-template#34. Verified it fails (preflight 400, POST absent) if the methods default regresses to `[""]`. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
settings.pydefaulted the CORS lists to a single empty string:[""]is neither "allow all" (["*"]) nor empty ([]). It flows throughlite-bootstrapinto Litestar'sCORSConfig, which on preflight produces:is_allow_all_methodsis"*" in [""]→False, but the list is non-empty, so Litestar emits", ".join({""})→ the empty string.Impact
A browser SPA at the configured dev origin
http://localhost:5173doing any preflighted request — everyPOST/PUTwrite endpoint withContent-Type: application/json— receives an emptyAccess-Control-Allow-Methodsand the browser blocks the request. The template's default CORS was broken for the exact origin it ships with. (allowed_headerswas functionally OK —Content-Typeis covered by Litestar's defaults — but emitted a stray empty token.)Fix
Permissive dev defaults; tighten via env in prod.
Test (TDD — failing first)
tests/test_cors.pysends a preflightOPTIONS /api/decks/(Origin: localhost:5173,Access-Control-Request-Method: POST) through theclientfixture and assertsPOST/PUTappear inAccess-Control-Allow-Methods— exercising the real settings → bootstrap → middleware wiring.''→POSTabsent → fails.22 passed, 100% coverage.
🤖 Generated with Claude Code