Skip to content

Commit

Permalink
Fixes regression where regular expressions containing a '?' are not p…
Browse files Browse the repository at this point in the history
…roperly matched (#184)
  • Loading branch information
corydolphin committed Aug 31, 2016
1 parent c848d9c commit 3b80bc3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion flask_cors/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ def probably_regex(maybe_regex):
if isinstance(maybe_regex, RegexObject):
return True
else:
common_regex_chars = ['*','\\',']', '?']
# Use common characters used in regular expressions as a proxy
# for if this string is in fact a regex.
return any((c in maybe_regex for c in ['*','\\',']']))
return any((c in maybe_regex for c in common_regex_chars))

def re_fix(reg):
"""
Expand Down
1 change: 1 addition & 0 deletions tests/core/helper_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ def test_probably_regex(self):
self.assertFalse(probably_regex("http://example.com"))
self.assertTrue(probably_regex("http://[\w].example.com"))
self.assertTrue(probably_regex("http://\w+.example.com"))
self.assertTrue(probably_regex("https?://example.com"))
11 changes: 11 additions & 0 deletions tests/decorator/test_origins.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def test_regex_list():
def test_regex_mixed_list():
return ''

@self.app.route('/test_multiple_protocols')
@cross_origin(origins="https?://example.com")
def test_multiple_protocols():
return ''

def test_defaults_no_origin(self):
''' If there is no Origin header in the request, the
Access-Control-Allow-Origin header should be '*' by default.
Expand Down Expand Up @@ -188,6 +193,12 @@ def test_regex_mixed_list(self):
self.assertEquals("http://example.com",
self.get('/test_regex_mixed_list', origin='http://example.com').headers.get(ACL_ORIGIN))

def test_multiple_protocols(self):
import logging
logging.getLogger('flask_cors').level = logging.DEBUG
resp = self.get('test_multiple_protocols', origin='https://example.com')
self.assertEqual('https://example.com', resp.headers.get(ACL_ORIGIN))


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

0 comments on commit 3b80bc3

Please sign in to comment.