Every feature has a second user
The canteen team writes a story: "As a student, I can cancel my order before 11:30." It is a kind feature. Sam uses it when plans change.
Mallory reads the same story differently. What if she cancels Sam's order instead? A feature is used by people who want it to work and by people who want it to fail.
By the end of this lesson
Note
I can turn an abuse case into a test that fails until the system is fixed.
Key words
| Word | What it means |
|---|---|
| User story | a short description of what a user wants to do |
| Abuse case | a story told by someone who wants to misuse a feature |
| Security requirement | a rule the system must keep to stay safe |
| Ownership check | code that confirms the record belongs to the user |
| Alert | a message raised when something suspicious happens |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsOnly your own lab copy
Warning
This lesson runs abuse cases, which are attacks in small. Run them only against your own lab copy. Testing a real system needs the owner's written permission. In the UK, the Computer Misuse Act 1990 makes access without permission a crime, even to look.
An abuse case flips a user story
Take each user story and ask who could misuse it. Write the answer the same way: "As Mallory, I cancel Sam's order by changing the order number." That is an abuse case.
Then write the rule that stops it. That is a security requirement. Here it is "only the owner of an order can cancel it". Start with the threats you ranked in lesson 4.
Three canteen stories, flipped
| User story | Abuse case | Security requirement |
|---|---|---|
| I can cancel my order | Mallory cancels the order of Sam | Only the owner can cancel |
| I can choose how many meals | Mallory orders -3 meals to get money back | An order holds 1 to 10 meals |
| I can look up my order by number | Mallory tries every number | Refusals are logged and many raise an alert |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsTry it: an abuse case as a test
orders = {1: "sam", 2: "mallory"}
def cancel(order_id, user):
# The fix: only the owner may cancel.
if orders[order_id] == user:
return "cancelled"
return "refused"
# The abuse case as a test: Mallory must not cancel the order of Sam.
answer = cancel(1, "mallory")
print("abuse case:", "PASS" if answer == "refused" else "FAIL")
# Break the fix: change the if line to if True: and run it again.
Make a free account to save your work and track your progress.
A test that fails first proves the fix
Write each abuse case as a test that expects a refusal. Run it before the fix: it must fail. Add the fix and run it again: it must pass.
A test that never failed proves nothing, since it may not test anything. The lab script runs three abuse cases against a small stand-in for the canteen app.
Write the abuse case script, part 1
docker exec jupyter sh -c "mkdir -p steps && printf '%s\n' 'import sys' 'fixed = \"fixed\" in sys.argv' 'orders = {1: [\"sam\", \"placed\"], 2: [\"mallory\", \"placed\"]}' 'log = []' 'def cancel(order_id, user):' ' if order_id not in orders:' ' if fixed:' ' log.append(user)' ' return \"no such order\"' ' if fixed and not orders[order_id][0] == user:' ' log.append(user)' ' return \"refused\"' ' orders[order_id][1] = \"cancelled\"' ' return \"cancelled\"' 'def place(user, meals):' ' if fixed and (meals < 1 or meals > 10):' ' log.append(user)' ' return \"refused\"' ' return \"charged \" + str(meals * 4)' 'print(\"version:\", \"fixed\" if fixed else \"as written\")' 'answer = cancel(1, \"mallory\")' 'print(\"abuse 1, mallory cancels the order of sam:\", answer, \" PASS\" if answer == \"refused\" else \" FAIL\")' 'answer = place(\"mallory\", -3)' > steps/abuse.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the abuse case script, part 2
docker exec jupyter sh -c "printf '%s\n' 'print(\"abuse 2, mallory orders -3 meals:\", answer, \" PASS\" if answer == \"refused\" else \" FAIL\")' 'for n in range(3, 21):' ' cancel(n, \"mallory\")' 'count = log.count(\"mallory\")' 'print(\"abuse 3, mallory tries 18 more order numbers:\", count, \"refusals logged\", \" PASS\" if count > 5 else \" FAIL\")' 'if count > 5:' ' print(\"ALERT: mallory was refused\", count, \"times\")' >> steps/abuse.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the abuse cases as written
docker exec jupyter python3 steps/abuse.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run them against the fixed app
docker exec jupyter python3 steps/abuse.py fixedShown here, not run. Press Start lab to type it into your own lab’s terminal.
What abuse.py printed
| Abuse case | As written | Fixed |
|---|---|---|
| Mallory cancels the order of Sam | cancelled, FAIL | refused, PASS |
| Mallory orders -3 meals | charged -12, FAIL | refused, PASS |
| Mallory tries 18 more order numbers | 0 refusals logged, FAIL | 20 refusals logged, PASS |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think hiding the cancel button from other students is enough. Mallory does not need the button; she can send the request herself. Put the ownership check in the code that cancels, where every request passes.
Fix it, then watch for it
The fixed app checks the owner, checks the number of meals and logs every refusal. One refusal is a mistake. Twenty in a row from one account is someone trying every number, so the script raises an alert. Detection catches the attacker the fix stopped.
Check
Have a go
Check
3 questionsAssignment: fix the cancel feature
Write the fixed canteen functions so the abuse cases pass.
cancel(orders, order_id, user, log) cancels only the user's own order and returns True. For a missing order or someone else's, it adds the user to log and returns False. valid_meals(meals) allows 1 to 10. flagged(log, limit) returns the users in the log more than limit times, in the order they first appear.
def cancel(orders, order_id, user, log):
# orders maps a number to [owner, status]. Cancel only your own order.
# A missing order or someone else's order: add user to log and return False.
return True
def valid_meals(meals):
# One order holds 1 to 10 meals.
return True
def flagged(log, limit):
# The users in log more than limit times, in the order they first appear.
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go