Skip to content

Commit 48ff898

Browse files
author
Andres Mejia
committed
Fix issue that occurs when using bytes as secret key.
The Django secret key can and should be random bytes which may or may not be decodable to UTF-8.
1 parent cef000a commit 48ff898

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

debug_toolbar/panels/sql/forms.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.core.exceptions import ValidationError
1010
from django.db import connections
1111
from django.utils.crypto import constant_time_compare
12-
from django.utils.encoding import force_text
12+
from django.utils.encoding import force_bytes
1313
from django.utils.functional import cached_property
1414

1515
from debug_toolbar.panels.sql.utils import reformat_sql
@@ -79,12 +79,10 @@ def reformat_sql(self):
7979
return reformat_sql(self.cleaned_data['sql'])
8080

8181
def make_hash(self, data):
82-
items = [data['sql'], data['params']]
83-
# Replace lines endings with spaces to preserve the hash value
84-
# even when the browser normalizes \r\n to \n in inputs.
85-
items = [' '.join(force_text(item).splitlines()) for item in items]
86-
return hmac.new(settings.SECRET_KEY.encode('utf-8'),
87-
''.join(items).encode('utf-8'), hashlib.sha1).hexdigest()
82+
m = hmac.new(key=force_bytes(settings.SECRET_KEY), digestmod=hashlib.sha1)
83+
for item in [data['sql'], data['params']]:
84+
m.update(force_bytes(item))
85+
return m.hexdigest()
8886

8987
@property
9088
def connection(self):

tests/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# Quick-start development settings - unsuitable for production
99

10-
SECRET_KEY = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
10+
SECRET_KEY = bytes(bytearray([i for i in range(256)]))
1111

1212
INTERNAL_IPS = ['127.0.0.1']
1313

0 commit comments

Comments
 (0)