Skip to content

Commit 32024d3

Browse files
committed
fix(secrets): recognise password hashes by their format's shape
Review of the hash check: an algorithm identifier followed by plaintext, such as $2y$Summer2024!, was skipped, while Werkzeug, Django, md5-crypt, apr1 and yescrypt hashes were still reported. A value is now a hash when the identifier is followed by fields of its format's shape, or stands alone as an incomplete hash. Refs #211
1 parent 91eda0b commit 32024d3

2 files changed

Lines changed: 49 additions & 14 deletions

File tree

‎src/coretrace_python/plugins/secrets.py‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,31 @@
4747
# Subresource integrity, as in lock files: ``sha512-<base64>``.
4848
_INTEGRITY = re.compile(r"^(md5|sha1|sha256|sha384|sha512)-[A-Za-z0-9+/=]+$", re.IGNORECASE)
4949
_ALPHABET_WORDS = ("alphabet", "charset", "characters", "letters", "digits")
50-
# PHC strings and modular crypt strings open with their algorithm: argon2, bcrypt,
51-
# SHA-crypt, PBKDF2 and scrypt.
52-
_PASSWORD_HASH = re.compile(r"^\$(argon2(i|d|id)|2[abxy]|5|6|pbkdf2(-sha(1|256|512))?|scrypt)\$")
50+
# A password hash names its algorithm, then its parameters, salt and digest, each in a
51+
# hash alphabet: modular crypt and PHC strings (``$6$salt$digest``, ``$argon2id$v=19$...``;
52+
# bcrypt has a fixed shape), Werkzeug's ``method:parameters$salt$hex digest`` and
53+
# Django's ``algorithm$...$digest``. An algorithm identifier alone is an incomplete hash.
54+
_HASH_FIELD = r"[A-Za-z0-9./+=,-]"
55+
_CRYPT_ALGORITHMS = r"1|5|6|7|y|apr1|argon2(?:i|d|id)|pbkdf2(?:-sha(?:1|256|512))?|scrypt"
56+
_PASSWORD_HASHES = tuple(
57+
re.compile(regex)
58+
for regex in (
59+
r"^\$2[abxy]\$\d\d\$[./A-Za-z0-9]{53}$",
60+
rf"^\$(?:{_CRYPT_ALGORITHMS}|2[abxy])\$$",
61+
rf"^\$(?:{_CRYPT_ALGORITHMS})(?:\${_HASH_FIELD}+){{2,}}$",
62+
r"^(?:scrypt|pbkdf2):[a-z0-9:]+\$[^$\s]+\$[0-9a-f]+$",
63+
rf"^(?:pbkdf2_sha(?:1|256)|argon2|bcrypt(?:_sha256)?|scrypt|md5|sha1)(?:\${_HASH_FIELD}*){{2,}}$",
64+
)
65+
)
5366

5467

5568
def is_password_hash(value: str) -> bool:
56-
"""A password hash in PHC or modular crypt format, recognised by its algorithm
57-
identifier: what an application stores instead of the password, which it does not
58-
reveal, even when the rest of the hash is incomplete."""
69+
"""A password hash of a known format, which an application stores instead of the
70+
password and which does not reveal it: the algorithm identifier followed by fields
71+
of that format's shape, or the identifier alone. An identifier followed by anything
72+
else (``$2y$Summer2024!``) is a plaintext password."""
5973

60-
return _PASSWORD_HASH.match(value) is not None
74+
return any(pattern.match(value) is not None for pattern in _PASSWORD_HASHES)
6175

6276

6377
def looks_like_digest(value: str, name: str | None) -> bool:

‎tests/test_password_hashes.py‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Acceptance tests for issue #211: password hashes are not hardcoded passwords.
22
3-
A value in a password-hash format (a PHC string such as ``$argon2id$...`` or a modular
4-
crypt string such as ``$2b$...`` or ``$6$...``) is what an application stores instead of
5-
the password; it does not reveal the password, so a credential-like name bound to it is
6-
not a hardcoded credential. The algorithm identifier is what tells a hash: an incomplete
7-
hash such as ``$argon2id$`` reveals nothing either. A plaintext value bound to the same
8-
name is still reported, dollar signs included.
3+
A value in a password-hash format is what an application stores instead of the
4+
password; it does not reveal the password, so a credential-like name bound to it is not
5+
a hardcoded credential. The formats are PHC and modular crypt strings (``$argon2id$...``,
6+
``$2b$...``, ``$6$...``), and the encodings of Werkzeug (``scrypt:32768:8:1$...``) and
7+
Django (``pbkdf2_sha256$...``). A hash has the shape of its format: an algorithm
8+
identifier followed by text that is not a hash (``$2y$Summer2024!``) is a plaintext
9+
password, while the identifier alone (``$argon2id$``, an incomplete hash) reveals
10+
nothing. A plaintext value bound to the same name is still reported.
911
"""
1012

1113
from __future__ import annotations
@@ -39,13 +41,32 @@ def check(text: str) -> tuple[Finding, ...]:
3941
"$6$saltsalt$qFmFH.bQmmtXzyBY0s9v7Oicd2z4XSIecDzlB5KiA2/jctKu9YterLp8wwnSq.qc.eoxqOmSuNp2xS0ktL3nh/",
4042
"$pbkdf2-sha256$29000$N2YMIWQsBWBMae09x1jrPQ$1t8iyB2A.WF/Z5JZv.lfCIhXXN33N23OSgQYThBYRfk",
4143
"$scrypt$ln=16,r=8,p=1$aM15713r3Xsvxbi31lqr1Q$nFNh2CVHVjNldFVKDHDlm4CbdRSCdEBsjjJxD+iCs5E",
44+
"$1$O3JMY.Tw$AdLnLjQ/5jXF9.MTp3gHv/",
45+
"$apr1$71850310$gh9m4xcAn3MGxogwX/ztb.",
46+
"$y$j9T$F5Jx5fExrKuPp53xLKQ..1$X3DX6M94c7o.9agCG9G317fhZg9SqC.5i5rd.RhAtQ7",
47+
"scrypt:32768:8:1$KbI3sQ1x9cBnGz7a$d624ca2ead7454931117ad5ab4342ed9d93ae0407f730264d97f655506d36fb308d1ddb7f4c5f9046884ba4516df4b6e492ba26a31786ff43bec1f2d8999bc47",
48+
"pbkdf2:sha256:600000$KbI3sQ1x9cBnGz7a$57fab0cc5de31d8fb5518804d402a6dd807e483ba7c6e3968a10cd466d28c46e",
49+
"pbkdf2_sha256$870000$Qx7mR2vT9kLp4sWz$q8bVU34ZbIqSroAvvFgZhHZ6jNeCNMPqYZOg52ta260=",
50+
"argon2$argon2id$v=19$m=102400,t=2,p=8$c29tZXNhbHQ$ieVgG5ysTJFx4k/KvmC9aQ",
51+
"bcrypt_sha256$$2b$12$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy",
52+
"scrypt$16384$c29tZXNhbHQ$8$1$nFNh2CVHVjNldFVKDHDlm4CbdRSCdEBsjjJxD+iCs5E=",
4253
],
4354
)
4455
def test_a_password_hash_is_not_a_hardcoded_credential(value: str) -> None:
4556
assert check(f'web_password = "{value}"\n') == ()
4657

4758

48-
@pytest.mark.parametrize("value", ["hunter2-not-a-placeholder", "pa$$w0rd-really", "$ecret$value"])
59+
@pytest.mark.parametrize(
60+
"value",
61+
[
62+
"hunter2-not-a-placeholder",
63+
"pa$$w0rd-really",
64+
"$ecret$value",
65+
"$2y$Summer2024!",
66+
"$scrypt$MyRealPassw0rd",
67+
"$5$hunter2-plain",
68+
],
69+
)
4970
def test_a_plaintext_password_is_still_reported(value: str) -> None:
5071
(finding,) = check(f'web_password = "{value}"\n')
5172

0 commit comments

Comments
 (0)