|
| 1 | +.. SPDX-FileCopyrightText: 2026 cusy GmbH |
| 2 | +.. |
| 3 | +.. SPDX-License-Identifier: BSD-3-Clause |
| 4 | +
|
| 5 | +Software Bill of Materials (SBOM) |
| 6 | +================================= |
| 7 | + |
| 8 | +A Software Bill of Materials (SBOM) is a document used to exchange information |
| 9 | +about software and its composition. This format is primarily used in the |
| 10 | +security field to check software and its dependencies for vulnerabilities using |
| 11 | +vulnerability databases such as `CVE <https://www.cve.org/>`_ and `OSV |
| 12 | +<https://osv.dev/>`_. The SBOM format used by the CPython project is `SPDX |
| 13 | +<https://spdx.github.io/spdx-spec/v3.0.1/model/Software/Classes/Sbom/>`_, which |
| 14 | +can be converted to other formats as needed. The SBOM file for the dependencies |
| 15 | +included in CPython is maintained at `Misc/sbom.spdx.json |
| 16 | +<https://github.com/python/cpython/blob/main/Misc/sbom.spdx.json>`_. The file is |
| 17 | +generated using `Tools/build/generate_sbom.py |
| 18 | +<https://github.com/python/cpython/blob/main/Tools/build/generate_sbom.py>`_. |
| 19 | + |
| 20 | +Generating an SBOM File |
| 21 | +----------------------- |
| 22 | + |
| 23 | +… with uv |
| 24 | +~~~~~~~~~ |
| 25 | + |
| 26 | +:term:`uv` provides an easy way to generate an SBOM file in the CycloneDX v1.5 |
| 27 | +format using: |
| 28 | + |
| 29 | +.. code-block:: console |
| 30 | +
|
| 31 | + $ uv export --format='cyclonedx1.5' > sbom.cdx.json |
| 32 | +
|
| 33 | +However, the file contains only very basic information; for example, for |
| 34 | +`cusy.tasks <https://github.com/cusyio/cusy.tasks>`_: |
| 35 | + |
| 36 | +.. code-block:: json |
| 37 | +
|
| 38 | + "component": { |
| 39 | + "type": "library", |
| 40 | + "bom-ref": "cusy-tasks-1@26.2.0", |
| 41 | + "name": "cusy-tasks", |
| 42 | + "version": "26.2.0", |
| 43 | + "properties": [ |
| 44 | + { |
| 45 | + "name": "uv:package:is_project_root", |
| 46 | + "value": "true" |
| 47 | + } |
| 48 | + ] |
| 49 | + } |
| 50 | +
|
| 51 | +Using ``uv export --all-groups --format='cyclonedx1.5' > sbom.cdx.json``, you |
| 52 | +can also include all dependency groups in the SBOM file. |
| 53 | + |
| 54 | +… using CycloneDX Python |
| 55 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 56 | + |
| 57 | +The output from `CycloneDX Python |
| 58 | +<https://cyclonedx-bom-tool.readthedocs.io/en/latest/index.html>`_ is |
| 59 | +considerably more comprehensive: |
| 60 | + |
| 61 | +.. code-block:: json |
| 62 | +
|
| 63 | + { |
| 64 | + "bom-ref": "cusy-tasks==26.2.0", |
| 65 | + "description": "", |
| 66 | + "externalReferences": [ |
| 67 | + { |
| 68 | + "comment": "PackageSource: Local", |
| 69 | + "type": "distribution", |
| 70 | + "url": "file:///Users/veit/cusy/prj/cusy.tasks" |
| 71 | + }, |
| 72 | + { |
| 73 | + "comment": "from packaging metadata Project-URL: Documentation", |
| 74 | + "type": "documentation", |
| 75 | + "url": "https://tasks.cusy.io/" |
| 76 | + }, |
| 77 | + { |
| 78 | + "comment": "from packaging metadata Project-URL: Mastodon", |
| 79 | + "type": "other", |
| 80 | + "url": "https://mastodon.social/@Python4DataScience" |
| 81 | + }, |
| 82 | + { |
| 83 | + "comment": "from packaging metadata Project-URL: GitHub", |
| 84 | + "type": "vcs", |
| 85 | + "url": "https://github.com/cusyio/cusy.tasks" |
| 86 | + } |
| 87 | + ], |
| 88 | + "licenses": [ |
| 89 | + { |
| 90 | + "license": { |
| 91 | + "acknowledgement": "declared", |
| 92 | + "id": "BSD-3-Clause" |
| 93 | + } |
| 94 | + } |
| 95 | + ], |
| 96 | + "name": "cusy-tasks", |
| 97 | + "type": "library", |
| 98 | + "version": "26.2.0" |
| 99 | + } |
| 100 | +
|
| 101 | +The command to generate the file is: |
| 102 | + |
| 103 | +.. code-block:: console |
| 104 | +
|
| 105 | + $ uvx --from cyclonedx-bom cyclonedx-py environment .venv --output-file sbom.cdx.json |
| 106 | +
|
| 107 | +.. warning:: |
| 108 | + CycloneDX Python generates the SBOM file from the current :file:`.venv` |
| 109 | + directory. So if you run ``cyclonedx-bom`` in your development environment, |
| 110 | + you will also find all your development tools listed in your SBOM file. |
| 111 | + |
| 112 | +… with sbomify |
| 113 | +~~~~~~~~~~~~~~ |
| 114 | + |
| 115 | +sbomify provides a GitHub Action for generating the SBOM file, which uses |
| 116 | +CycloneDX Python under the bonnet. The file can then be attested using |
| 117 | +`actions/attest-sbom <https://github.com/actions/attest-sbom>`_: |
| 118 | + |
| 119 | +.. literalinclude:: sbomify.yml |
| 120 | + :caption: .github/workflows/sbomify.yml |
| 121 | + :language: yaml |
| 122 | + |
| 123 | +In GitLab CI, you can use sbomify as follows: |
| 124 | + |
| 125 | +.. literalinclude:: .gitlab-ci.yml |
| 126 | + :caption: .gitlab-ci.yml |
| 127 | + :language: yaml |
| 128 | + :lines: 1-12, 32- |
| 129 | + |
| 130 | +You can also integrate dependency scanning: |
| 131 | + |
| 132 | +.. literalinclude:: .gitlab-ci.yml |
| 133 | + :caption: .gitlab-ci.yml |
| 134 | + :language: yaml |
| 135 | + :lines: 13-30 |
| 136 | + |
| 137 | +.. seealso:: |
| 138 | + * `SBOM Generation in CI/CD Pipelines <https://sbomify.com/guides/ci-cd/>`_ |
| 139 | + * `Dependency scanning by using SBOM |
| 140 | + <https://docs.gitlab.com/user/application_security/dependency_scanning/dependency_scanning_sbom/>`_ |
| 141 | + |
| 142 | +PEP 770 – SBOMs in Python packages |
| 143 | +---------------------------------- |
| 144 | + |
| 145 | +:pep:`770` standardises the inclusion of SBOMs in Python wheels via the |
| 146 | +:file:`.dist-info/sboms/` directory. If you publish Python packages on |
| 147 | +:term:`PyPI`, this means that your users will automatically receive the SBOM |
| 148 | +file when they install your package, for example: |
| 149 | + |
| 150 | +.. code-block:: console |
| 151 | +
|
| 152 | + myapp-26.2.0.dist-info |
| 153 | + ├── METADATA |
| 154 | + ├── RECORD |
| 155 | + └── sboms/ |
| 156 | + └── myapp.cdx.json |
| 157 | +
|
| 158 | +Build backends such as Hatchling ≥ 1.28 already support this via the |
| 159 | +``sbom-files`` configuration: |
| 160 | + |
| 161 | +.. code-block:: toml |
| 162 | + :caption: pyproject.toml |
| 163 | +
|
| 164 | + [tool.hatch.build.targets.wheel] |
| 165 | + sbom-files = ["myapp.cdx.json"] |
| 166 | +
|
| 167 | +Instead of generating the SBOM file from scratch during the build, a minimal |
| 168 | +CycloneDX SBOM file can be checked into the repository: |
| 169 | + |
| 170 | +.. code-block:: json |
| 171 | +
|
| 172 | + { |
| 173 | + "bomFormat": "CycloneDX", |
| 174 | + "specVersion": "1.6", |
| 175 | + "version": 1, |
| 176 | + "metadata": { |
| 177 | + "component": { |
| 178 | + "type": "library", |
| 179 | + "name": "mylib", |
| 180 | + "version": "0.0.0-placeholder" |
| 181 | + } |
| 182 | + }, |
| 183 | + "components": [] |
| 184 | + } |
| 185 | +
|
| 186 | +In the :term:`CI` workflow, the placeholder SBOM file is then checked out along |
| 187 | +with the code. sbomify then enriches it with the latest information. Hatchling |
| 188 | +subsequently builds the wheel using the latest SBOM file, and finally the :term:`wheel` is published to :term:`PyPI`. |
| 189 | + |
| 190 | +Analysis |
| 191 | +-------- |
| 192 | + |
| 193 | +There are numerous tools available for security and licence checks, each |
| 194 | +focusing on different problem areas. Two open-source tools for SBOM analysis are |
| 195 | +`Dependency Track <https://dependencytrack.org>`_ and `GUAC <https://guac.sh>`_. |
| 196 | +With ``sbomify-action``, you can upload SBOMs directly from the :term:`CI` |
| 197 | +pipeline to your Dependency Track instance using: |
| 198 | + |
| 199 | +.. code-block:: yaml |
| 200 | +
|
| 201 | + UPLOAD_DESTINATIONS=dependency-track |
0 commit comments