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

Free lesson

Scanning dependencies for known flaws

After this lesson: I can scan a project's dependencies and choose the versions that fix them.

55 min lesson8 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

Code you did not write

The tuck shop's own code is about a hundred lines. The three libraries it lists are many thousands more, written by strangers.

When a flaw is found in one of them, it is published with an id. A pipeline can check every push against those lists.

By the end of this lesson

Note

I can scan a project's dependencies and choose the versions that fix them.

Key words

WordWhat it meansExample
Dependencya library your project uses
Pinnedfixed to one exact versionrequests==2.19.1
Advisorya public notice of a flaw and its fix
Fix versionthe first version without the flaw
Transitiveneeded by a library you use

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

A scanner compares versions with advisories

A dependency scanner reads which versions you pinned and asks a list of advisories which of them are affected. It never reads the library's code.

pip-audit does this for Python, using the advisories PyPI publishes. The lab installs it from PyPI, one of the few places the lab may reach.

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.

Read what the shop pins

Command
docker exec ci cat /work/tuckshop/requirements.txt

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

Install pip-audit in the step runner

Command
docker exec ci pip install -q --user --no-warn-script-location pip-audit==2.10.1

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

Audit the pinned versions

Command
docker exec ci python3 -m pip_audit -r /work/tuckshop/requirements.txt --no-deps --disable-pip

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

Read the report before acting

Each id is listed twice, because two sources describe the same advisory. Count the unique ids, not the lines.

For each package, the version to move to is the highest fix version it lists: that one fixes every advisory at once.

Save the report as JSON

Command
docker exec ci python3 -m pip_audit -r /work/tuckshop/requirements.txt --no-deps --disable-pip -f json -o /work/reports/audit.json

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

Count each package's unique advisories

Command
docker exec ci python3 -c "import json; d=json.load(open('/work/reports/audit.json')); [print(p['name'], p['version'], len({v['id'] for v in p['vulns']})) for p in d['dependencies']]"

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

Try it: compare versions as numbers

Code: python
def parse(version):
    return tuple(int(part) for part in version.split("."))

pinned = "2.19.1"
fixed_in = "2.20.0"

print("as text:", pinned < fixed_in)
print("as numbers:", parse(pinned) < parse(fixed_in))
print("2.9.0 < 2.10.0 as text:", "2.9.0" < "2.10.0")

# Try 2.9.0 against 2.10.0 with parse(). Which answer is right?

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

A common mistake

Warning

Many people think only the libraries they list matter. Each one brings transitive dependencies of its own, and those have flaws too. With --no-deps pip-audit checks only the listed lines, which is why it warns you. Pin every package, with hashes, in a lock file.

Bump the versions, then scan again

The fix is a change to requirements.txt, tested like any other change. Write the new versions to a scratch file and audit it before you commit.

A new advisory can appear tomorrow for any version, so the pipeline runs this step on every push and every night.

Write the bumped versions

Command
docker exec ci sh -c "printf 'requests==2.33.0\nPyYAML==6.0.2\nJinja2==3.1.6\n' > /work/reports/requirements-new.txt"

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

Audit the bumped versions

Command
docker exec ci python3 -m pip_audit -r /work/reports/requirements-new.txt --no-deps --disable-pip

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

Assignment: choose the upgrades

Advisories come as a list of dictionaries such as {"package": "requests", "id": "PYSEC-2018-28", "fixed_in": "2.20.0"}, and the same advisory may appear twice. Write three functions.

parse(version) turns "2.19.1" into the tuple (2, 19, 1). affected(pinned, advisories) takes a dictionary of pinned versions, such as {"requests": "2.19.1"}, and returns a sorted list of unique (package, id) pairs where the pinned version is lower than fixed_in. upgrades(pinned, advisories) returns a dictionary from each affected package to the highest fixed_in among its affecting advisories, compared as numbers.

Code: python
def parse(version):
    return ()


def affected(pinned, advisories):
    return []


def upgrades(pinned, advisories):
    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.