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

Free lesson

Writing abuse cases as tests

After this lesson: I can turn an abuse case into a test that fails until the system is fixed.

60 min lesson4 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it means
User storya short description of what a user wants to do
Abuse casea story told by someone who wants to misuse a feature
Security requirementa rule the system must keep to stay safe
Ownership checkcode that confirms the record belongs to the user
Alerta message raised when something suspicious happens

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

Only 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 storyAbuse caseSecurity requirement
I can cancel my orderMallory cancels the order of SamOnly the owner can cancel
I can choose how many mealsMallory orders -3 meals to get money backAn order holds 1 to 10 meals
I can look up my order by numberMallory tries every numberRefusals are logged and many raise an alert

The cells students fill in are left blank in this preview.

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

Try it: an abuse case as a test

Code: python
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

Command
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

Command
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

Command
docker exec jupyter python3 steps/abuse.py

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

Run them against the fixed app

Command
docker exec jupyter python3 steps/abuse.py fixed

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

What abuse.py printed

Abuse caseAs writtenFixed
Mallory cancels the order of Samcancelled, FAILrefused, PASS
Mallory orders -3 mealscharged -12, FAILrefused, PASS
Mallory tries 18 more order numbers0 refusals logged, FAIL20 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 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

Assignment: 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.

Code: python
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

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.