Skip to content

Commit

Permalink
Use the current upstream (python3.9) authreq header parsing regex
Browse files Browse the repository at this point in the history
Fixes a DoS when parsing a malformed auth header. Reported by CodeQL.
Reference GHSL-2021-108
  • Loading branch information
kovidgoyal committed Aug 31, 2021
1 parent 1af1582 commit dd05334
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 9 additions & 2 deletions mechanize/_urllib2_fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,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"
Expand Down
6 changes: 5 additions & 1 deletion test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
HTTPCookieProcessor, HTTPRefererProcessor, \
HTTPErrorProcessor, HTTPHandler
from mechanize import OpenerDirector, build_opener, Request
from mechanize._urllib2_fork import AbstractHTTPHandler, normalize_url
from mechanize._urllib2_fork import AbstractHTTPHandler, normalize_url, AbstractBasicAuthHandler
from mechanize._util import write_file

import mechanize._response
Expand Down Expand Up @@ -69,6 +69,10 @@ def test_parse_http_list(self):
self.assertEqual(
mechanize._urllib2_fork.parse_http_list(string), list)

def test_parse_authreq(self):
for bad in (",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,",):
self.assertIsNone(AbstractBasicAuthHandler.rx.search(bad))


def test_request_headers_dict():
"""
Expand Down

0 comments on commit dd05334

Please # to comment.