| 1 |
from contextlib import contextmanager |
| 2 |
from unittest import mock, skipUnless |
| 3 |
|
| 4 |
from django.conf.global_settings import PASSWORD_HASHERS |
| 5 |
from django.contrib.auth.hashers import ( |
| 6 |
UNUSABLE_PASSWORD_PREFIX, |
| 7 |
UNUSABLE_PASSWORD_SUFFIX_LENGTH, |
| 8 |
BasePasswordHasher, |
| 9 |
BCryptPasswordHasher, |
| 10 |
BCryptSHA256PasswordHasher, |
| 11 |
MD5PasswordHasher, |
| 12 |
PBKDF2PasswordHasher, |
| 13 |
PBKDF2SHA1PasswordHasher, |
| 14 |
ScryptPasswordHasher, |
| 15 |
acheck_password, |
| 16 |
check_password, |
| 17 |
get_hasher, |
| 18 |
identify_hasher, |
| 19 |
is_password_usable, |
| 20 |
make_password, |
| 21 |
) |
| 22 |
from django.test import SimpleTestCase |
| 23 |
from django.test.utils import override_settings |
| 24 |
|
| 25 |
try: |
| 26 |
import bcrypt |
| 27 |
except ImportError: |
| 28 |
bcrypt = None |
| 29 |
|
| 30 |
try: |
| 31 |
import argon2 |
| 32 |
except ImportError: |
| 33 |
argon2 = None |
| 34 |
|
| 35 |
# scrypt requires OpenSSL 1.1+ |
| 36 |
try: |
| 37 |
import hashlib |
| 38 |
|
| 39 |
scrypt = hashlib.scrypt |
| 40 |
except ImportError: |
| 41 |
scrypt = None |
| 42 |
|
| 43 |
|
| 44 |
class PBKDF2SingleIterationHasher(PBKDF2PasswordHasher): |
| 45 |
iterations = 1 |
| 46 |
|
| 47 |
|
| 48 |
@override_settings(PASSWORD_HASHERS=PASSWORD_HASHERS) |
| 49 |
class TestUtilsHashPass(SimpleTestCase): |
| 50 |
def test_simple(self): |
| 51 |
encoded = make_password("lètmein") |
| 52 |
self.assertTrue(encoded.startswith("pbkdf2_sha256$")) |
| 53 |
self.assertTrue(is_password_usable(encoded)) |
| 54 |
self.assertTrue(check_password("lètmein", encoded)) |
| 55 |
self.assertFalse(check_password("lètmeinz", encoded)) |
| 56 |
# Blank passwords |
| 57 |
blank_encoded = make_password("") |
| 58 |
self.assertTrue(blank_encoded.startswith("pbkdf2_sha256$")) |
| 59 |
self.assertTrue(is_password_usable(blank_encoded)) |
| 60 |
self.assertTrue(check_password("", blank_encoded)) |
| 61 |
self.assertFalse(check_password(" ", blank_encoded)) |
| 62 |
|
| 63 |
async def test_acheck_password(self): |
| 64 |
encoded = make_password("lètmein") |
| 65 |
self.assertIs(await acheck_password("lètmein", encoded), True) |
| 66 |
self.assertIs(await acheck_password("lètmeinz", encoded), False) |
| 67 |
# Blank passwords. |
| 68 |
blank_encoded = make_password("") |
| 69 |
self.assertIs(await acheck_password("", blank_encoded), True) |
| 70 |
self.assertIs(await acheck_password(" ", blank_encoded), False) |
| 71 |
|
| 72 |
def test_bytes(self): |
| 73 |
encoded = make_password(b"bytes_password") |
| 74 |
self.assertTrue(encoded.startswith("pbkdf2_sha256$")) |
| 75 |
self.assertIs(is_password_usable(encoded), True) |
| 76 |
self.assertIs(check_password(b"bytes_password", encoded), True) |
| 77 |
|
| 78 |
def test_invalid_password(self): |
| 79 |
msg = "Password must be a string or bytes, got int." |
| 80 |
with self.assertRaisesMessage(TypeError, msg): |
| 81 |
make_password(1) |
| 82 |
|
| 83 |
def test_pbkdf2(self): |
| 84 |
encoded = make_password("lètmein", "seasalt", "pbkdf2_sha256") |
| 85 |
self.assertEqual( |
| 86 |
encoded, |
| 87 |
"pbkdf2_sha256$1000000$" |
| 88 |
"seasalt$r1uLUxoxpP2Ued/qxvmje7UH9PUJBkRrvf9gGPL7Cps=", |
| 89 |
) |
| 90 |
self.assertTrue(is_password_usable(encoded)) |
| 91 |
self.assertTrue(check_password("lètmein", encoded)) |
| 92 |
self.assertFalse(check_password("lètmeinz", encoded)) |
| 93 |
self.assertEqual(identify_hasher(encoded).algorithm, "pbkdf2_sha256") |
| 94 |
# Blank passwords |
| 95 |
blank_encoded = make_password("", "seasalt", "pbkdf2_sha256") |
| 96 |
self.assertTrue(blank_encoded.startswith("pbkdf2_sha256$")) |
| 97 |
self.assertTrue(is_password_usable(blank_encoded)) |
| 98 |
self.assertTrue(check_password("", blank_encoded)) |
| 99 |
self.assertFalse(check_password(" ", blank_encoded)) |
| 100 |
# Salt entropy check. |
| 101 |
hasher = get_hasher("pbkdf2_sha256") |
| 102 |
encoded_weak_salt = make_password("lètmein", "iodizedsalt", "pbkdf2_sha256") |
| 103 |
encoded_strong_salt = make_password("lètmein", hasher.salt(), "pbkdf2_sha256") |
| 104 |
self.assertIs(hasher.must_update(encoded_weak_salt), True) |
| 105 |
self.assertIs(hasher.must_update(encoded_strong_salt), False) |
| 106 |
|
| 107 |
@override_settings( |
| 108 |
PASSWORD_HASHERS=["django.contrib.auth.hashers.MD5PasswordHasher"] |
| 109 |
) |
| 110 |
def test_md5(self): |
| 111 |
encoded = make_password("lètmein", "seasalt", "md5") |
| 112 |
self.assertEqual(encoded, "md5$seasalt$3f86d0d3d465b7b458c231bf3555c0e3") |
| 113 |
self.assertTrue(is_password_usable(encoded)) |
| 114 |
self.assertTrue(check_password("lètmein", encoded)) |
| 115 |
self.assertFalse(check_password("lètmeinz", encoded)) |
| 116 |
self.assertEqual(identify_hasher(encoded).algorithm, "md5") |
| 117 |
# Blank passwords |
| 118 |
blank_encoded = make_password("", "seasalt", "md5") |
| 119 |
self.assertTrue(blank_encoded.startswith("md5$")) |
| 120 |
self.assertTrue(is_password_usable(blank_encoded)) |
| 121 |
self.assertTrue(check_password("", blank_encoded)) |
| 122 |
self.assertFalse(check_password(" ", blank_encoded)) |
| 123 |
# Salt entropy check. |
| 124 |
hasher = get_hasher("md5") |
| 125 |
encoded_weak_salt = make_password("lètmein", "iodizedsalt", "md5") |
| 126 |
encoded_strong_salt = make_password("lètmein", hasher.salt(), "md5") |
| 127 |
self.assertIs(hasher.must_update(encoded_weak_salt), True) |
| 128 |
self.assertIs(hasher.must_update(encoded_strong_salt), False) |
| 129 |
|
| 130 |
@skipUnless(bcrypt, "bcrypt not installed") |
| 131 |
def test_bcrypt_sha256(self): |
| 132 |
encoded = make_password("lètmein", hasher="bcrypt_sha256") |
| 133 |
self.assertTrue(is_password_usable(encoded)) |
| 134 |
self.assertTrue(encoded.startswith("bcrypt_sha256$")) |
| 135 |
self.assertTrue(check_password("lètmein", encoded)) |
| 136 |
self.assertFalse(check_password("lètmeinz", encoded)) |
| 137 |
self.assertEqual(identify_hasher(encoded).algorithm, "bcrypt_sha256") |
| 138 |
|
| 139 |
# password truncation no longer works |
| 140 |
password = ( |
| 141 |
"VSK0UYV6FFQVZ0KG88DYN9WADAADZO1CTSIVDJUNZSUML6IBX7LN7ZS3R5" |
| 142 |
"JGB3RGZ7VI7G7DJQ9NI8BQFSRPTG6UWTTVESA5ZPUN" |
| 143 |
) |
| 144 |
encoded = make_password(password, hasher="bcrypt_sha256") |
| 145 |
self.assertTrue(check_password(password, encoded)) |
| 146 |
self.assertFalse(check_password(password[:72], encoded)) |
| 147 |
# Blank passwords |
| 148 |
blank_encoded = make_password("", hasher="bcrypt_sha256") |
| 149 |
self.assertTrue(blank_encoded.startswith("bcrypt_sha256$")) |
| 150 |
self.assertTrue(is_password_usable(blank_encoded)) |
| 151 |
self.assertTrue(check_password("", blank_encoded)) |
| 152 |
self.assertFalse(check_password(" ", blank_encoded)) |
| 153 |
|
| 154 |
@skipUnless(bcrypt, "bcrypt not installed") |
| 155 |
@override_settings( |
| 156 |
PASSWORD_HASHERS=["django.contrib.auth.hashers.BCryptPasswordHasher"] |
| 157 |
) |
| 158 |
def test_bcrypt(self): |
| 159 |
encoded = make_password("lètmein", hasher="bcrypt") |
| 160 |
self.assertTrue(is_password_usable(encoded)) |
| 161 |
self.assertTrue(encoded.startswith("bcrypt$")) |
| 162 |
self.assertTrue(check_password("lètmein", encoded)) |
| 163 |
self.assertFalse(check_password("lètmeinz", encoded)) |
| 164 |
self.assertEqual(identify_hasher(encoded).algorithm, "bcrypt") |
| 165 |
# Blank passwords |
| 166 |
blank_encoded = make_password("", hasher="bcrypt") |
| 167 |
self.assertTrue(blank_encoded.startswith("bcrypt$")) |
| 168 |
self.assertTrue(is_password_usable(blank_encoded)) |
| 169 |
self.assertTrue(check_password("", blank_encoded)) |
| 170 |
self.assertFalse(check_password(" ", blank_encoded)) |
| 171 |
|
| 172 |
@skipUnless(bcrypt, "bcrypt not installed") |
| 173 |
@override_settings( |
| 174 |
PASSWORD_HASHERS=["django.contrib.auth.hashers.BCryptPasswordHasher"] |
| 175 |
) |
| 176 |
def test_bcrypt_upgrade(self): |
| 177 |
hasher = get_hasher("bcrypt") |
| 178 |
self.assertEqual("bcrypt", hasher.algorithm) |
| 179 |
self.assertNotEqual(hasher.rounds, 4) |
| 180 |
|
| 181 |
old_rounds = hasher.rounds |
| 182 |
try: |
| 183 |
# Generate a password with 4 rounds. |
| 184 |
hasher.rounds = 4 |
| 185 |
encoded = make_password("letmein", hasher="bcrypt") |
| 186 |
rounds = hasher.safe_summary(encoded)["work factor"] |
| 187 |
self.assertEqual(rounds, 4) |
| 188 |
|
| 189 |
state = {"upgraded": False} |
| 190 |
|
| 191 |
def setter(password): |
| 192 |
state["upgraded"] = True |
| 193 |
|
| 194 |
# No upgrade is triggered. |
| 195 |
self.assertTrue(check_password("letmein", encoded, setter, "bcrypt")) |
| 196 |
self.assertFalse(state["upgraded"]) |
| 197 |
|
| 198 |
# Revert to the old rounds count and ... |
| 199 |
hasher.rounds = old_rounds |
| 200 |
|
| 201 |
# ... check if the password would get updated to the new count. |
| 202 |
self.assertTrue(check_password("letmein", encoded, setter, "bcrypt")) |
| 203 |
self.assertTrue(state["upgraded"]) |
| 204 |
finally: |
| 205 |
hasher.rounds = old_rounds |
| 206 |
|
| 207 |
@skipUnless(bcrypt, "bcrypt not installed") |
| 208 |
@override_settings( |
| 209 |
PASSWORD_HASHERS=["django.contrib.auth.hashers.BCryptPasswordHasher"] |
| 210 |
) |
| 211 |
def test_bcrypt_harden_runtime(self): |
| 212 |
hasher = get_hasher("bcrypt") |
| 213 |
self.assertEqual("bcrypt", hasher.algorithm) |
| 214 |
|
| 215 |
with mock.patch.object(hasher, "rounds", 4): |
| 216 |
encoded = make_password("letmein", hasher="bcrypt") |
| 217 |
|
| 218 |
with ( |
| 219 |
mock.patch.object(hasher, "rounds", 6), |
| 220 |
mock.patch.object(hasher, "encode", side_effect=hasher.encode), |
| 221 |
): |
| 222 |
hasher.harden_runtime("wrong_password", encoded) |
| 223 |
|
| 224 |
# Increasing rounds from 4 to 6 means an increase of 4 in workload, |
| 225 |
# therefore hardening should run 3 times to make the timing the |
| 226 |
# same (the original encode() call already ran once). |
| 227 |
self.assertEqual(hasher.encode.call_count, 3) |
| 228 |
|
| 229 |
# Get the original salt (includes the original workload factor) |
| 230 |
algorithm, data = encoded.split("$", 1) |
| 231 |
expected_call = (("wrong_password", data[:29].encode()),) |
| 232 |
self.assertEqual(hasher.encode.call_args_list, [expected_call] * 3) |
| 233 |
|
| 234 |
def test_unusable(self): |
| 235 |
encoded = make_password(None) |
| 236 |
self.assertEqual( |
| 237 |
len(encoded), |
| 238 |
len(UNUSABLE_PASSWORD_PREFIX) + UNUSABLE_PASSWORD_SUFFIX_LENGTH, |
| 239 |
) |
| 240 |
self.assertFalse(is_password_usable(encoded)) |
| 241 |
self.assertFalse(check_password(None, encoded)) |
| 242 |
self.assertFalse(check_password(encoded, encoded)) |
| 243 |
self.assertFalse(check_password(UNUSABLE_PASSWORD_PREFIX, encoded)) |
| 244 |
self.assertFalse(check_password("", encoded)) |
| 245 |
self.assertFalse(check_password("lètmein", encoded)) |
| 246 |
self.assertFalse(check_password("lètmeinz", encoded)) |
| 247 |
with self.assertRaisesMessage(ValueError, "Unknown password hashing algorithm"): |
| 248 |
identify_hasher(encoded) |
| 249 |
# Assert that the unusable passwords actually contain a random part. |
| 250 |
# This might fail one day due to a hash collision. |
| 251 |
self.assertNotEqual(encoded, make_password(None), "Random password collision?") |
| 252 |
|
| 253 |
def test_unspecified_password(self): |
| 254 |
""" |
| 255 |
Makes sure specifying no plain password with a valid encoded password |
| 256 |
returns `False`. |
| 257 |
""" |
| 258 |
self.assertFalse(check_password(None, make_password("lètmein"))) |
| 259 |
|
| 260 |
def test_bad_algorithm(self): |
| 261 |
msg = ( |
| 262 |
"Unknown password hashing algorithm '%s'. Did you specify it in " |
| 263 |
"the PASSWORD_HASHERS setting?" |
| 264 |
) |
| 265 |
with self.assertRaisesMessage(ValueError, msg % "lolcat"): |
| 266 |
make_password("lètmein", hasher="lolcat") |
| 267 |
with self.assertRaisesMessage(ValueError, msg % "lolcat"): |
| 268 |
identify_hasher("lolcat$salt$hash") |
| 269 |
|
| 270 |
def test_is_password_usable(self): |
| 271 |
passwords = ("lètmein_badencoded", "", None) |
| 272 |
for password in passwords: |
| 273 |
with self.subTest(password=password): |
| 274 |
self.assertIs(is_password_usable(password), True) |
| 275 |
|
| 276 |
def test_low_level_pbkdf2(self): |
| 277 |
hasher = PBKDF2PasswordHasher() |
| 278 |
encoded = hasher.encode("lètmein", "seasalt2") |
| 279 |
self.assertEqual( |
| 280 |
encoded, |
| 281 |
"pbkdf2_sha256$1000000$" |
| 282 |
"seasalt2$egbhFghgsJVDo5Tpg/k9ZnfbySKQ1UQnBYXhR97a7sk=", |
| 283 |
) |
| 284 |
self.assertTrue(hasher.verify("lètmein", encoded)) |
| 285 |
|
| 286 |
def test_low_level_pbkdf2_sha1(self): |
| 287 |
hasher = PBKDF2SHA1PasswordHasher() |
| 288 |
encoded = hasher.encode("lètmein", "seasalt2") |
| 289 |
self.assertEqual( |
| 290 |
encoded, "pbkdf2_sha1$1000000$seasalt2$3R9hvSAiAy5ARspAFy5GJ/2rjXo=" |
| 291 |
) |
| 292 |
self.assertTrue(hasher.verify("lètmein", encoded)) |
| 293 |
|
| 294 |
@skipUnless(bcrypt, "bcrypt not installed") |
| 295 |
def test_bcrypt_salt_check(self): |
| 296 |
hasher = BCryptPasswordHasher() |
| 297 |
encoded = hasher.encode("lètmein", hasher.salt()) |
| 298 |
self.assertIs(hasher.must_update(encoded), False) |
| 299 |
|
| 300 |
@skipUnless(bcrypt, "bcrypt not installed") |
| 301 |
def test_bcryptsha256_salt_check(self): |
| 302 |
hasher = BCryptSHA256PasswordHasher() |
| 303 |
encoded = hasher.encode("lètmein", hasher.salt()) |
| 304 |
self.assertIs(hasher.must_update(encoded), False) |
| 305 |
|
| 306 |
@override_settings( |
| 307 |
PASSWORD_HASHERS=[ |
| 308 |
"django.contrib.auth.hashers.PBKDF2PasswordHasher", |
| 309 |
"django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher", |
| 310 |
"django.contrib.auth.hashers.MD5PasswordHasher", |
| 311 |
], |
| 312 |
) |
| 313 |
def test_upgrade(self): |
| 314 |
self.assertEqual("pbkdf2_sha256", get_hasher("default").algorithm) |
| 315 |
for algo in ("pbkdf2_sha1", "md5"): |
| 316 |
with self.subTest(algo=algo): |
| 317 |
encoded = make_password("lètmein", hasher=algo) |
| 318 |
state = {"upgraded": False} |
| 319 |
|
| 320 |
def setter(password): |
| 321 |
state["upgraded"] = True |
| 322 |
|
| 323 |
self.assertTrue(check_password("lètmein", encoded, setter)) |
| 324 |
self.assertTrue(state["upgraded"]) |
| 325 |
|
| 326 |
def test_no_upgrade(self): |
| 327 |
encoded = make_password("lètmein") |
| 328 |
state = {"upgraded": False} |
| 329 |
|
| 330 |
def setter(): |
| 331 |
state["upgraded"] = True |
| 332 |
|
| 333 |
self.assertFalse(check_password("WRONG", encoded, setter)) |
| 334 |
self.assertFalse(state["upgraded"]) |
| 335 |
|
| 336 |
@override_settings( |
| 337 |
PASSWORD_HASHERS=[ |
| 338 |
"django.contrib.auth.hashers.PBKDF2PasswordHasher", |
| 339 |
"django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher", |
| 340 |
"django.contrib.auth.hashers.MD5PasswordHasher", |
| 341 |
], |
| 342 |
) |
| 343 |
def test_no_upgrade_on_incorrect_pass(self): |
| 344 |
self.assertEqual("pbkdf2_sha256", get_hasher("default").algorithm) |
| 345 |
for algo in ("pbkdf2_sha1", "md5"): |
| 346 |
with self.subTest(algo=algo): |
| 347 |
encoded = make_password("lètmein", hasher=algo) |
| 348 |
state = {"upgraded": False} |
| 349 |
|
| 350 |
def setter(): |
| 351 |
state["upgraded"] = True |
| 352 |
|
| 353 |
self.assertFalse(check_password("WRONG", encoded, setter)) |
| 354 |
self.assertFalse(state["upgraded"]) |
| 355 |
|
| 356 |
def test_pbkdf2_upgrade(self): |
| 357 |
hasher = get_hasher("default") |
| 358 |
self.assertEqual("pbkdf2_sha256", hasher.algorithm) |
| 359 |
self.assertNotEqual(hasher.iterations, 1) |
| 360 |
|
| 361 |
old_iterations = hasher.iterations |
| 362 |
try: |
| 363 |
# Generate a password with 1 iteration. |
| 364 |
hasher.iterations = 1 |
| 365 |
encoded = make_password("letmein") |
| 366 |
algo, iterations, salt, hash = encoded.split("$", 3) |
| 367 |
self.assertEqual(iterations, "1") |
| 368 |
|
| 369 |
state = {"upgraded": False} |
| 370 |
|
| 371 |
def setter(password): |
| 372 |
state["upgraded"] = True |
| 373 |
|
| 374 |
# No upgrade is triggered |
| 375 |
self.assertTrue(check_password("letmein", encoded, setter)) |
| 376 |
self.assertFalse(state["upgraded"]) |
| 377 |
|
| 378 |
# Revert to the old iteration count and ... |
| 379 |
hasher.iterations = old_iterations |
| 380 |
|
| 381 |
# ... check if the password would get updated to the new iteration count. |
| 382 |
self.assertTrue(check_password("letmein", encoded, setter)) |
| 383 |
self.assertTrue(state["upgraded"]) |
| 384 |
finally: |
| 385 |
hasher.iterations = old_iterations |
| 386 |
|
| 387 |
def test_pbkdf2_harden_runtime(self): |
| 388 |
hasher = get_hasher("default") |
| 389 |
self.assertEqual("pbkdf2_sha256", hasher.algorithm) |
| 390 |
|
| 391 |
with mock.patch.object(hasher, "iterations", 1): |
| 392 |
encoded = make_password("letmein") |
| 393 |
|
| 394 |
with ( |
| 395 |
mock.patch.object(hasher, "iterations", 6), |
| 396 |
mock.patch.object(hasher, "encode", side_effect=hasher.encode), |
| 397 |
): |
| 398 |
hasher.harden_runtime("wrong_password", encoded) |
| 399 |
|
| 400 |
# Encode should get called once ... |
| 401 |
self.assertEqual(hasher.encode.call_count, 1) |
| 402 |
|
| 403 |
# ... with the original salt and 5 iterations. |
| 404 |
algorithm, iterations, salt, hash = encoded.split("$", 3) |
| 405 |
expected_call = (("wrong_password", salt, 5),) |
| 406 |
self.assertEqual(hasher.encode.call_args, expected_call) |
| 407 |
|
| 408 |
def test_pbkdf2_upgrade_new_hasher(self): |
| 409 |
hasher = get_hasher("default") |
| 410 |
self.assertEqual("pbkdf2_sha256", hasher.algorithm) |
| 411 |
self.assertNotEqual(hasher.iterations, 1) |
| 412 |
|
| 413 |
state = {"upgraded": False} |
| 414 |
|
| 415 |
def setter(password): |
| 416 |
state["upgraded"] = True |
| 417 |
|
| 418 |
with self.settings( |
| 419 |
PASSWORD_HASHERS=["auth_tests.test_hashers.PBKDF2SingleIterationHasher"] |
| 420 |
): |
| 421 |
encoded = make_password("letmein") |
| 422 |
algo, iterations, salt, hash = encoded.split("$", 3) |
| 423 |
self.assertEqual(iterations, "1") |
| 424 |
|
| 425 |
# No upgrade is triggered |
| 426 |
self.assertTrue(check_password("letmein", encoded, setter)) |
| 427 |
self.assertFalse(state["upgraded"]) |
| 428 |
|
| 429 |
# Revert to the old iteration count and check if the password would get |
| 430 |
# updated to the new iteration count. |
| 431 |
with self.settings( |
| 432 |
PASSWORD_HASHERS=[ |
| 433 |
"django.contrib.auth.hashers.PBKDF2PasswordHasher", |
| 434 |
"auth_tests.test_hashers.PBKDF2SingleIterationHasher", |
| 435 |
] |
| 436 |
): |
| 437 |
self.assertTrue(check_password("letmein", encoded, setter)) |
| 438 |
self.assertTrue(state["upgraded"]) |
| 439 |
|
| 440 |
def test_check_password_calls_harden_runtime(self): |
| 441 |
hasher = get_hasher("default") |
| 442 |
encoded = make_password("letmein") |
| 443 |
|
| 444 |
with ( |
| 445 |
mock.patch.object(hasher, "harden_runtime"), |
| 446 |
mock.patch.object(hasher, "must_update", return_value=True), |
| 447 |
): |
| 448 |
# Correct password supplied, no hardening needed |
| 449 |
check_password("letmein", encoded) |
| 450 |
self.assertEqual(hasher.harden_runtime.call_count, 0) |
| 451 |
|
| 452 |
# Wrong password supplied, hardening needed |
| 453 |
check_password("wrong_password", encoded) |
| 454 |
self.assertEqual(hasher.harden_runtime.call_count, 1) |
| 455 |
|
| 456 |
@contextmanager |
| 457 |
def assertMakePasswordCalled(self, password, encoded, hasher_side_effect): |
| 458 |
hasher = get_hasher("default") |
| 459 |
with ( |
| 460 |
mock.patch( |
| 461 |
"django.contrib.auth.hashers.identify_hasher", |
| 462 |
side_effect=hasher_side_effect, |
| 463 |
) as mock_identify_hasher, |
| 464 |
mock.patch( |
| 465 |
"django.contrib.auth.hashers.make_password" |
| 466 |
) as mock_make_password, |
| 467 |
mock.patch( |
| 468 |
"django.contrib.auth.hashers.get_random_string", |
| 469 |
side_effect=lambda size: "x" * size, |
| 470 |
), |
| 471 |
mock.patch.object(hasher, "verify"), |
| 472 |
): |
| 473 |
# Ensure make_password is called to standardize timing. |
| 474 |
yield |
| 475 |
self.assertEqual(hasher.verify.call_count, 0) |
| 476 |
self.assertEqual(mock_identify_hasher.mock_calls, [mock.call(encoded)]) |
| 477 |
self.assertEqual( |
| 478 |
mock_make_password.mock_calls, |
| 479 |
[mock.call("x" * UNUSABLE_PASSWORD_SUFFIX_LENGTH)], |
| 480 |
) |
| 481 |
|
| 482 |
def test_check_password_calls_make_password_to_fake_runtime(self): |
| 483 |
cases = [ |
| 484 |
(None, None, None), # no plain text password provided |
| 485 |
("foo", make_password(password=None), None), # unusable encoded |
| 486 |
("letmein", make_password(password="letmein"), ValueError), # valid encoded |
| 487 |
] |
| 488 |
for password, encoded, hasher_side_effect in cases: |
| 489 |
with ( |
| 490 |
self.subTest(encoded=encoded), |
| 491 |
self.assertMakePasswordCalled(password, encoded, hasher_side_effect), |
| 492 |
): |
| 493 |
check_password(password, encoded) |
| 494 |
|
| 495 |
async def test_acheck_password_calls_make_password_to_fake_runtime(self): |
| 496 |
cases = [ |
| 497 |
(None, None, None), # no plain text password provided |
| 498 |
("foo", make_password(password=None), None), # unusable encoded |
| 499 |
("letmein", make_password(password="letmein"), ValueError), # valid encoded |
| 500 |
] |
| 501 |
for password, encoded, hasher_side_effect in cases: |
| 502 |
with ( |
| 503 |
self.subTest(encoded=encoded), |
| 504 |
self.assertMakePasswordCalled(password, encoded, hasher_side_effect), |
| 505 |
): |
| 506 |
await acheck_password(password, encoded) |
| 507 |
|
| 508 |
def test_encode_invalid_salt(self): |
| 509 |
hasher_classes = [ |
| 510 |
MD5PasswordHasher, |
| 511 |
PBKDF2PasswordHasher, |
| 512 |
PBKDF2SHA1PasswordHasher, |
| 513 |
ScryptPasswordHasher, |
| 514 |
] |
| 515 |
msg = "salt must be provided and cannot contain $." |
| 516 |
for hasher_class in hasher_classes: |
| 517 |
hasher = hasher_class() |
| 518 |
for salt in [None, "", "sea$salt"]: |
| 519 |
with self.subTest(hasher_class.__name__, salt=salt): |
| 520 |
with self.assertRaisesMessage(ValueError, msg): |
| 521 |
hasher.encode("password", salt) |
| 522 |
|
| 523 |
def test_encode_password_required(self): |
| 524 |
hasher_classes = [ |
| 525 |
MD5PasswordHasher, |
| 526 |
PBKDF2PasswordHasher, |
| 527 |
PBKDF2SHA1PasswordHasher, |
| 528 |
ScryptPasswordHasher, |
| 529 |
] |
| 530 |
msg = "password must be provided." |
| 531 |
for hasher_class in hasher_classes: |
| 532 |
hasher = hasher_class() |
| 533 |
with self.subTest(hasher_class.__name__): |
| 534 |
with self.assertRaisesMessage(TypeError, msg): |
| 535 |
hasher.encode(None, "seasalt") |
| 536 |
|
| 537 |
|
| 538 |
class BasePasswordHasherTests(SimpleTestCase): |
| 539 |
not_implemented_msg = "subclasses of BasePasswordHasher must provide %s() method" |
| 540 |
|
| 541 |
def setUp(self): |
| 542 |
self.hasher = BasePasswordHasher() |
| 543 |
|
| 544 |
def test_load_library_no_algorithm(self): |
| 545 |
msg = "Hasher 'BasePasswordHasher' doesn't specify a library attribute" |
| 546 |
with self.assertRaisesMessage(ValueError, msg): |
| 547 |
self.hasher._load_library() |
| 548 |
|
| 549 |
def test_load_library_importerror(self): |
| 550 |
PlainHasher = type( |
| 551 |
"PlainHasher", |
| 552 |
(BasePasswordHasher,), |
| 553 |
{"algorithm": "plain", "library": "plain"}, |
| 554 |
) |
| 555 |
msg = "Couldn't load 'PlainHasher' algorithm library: No module named 'plain'" |
| 556 |
with self.assertRaisesMessage(ValueError, msg): |
| 557 |
PlainHasher()._load_library() |
| 558 |
|
| 559 |
def test_attributes(self): |
| 560 |
self.assertIsNone(self.hasher.algorithm) |
| 561 |
self.assertIsNone(self.hasher.library) |
| 562 |
|
| 563 |
def test_encode(self): |
| 564 |
msg = self.not_implemented_msg % "an encode" |
| 565 |
with self.assertRaisesMessage(NotImplementedError, msg): |
| 566 |
self.hasher.encode("password", "salt") |
| 567 |
|
| 568 |
def test_decode(self): |
| 569 |
msg = self.not_implemented_msg % "a decode" |
| 570 |
with self.assertRaisesMessage(NotImplementedError, msg): |
| 571 |
self.hasher.decode("encoded") |
| 572 |
|
| 573 |
def test_harden_runtime(self): |
| 574 |
msg = ( |
| 575 |
"subclasses of BasePasswordHasher should provide a harden_runtime() method" |
| 576 |
) |
| 577 |
with self.assertWarnsMessage(Warning, msg): |
| 578 |
self.hasher.harden_runtime("password", "encoded") |
| 579 |
|
| 580 |
def test_must_update(self): |
| 581 |
self.assertIs(self.hasher.must_update("encoded"), False) |
| 582 |
|
| 583 |
def test_safe_summary(self): |
| 584 |
msg = self.not_implemented_msg % "a safe_summary" |
| 585 |
with self.assertRaisesMessage(NotImplementedError, msg): |
| 586 |
self.hasher.safe_summary("encoded") |
| 587 |
|
| 588 |
def test_verify(self): |
| 589 |
msg = self.not_implemented_msg % "a verify" |
| 590 |
with self.assertRaisesMessage(NotImplementedError, msg): |
| 591 |
self.hasher.verify("password", "encoded") |
| 592 |
|
| 593 |
|
| 594 |
@skipUnless(argon2, "argon2-cffi not installed") |
| 595 |
@override_settings(PASSWORD_HASHERS=PASSWORD_HASHERS) |
| 596 |
class TestUtilsHashPassArgon2(SimpleTestCase): |
| 597 |
def test_argon2(self): |
| 598 |
encoded = make_password("lètmein", hasher="argon2") |
| 599 |
self.assertTrue(is_password_usable(encoded)) |
| 600 |
self.assertTrue(encoded.startswith("argon2$argon2id$")) |
| 601 |
self.assertTrue(check_password("lètmein", encoded)) |
| 602 |
self.assertFalse(check_password("lètmeinz", encoded)) |
| 603 |
self.assertEqual(identify_hasher(encoded).algorithm, "argon2") |
| 604 |
# Blank passwords |
| 605 |
blank_encoded = make_password("", hasher="argon2") |
| 606 |
self.assertTrue(blank_encoded.startswith("argon2$argon2id$")) |
| 607 |
self.assertTrue(is_password_usable(blank_encoded)) |
| 608 |
self.assertTrue(check_password("", blank_encoded)) |
| 609 |
self.assertFalse(check_password(" ", blank_encoded)) |
| 610 |
# Old hashes without version attribute |
| 611 |
encoded = ( |
| 612 |
"argon2$argon2i$m=8,t=1,p=1$c29tZXNhbHQ$gwQOXSNhxiOxPOA0+PY10P9QFO" |
| 613 |
"4NAYysnqRt1GSQLE55m+2GYDt9FEjPMHhP2Cuf0nOEXXMocVrsJAtNSsKyfg" |
| 614 |
) |
| 615 |
self.assertTrue(check_password("secret", encoded)) |
| 616 |
self.assertFalse(check_password("wrong", encoded)) |
| 617 |
# Old hashes with version attribute. |
| 618 |
encoded = "argon2$argon2i$v=19$m=8,t=1,p=1$c2FsdHNhbHQ$YC9+jJCrQhs5R6db7LlN8Q" |
| 619 |
self.assertIs(check_password("secret", encoded), True) |
| 620 |
self.assertIs(check_password("wrong", encoded), False) |
| 621 |
# Salt entropy check. |
| 622 |
hasher = get_hasher("argon2") |
| 623 |
encoded_weak_salt = make_password("lètmein", "iodizedsalt", "argon2") |
| 624 |
encoded_strong_salt = make_password("lètmein", hasher.salt(), "argon2") |
| 625 |
self.assertIs(hasher.must_update(encoded_weak_salt), True) |
| 626 |
self.assertIs(hasher.must_update(encoded_strong_salt), False) |
| 627 |
|
| 628 |
def test_argon2_decode(self): |
| 629 |
salt = "abcdefghijk" |
| 630 |
encoded = make_password("lètmein", salt=salt, hasher="argon2") |
| 631 |
hasher = get_hasher("argon2") |
| 632 |
decoded = hasher.decode(encoded) |
| 633 |
self.assertEqual(decoded["memory_cost"], hasher.memory_cost) |
| 634 |
self.assertEqual(decoded["parallelism"], hasher.parallelism) |
| 635 |
self.assertEqual(decoded["salt"], salt) |
| 636 |
self.assertEqual(decoded["time_cost"], hasher.time_cost) |
| 637 |
|
| 638 |
def test_argon2_upgrade(self): |
| 639 |
self._test_argon2_upgrade("time_cost", "time cost", 1) |
| 640 |
self._test_argon2_upgrade("memory_cost", "memory cost", 64) |
| 641 |
self._test_argon2_upgrade("parallelism", "parallelism", 1) |
| 642 |
|
| 643 |
def test_argon2_version_upgrade(self): |
| 644 |
hasher = get_hasher("argon2") |
| 645 |
state = {"upgraded": False} |
| 646 |
encoded = ( |
| 647 |
"argon2$argon2id$v=19$m=102400,t=2,p=8$Y041dExhNkljRUUy$TMa6A8fPJh" |
| 648 |
"CAUXRhJXCXdw" |
| 649 |
) |
| 650 |
|
| 651 |
def setter(password): |
| 652 |
state["upgraded"] = True |
| 653 |
|
| 654 |
old_m = hasher.memory_cost |
| 655 |
old_t = hasher.time_cost |
| 656 |
old_p = hasher.parallelism |
| 657 |
try: |
| 658 |
hasher.memory_cost = 8 |
| 659 |
hasher.time_cost = 1 |
| 660 |
hasher.parallelism = 1 |
| 661 |
self.assertTrue(check_password("secret", encoded, setter, "argon2")) |
| 662 |
self.assertTrue(state["upgraded"]) |
| 663 |
finally: |
| 664 |
hasher.memory_cost = old_m |
| 665 |
hasher.time_cost = old_t |
| 666 |
hasher.parallelism = old_p |
| 667 |
|
| 668 |
def _test_argon2_upgrade(self, attr, summary_key, new_value): |
| 669 |
hasher = get_hasher("argon2") |
| 670 |
self.assertEqual("argon2", hasher.algorithm) |
| 671 |
self.assertNotEqual(getattr(hasher, attr), new_value) |
| 672 |
|
| 673 |
old_value = getattr(hasher, attr) |
| 674 |
try: |
| 675 |
# Generate hash with attr set to 1 |
| 676 |
setattr(hasher, attr, new_value) |
| 677 |
encoded = make_password("letmein", hasher="argon2") |
| 678 |
attr_value = hasher.safe_summary(encoded)[summary_key] |
| 679 |
self.assertEqual(attr_value, new_value) |
| 680 |
|
| 681 |
state = {"upgraded": False} |
| 682 |
|
| 683 |
def setter(password): |
| 684 |
state["upgraded"] = True |
| 685 |
|
| 686 |
# No upgrade is triggered. |
| 687 |
self.assertTrue(check_password("letmein", encoded, setter, "argon2")) |
| 688 |
self.assertFalse(state["upgraded"]) |
| 689 |
|
| 690 |
# Revert to the old rounds count and ... |
| 691 |
setattr(hasher, attr, old_value) |
| 692 |
|
| 693 |
# ... check if the password would get updated to the new count. |
| 694 |
self.assertTrue(check_password("letmein", encoded, setter, "argon2")) |
| 695 |
self.assertTrue(state["upgraded"]) |
| 696 |
finally: |
| 697 |
setattr(hasher, attr, old_value) |
| 698 |
|
| 699 |
|
| 700 |
@skipUnless(scrypt, "scrypt not available") |
| 701 |
@override_settings(PASSWORD_HASHERS=PASSWORD_HASHERS) |
| 702 |
class TestUtilsHashPassScrypt(SimpleTestCase): |
| 703 |
def test_scrypt(self): |
| 704 |
encoded = make_password("lètmein", "seasalt", "scrypt") |
| 705 |
self.assertEqual( |
| 706 |
encoded, |
| 707 |
"scrypt$16384$seasalt$8$5$ECMIUp+LMxMSK8xB/IVyba+KYGTI7FTnet025q/1f" |
| 708 |
"/vBAVnnP3hdYqJuRi+mJn6ji6ze3Fbb7JEFPKGpuEf5vw==", |
| 709 |
) |
| 710 |
self.assertIs(is_password_usable(encoded), True) |
| 711 |
self.assertIs(check_password("lètmein", encoded), True) |
| 712 |
self.assertIs(check_password("lètmeinz", encoded), False) |
| 713 |
self.assertEqual(identify_hasher(encoded).algorithm, "scrypt") |
| 714 |
# Blank passwords. |
| 715 |
blank_encoded = make_password("", "seasalt", "scrypt") |
| 716 |
self.assertIs(blank_encoded.startswith("scrypt$"), True) |
| 717 |
self.assertIs(is_password_usable(blank_encoded), True) |
| 718 |
self.assertIs(check_password("", blank_encoded), True) |
| 719 |
self.assertIs(check_password(" ", blank_encoded), False) |
| 720 |
|
| 721 |
def test_scrypt_decode(self): |
| 722 |
encoded = make_password("lètmein", "seasalt", "scrypt") |
| 723 |
hasher = get_hasher("scrypt") |
| 724 |
decoded = hasher.decode(encoded) |
| 725 |
tests = [ |
| 726 |
("block_size", hasher.block_size), |
| 727 |
("parallelism", hasher.parallelism), |
| 728 |
("salt", "seasalt"), |
| 729 |
("work_factor", hasher.work_factor), |
| 730 |
] |
| 731 |
for key, excepted in tests: |
| 732 |
with self.subTest(key=key): |
| 733 |
self.assertEqual(decoded[key], excepted) |
| 734 |
|
| 735 |
def _test_scrypt_upgrade(self, attr, summary_key, new_value): |
| 736 |
hasher = get_hasher("scrypt") |
| 737 |
self.assertEqual(hasher.algorithm, "scrypt") |
| 738 |
self.assertNotEqual(getattr(hasher, attr), new_value) |
| 739 |
|
| 740 |
old_value = getattr(hasher, attr) |
| 741 |
try: |
| 742 |
# Generate hash with attr set to the new value. |
| 743 |
setattr(hasher, attr, new_value) |
| 744 |
encoded = make_password("lètmein", "seasalt", "scrypt") |
| 745 |
attr_value = hasher.safe_summary(encoded)[summary_key] |
| 746 |
self.assertEqual(attr_value, new_value) |
| 747 |
|
| 748 |
state = {"upgraded": False} |
| 749 |
|
| 750 |
def setter(password): |
| 751 |
state["upgraded"] = True |
| 752 |
|
| 753 |
# No update is triggered. |
| 754 |
self.assertIs(check_password("lètmein", encoded, setter, "scrypt"), True) |
| 755 |
self.assertIs(state["upgraded"], False) |
| 756 |
# Revert to the old value. |
| 757 |
setattr(hasher, attr, old_value) |
| 758 |
# Password is updated. |
| 759 |
self.assertIs(check_password("lètmein", encoded, setter, "scrypt"), True) |
| 760 |
self.assertIs(state["upgraded"], True) |
| 761 |
finally: |
| 762 |
setattr(hasher, attr, old_value) |
| 763 |
|
| 764 |
def test_scrypt_upgrade(self): |
| 765 |
tests = [ |
| 766 |
("work_factor", "work factor", 2**11), |
| 767 |
("block_size", "block size", 10), |
| 768 |
("parallelism", "parallelism", 2), |
| 769 |
] |
| 770 |
for attr, summary_key, new_value in tests: |
| 771 |
with self.subTest(attr=attr): |
| 772 |
self._test_scrypt_upgrade(attr, summary_key, new_value) |