Files
FileCodeBox/tests/test_password_hashing.py
T
Marrrrrrrrry 443cfe7472 feat: upgrade password hashing to memory-hard scrypt with transparent rehash
- new hashes use scrypt (n=2^14, r=8, p=1); sha256 and plaintext formats
  keep verifying unchanged, so existing deployments are unaffected
- on successful login a legacy-stored admin password is rehashed to scrypt
  in the settings KeyValue row
- add password_needs_rehash() and extend scheme detection; stdlib only, no
  new dependency
2026-09-09 16:33:07 +08:00

90 lines
3.4 KiB
Python

"""Tests for the scrypt password-hashing scheme and transparent rehash.
Legacy formats (sha256$salt$hash and plaintext) must keep verifying; new
hashes are scrypt (memory-hard). A successful login with a legacy-stored
password upgrades the stored hash in place.
"""
import asyncio
import hashlib
import unittest
from tests.helpers import SettingsOverrideMixin, close_db, init_memory_db
from apps.admin import views as admin_views
from apps.base.models import KeyValue
from core.settings import settings
from core.utils import (
hash_password,
is_password_hashed,
password_needs_rehash,
verify_password,
)
class PasswordHashSchemeTests(unittest.TestCase):
def test_scrypt_roundtrip_and_rejection(self):
hashed = hash_password("s3cret-pass")
self.assertTrue(hashed.startswith("scrypt$"))
self.assertTrue(verify_password("s3cret-pass", hashed))
self.assertFalse(verify_password("wrong-pass", hashed))
def test_legacy_sha256_and_plaintext_still_verify(self):
salt = "abcd1234"
sha_hash = f"sha256${salt}${hashlib.sha256(f'{salt}legacy'.encode()).hexdigest()}"
self.assertTrue(verify_password("legacy", sha_hash))
self.assertFalse(verify_password("other", sha_hash))
self.assertTrue(verify_password("plain", "plain"))
self.assertFalse(verify_password("plain", "other-plain"))
def test_scheme_detection_helpers(self):
scrypt_hash = hash_password("x")
sha_hash = "sha256$a$b"
self.assertTrue(is_password_hashed(scrypt_hash))
self.assertTrue(is_password_hashed(sha_hash))
self.assertFalse(is_password_hashed("plaintext"))
self.assertFalse(password_needs_rehash(scrypt_hash))
self.assertTrue(password_needs_rehash(sha_hash))
self.assertTrue(password_needs_rehash("plaintext"))
self.assertFalse(password_needs_rehash(""))
def test_malformed_hashes_are_rejected_not_crashing(self):
self.assertFalse(verify_password("x", "scrypt$1$2$3"))
self.assertFalse(verify_password("x", "scrypt$n$r$p$salt$zzz"))
self.assertFalse(verify_password("x", "sha256$only-two"))
self.assertFalse(verify_password("x", ""))
class TransparentRehashTests(SettingsOverrideMixin, unittest.TestCase):
def test_legacy_hash_upgraded_on_successful_login(self):
asyncio.run(self._scenario())
async def _scenario(self):
await init_memory_db()
try:
legacy_password = "old-style-pass"
salt = "feed1234"
legacy_hash = (
f"sha256${salt}${hashlib.sha256(f'{salt}{legacy_password}'.encode()).hexdigest()}"
)
record = await KeyValue.create(
key="settings",
value={"admin_token": legacy_hash, "jwt_secret": "s" * 48},
)
settings.user_config = dict(record.value)
ok = await admin_views.login(
type("D", (), {"password": legacy_password})(), ip="1.2.3.4"
)
self.assertEqual(ok.detail["username"], "admin")
upgraded = (await KeyValue.filter(key="settings").first()).value["admin_token"]
self.assertTrue(upgraded.startswith("scrypt$"), upgraded[:20])
self.assertTrue(verify_password(legacy_password, upgraded))
self.assertFalse(password_needs_rehash(upgraded))
finally:
await close_db()
if __name__ == "__main__":
unittest.main()