Skip to content

Commit 875c78b

Browse files
brnosouzaBruno Souza
andauthored
Making sure the cache key accepts any data (#110)
Co-authored-by: Bruno Souza <[email protected]>
1 parent 8a37130 commit 875c78b

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-security"
3-
version = "1.0.2"
3+
version = "1.0.3"
44
homepage = "https://github.com/sdelements/django-security"
55
description = "Models, views, middlewares and forms to facilitate security hardening of Django applications."
66
authors = ["Security Compass <[email protected]>"]

security/auth_throttling/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import hashlib
22
import logging
33
import time
4+
import typing
45
import urllib.parse
56
from math import ceil
67

@@ -44,8 +45,8 @@ def delay_message(remainder):
4445
return _("%d seconds") % ceil(remainder)
4546

4647

47-
def _to_ascii_compatible(value: str):
48-
if not value.isascii():
48+
def _to_ascii_compatible(value: typing.Any):
49+
if isinstance(value, str) and not value.isascii():
4950
value = urllib.parse.quote(value)
5051

5152
return value

tests/tests.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,13 +1078,28 @@ class UnicodeDataTests(TestCase):
10781078
IP_ADDRESS = "127.0.0.1"
10791079

10801080
def test_unicode_data_in_cache_key(self):
1081-
self._execute_with_unicode_data(self.USERNAME, self.IP_ADDRESS)
1081+
self._execute_with_data(self.USERNAME, self.IP_ADDRESS)
10821082

1083-
def _execute_with_unicode_data(self, username, ip):
1083+
def test_types_in_cache_key(self):
1084+
"""
1085+
We can send any kind of data for the downstream functions,
1086+
usually strings (maybe the username or email) and ints (maybe the userId)
1087+
"""
1088+
1089+
self._execute_with_data(1, self.IP_ADDRESS)
1090+
self._execute_with_data(2.67, self.IP_ADDRESS)
1091+
self._execute_with_data(bool, self.IP_ADDRESS)
1092+
self._execute_with_data({"key": "value"}, self.IP_ADDRESS)
1093+
self._execute_with_data([1], self.IP_ADDRESS)
1094+
self._execute_with_data({1, 2}, self.IP_ADDRESS)
1095+
self._execute_with_data((1, 2), self.IP_ADDRESS)
1096+
self._execute_with_data("some_string", self.IP_ADDRESS)
1097+
1098+
def _execute_with_data(self, data, ip):
10841099
try:
1085-
increment_counters(username=username, ip=ip)
1086-
reset_counters(username=username, ip=ip)
1087-
throttling_delay(username=username, ip=ip)
1088-
attempt_count(attempt_type="auth", id=username)
1100+
increment_counters(key=data, ip=ip)
1101+
reset_counters(key=data, ip=ip)
1102+
throttling_delay(username=data, ip=ip)
1103+
attempt_count(attempt_type="auth", id=data)
10891104
except Exception:
1090-
self.fail("Unicode data not allowed")
1105+
self.fail("Unicode or incompatible data not allowed")

0 commit comments

Comments
 (0)