Lesson 07 · 45 minutes
Model the threats of a small chat application
- Describe a system as elements, data flows and trust boundaries in plain text
- Use STRIDE to list what can go wrong at each element
- Write a table of threats and controls and check it for gaps with a script
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
A start-up will release a small chat application in three weeks. On Friday the team holds a design review, and the lead asks you: "Tell us what can go wrong before we write more code." There is no diagram and no test yet, only a description. You will turn the description into a model and a threat table. Then another team asks for the same help on its photo-sharing service, and you do it again on their data.
Recall
Lesson 4: which rule of access control answers the threat "a normal user calls the admin interface"?
Least privilege and deny by default, enforced by a role check on the server for every request. Hiding the button in the browser is not a control.
Lesson 6: which control answers the threat "a user says that they never sent this message"?
A trustworthy log of the user, the time and the address of each action, kept where the user cannot change it. It gives the system accountability.
The idea
Four questions drive every threat model:
- What are we building?
- What can go wrong?
- What will we do about it?
- Did we do a good job?
For the first question, describe the system as elements (people, programs, data stores), data flows between them, and trust boundaries. A trust boundary is a line where the level of trust changes: from the internet to your server, or from a web program to its database. Most threats live next to one.
- User → Web app: F1 login and messages, crosses B1
- Web app → Message database: F2 save and read, crosses B2
- Web app → User: F3 messages of the room, crosses B1
- Attacker → Web app: T1 guesses a password at B1
The dashed edge between Internet and Web server is B1. The dashed edge between Web server and Database host is B2.
For the second question, STRIDE is a list of six prompts. For each element, ask whether each one can happen.
| Letter | Threat | Goal it breaks |
|---|---|---|
| S | Spoofing: pretending to be someone else | Authentication |
| T | Tampering: changing data or code | Integrity |
| R | Repudiation: denying an act that you did | Accountability |
| I | Information disclosure: data reaches the wrong reader | Confidentiality |
| D | Denial of service: making the system unavailable | Availability |
| E | Elevation of privilege: doing what you may not do | Authorisation |
For the third question, choose for each threat to reduce, avoid, transfer or accept it (lesson 1). The fourth question is review: is every element covered, and does each threat have a control?
Worked example
1. Describe what you build
The system is a small chat application: users sign in, post messages to rooms and read the messages of their rooms. Write model.md. A heredoc (cat > file <<'EOF') writes all the lines up to EOF into the file.
mkdir -p ~/krlab/security-basics/threat
cd ~/krlab/security-basics/threat
cat > model.md <<'EOF'
# Chat application: model
Elements
- User: a person with a browser.
- P1 Web app: checks logins, keeps rooms, stores and sends messages.
- S1 Message database: users (name, password hash) and messages.
Data flows
- F1 User -> P1: login and new messages, over HTTPS.
- F2 P1 -> S1: save and read messages, over SQL.
- F3 P1 -> User: the messages of a room, over HTTPS.
Trust boundaries
- B1 between the internet and the web app (crossed by F1 and F3).
- B2 between the web app and the database (crossed by F2).
EOFThis answers question one.
2. Start the threat table
Walk through STRIDE. Write the first four rows in threats.md: ID, element, STRIDE letter, threat and control. The table is a Markdown table: one row per line, cells between | signs. In row T2, HTTPS is HTTP over TLS (lesson 5), and HSTS is a header that tells a browser to use HTTPS only.
cat > threats.md <<'EOF'
| ID | Element | STRIDE | Threat | Control |
|----|---------|--------|--------|---------|
| T1 | F1 | S | An attacker guesses a password and posts as another user | scrypt hashes, rate limit, second factor |
| T2 | F1 | T | Someone on the network changes a message in transit | HTTPS only, HSTS |
| T3 | P1 | R | A user says that they never sent an abusive message | Log user, time and address of every post |
| T4 | S1 | I | A stolen database dump exposes all messages | Encrypt the disk, hash passwords, restrict who can read |
EOF3. Check the gaps with a script
A program can do the dull part of the review. Write check.py. It counts the rows for each STRIDE letter, and lists elements without a threat and threats without a control. Three parts are new: Counter counts how often each value occurs, strip and split("|") cut a table line into its cells, and sys.argv[2:] is every word after the file name. You pass the elements and the flows of the model on the command line, so that the script works for any model. Two more forms: [...] or "none" prints the word none when the list is empty, and {... for ...} is a comprehension that builds a set.
check.py
import sys
from collections import Counter
rows = []
for line in open(sys.argv[1]):
if line.startswith("|"):
rows.append([c.strip() for c in line.strip().strip("|").split("|")])
rows = rows[2:] # skip the header and the separator line
count = Counter(row[2] for row in rows)
for letter in "STRIDE":
print(letter, count[letter])
elements = sys.argv[2:] # the elements and flows of the model
print("elements with no threat:", [e for e in elements if e not in {row[1] for row in rows}] or "none")
print("threats with no control:", [row[0] for row in rows if not row[4]] or "none")python3 check.py threats.md User P1 S1 F1 F2 F3S 1
T 1
R 1
I 1
D 0
E 0
elements with no threat: ['User', 'F2', 'F3']
threats with no control: noneThe model has no denial-of-service row, no elevation row, and three elements that nobody has examined.
4. Close the gaps
Add rows for those gaps. A message is data that another user reads, so what if it holds a script? A room has a number, so what if the user changes it? The sign >> adds the lines to the end of the file.
cat >> threats.md <<'EOF'
| T5 | P1 | D | A script floods the login and the rooms | Rate limit per address, request size limit |
| T6 | P1 | E | A normal user calls the admin API | Server-side role check, deny by default |
| T7 | F2 | T | A message holds SQL that changes the query | Parameterised queries |
| T8 | F3 | I | A user reads a private room by changing the room number | Check room membership on every request |
| T9 | F3 | T | A message holds a script that runs in other browsers | Escape output, Content-Security-Policy |
| T10 | User | S | An attacker steals the session cookie from the browser | Secure, HttpOnly and SameSite cookie, short session life |
EOF
python3 check.py threats.md User P1 S1 F1 F2 F3S 2
T 3
R 1
I 2
D 1
E 1
elements with no threat: none
threats with no control: noneThe controls of T7, T9 and T10 are named fixes for SQL injection, script injection and cookie theft. The Web applications and APIs course teaches how each one works. Here, take them as names of known fixes.
5. Read the result as a plan
Each control now goes to a ticket or a test. T6 is the check of lesson 4, and T1 is the password code of lesson 3. The script finds gaps in the count, not in your thinking, so read the table once more, one boundary at a time.
6. The finished file
This is the whole file as it stands after the steps above. The lab files of the course hold it.
The finished file: threats.md
threats.md
| ID | Element | STRIDE | Threat | Control |
|----|---------|--------|--------|---------|
| T1 | F1 | S | An attacker guesses a password and posts as another user | scrypt hashes, rate limit, second factor |
| T2 | F1 | T | Someone on the network changes a message in transit | HTTPS only, HSTS |
| T3 | P1 | R | A user says that they never sent an abusive message | Log user, time and address of every post |
| T4 | S1 | I | A stolen database dump exposes all messages | Encrypt the disk, hash passwords, restrict who can read |
| T5 | P1 | D | A script floods the login and the rooms | Rate limit per address, request size limit |
| T6 | P1 | E | A normal user calls the admin API | Server-side role check, deny by default |
| T7 | F2 | T | A message holds SQL that changes the query | Parameterised queries |
| T8 | F3 | I | A user reads a private room by changing the room number | Check room membership on every request |
| T9 | F3 | T | A message holds a script that runs in other browsers | Escape output, Content-Security-Policy |
| T10 | User | S | An attacker steals the session cookie from the browser | Secure, HttpOnly and SameSite cookie, short session life |Your turn
The photo-sharing team gives you its design and a first threat table that it wrote alone. Find the gaps. There are no steps here: use check.py and read the table with STRIDE in mind.
The model: the elements are the User, P1 (the upload app), P2 (the thumbnailer, which makes small copies of photos) and S1 (the photo storage). The flows are F1 (User to P1, photos and logins), F2 (P1 to S1, save a photo), F3 (P1 to P2, a job to make thumbnails) and F4 (P1 to User, gallery pages). Boundary B1 lies between the internet and P1, and it is crossed by F1 and F4. Boundary B2 lies between P1 and S1, and it is crossed by F2.
Save the table as threats2.md in the same folder.
threats2.md
| ID | Element | STRIDE | Threat | Control |
|----|---------|--------|--------|---------|
| T1 | F1 | S | A stolen session cookie lets an attacker act as the user | Short session life, HttpOnly cookie |
| T2 | F1 | T | A photo carries a script that runs in a viewer's browser | Escape output, check the file type |
| T3 | P1 | D | A flood of uploads fills the disk | Size limit, rate limit per user |
| T4 | S1 | I | A public storage setting exposes all photos | Private by default, review the setting |
| T5 | User | R | A user denies that they uploaded a banned photo | |
| T6 | F2 | T | A file name with ../ writes outside the folder | Generate names, never use the user's |
| T7 | P2 | D | A huge image crashes the thumbnailer | Size limit and time limit |
| T8 | F4 | I | A user changes the photo number and reads a private photo | Check the owner on every request |Then add one row for each gap, with a control, and run the check again until it reports no gap. Last, rank your new rows with the method of lesson 1.
Compare with this
Three rows close the three gaps. Add a control to T5, such as "Log user, time and address of every upload". Add a row for E, such as "T9 | P1 | E | A normal user calls the admin page of the upload app | Server-side role check, deny by default". Add a row for F3, such as "T10 | F3 | T | A forged job makes the thumbnailer read a file that is not a photo | P2 accepts only the id of a photo that P1 has stored". The check then reports no gap.
A ranking with likelihood times impact, each with a reason: T9 is 3 x 5 = 15 (admin rights open every photo), T10 is 2 x 4 = 8, and T5 is 3 x 2 = 6. Your wording and numbers may differ. Each row needs a control, a number and a reason for the number.
Check yourself
Why start with trust boundaries and not with a list of attacks?
Data that crosses a boundary comes from a source that you do not control, so it needs checking there. Boundaries show you where to look.
Which STRIDE letter fits "a user denies that they placed an order", and which control answers it?
R, repudiation. The control is a trustworthy record: logs of the user, the time and the action, kept where the user cannot change them (lesson 6).
You decide not to fix a threat. What must you write in the table?
The word accept, the name of the person who accepted, the reason and a review date.
Field notes
- STRIDE was made at Microsoft and is one of several methods. PASTA is risk-centred, and LINDDUN covers privacy threats.
- The Threat Modeling Manifesto matches the four questions above. Keep the model in the same repository as the code and review it in the same pull request as a design change.
- For AI systems, add MITRE ATLAS (techniques against machine learning systems) and the OWASP Top 10 for LLM applications as prompts.
- Rank the threats with the risk method of lesson 1. A model with 80 flat rows is not a plan.