8000 [1.7.x] Fixed #23333 -- Made urlsafe_base64_decode() return proper ty… · alex-python/django@d830665 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit d830665

Browse files
LilyAcorntimgraham
authored andcommitted
[1.7.x] Fixed #23333 -- Made urlsafe_base64_decode() return proper type on Python 3.
Backport of 03d8916 from master
1 parent b2f7b51 commit d830665

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

django/utils/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from email.utils import formatdate
1111

1212
from django.utils.datastructures import MultiValueDict
13-
from django.utils.encoding import force_str, force_text
13+
from django.utils.encoding import force_bytes, force_str, force_text
1414
from django.utils.functional import allow_lazy
1515
from django.utils import six
1616
from django.utils.six.moves.urllib.parse import (
@@ -224,7 +224,7 @@ def urlsafe_base64_decode(s):
224224
Decodes a base64 encoded string, adding back any trailing equal signs that
225225
might have been stripped.
226226
"""
227-
s = s.encode('utf-8') # base64encode should only return ASCII.
227+
s = force_bytes(s)
228228
try:
229229
return base64.urlsafe_b64decode(s.ljust(len(s) + len(s) % 4, b'='))
230230
except (LookupError, BinasciiError) as e:

docs/releases/1.7.1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,6 @@ Bugfixes
103103

104104
* Fixed missing ``get_or_create`` and ``update_or_create`` on related managers
105105
causing ``IntegrityError`` (:ticket:`23611`).
106+
107+
* Made :func:`~django.utils.http.urlsafe_base64_decode` return the proper
108+
type (byte string) on Python 3 (:ticket:`23333`).

tests/utils_tests/test_http.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ def test_is_safe_url(self):
119119
'/url%20with%20spaces/'):
120120
self.assertTrue(http.is_safe_url(good_url, host='testserver'), "%s should be allowed" % good_url)
121121

122+
def test_urlsafe_base64_roundtrip(self):
123+
bytestring = b'foo'
124+
encoded = http.urlsafe_base64_encode(bytestring)
125+
decoded = http.urlsafe_base64_decode(encoded)
126+
self.assertEqual(bytestring, decoded)
127+
122128

123129
class ETagProcessingTests(unittest.TestCase):
124130
def test_parsing(self):

0 commit comments

Comments
 (0)
0