Skip to content

Commit

Permalink
Add Vary header to preflight response if allow_credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Wilson committed Dec 11, 2020
1 parent bceab59 commit f3ab6a6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions starlette/middleware/cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def __init__(
preflight_headers = {}
if "*" in allow_origins:
preflight_headers["Access-Control-Allow-Origin"] = "*"
if allow_credentials:
preflight_headers["Vary"] = "Origin"
else:
preflight_headers["Vary"] = "Origin"
preflight_headers.update(
Expand Down
3 changes: 3 additions & 0 deletions tests/middleware/test_cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def homepage(request):
assert response.headers["access-control-allow-origin"] == "https://example.org"
assert response.headers["access-control-allow-headers"] == "X-Example"
assert response.headers["access-control-allow-credentials"] == "true"
assert response.headers["vary"] == "Origin"

# Test standard response
headers = {"Origin": "https://example.org"}
Expand Down Expand Up @@ -89,6 +90,7 @@ def homepage(request):
assert response.headers["access-control-allow-origin"] == "*"
assert response.headers["access-control-allow-headers"] == "X-Example"
assert "access-control-allow-credentials" not in response.headers
assert "vary" not in response.headers

# Test standard response
headers = {"Origin": "https://example.org"}
Expand Down Expand Up @@ -206,6 +208,7 @@ def homepage(request):
assert response.status_code == 200
assert response.headers["access-control-allow-origin"] == "https://example.org"
assert response.headers["access-control-allow-credentials"] == "true"
assert response.headers["vary"] == "Origin"


def test_cors_allow_origin_regex():
Expand Down

0 comments on commit f3ab6a6

Please sign in to comment.