Quick question for anyone who has had to hand over an SBOM for a Python service (customer questionnaire, security review, or just your own "what is actually running in prod?").
There are two places you can build it from, and they don't always agree:
- What you declared: requirements.txt, a lock file, pyproject.toml.
- What is actually installed in the environment that runs the code: the venv or the container image.
The second one picks up things the first can miss: transitive dependencies you never pinned, a package someone pip installed by hand to debug something, or a version that drifted because a pin was looser than you thought.
Disclosure: I built a small tool for the second approach, so I'm biased. It's a single stdlib Python file that reads the installed packages with importlib.metadata and writes CycloneDX 1.5 JSON with Package URLs, fully offline (no PyPI calls). Writing it surfaced a few details I didn't expect:
-
Name normalization matters. Package URLs expect the normalized PyPI name, so
Ruamel.Yamlhas to becomeruamel-yaml. If two tools normalize differently, diffing their SBOMs gets noisy fast. -
License data is only as good as the package metadata. It can come from
License-Expression, the free-textLicensefield, or a classifier, and some packages fill in none of them. - The same name can show up twice in one environment (duplicate distributions), so you have to decide how to report it.
- An inventory is not a vulnerability scan. The SBOM tells you what's there; matching it against CVEs is a separate step on top.
So I'm curious how others handle it:
- Do you generate the SBOM from the lock file, from the built image, or both?
- If you scan the image, do you run something like Syft/Trivy in CI, or generate it inside the running environment?
- Has anyone actually been asked for an SBOM by a customer, and in which format: CycloneDX or SPDX?
- How do you deal with missing or messy license fields?
Would love to hear what your pipeline looks like, especially for small teams that don't have a dedicated security person.
Top comments (8)
From what's actually installed, every time — the lock file is what you intended to ship, the image is what you actually shipped, and the drift between them is the whole point of the exercise. A hand-installed debug package or a looser pin than you thought is exactly the kind of thing that never shows up in requirements.txt.
The normalization point is the one I'd underline: if two tools normalize Ruamel.Yaml differently, diffing their SBOMs turns into noise, and then nobody trusts either one. On the duplicate-distributions question, I'd report both with where each came from rather than deduping silently — dropping one hides which version actually loads.
One question on the license gap: when a package has no license metadata at all, do you leave the field out or mark it unknown? Absent and unknown read very differently to whoever is reviewing it, and I'd guess most tools pick one without saying so.
Thanks! You guessed right: the lite version picks one without saying so. When a package has no license metadata, the licenses field is simply left out, so "absent" and "not declared" look the same, and that's a fair criticism. Duplicates work the same way: it keeps the first distribution it finds for a normalized name and drops the rest. That's usually the one that imports, but you can't see the shadowed one. Listing both with their paths is a better default, and I'm adding it to the list.
Both, at different points. The lock file is useful at PR time because it can fail a build before anything ships. The built image is what you hand a customer, and it needs an image scanner such as Syft or Trivy rather than importlib.metadata alone, because a Python-only inventory leaves out the OS packages (libssl, libpq) that sit in the same container. On duplicates: when a name appears under two sys.path entries, the first one on the path is the one that imports, so report that as the installed version and list the others as shadowed. For missing licenses, SPDX has an explicit NOASSERTION value for "nobody stated one". CycloneDX has no direct equivalent, so I would leave the field empty and add a property saying "not declared in package metadata", which lets a reviewer tell that apart from "not checked". PEP 639 License-Expression is worth reading first, since newer packages are starting to fill it in.
Really useful, thanks. Agreed on both points in time: the lock file at PR time, the image at ship time. And fair point that a Python-only inventory misses libssl and friends; this is meant to sit next to Syft or Trivy, not replace them. Leaving the field empty plus a "not declared in package metadata" property is a clean fix for the CycloneDX gap. The tool already reads License-Expression first, so PEP 639 packages come through.
I agree that the image is what you actually shipped, and the lock file is still worth generating at PR time so drift is visible before a release. One gap I still see in both sources is install-time behavior: a pinned package can drop extra files or pull a script during post-install that never appears as its own distribution in the SBOM. I keep a separate check of lifecycle scripts in the resolved tree, because a clean CycloneDX document will not mention them.
Fair point, and it's a real blind spot. An SBOM lists what got installed, not what ran while it was installing. In Python the risk sits mostly with sdists, since setup.py runs at build time, while wheels don't run any install hooks. So a cheap guard is pip install --only-binary=:all: in CI, with the few exceptions listed explicitly. For anything that slips through, comparing each package's RECORD file against what's actually on disk catches files nobody declared. The lite version doesn't do that yet, and it's going on my list. What do you use for the lifecycle-script check, your own script or an existing tool?
if it's a multi stage image i'd generate it inside the final stage, otherwise build only packages slip into the list and someone asks about things that never run in prod
Yes, that's the cleanest rule for a multi-stage image. Generate the SBOM in the final stage, after the last COPY --from, so compilers and build-only wheels never show up in the inventory. A useful extra check is to generate one in the builder stage as well and diff the two: anything that only appears in the builder is what you meant to leave behind.