FIXUP: Invalidate downloaded diffoci helper if checksum differs

This commit is contained in:
Alex Pyrgiotis 2025-01-14 23:28:07 +02:00
parent 6cf4c5cc46
commit c1f25484ff
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA

View file

@ -39,15 +39,27 @@ def git_verify(commit, source):
) )
def diffoci_hash_matches(diffoci):
"""Check if the hash of the downloaded diffoci bin matches the expected one."""
m = hashlib.sha256()
m.update(DIFFOCI_PATH.open().read())
diffoci_checksum = m.hexdigest()
return diffoci_checksum == DIFFOCI_CHECKSUM
def diffoci_exists():
"""Check if the diffoci helper exists, and if the hash matches."""
if not DIFFOCI_PATH.exists():
return False
return diffoci_hash_matches(DIFFOCI_PATH.open().read())
def diffoci_download(): def diffoci_download():
"""Download the diffoci tool, based on a URL and its checksum.""" """Download the diffoci tool, based on a URL and its checksum."""
with urllib.request.urlopen(DIFFOCI_URL) as f: with urllib.request.urlopen(DIFFOCI_URL) as f:
diffoci_bin = f.read() diffoci_bin = f.read()
m = hashlib.sha256() if not diffoci_hash_matches(diffoci_bin):
m.update(diffoci_bin)
diffoci_checksum = m.hexdigest()
if not diffoci_checksum == DIFFOCI_CHECKSUM:
raise ValueError( raise ValueError(
"Unexpected checksum for downloaded diffoci binary:" "Unexpected checksum for downloaded diffoci binary:"
f" {diffoci_checksum} !={DIFFOCI_CHECKSUM}" f" {diffoci_checksum} !={DIFFOCI_CHECKSUM}"
@ -122,7 +134,7 @@ def main():
commit = git_commit_get() commit = git_commit_get()
git_verify(commit, args.source) git_verify(commit, args.source)
if not DIFFOCI_PATH.exists(): if diffoci_exists():
logger.info(f"Downloading diffoci helper from {DIFFOCI_URL}") logger.info(f"Downloading diffoci helper from {DIFFOCI_URL}")
diffoci_download() diffoci_download()