Skip to content
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
41 changes: 41 additions & 0 deletions sbom.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,38 @@
recursive_sort_in_place(cast(dict[str, Any], sbom_data))


def check_sbom_data(sbom_data: SBOM) -> None:
"""Check SBOM data for common issues"""

def check_id_duplicates(sbom_components: list[Package] | list[File]) -> set[str]:
all_ids = set()
for sbom_component in sbom_components:
sbom_component_id = sbom_component["SPDXID"]
assert sbom_component_id not in all_ids
all_ids.add(sbom_component_id)
return all_ids

Check warning on line 235 in sbom.py

View check run for this annotation

Codecov / codecov/patch

sbom.py#L229-L235

Added lines #L229 - L235 were not covered by tests

all_package_ids = check_id_duplicates(sbom_data["packages"])
all_file_ids = check_id_duplicates(sbom_data["files"])

Check warning on line 238 in sbom.py

View check run for this annotation

Codecov / codecov/patch

sbom.py#L237-L238

Added lines #L237 - L238 were not covered by tests

# Check that no files and packages have the same ID.
assert not all_package_ids.intersection(all_file_ids)
all_sbom_ids = all_package_ids | all_file_ids

Check warning on line 242 in sbom.py

View check run for this annotation

Codecov / codecov/patch

sbom.py#L241-L242

Added lines #L241 - L242 were not covered by tests

# Check that all relationships use existing IDs.
for sbom_relationship in sbom_data["relationships"]:

Check warning on line 245 in sbom.py

View check run for this annotation

Codecov / codecov/patch

sbom.py#L245

Added line #L245 was not covered by tests

# The exception being 'DESCRIBES' with the meta 'document' ID
if (

Check warning on line 248 in sbom.py

View check run for this annotation

Codecov / codecov/patch

sbom.py#L248

Added line #L248 was not covered by tests
sbom_relationship["spdxElementId"] == "SPDXRef-DOCUMENT"
and sbom_relationship["relationshipType"] == "DESCRIBES"
):
continue

Check warning on line 252 in sbom.py

View check run for this annotation

Codecov / codecov/patch

sbom.py#L252

Added line #L252 was not covered by tests

assert sbom_relationship["spdxElementId"] in all_sbom_ids
assert sbom_relationship["relatedSpdxElement"] in all_sbom_ids

Check warning on line 255 in sbom.py

View check run for this annotation

Codecov / codecov/patch

sbom.py#L254-L255

Added lines #L254 - L255 were not covered by tests


def fetch_package_metadata_from_pypi(
project: str, version: str, filename: str | None = None
) -> tuple[str, str]:
Expand Down Expand Up @@ -686,6 +718,11 @@
with (cpython_source_dir / "Misc/sbom.spdx.json").open() as f:
source_sbom_data = json.loads(f.read())
for sbom_package in source_sbom_data["packages"]:
# Update the SPDX ID to avoid collisions with
# the 'externals' SBOM.
sbom_package["SPDXID"] = spdx_id(

Check warning on line 723 in sbom.py

View check run for this annotation

Codecov / codecov/patch

sbom.py#L723

Added line #L723 was not covered by tests
f"SPDXRef-PACKAGE-{sbom_package['name']}-{sbom_package['versionInfo']}"
)
sbom_data["packages"].append(sbom_package)

create_cpython_sbom(
Expand Down Expand Up @@ -755,6 +792,10 @@

# Normalize SBOM data for reproducibility.
normalize_sbom_data(sbom_data)

# Check SBOM for validity.
check_sbom_data(sbom_data)

with open(artifact_path + ".spdx.json", mode="w") as f:
f.truncate()
f.write(json.dumps(sbom_data, indent=2, sort_keys=True))
Expand Down