Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

SBOMs: a list of what is inside

After this lesson: I can make an SBOM for a project and find what changed between two of them.

55 min lesson10 commandsDevSecOps lab: build a security pipeline and a gate

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Do we use it?

In December 2021 a flaw called Log4Shell was found in Log4j, a library used by thousands of programs. Every security team asked the same question that morning: do we use it?

The teams with a list of every component in every release answered in minutes. The others searched for weeks.

By the end of this lesson

Note

I can make an SBOM for a project and find what changed between two of them.

Key words

WordWhat it meansExample
Supply chaineverything and everyone that goes into your software
SBOMa list of every component in a piece of software
Componentone library or package inside it
Package URLone standard name for a componentpkg:pypi/[email protected]
Transitiveneeded by a component you chose
CycloneDXone common SBOM format; SPDX is the other

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

An SBOM lists every component

An SBOM, a software bill of materials, is like the ingredients label on food. It lists each component with its version and a package URL.

It is written as JSON, so a program can read it. Trivy can make one offline from a project's files. Start with the tuck shop's repository.

Bring the lab's copy up to date

Command
docker exec gitleaks git -C /work/tuckshop pull -q

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Make an SBOM from the repository

Command
docker exec trivy trivy fs -q --format cyclonedx -o /work/reports/source.cdx.json /work/tuckshop

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

List the libraries it found

Command
docker exec ci python3 -c "import json; d=json.load(open('/work/reports/source.cdx.json')); [print(c['name'], c['version'], c['purl']) for c in d['components'] if c['type'] == 'library']"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

What you install is more than you list

The requirements file names three libraries. Each of them needs others to run, and pip installs those too. They are transitive components.

An SBOM made from what was really installed shows them all. Install the shop's libraries into a folder, then ask Trivy about that folder.

Install the shop's libraries into a folder

Command
docker exec ci pip install -q --no-cache-dir --upgrade --target /work/reports/env -r /work/tuckshop/requirements.txt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Make an SBOM of what was installed

Command
docker exec trivy trivy rootfs -q --format cyclonedx -o /work/reports/built.cdx.json /work/reports/env

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

List what the build really holds

Command
docker exec ci python3 -c "import json; d=json.load(open('/work/reports/built.cdx.json')); [print(c['name'], c['version'], c['purl']) for c in d['components'] if c['type'] == 'library']"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Count what the scanner's own image holds

Command
docker exec trivy trivy rootfs -q --format cyclonedx --skip-dirs /proc,/sys,/work -o /work/reports/scanner.cdx.json / && docker exec ci python3 -c "import json, collections; d=json.load(open('/work/reports/scanner.cdx.json')); print(collections.Counter(c['purl'].split('/')[0] for c in d['components'] if 'purl' in c))"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think an SBOM made from the requirements file is complete. It only lists what you asked for, not what was installed. Make the SBOM from the built artifact, the folder or the image you ship, because that is what runs.

Two formats, and a diff shows change

CycloneDX and SPDX are the two common formats. Both list the same components; tools read either.

Keep an SBOM with every release. Comparing two of them by name shows what a change brought in, which a reviewer may never see in the code.

Write the same list as SPDX

Command
docker exec trivy trivy rootfs -q --format spdx-json -o /work/reports/built.spdx.json /work/reports/env && docker exec ci python3 -c "import json; d=json.load(open('/work/reports/built.spdx.json')); print(d['spdxVersion'], len(d['packages']), 'packages')"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Compare the two SBOMs by name

Command
docker exec ci python3 -c "import json; names = lambda f: {c['name'].lower() for c in json.load(open(f))['components'] if c['type'] == 'library'}; print('Only in the build:', *sorted(names('/work/reports/built.cdx.json') - names('/work/reports/source.cdx.json')))"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try it: compare two releases

Code: python
old = {"requests": "2.19.1", "jinja2": "2.10"}
new = {"requests": "2.33.0", "jinja2": "2.10", "urllib3": "2.6.3"}

print("added:", sorted(set(new) - set(old)))
print("removed:", sorted(set(old) - set(new)))
for name in sorted(set(old) & set(new)):
    if old[name] != new[name]:
        print("changed:", name, old[name], "->", new[name])

# Remove "jinja2" from new and press Run again. What is listed now?

Make a free account to save your work and track your progress.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

An SBOM answers "are we affected?"

When the next Log4Shell is announced, you search every release's SBOM for the component's name, not the code.

An SBOM says what is inside, not whether it is safe. Scanning one for known flaws is covered in the vulnerability management module.

Is urllib3 in this release?

Command
docker exec ci grep -o 'pkg:pypi/urllib3@[0-9.]*' /work/reports/built.cdx.json | sort -u

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Assignment: compare two SBOMs

A CycloneDX SBOM is a dictionary with a components list; each component has type, name and version. Write three functions.

components(sbom) returns a dictionary from each library component's name, in lower case, to its version, skipping every other type. diff(old, new) takes two SBOMs and returns {"added": [...], "removed": [...], "changed": [...]}: sorted names only in the new one, sorted names only in the old one, and sorted (name, old_version, new_version) tuples for names in both with different versions. releases_using(sboms, name) takes a dictionary from a release name to its SBOM and returns the sorted release names that contain a library called name, in any case.

Code: python
def components(sbom):
    return {}


def diff(old, new):
    return {"added": [], "removed": [], "changed": []}


def releases_using(sboms, name):
    return []

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.