Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/hookrelay/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@


def compute(secret: str, body: bytes, algorithm: str = "sha256") -> str:
algo = getattr(hashlib, algorithm, None)
if algo is None or algorithm not in hashlib.algorithms_available:
if algorithm not in hashlib.algorithms_available:
raise ValueError(f"unsupported algorithm: {algorithm}")
return hmac.new(secret.encode("utf-8"), body, algo).hexdigest()
# pass the name straight to hmac/hashlib.new instead of getattr(hashlib, ...):
# some entries in algorithms_available (ripemd160, sm3, sha512_224, md5-sha1)
# have no matching hashlib module attribute and would falsely raise here.
return hmac.new(secret.encode("utf-8"), body, algorithm).hexdigest()


def verify(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ def test_verify_empty_provided():
def test_unsupported_algorithm():
with pytest.raises(ValueError):
compute("secret", b"x", algorithm="rot13")


def test_compute_algorithm_without_hashlib_attribute():
# ripemd160 is in hashlib.algorithms_available on most builds but has no
# hashlib.ripemd160 attribute, so getattr(hashlib, ...) would wrongly
# reject it even though it's a valid, config-approved algorithm.
if "ripemd160" not in hashlib.algorithms_available:
pytest.skip("ripemd160 not available on this build")
expected = hmac.new(b"secret", b"x", "ripemd160").hexdigest()
assert compute("secret", b"x", algorithm="ripemd160") == expected