Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusting CORS logic to return the first match instead of sent Origin #3471

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions kinto/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,9 @@ def reapply_cors(request, response):
settings = request.registry.settings
allowed_origins = set(aslist(settings["cors_origins"]))
required_origins = {"*", origin}
if allowed_origins.intersection(required_origins):
response.headers["Access-Control-Allow-Origin"] = origin
matches = allowed_origins.intersection(required_origins)
if matches:
response.headers["Access-Control-Allow-Origin"] = matches.pop()

# Import service here because kinto.core import utils
from kinto.core import Service
Expand Down
9 changes: 8 additions & 1 deletion tests/core/resource/test_views_cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,16 @@ def test_present_on_deletion(self):
response = self.app.delete(self.get_item_url(), headers=self.headers)
self.assertIn("Access-Control-Allow-Origin", response.headers)

def test_present_on_specified_domain(self):
with mock.patch.dict(
self.app.app.registry.settings, [("cors_origins", ["foo.bar", "notmyidea.org"])]
):
response = self.app.get("/unknown", headers=self.headers, status=404)
self.assertEqual(response.headers["Access-Control-Allow-Origin"], "notmyidea.org")

def test_present_on_unknown_url(self):
response = self.app.get("/unknown", headers=self.headers, status=404)
self.assertEqual(response.headers["Access-Control-Allow-Origin"], "notmyidea.org")
self.assertEqual(response.headers["Access-Control-Allow-Origin"], "*")

def test_not_present_on_unknown_url_if_setting_does_not_match(self):
with mock.patch.dict(self.app.app.registry.settings, [("cors_origins", "daybed.io")]):
Expand Down
Loading