Skip to content
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
4 changes: 2 additions & 2 deletions flask_cors/extension.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from urllib.parse import unquote_plus
from urllib.parse import unquote

from flask import request

Expand Down Expand Up @@ -188,7 +188,7 @@ def cors_after_request(resp):
if resp.headers is not None and resp.headers.get(ACL_ORIGIN):
LOG.debug("CORS have been already evaluated, skipping")
return resp
normalized_path = unquote_plus(request.path)
normalized_path = unquote(request.path)
for res_regex, res_options in resources:
if try_match(normalized_path, res_regex):
LOG.debug(
Expand Down
56 changes: 56 additions & 0 deletions tests/extension/test_app_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,61 @@ def index():
self.assertEqual(resp.status_code, 200)


class AppExtensionPlusInPath(FlaskCorsTestCase):
'''
Regression test for CVE-2024-6844:
Ensures that we correctly differentiate '+' from ' ' in URL paths.
'''

def setUp(self):
self.app = Flask(__name__)
CORS(self.app, resources={
r'/service\+path': {'origins': ['http://foo.com']},
r'/service path': {'origins': ['http://bar.com']},
})

@self.app.route('/service+path')
def plus_path():
return 'plus'

@self.app.route('/service path')
def space_path():
return 'space'

self.client = self.app.test_client()

def test_plus_path_origin_allowed(self):
'''
Ensure that CORS matches + literally and allows the correct origin
'''
response = self.client.get('/service+path', headers={'Origin': 'http://foo.com'})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers.get(ACL_ORIGIN), 'http://foo.com')

def test_space_path_origin_allowed(self):
'''
Ensure that CORS treats /service path differently and allows correct origin
'''
response = self.client.get('/service%20path', headers={'Origin': 'http://bar.com'})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers.get(ACL_ORIGIN), 'http://bar.com')

def test_plus_path_rejects_other_origin(self):
'''
Origin not allowed for + path should be rejected
'''
response = self.client.get('/service+path', headers={'Origin': 'http://bar.com'})
self.assertEqual(response.status_code, 200)
self.assertIsNone(response.headers.get(ACL_ORIGIN))

def test_space_path_rejects_other_origin(self):
'''
Origin not allowed for space path should be rejected
'''
response = self.client.get('/service%20path', headers={'Origin': 'http://foo.com'})
self.assertEqual(response.status_code, 200)
self.assertIsNone(response.headers.get(ACL_ORIGIN))


if __name__ == "__main__":
unittest.main()