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
| Word | What it means | Example |
|---|---|---|
| Dependency | a library your project uses | |
| Pinned | fixed to one exact version | requests==2.19.1 |
| Advisory | a public notice of a flaw and its fix | |
| Fix version | the first version without the flaw | |
| Transitive | needed by a library you use |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsA 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
docker exec gitleaks git -C /work/tuckshop pull -qShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read what the shop pins
docker exec ci cat /work/tuckshop/requirements.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Install pip-audit in the step runner
docker exec ci pip install -q --user --no-warn-script-location pip-audit==2.10.1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Audit the pinned versions
docker exec ci python3 -m pip_audit -r /work/tuckshop/requirements.txt --no-deps --disable-pipShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsRead 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
docker exec ci python3 -m pip_audit -r /work/tuckshop/requirements.txt --no-deps --disable-pip -f json -o /work/reports/audit.jsonShown here, not run. Press Start lab to type it into your own lab’s terminal.
Count each package's unique advisories
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
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 questionsA 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
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
docker exec ci python3 -m pip_audit -r /work/reports/requirements-new.txt --no-deps --disable-pipShown 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.
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