Skip to content

Lesson 2 of 5

Rules the code must enforce

Listen to an AI summary of Rules the code must enforce

0:00 / 0:30

To enforce a rule is to check the rule and refuse anything that breaks the rule.

The price on a paper order form

A customer hands in a paper order form with a price of 1p written beside a hoodie. The person behind the counter reads the price from the shop's own price list and charges £25.

The customer is the browser and the person behind the counter is the server. Each box on the form is one part of the request, and the price list is the shop's database. The sender writes every part of a request, so the sender can write any price.

A checkout that trusts the request

Sam, a customer, orders one hoodie from the sports shop. The browser sends a request to the server. The body, the last part of the request, holds three named values.

product_id=7&price=2500&quantity=1

  1. product_id=7the id of the hoodie
  2. price=2500the price in pence
  3. quantity=1the quantity
The body of Sam's request. The two marked values decide how much Sam pays.

The price is in pence, so 2500 means £25. The checkout, the part of the shop that takes payment, is seven lines of Python on the server. Look at where the price and the quantity come from.

  1. def checkout():
  2. product_id = request.form["product_id"]# reads the product id from the body
  3. price = int(request.form["price"])# reads the price from the body
  4. quantity = int(request.form["quantity"])# reads the quantity from the body
  5. total = price * quantity# works out the total
  6. charge_card(total)# takes the payment
  7. send_order(product_id, quantity)# sends the order to be packed
The first checkout. The two marked lines trust the request.

request.form holds the values from the body, and int turns text into a whole number. No line checks the price or the quantity against a rule of the shop.

Two requests the checkout accepts

Sam can change the values and send the request again.

  1. product_id=7&price=1&quantity=1the total is 1, so the hoodie costs 1p
  2. product_id=7&price=2500&quantity=-3the total is -7500, which is minus £75
Two bodies that break a rule of the shop.

A quantity of minus 3 gives a total of minus £75. A total below zero can pay money to the customer, or take money off the rest of the order.

Every value in both requests is a whole number, so a check for a whole number passes both requests. Static analysis does not know the rules of the shop. A reviewer who knows the rules sees the rule the code does not enforce.

The safer checkout

In the safer version, the browser sends the product id and the quantity, and no price.

product_id=7&quantity=1

  1. product_id=7the id of the hoodie
  2. quantity=1the quantity
The body the safer checkout reads.

The safer checkout is nine lines of Python. Look at the first if statement and at the line that sets price.

  1. def checkout():
  2. product_id = request.form["product_id"]
  3. quantity = int(request.form["quantity"])
  4. if quantity < 1 or quantity > 10:# refuses a quantity outside 1 to 10
  5. return "Choose a quantity from 1 to 10"
  6. price = find_price(product_id)# reads the price from the shop's database
  7. total = price * quantity
  8. if charge_card(total):# checks that the payment worked
  9. send_order(product_id, quantity)
The safer checkout.

A quantity of minus 3 is below 1, so the checkout stops. No line reads a price from the request.

Values the server works out

The price, a discount and a total are values that decide money. A role decides what a person may do on the website. The request sends an id and a quantity, and the server works out the rest.

The sender can still change a price in a hidden field (the next lesson).

Money in whole pence

A floating point number is a number with a decimal point, the data type that Python calls float. Some decimal numbers, such as 0.1, have no exact floating point form. The computer stores the nearest number instead.

  1. 0.1 + 0.2 gives 0.3000000000000000410p and 20p as floating point pounds
  2. 10 + 20 gives 30the same sum in whole pence
One sum in floating point numbers and in whole pence.

A whole number has no such error, so safer code stores £19.99 as 1999 pence. A sum such as a 15% discount can give part of a penny. Safer code rounds the answer once, up or down as the shop has chosen.

Steps in a fixed order

A workflow is a set of steps that must happen in a fixed order, such as payment before delivery. A request can go straight to the address of the last step, such as shop.example/delivery. The code for a later step has to check that the earlier step finished.

  1. Basket

  2. then

    Payment

  3. then

    Delivery

    must check the payment first

The workflow of one order. A request can ask for delivery first.

Actions allowed once

Some actions are allowed once, such as using a discount code, voting or taking a refund. The code records the first use and refuses the second. Two requests sent at the same moment can both pass the check before the code records either use. A later lesson on race conditions covers the fix.

A step that fails

The function charge_card returns True when the payment works and False when the payment fails. The first checkout never reads the return value, so a failed payment still sends the order. The safer checkout sends the order only when charge_card returns True.

Names of the weaknesses

The whole group is business logic errors (CWE-840).

A price read from the request
Client-side enforcement of server-side security (CWE-602). The client is the browser.
A quantity with no range check
Improper validation of specified quantity in input (CWE-1284).
Money as a floating point number
Insufficient precision or accuracy of a real number (CWE-1339).
A step out of order
Improper enforcement of behavioural workflow (CWE-841).
An action used twice
Improper enforcement of a single, unique action (CWE-837).
A failed step ignored
Unchecked return value (CWE-252).

Examples of what to look for

  • A price, a total, a discount or a role read from the request, such as request.form["price"].
  • A quantity or an amount of money used in a sum with no check of the range.
  • Money stored as a floating point number, such as price = 19.99.
  • Code for a later step, such as delivery, with no check that the payment finished.
  • A discount code or a refund used with no record of the first use.
  • A function that takes a payment or sends an email, with the return value never read.

Check your understanding

  1. State the total the first checkout works out for a price of 2500 and a quantity of minus 3.

    Show the answer

    Minus 7500 pence, which is minus £75.

  2. Which value in the body product_id=7&price=2500&quantity=1 must the server work out for itself?

    Show the answer

    The price. The safer checkout reads the price from the shop's database.

  3. Give one reason why money is stored as a whole number of pence.

    Show the answer

    Some decimal numbers, such as 0.1, have no exact floating point form, so a sum can be slightly wrong. A whole number of pence has no such error.

Practise it

Challenges with a weakness this lesson teaches.