Backport of 0b297d4ff1c0e4480ad33acae793fbaf4bf015b4, trimmed down to the fix for CVE-2020-8492 Co-Authored-By: Serhiy Storchaka diff --git a/Lib/urllib2.py b/Lib/urllib2.py index 8b634ad..11a62a4 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -856,8 +856,15 @@ class AbstractBasicAuthHandler: # allow for double- and single-quoted realm values # (single quotes are a violation of the RFC, but appear in the wild) - rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+' - 'realm=(["\']?)([^"\']*)\\2', re.I) + rx = re.compile('(?:^|,)' # start of the string or ',' + '[ \t]*' # optional whitespaces + '([^ \t]+)' # scheme like "Basic" + '[ \t]+' # mandatory whitespaces + # realm=xxx + # realm='xxx' + # realm="xxx" + 'realm=(["\']?)([^"\']*)\\2', + re.I) # XXX could pre-emptively send auth info already accepted (RFC 2617, # end of section 2, and section 1.2 immediately after "credentials"