The diagram nobody updated
Six months after launch, the canteen app sends delivery addresses to a courier. Nobody redrew the diagram. Nobody asked STRIDE about the new flow.
A threat model in a document goes stale the week it is written. A threat model kept as code can be checked every time the code changes.
By the end of this lesson
Note
I can write a threat model as code that fails a build on a new finding.
Key words
| Word | What it means | Example |
|---|---|---|
| Threat model as code | a threat model written as a program kept with the system | |
| Rule | a function that looks for one kind of threat in the model | |
| Finding | one threat a rule found | |
| Accepted finding | a finding someone reviewed and agreed to live with | |
| Gate | a check that stops a build when it fails | exit code 1 |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsThe model is data, the rules are code
Write the elements and flows as data, as you did in lesson 1. Add facts to each flow: is it encrypted, and does the receiver check who sent it?
Then write each rule as a function. "A flow that crosses a boundary must be encrypted" becomes a few lines. Every rule that fires adds a finding. Tools such as OWASP pytm work this way; this lab writes its own, in plain Python.
Try it: one rule over two flows
zone = {"Web app": "college", "Card payments": "payments", "Orders database": "college"}
# label, from, to, encrypted
flows = [
["payment request", "Web app", "Card payments", True],
["save order", "Web app", "Orders database", False],
]
for f in flows:
crosses = not zone[f[1]] == zone[f[2]]
if crosses and not f[3]:
print("FINDING:", f[0], "crosses a boundary unencrypted")
print("checked", len(flows), "flows")
# Change True to False on the payment request and run it again.
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsAccepted findings make a gate
Some findings are reviewed and kept on purpose, as lesson 4 taught. Put them in a list of accepted findings with the name of whoever agreed.
The script compares what the rules found with that list. Anything new makes it stop with exit code 1. A build pipeline treats that as a failed step, so a new risky flow cannot ship unseen. That is a gate.
Write the threat model script, part 1
docker exec jupyter sh -c "mkdir -p steps && printf '%s\n' 'import sys' 'zone = {' ' \"Student\": \"internet\", \"Web app\": \"college\", \"Orders database\": \"college\",' ' \"Kitchen screen\": \"college\", \"Card payments\": \"payments\", \"Admin\": \"staff\", \"Admin page\": \"college\",' '}' 'processes = [\"Web app\", \"Kitchen screen\", \"Admin page\"]' 'flows = [' ' [\"sign in and order\", \"Student\", \"Web app\", True, True],' ' [\"order status\", \"Web app\", \"Student\", True, True],' ' [\"save order\", \"Web app\", \"Orders database\", False, True],' ' [\"orders to cook\", \"Orders database\", \"Kitchen screen\", False, False],' ' [\"payment request\", \"Web app\", \"Card payments\", True, True],' ' [\"payment result\", \"Card payments\", \"Web app\", True, True],' ' [\"edit menu\", \"Admin\", \"Admin page\", True, False],' ' [\"menu changes\", \"Admin page\", \"Orders database\", False, True],' ']' 'accepted = [\"S edit menu\"]' > steps/tm.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the threat model script, part 2
docker exec jupyter sh -c "printf '%s\n' 'if \"new\" in sys.argv:' ' zone[\"Delivery partner\"] = \"delivery\"' ' flows.append([\"delivery address\", \"Web app\", \"Delivery partner\", False, True])' 'if \"diagram\" in sys.argv:' ' print(\"flowchart LR\")' ' for f in flows:' ' print(\" \" + f[1].replace(\" \", \"_\") + \" -->|\" + f[0] + \"| \" + f[2].replace(\" \", \"_\"))' ' sys.exit(0)' 'found = []' 'for f in flows:' ' crosses = not zone[f[1]] == zone[f[2]]' ' if crosses and not f[3]:' ' found.append([\"I \" + f[0], \"crosses a trust boundary unencrypted\"])' ' if crosses and f[2] in processes and not f[4]:' ' found.append([\"S \" + f[0], \"reaches \" + f[2] + \" across a boundary with no sign-in\"])' 'print(\"findings:\", len(found))' 'new = 0' 'for item in found:' ' mark = \"accepted\" if item[0] in accepted else \"NEW\"' ' if mark == \"NEW\":' ' new = new + 1' >> steps/tm.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the threat model script, part 3
docker exec jupyter sh -c "printf '%s\n' ' print(\" \" + item[0] + \": \" + item[1] + \" [\" + mark + \"]\")' 'print(\"new findings:\", new)' 'if new > 0:' ' print(\"gate: FAIL\")' ' sys.exit(1)' 'print(\"gate: pass\")' >> steps/tm.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check the model as it is
docker exec jupyter python3 steps/tm.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Add the delivery partner
docker exec jupyter sh -c "python3 steps/tm.py new || echo the gate stopped the build"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
What tm.py printed
| Run | Findings | New | Gate |
|---|---|---|---|
| As it is | S edit menu, accepted | 0 | pass |
| With the delivery partner | S edit menu, accepted; I delivery address, NEW | 1 | FAIL, then "the gate stopped the build" |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think a threat model as code finds every threat by itself. The rules only find what someone wrote a rule for. People still draw, ask STRIDE and rank. The code keeps their answers checked as the system changes.
Draw the diagram from the model
docker exec jupyter python3 steps/tm.py diagramShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAssignment: a gate for the model
Write the rules and the gate the lab script used.
zones maps an element to its zone, processes lists the processes, and a flow is [label, from, to, encrypted, authenticated]. findings(zones, processes, flows) goes through the flows that cross a boundary, in order, adding "I " plus the label if the flow is not encrypted, then "S " plus the label if it goes into a process and is not authenticated. new_findings(found, accepted) keeps those not accepted. gate(found, accepted) returns 1 if anything is new, else 0.
# zones maps an element to its zone. processes lists the processes.
# A flow is [label, from, to, encrypted, authenticated].
def findings(zones, processes, flows):
# For each flow that crosses a boundary, in order:
# "I " + label if it is not encrypted, then
# "S " + label if it goes into a process and is not authenticated.
return []
def new_findings(found, accepted):
# The findings that are not in accepted, in order.
return []
def gate(found, accepted):
# 1 if there is any new finding, else 0.
return 0
Make a free account to save your work and track your progress.
Exit quiz
Have a go