Skip to content

Commit 15761fb

Browse files
laggardkernelJayH5
andauthored
Deduplicate failure text in CORS preflight response (#1199)
Co-authored-by: Jamie Hewland <jhewland@gmail.com>
1 parent 310194e commit 15761fb

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

starlette/middleware/cors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def preflight_response(self, request_headers: Headers) -> Response:
129129
for header in [h.lower() for h in requested_headers.split(",")]:
130130
if header.strip() not in self.allow_headers:
131131
failures.append("headers")
132+
break
132133

133134
# We don't strictly need to use 400 responses here, since its up to
134135
# the browser to enforce the CORS policy, but its more informative

tests/middleware/test_cors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,16 @@ def homepage(request):
179179
assert response.text == "Disallowed CORS origin, method, headers"
180180
assert "access-control-allow-origin" not in response.headers
181181

182+
# Bug specific test, https://github.com/encode/starlette/pull/1199
183+
# Test preflight response text with multiple disallowed headers
184+
headers = {
185+
"Origin": "https://example.org",
186+
"Access-Control-Request-Method": "GET",
187+
"Access-Control-Request-Headers": "X-Nope-1, X-Nope-2",
188+
}
189+
response = client.options("/", headers=headers)
190+
assert response.text == "Disallowed CORS headers"
191+
182192

183193
def test_preflight_allows_request_origin_if_origins_wildcard_and_credentials_allowed():
184194
app = Starlette()

0 commit comments

Comments
 (0)