Skip to content

Commit 48238c7

Browse files
committed
Issue #2202: Fix UnboundLocalError in AbstractDigestAuthHandler.get_algorithm_impls
Raise ValueError if algorithm is not MD5 or SHA. Initial patch by Mathieu Dupuy.
2 parents 656e41e + e88dd1c commit 48238c7

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

Lib/test/test_urllib2.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
# proxy config data structure but is testable on all platforms.
1414
from urllib.request import (Request, OpenerDirector, HTTPBasicAuthHandler,
1515
HTTPPasswordMgrWithPriorAuth, _parse_proxy,
16-
_proxy_bypass_macosx_sysconf)
16+
_proxy_bypass_macosx_sysconf,
17+
AbstractDigestAuthHandler)
1718
from urllib.parse import urlparse
1819
import urllib.error
1920
import http.client
@@ -1680,6 +1681,15 @@ def test_parse_proxy(self):
16801681

16811682
self.assertRaises(ValueError, _parse_proxy, 'file:/ftp.example.com'),
16821683

1684+
def test_unsupported_algorithm(self):
1685+
handler = AbstractDigestAuthHandler()
1686+
with self.assertRaises(ValueError) as exc:
1687+
handler.get_algorithm_impls('invalid')
1688+
self.assertEqual(
1689+
str(exc.exception),
1690+
"Unsupported digest authentication algorithm 'invalid'"
1691+
)
1692+
16831693

16841694
class RequestTests(unittest.TestCase):
16851695
class PutRequest(Request):

Lib/urllib/request.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,9 @@ def get_algorithm_impls(self, algorithm):
11711171
elif algorithm == 'SHA':
11721172
H = lambda x: hashlib.sha1(x.encode("ascii")).hexdigest()
11731173
# XXX MD5-sess
1174+
else:
1175+
raise ValueError("Unsupported digest authentication "
1176+
"algorithm %r" % algorithm)
11741177
KD = lambda s, d: H("%s:%s" % (s, d))
11751178
return H, KD
11761179

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ Core and Builtins
201201
Library
202202
-------
203203

204+
- Issue #2202: Fix UnboundLocalError in
205+
AbstractDigestAuthHandler.get_algorithm_impls. Initial patch by Mathieu Dupuy.
206+
204207
- Issue #26167: Minimized overhead in copy.copy() and copy.deepcopy().
205208
Optimized copying and deepcopying bytearrays, NotImplemented, slices,
206209
short lists, tuples, dicts, sets.

0 commit comments

Comments
 (0)