8000 bpo-42833: make digest algorithms case insensitive · python/cpython@067db45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 067db45

Browse files
committed
bpo-42833: make digest algorithms case insensitive
as per https://tools.ietf.org/html/rfc3230#section-4.1.1 digest names shall be case insensitive
1 parent ee9f98d commit 067db45

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Lib/test/test_urllib2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
from test import support
3+
from test.support import hashlib_helper
34
from test.support import os_helper
45
from test.support import socket_helper
56
from test.support import warnings_helper
@@ -1876,6 +1877,14 @@ def test_unsupported_algorithm(self):
18761877
"Unsupported digest authentication algorithm 'invalid'"
18771878
)
18781879

1880+
@hashlib_helper.requires_hashdigest('sha1')
1881+
def test_lowercase_algorithm(self):
1882+
handler = AbstractDigestAuthHandler()
1883+
# make sure both algorithms are equivalent
1884+
self.assertEqual(
1885+
handler.get_algorithm_impls('sha')[0]("TEST"),
1886+
handler.get_algorithm_impls('SHA')[0]("TEST"))
1887+
18791888

18801889
class RequestTests(unittest.TestCase):
18811890
class PutRequest(Request):

Lib/urllib/request.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,10 +1206,13 @@ def get_authorization(self, req, chal):
12061206
return base
12071207

12081208
def get_algorithm_impls(self, algorithm):
1209+
# as per https://tools.ietf.org/html/rfc3230#section-4.1.1
1210+
# algorithm is case insensitive
1211+
upper_algorithm = algorithm.upper()
12091212
# lambdas assume digest modules are imported at the top level
1210-
if algorithm == 'MD5':
1213+
if upper_algorithm == 'MD5':
12111214
H = lambda x: hashlib.md5(x.encode("ascii")).hexdigest()
1212-
elif algorithm == 'SHA':
1215+
elif upper_algorithm == 'SHA':
12131216
H = lambda x: hashlib.sha1(x.encode("ascii")).hexdigest()
12141217
# XXX MD5-sess
12151218
else:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
urllib2 digest algorithm selection is now case insensitive

0 commit comments

Comments
 (0)
0