Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hash collision detection #49

Merged
merged 3 commits into from
Oct 16, 2023
Merged
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
26 changes: 25 additions & 1 deletion surfactant/cmd/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pathlib
import re
import sys
from typing import Dict, List, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union

import click
from loguru import logger
Expand Down Expand Up @@ -102,6 +102,28 @@ def print_output_formats(ctx, _, value):
ctx.exit()


def warn_if_hash_collision(soft1: Optional[Software], soft2: Optional[Software]):
if not soft1 or not soft2:
return
# A hash collision occurs if one or more but less than all hashes match or
# any hash matches but the filesize is different
collision = False
if soft1.sha256 == soft2.sha256 or soft1.sha1 == soft2.sha1 or soft1.md5 == soft2.md5:
# Hashes can be None; make sure they aren't before checking for inequality
if soft1.sha256 and soft2.sha256 and soft1.sha256 != soft2.sha256:
collision = True
elif soft1.sha1 and soft2.sha1 and soft1.sha1 != soft2.sha1:
collision = True
elif soft1.md5 and soft2.md5 and soft1.md5 != soft2.md5:
collision = True
elif soft1.size != soft2.size:
collision = True
if collision:
logger.warn(
f"Hash collision between {soft1.name} and {soft2.name}; unexpected results may occur"
)


@click.command("generate")
@click.argument("config_file", envvar="CONFIG_FILE", type=click.Path(exists=True), required=True)
@click.argument("sbom_outfile", envvar="SBOM_OUTPUT", type=click.File("w"), required=True)
Expand Down Expand Up @@ -209,6 +231,7 @@ def sbom(
pm, new_sbom, entry["archive"], user_institution_name=recorded_institution
)
archive_entry = new_sbom.find_software(parent_entry.sha256)
warn_if_hash_collision(archive_entry, parent_entry)
if archive_entry:
parent_entry = archive_entry
else:
Expand Down Expand Up @@ -315,6 +338,7 @@ def sbom(
# if a software entry already exists with a matching file hash, augment the info in the existing entry
for e in entries:
existing_sw = new_sbom.find_software(e.sha256)
warn_if_hash_collision(existing_sw, e)
if not existing_sw:
new_sbom.add_software(e)
# if the config file specified a parent/container for the file, add the new entry as a "Contains" relationship
Expand Down