CORSMiddleware rejects empty elements in Access-Control-Request-Headers
CORSMiddleware rejects empty elements in Access-Control-Request-Headers
Description
CORSMiddleware may reject a preflight request when the Access-Control-Request-Headers field contains empty list elements, even when every non-empty header name is explicitly allowed.
For example, with:
allow_headers=["x-token", "authorization"]
the following request header results in a 400 Disallowed CORS headers response:
Access-Control-Request-Headers: x-token, authorization
The same occurs with an empty element between values:
Access-Control-Request-Headers: x-token, , authorization
Expected behavior
RFC 9110 Section 5.6.1.2 specifies that recipients parsing comma-separated lists must ignore a reasonable number of empty list elements.
Therefore, empty elements should not cause an otherwise valid preflight request to be rejected, provided all non-empty header names are permitted by the configured CORS policy.
Actual behavior
In starlette/middleware/cors.py, CORSMiddleware.preflight_response() splits the requested headers on commas and checks each stripped, lowercased element against self.allow_headers.
An empty element becomes an empty string (""), which is not in the configured allowlist. Consequently, the middleware adds "headers" to the failures list and returns HTTP 400.
Reproduction
from starlette.middleware.cors import CORSMiddleware
app = CORSMiddleware(
app=lambda scope, receive, send: None,
allow_origins=["https://example.com"],
allow_methods=["GET"],
allow_headers=["x-token", "authorization"],
)
Send a preflight request with:
OPTIONS /
Origin: https://example.com
Access-Control-Request-Method: GET
Access-Control-Request-Headers: x-token, authorization
The request is rejected with HTTP 400.
Proposed solution
Normalize and validate non-empty header elements while ignoring a reasonable number of empty list elements, consistent with RFC 9110 Section 5.6.1.2.
A regression test should cover both trailing and intermediate empty elements.
Verification
The behavior was reproduced against the unpatched implementation. A local proposed fix was also tested, and the CORS middleware test module passed with 20 tests.
Related discussion
https://github.com/Kludex/starlette/discussions/3785
I have investigated this and reproduced the issue. As noted in RFC 9110 Section 5.6.1.2, empty list elements (trailing, leading, or intermediate) in Access-Control-Request-Headers should be ignored rather than treated as disallowed headers.
I have implemented the fix and added regression tests covering trailing, intermediate, and leading empty commas in my branch: https://github.com/prachizala/starlette/tree/fix/cors-empty-header-elements
Diff:
# starlette/middleware/cors.py
for header in (h.strip().lower() for h in requested_headers.split(",")):
if header and header not in self.allow_headers:
failures.append("headers")
break
All tests pass (19/19) with ruff clean. Direct PRs are currently restricted to collaborators on this repo, but maintainers can pull from this branch or I'd be happy to open a PR if collaborator access is granted!
I've prepared a fix for this on a branch: https://github.com/Sun-Wukhan/starlette/tree/fix/cors-empty-request-headers
It skips empty tokens in preflight_response() before the allow-list check (+2 lines in starlette/middleware/cors.py), with regression tests for trailing and intermediate empty elements, plus a guard test that a disallowed header next to an empty element still returns 400. The CORS test module passes locally (42 tests) and the new tests fail on the unpatched code.
I tried to open a PR but GitHub reports that pull request creation on this repository is limited to collaborators, so I can't submit it directly. Happy for a maintainer to pull the branch, or to open the PR if I'm given access.
Fix is ready on a branch: https://github.com/TejasLamba2006/starlette/tree/fix/cors-empty-header-elements
It filters empty tokens from Access-Control-Request-Headers in preflight_response() before the allow-list check, per RFC 9110 Section 5.6.1.2. Regression tests cover trailing, leading, intermediate, and adjacent commas. I could not open a PR because the repository limits PR creation to collaborators, so a maintainer can pull from the branch.
Hi! Just following up on this issue. Could you please take a look when you get a chance and let me know whether the reported behavior is valid or if you need any additional information or clarification?
I'd really appreciate your feedback. Thank you for your time!