@@ -121,9 +121,10 @@ def get_cors_origins(options, request_origin):
121121 if wildcard and options .get ("send_wildcard" ):
122122 LOG .debug ("Allowed origins are set to '*'. Sending wildcard CORS header." )
123123 return ["*" ]
124- # If the value of the Origin header is a case-sensitive match
125- # for any of the values in list of origins
126- elif try_match_any (request_origin , origins ):
124+ # If the value of the Origin header is a case-insensitive match
125+ # for any of the values in list of origins.
126+ # NOTE: Per RFC 1035 and RFC 4343 schemes and hostnames are case insensitive.
127+ elif try_match_any_pattern (request_origin , origins , caseSensitive = False ):
127128 LOG .debug (
128129 "The request's Origin header matches. Sending CORS headers." ,
129130 )
@@ -164,7 +165,7 @@ def get_allow_headers(options, acl_request_headers):
164165 request_headers = [h .strip () for h in acl_request_headers .split ("," )]
165166
166167 # any header that matches in the allow_headers
167- matching_headers = filter (lambda h : try_match_any (h , options .get ("allow_headers" )), request_headers )
168+ matching_headers = filter (lambda h : try_match_any_pattern (h , options .get ("allow_headers" ), caseSensitive = False ), request_headers )
168169
169170 return ", " .join (sorted (matching_headers ))
170171
@@ -277,22 +278,31 @@ def re_fix(reg):
277278 return r".*" if reg == r"*" else reg
278279
279280
280- def try_match_any (inst , patterns ):
281- return any (try_match (inst , pattern ) for pattern in patterns )
281+ def try_match_any_pattern (inst , patterns , caseSensitive = True ):
282+ return any (try_match_pattern (inst , pattern , caseSensitive ) for pattern in patterns )
282283
283-
284- def try_match (request_origin , maybe_regex ):
285- """Safely attempts to match a pattern or string to a request origin."""
286- if isinstance (maybe_regex , RegexObject ):
287- return re .match (maybe_regex , request_origin )
288- elif probably_regex (maybe_regex ):
289- return re .match (maybe_regex , request_origin , flags = re .IGNORECASE )
290- else :
284+ def try_match_pattern (value , pattern , caseSensitive = True ):
285+ """
286+ Safely attempts to match a pattern or string to a value. This
287+ function can be used to match request origins, headers, or paths.
288+ The value of caseSensitive should be set in accordance to the
289+ data being compared e.g. origins and headers are case insensitive
290+ whereas paths are case-sensitive
291+ """
292+ if isinstance (pattern , RegexObject ):
293+ return re .match (pattern , value )
294+ if probably_regex (pattern ):
295+ flags = 0 if caseSensitive else re .IGNORECASE
291296 try :
292- return request_origin .lower () == maybe_regex .lower ()
293- except AttributeError :
294- return request_origin == maybe_regex
295-
297+ return re .match (pattern , value , flags = flags )
298+ except re .error :
299+ return False
300+ try :
301+ v = str (value )
302+ p = str (pattern )
303+ return v == p if caseSensitive else v .casefold () == p .casefold ()
304+ except Exception :
305+ return value == pattern
296306
297307def get_cors_options (appInstance , * dicts ):
298308 """
0 commit comments