Skip to content

Commit

Permalink
Deduplicate failure text in CORS preflight response (#1199)
Browse files Browse the repository at this point in the history
Co-authored-by: Jamie Hewland <jhewland@gmail.com>
  • Loading branch information
laggardkernel and JayH5 committed Jun 13, 2021
1 parent 310194e commit 15761fb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions starlette/middleware/cors.py
Expand Up @@ -129,6 +129,7 @@ def preflight_response(self, request_headers: Headers) -> Response:
for header in [h.lower() for h in requested_headers.split(",")]:
if header.strip() not in self.allow_headers:
failures.append("headers")
break

# We don't strictly need to use 400 responses here, since its up to
# the browser to enforce the CORS policy, but its more informative
Expand Down
10 changes: 10 additions & 0 deletions tests/middleware/test_cors.py
Expand Up @@ -179,6 +179,16 @@ def homepage(request):
assert response.text == "Disallowed CORS origin, method, headers"
assert "access-control-allow-origin" not in response.headers

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


def test_preflight_allows_request_origin_if_origins_wildcard_and_credentials_allowed():
app = Starlette()
Expand Down

0 comments on commit 15761fb

Please sign in to comment.