MD5 or SHA-1 is used without usedforsecurity=False

tx0/python:weak-hashrecommendedwarningsecurity, cwe-328

What it finds

hashlib.md5(data), hashlib.sha1(data): collisions for MD5 and SHA-1 can be made on purpose, so neither protects signatures, certificates, tokens or passwords any more (CWE-328). SHA-256 or newer, or a password hash such as hashlib.scrypt, fits those uses.

Calls that say usedforsecurity=False, which Python 3.9 added for checksums and cache keys, are left alone. hashlib.new is reported when its name is a literal naming one of the weak algorithms.

From the rule's source, weak_hash.rs.

Example from the rule's tests
import hashlibfrom hashlib import md5  def digest(data):    a = hashlib.md5(data)tx0 reports: MD5 is not safe against collisions made on purpose    b = hashlib.sha1(data).hexdigest()tx0 reports: SHA-1 is not safe    c = md5(data)tx0 reports: MD5 is not safe    d = hashlib.new("sha1", data)tx0 reports: SHA1 is not safe    e = hashlib.md5(data, usedforsecurity=False)    f = hashlib.sha256(data)    g = hashlib.new("sha256")    return a, b, c, d, e, f, g
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:weak-hash" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:weak-hash] reason="..."

tx0 explain tx0/python:weak-hash shows the same in the terminal. Found a false positive? Report it.