Lib4SBOM
Lib4SBOM is a Python library designed for parsing, generating, and validating Software Bills of Materials (SBOMs). It supports both SPDX and CycloneDX formats, offering a generic abstraction for SBOM data regardless of the underlying specification. Currently at version 0.10.3, the library maintains an active development pace with frequent minor releases and regular feature updates, addressing new specification versions and user-reported issues.
Common errors
-
SBOMParserException: Error parsing SBOM file
cause An error occurred during the internal processing or validation of the SBOM file content, or the file is malformed.fixExamine the traceback for more details. Check the input SBOM file for syntax errors or adherence to its declared specification. Try parsing with `sbom_type='auto'` or explicitly specifying the type to help narrow down the issue. -
FileNotFoundError: [Errno 2] No such file or directory: 'your_sbom_file.json'
cause The specified SBOM file path does not exist or is incorrect.fixVerify that the file path provided to `parse_file()` is correct and accessible. Use an absolute path or ensure the file is in the current working directory. -
SBOM parser returns empty lists (e.g., for packages, files, relationships) when parsing a valid CycloneDX 1.5 JSON file.
cause The parser might not correctly detect or fully support certain nuances of newer CycloneDX versions, leading to data extraction failures, even if the file seems syntactically valid. This was reported for CycloneDX 1.5 JSON.fixExplicitly set `sbom_type='cyclonedx'` in the `SBOMParser` constructor. If the issue persists, consider downgrading the CycloneDX spec version if possible, or review GitHub issues for specific fixes related to CycloneDX 1.5+ parsing.
Warnings
- breaking Major version updates (e.g., v0.9.0, v0.10.0) introduce support for newer SPDX and CycloneDX specifications (e.g., CycloneDX 1.7, SPDX3). While efforts are made for backward compatibility, ensure your schemas and data adhere to the expected version, especially when converting between formats.
- gotcha The `SBOMParser`'s `auto` detection mode relies on file extensions and content heuristics. Providing an incorrect file type (e.g., an SPDX JSON to a CycloneDX parser) or a non-standard file extension can lead to silent failures or empty results.
- breaking When converting SBOMs, especially from SPDX 2 to SPDX 3, specific license expressions (e.g., `Apache-2.0 WITH LLVM-exception` or `Apache-1.0+`) can be lost or incorrectly handled, leading to compliance issues.
- gotcha The library relies on various external schema validators (e.g., `jsonschema`, `xmlschema`). Issues with these dependencies or schema mismatches can cause validation failures, even if the SBOM content appears correct.
Install
-
pip install lib4sbom
Imports
- SBOMParser
from lib4sbom.parser import SBOMParser
- SBOMGenerator
from lib4sbom.generator import SBOMGenerator
- SBOMOutput
from lib4sbom.output import SBOMOutput
- SBOM
from lib4sbom.generator import SBOM
from lib4sbom.sbom import SBOM
Quickstart
import os
from lib4sbom.parser import SBOMParser
# Create a dummy SPDX SBOM file for demonstration
sbom_content = """
SPDXVersion: SPDX-2.3
DataLicense: CC0-1.0
SPDXID: SPDXRef-DOCUMENT
DocumentName: example-sbom
DocumentNamespace: https://spdx.org/spdxdocs/spdx-example-44455566-31b3-40e1-b4f0-4660f9450c26
Creator: Tool: lib4sbom-example
Created: 2026-04-16T12:00:00Z
PackageName: SamplePackage
SPDXID: SPDXRef-Package-Sample
PackageVersion: 1.0.0
PackageSupplier: Organization: Example Org (contact@example.org)
PackageDownloadLocation: NOASSERTION
PackageLicenseConcluded: MIT
PackageLicenseDeclared: MIT
PackageCopyrightText: NOASSERTION
"""
example_sbom_file = "example.spdx"
with open(example_sbom_file, "w") as f:
f.write(sbom_content)
# Initialize the SBOM parser
sbom_parser = SBOMParser(sbom_type='spdx') # 'auto' can also be used, or 'cyclonedx'
# Parse the SBOM file
try:
sbom_parser.parse_file(example_sbom_file)
print(f"Successfully parsed SBOM type: {sbom_parser.get_type()}")
# Retrieve packages and print their names
packages = sbom_parser.get_packages()
if packages:
print("Packages found:")
for pkg in packages:
print(f" - {pkg.get_name()} ({pkg.get_version()})")
else:
print("No packages found in SBOM.")
except FileNotFoundError:
print(f"Error: SBOM file '{example_sbom_file}' not found.")
except Exception as e:
print(f"An error occurred during parsing: {e}")
finally:
# Clean up the dummy file
if os.path.exists(example_sbom_file):
os.remove(example_sbom_file)