Lesson 1 of 5
Misuse cases
Listen to an AI summary of Misuse cases
What is business logic
Business logic is the set of rules for what an application is for. One rule of a shop is one discount code for each order. Another rule is that a customer pays before the shop sends the goods.
Code enforces a rule when the code checks the rule and refuses anything that breaks the rule. A business logic weakness is a rule that the application should enforce and does not.
Why a tool does not find a missing rule
A static analysis tool does not know the rules of a shop. A missing rule is a check that nobody wrote, so the code has no risky pattern to report.
A large language model is an AI program that reads and writes text, including code. A large language model writes code that does what a person asked for. The person may not have said what must not happen, so the new code can have a rule missing. The lesson on ways of testing software covers both tools.
Use cases and misuse cases
A feature is one thing an application lets a person do, such as search or pay. A use case is what a person is meant to do with a feature. A misuse case is what a person can do with a feature and was not meant to do.
The sports shop website adds a feature, a box at the checkout for a discount code. The discount code WELCOME5 takes £5 off an order. The use case is one customer using one discount code once, on that customer's own unpaid order.
Sam, a customer, signs in and orders a hoodie for £30. Sam types WELCOME5 in the box and presses Apply. The browser sends a request. The total of Sam's order becomes £25.
Six questions that find a misuse case
The same six questions work for any feature. A value is one piece of data, such as the number 5.
- Who
- Can a person do the action to an order or an account that belongs to someone else?
- How many times
- What happens when a person does the action twice, or a thousand times?
- In what order
- What happens when a person skips a step, repeats a step or goes back a step?
- With what values
- What happens with zero, a number below zero, a very large number or a value the page does not offer?
- At the same time
- What happens when two requests arrive together?
- At what cost
- Does the action cost money, send a message or use up something limited, and who can start the action?
The Python behind the discount code box
The server of the sports shop runs the function add_discount when Sam's request arrives. The function is ten lines of Python and does the use case. Look at the three highlighted lines, where a rule is missing.
add_discount. Each highlighted line has a rule missing.request.form holds the values that the browser sent in the request. Line 4 looks for a discount with that discount code. None on line 5 means that line 4 found no discount, and line 6 then stops the function.
Line 7 finds the order with that order number. Line 8 takes the amount of the discount off the total of the order. order.total is the total of the order, and discount.amount is 5 for WELCOME5.
What the six questions find
Each question finds a rule that the function does not enforce.
- Who
- Sam sends the order number of another customer. Line 7 has no ownership check.
- How many times
- Sam sends
WELCOME5six times. No line records that Sam used the discount code, so the total falls to £0. - In what order
- Sam pays £30 and then sends
WELCOME5. No line checks that the order is still unpaid, so the total of a paid order falls to £25. - With what values
- Sam uses
WELCOME5on a pair of socks that costs £3. No line stops the total going below zero, so line 8 gives a total of minus £2. - At the same time
- A program sends
WELCOME5in two requests at the same moment. Even with a check for a used code, both requests can pass the check before either records the use (covered in a later lesson). - At what cost
- Each discount costs the shop money. No line limits guesses, so a program can try thousands of discount codes (covered in a later lesson).
Examples of what to look for
- A function that finds an order by a number from the request, with no check on who owns the order.
- An action that should happen once, such as using a discount code, with no line that records the first time.
- A later step, such as sending the goods, with no check that an earlier step, such as payment, happened.
- A number from the request used in a sum, with no lowest or highest number allowed.
- An action that costs money or sends a message, with no limit on repeats by one person.
Check your understanding
Name four of the six questions that find a misuse case.
Show the answer
Any four of Who, How many times, In what order, With what values, At the same time and At what cost.
Give one misuse case for the discount code box of the sports shop.
Show the answer
Sam sends
WELCOME5six times and the total falls to £0. Any other row of the second table is also a right answer.Give one reason why a static analysis tool does not find a missing rule.
Show the answer
The tool does not know the rules of the shop. A missing rule is a check that nobody wrote, so the code has no risky pattern to report.