fix(security): reject malformed Unicode passwords safely
Davood Mehraban committed
Nov 11, 2026 at 13:09 UTC
8ea415d236a4e98258d17455bace81b1a10433ba
2 files changed
+31
-5
backend/src/aram_backend/password_security.py
+17
-5
@@ -24,10 +24,18 @@ class PasswordVerification:
24
upgraded_hash: str | None = None
25
26
27
-def _validate_password(password: str) -> bytes:
27
+def _encode_password(password: str) -> bytes:
28
+ """Encode a password strictly; reject lone surrogates as invalid input."""
29
if not isinstance(password, str):
30
raise TypeError("invalid password")
30
- raw = password.encode("utf-8")
31
+ try:
32
+ return password.encode("utf-8")
33
+ except UnicodeEncodeError as exc:
34
+ raise ValueError("invalid password") from exc
35
+
36
+
37
+def _validate_password(password: str) -> bytes:
38
+ raw = _encode_password(password)
39
if not 14 <= len(password) <= 1024 or len(raw) > _MAX_PASSWORD_BYTES:
40
raise ValueError("invalid password")
41
return raw
@@ -41,7 +49,11 @@ def hash_password(password: str) -> str:
49
def verify_password(password: str, encoded: str) -> PasswordVerification:
50
if not isinstance(password, str) or not isinstance(encoded, str):
51
return PasswordVerification(False)
44
- if len(password) > 1024 or len(password.encode("utf-8")) > _MAX_PASSWORD_BYTES:
52
+ try:
53
+ raw = _encode_password(password)
54
+ except (TypeError, ValueError):
55
+ return PasswordVerification(False)
56
+ if len(password) > 1024 or len(raw) > _MAX_PASSWORD_BYTES:
57
return PasswordVerification(False)
58
if encoded.startswith("$argon2id$"):
59
try:
@@ -58,7 +70,7 @@ def verify_password(password: str, encoded: str) -> PasswordVerification:
70
expected = bytes.fromhex(parts[3])
71
if len(salt) != 32 or len(expected) != 32:
72
return PasswordVerification(False)
61
- candidate = hashlib.pbkdf2_hmac("sha256", password.encode("utf-8"), salt, _LEGACY_ITERATIONS)
73
+ candidate = hashlib.pbkdf2_hmac("sha256", raw, salt, _LEGACY_ITERATIONS)
74
if not hmac.compare_digest(candidate, expected):
75
return PasswordVerification(False)
76
return PasswordVerification(True, hash_password(password))
@@ -70,4 +82,4 @@ def encode_legacy_credential(salt: bytes, digest: bytes) -> str:
82
"""Serialize existing credential hashes without ever exposing the password."""
83
if not isinstance(salt, bytes) or len(salt) != 32 or not isinstance(digest, bytes) or len(digest) != 32:
84
raise ValueError("invalid legacy credential")
73
- return f"pbkdf2_sha256${_LEGACY_ITERATIONS}${salt.hex()}${digest.hex()}"
85
+ return "pbkdf2_sha256$" + str(_LEGACY_ITERATIONS) + "$" + salt.hex() + "$" + digest.hex()
backend/tests/test_password_security.py
+14
@@ -50,6 +50,20 @@ def test_password_unicode_and_boundary():
50
assert verify_password("x" * 1025, hash_password(PASSWORD)).valid is False
51
52
53
+def test_malformed_unicode_is_rejected_without_raising_during_verification():
54
+ malformed = "valid-prefix-\ud800"
55
+ encoded = hash_password(PASSWORD)
56
+ assert not verify_password(malformed, encoded).valid
57
+
58
+ salt = secrets.token_bytes(32)
59
+ digest = hashlib.pbkdf2_hmac("sha256", PASSWORD.encode("utf-8"), salt, 600_000)
60
+ legacy = encode_legacy_credential(salt, digest)
61
+ assert not verify_password(malformed, legacy).valid
62
+
63
+ with pytest.raises(ValueError, match="invalid password"):
64
+ hash_password(malformed)
65
+
66
+
67
def test_reject_bad_legacy_material():
68
with pytest.raises(ValueError):
69
encode_legacy_credential(b"x", b"y")