changeset: 100431:046c75c9cd33 parent: 100429:a8589d88deb4 parent: 100430:22eab50cb585 user: Berker Peksag date: Sun Mar 06 16:17:47 2016 +0200 files: Lib/urllib/request.py Misc/NEWS description: Issue #2202: Fix UnboundLocalError in AbstractDigestAuthHandler.get_algorithm_impls Raise ValueError if algorithm is not MD5 or SHA. Initial patch by Mathieu Dupuy. diff -r a8589d88deb4 -r 046c75c9cd33 Lib/test/test_urllib2.py --- a/Lib/test/test_urllib2.py Sun Mar 06 15:03:47 2016 +0200 +++ b/Lib/test/test_urllib2.py Sun Mar 06 16:17:47 2016 +0200 @@ -13,7 +13,8 @@ # proxy config data structure but is testable on all platforms. from urllib.request import (Request, OpenerDirector, HTTPBasicAuthHandler, HTTPPasswordMgrWithPriorAuth, _parse_proxy, - _proxy_bypass_macosx_sysconf) + _proxy_bypass_macosx_sysconf, + AbstractDigestAuthHandler) from urllib.parse import urlparse import urllib.error import http.client @@ -1680,6 +1681,15 @@ self.assertRaises(ValueError, _parse_proxy, 'file:/ftp.example.com'), + def test_unsupported_algorithm(self): + handler = AbstractDigestAuthHandler() + with self.assertRaises(ValueError) as exc: + handler.get_algorithm_impls('invalid') + self.assertEqual( + str(exc.exception), + "Unsupported digest authentication algorithm 'invalid'" + ) + class RequestTests(unittest.TestCase): class PutRequest(Request): diff -r a8589d88deb4 -r 046c75c9cd33 Lib/urllib/request.py --- a/Lib/urllib/request.py Sun Mar 06 15:03:47 2016 +0200 +++ b/Lib/urllib/request.py Sun Mar 06 16:17:47 2016 +0200 @@ -1171,6 +1171,9 @@ elif algorithm == 'SHA': H = lambda x: hashlib.sha1(x.encode("ascii")).hexdigest() # XXX MD5-sess + else: + raise ValueError("Unsupported digest authentication " + "algorithm %r" % algorithm) KD = lambda s, d: H("%s:%s" % (s, d)) return H, KD diff -r a8589d88deb4 -r 046c75c9cd33 Misc/NEWS --- a/Misc/NEWS Sun Mar 06 15:03:47 2016 +0200 +++ b/Misc/NEWS Sun Mar 06 16:17:47 2016 +0200 @@ -201,6 +201,9 @@ Library ------- +- Issue #2202: Fix UnboundLocalError in + AbstractDigestAuthHandler.get_algorithm_impls. Initial patch by Mathieu Dupuy. + - Issue #26167: Minimized overhead in copy.copy() and copy.deepcopy(). Optimized copying and deepcopying bytearrays, NotImplemented, slices, short lists, tuples, dicts, sets.