8000 bpo-42833: make digest algorithms case insensitive by tardyp · Pull Request #24122 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from test import support
from test.support import hashlib_helper
from test.support import os_helper
from test.support import socket_helper
from test.support import warnings_helper
Expand Down Expand Up @@ -1876,6 +1877,14 @@ def test_unsupported_algorithm(self):
"Unsupported digest authentication algorithm 'invalid'"
)

@hashlib_helper.requires_hashdigest('sha1')
def test_lowercase_algorithm(self):
handler = AbstractDigestAuthHandler()
# make sure both algorithms are equivalent
self.assertEqual(
handler.get_algorithm_impls('sha')[0]("TEST"),
handler.get_algorithm_impls('SHA')[0]("TEST"))


class RequestTests(unittest.TestCase):
class PutRequest(Request):
Expand Down
7 changes: 5 additions & 2 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,10 +1206,13 @@ def get_authorization(self, req, chal):
return base

def get_algorithm_impls(self, algorithm):
# as per https://tools.ietf.org/html/rfc3230#section-4.1.1
# algorithm is case insensitive
upper_algorithm = algorithm.upper()
# lambdas assume digest modules are imported at the top level
if algorithm == 'MD5':
if upper_algorithm == 'MD5':
H = lambda x: hashlib.md5(x.encode("ascii")).hexdigest()
elif algorithm == 'SHA':
elif upper_algorithm == 'SHA':
H = lambda x: hashlib.sha1(x.encode("ascii")).hexdigest()
# XXX MD5-sess
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
urllib2 digest algorithm selection is now case insensitive
0