Skip to content

Lesson 3 of 5

Checks made only in the browser

Listen to an AI summary of Checks made only in the browser

0:00 / 0:30

Where the code of a page runs

A browser is the program a person uses to visit websites, such as Chrome.

A page from the sports shop holds JavaScript. The browser runs the JavaScript on the customer's own phone or laptop. The browser is also called the client. Code that runs in the browser is client-side code, and code that runs on the server is server-side code.

The customer controls the phone or laptop, so the customer can read, change or stop client-side code. A customer cannot change server-side code.

The paper order form

The shop's paper order form has a printed note that says two tickets at most.

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. The printed note is the check in the page.

A customer can bring a home-made form with no note and ask for ten tickets. The person behind the counter has to count the tickets.

The check in the page

The sports shop sells tickets for the cup final, two at most for each customer. Sam, a customer, opens the ticket page. The page has a form field, a box that a person fills in, named quantity. Six lines of JavaScript run in Sam's browser when Sam presses Order. Look at the check on line 2.

  1. let quantity = readField("quantity")// reads the number Sam typed
  2. if (quantity > 2) {// is the check. The test is true for a quantity above 2.
  3. showError("Two tickets at most")// shows a message. The page sends nothing.
  4. } else {
  5. sendOrder(quantity)// sends the request to the server
  6. }
The JavaScript of the ticket page.

let makes a variable in JavaScript. The curly brackets mark the lines inside the if, as the indent does in Python. readField, showError and sendOrder are functions written for the sports shop.

The route on the server

The sports shop's route for /tickets is four lines of Python. Look for a check on the quantity.

  1. def order_tickets():
  2. quantity = int(request.form["quantity"])# reads the quantity from the request and makes the text a whole number
  3. make_order("cup final ticket", quantity)# makes the order. No check comes first.
  4. return "Order placed"
The route for /tickets.

Line 2 reads quantity from the body of the request. make_order is a function written for the sports shop that saves the order.

A request with no page

The program curl sends a request written by hand, with no browser. Sam wants ten tickets and types one curl command.

curl shop.example/tickets --data "quantity=10"

  1. curlthe program
  2. shop.example/ticketsthe address the request goes to
  3. --data "quantity=10"the body of the request
Sam's curl command. A real command also tells the server that Sam is signed in.
  1. Sam's browser

    the page checks the quantity

  2. quantity=2

    Server

    the route makes an order for 2 tickets

The check runs in the browser.

No page loads, so no JavaScript runs and no check happens.

  1. Sam's terminal

    curl sends the request

  2. quantity=10

    Server

    the route makes an order for 10 tickets

No check runs on the order.

What is a client-side check

A client-side check is a check that runs only in the browser, so the sender of a request can skip the check. The weakness is named client-side enforcement of server-side security (CWE-602).

A client-side check still helps an honest customer, who sees the message sooner. The server needs the same check.

The safer route

The safer route makes the check again on the server. Look at lines 3 and 4:

def order_tickets():
    quantity = int(request.form["quantity"])
    if quantity < 1 or quantity > 2:
        return "Two tickets at most"
    # a full check also counts the tickets in earlier orders
    make_order("cup final ticket", quantity)
    return "Order placed"

Line 3 tests whether the quantity is below 1 or above 2. Line 4 then sends back a message, and the route makes no order. Line 5 says what the route leaves out. The rule is two tickets for each customer, so a full check also counts the tickets in earlier orders of the same customer.

For every check in a page, a reviewer asks what the server does when a request skips the check. Safer code makes each decision again on the server, from data the server holds:

  • Who is signed in, from the session.
  • A price, from the database, the server's store of data.
  • A role, such as customer or administrator, from the database. A role sets what a person may do.

A price in a hidden field

The browser still sends the hidden field in the body of the request. The ticket page has a hidden field named price, the price of one ticket in pence.

  1. quantity=2&price=500The body the page sends. `&` separates the fields. One ticket costs £5.00.
  2. quantity=2&price=1The body Sam sends with curl. One ticket costs 1p.
Two bodies for an order of two tickets.

A route that reads price from the request charges Sam 1p for each ticket. The safer route reads the price from the database.

A button hidden by the page

JavaScript in a page can hide a button, such as Delete order, from a person who is not an administrator. Hiding the button is a client-side check. A curl command can still send the request, so the route needs a check of the role.

Examples of what to look for

  • A new check in the JavaScript of a page, with no matching check in the route.
  • A form field that the page hides or locks, whose value the route reads from the request. The word hidden marks such a field.
  • A route that reads a price, a total, a role or a customer number from the body of a request.
  • JavaScript that hides a page or a button by role, with no check of the role in the route.

Check your understanding

  1. State where the JavaScript in a page runs and who controls that computer.

    Show the answer

    In the browser, on the customer's own phone or laptop. The customer controls the phone or laptop.

  2. Which check does the ticket page make that the first route for /tickets does not make?

    Show the answer

    The check that the quantity is 2 at most, on line 2 of the JavaScript.

  3. Give one reason why a server cannot trust a price in a hidden field.

    Show the answer

    The sender of the request can change the value of a hidden field, such as from 500 to 1.

Practise it

Challenges with a weakness this lesson teaches.