Skip to content

Commit 3f646fb

Browse files
authored
Adjusting CORS logic to return the first match instead of sent Origin (#3471)
* Adjusting reapply_cors logic to return the first match rather than the specified origin header * Adjusted unit tests
1 parent 80f4315 commit 3f646fb

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

kinto/core/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,9 @@ def reapply_cors(request, response):
262262
settings = request.registry.settings
263263
allowed_origins = set(aslist(settings["cors_origins"]))
264264
required_origins = {"*", origin}
265-
if allowed_origins.intersection(required_origins):
266-
response.headers["Access-Control-Allow-Origin"] = origin
265+
matches = allowed_origins.intersection(required_origins)
266+
if matches:
267+
response.headers["Access-Control-Allow-Origin"] = matches.pop()
267268

268269
# Import service here because kinto.core import utils
269270
from kinto.core import Service

tests/core/resource/test_views_cors.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,16 @@ def test_present_on_deletion(self):
4444
response = self.app.delete(self.get_item_url(), headers=self.headers)
4545
self.assertIn("Access-Control-Allow-Origin", response.headers)
4646

47+
def test_present_on_specified_domain(self):
48+
with mock.patch.dict(
49+
self.app.app.registry.settings, [("cors_origins", ["foo.bar", "notmyidea.org"])]
50+
):
51+
response = self.app.get("/unknown", headers=self.headers, status=404)
52+
self.assertEqual(response.headers["Access-Control-Allow-Origin"], "notmyidea.org")
53+
4754
def test_present_on_unknown_url(self):
4855
response = self.app.get("/unknown", headers=self.headers, status=404)
49-
self.assertEqual(response.headers["Access-Control-Allow-Origin"], "notmyidea.org")
56+
self.assertEqual(response.headers["Access-Control-Allow-Origin"], "*")
5057

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

0 commit comments

Comments
 (0)