Skip to content

Lesson 4 of 5

Limits on what a request can use

Listen to an AI summary of Limits on what a request can use

0:00 / 0:35

The order list in the sports shop

Staff at the sports shop website open a page that lists the shop's orders. The address ends with a query parameter. The query parameter limit says how many orders the page shows:

shop.example/orders?limit=20

Two lines of Python on the server fetch the orders. Look at where the value of limit comes from.

  1. limit = int(request.args["limit"])# reads the number from the request
  2. rows = database.execute("SELECT * FROM orders LIMIT ?", [limit])# asks the database for that many rows
The code behind the order list.

On line 1, int turns the text into a number. The SQL word LIMIT sets how many rows the database returns. The ? marks where the database puts the value of limit (SQL injection).

The sender writes the address, so the sender chooses the number. The code accepts any number.

What ten million rows cost

A sender changes the end of the address to limit=10000000 and sends the request. The database reads up to ten million rows and passes every row to the server. The server keeps every row in memory, where a computer holds the data a running program is using.

  1. limit=1010 rows. About 0.001 seconds.
  2. limit=10001,000 rows. About 0.1 seconds.
  3. limit=1000000010,000,000 rows. About 1,000 seconds, which is 17 minutes.
Example times for a table that holds ten million orders. The time grows with the number of rows.

During those 17 minutes the server has less memory and less time for every other request. Other customers wait a long time for a page, or get an error. Slowing or stopping a website for everyone is denial of service (the lesson on threat modelling).

Uncontrolled resource consumption (CWE-400) is a request making the server use far more memory, time or money than the feature needs.

A default and a ceiling

The safer code gives limit a default, a ceiling and a floor. A default is the value the code uses when the request gives no value. A ceiling is the largest value the code accepts, and a floor is the smallest. Look at the numbers 20, 100 and 1.

  1. limit = int(request.args.get("limit", 20))# The default is 20
  2. limit = min(limit, 100)# The ceiling is 100
  3. limit = max(limit, 1)# The floor is 1
  4. rows = database.execute("SELECT * FROM orders LIMIT ?", [limit])# The database returns 100 rows at most
The safer code behind the order list.

On line 1, .get("limit", 20) returns 20 when the request has no limit. On line 2, min returns the smaller of two numbers, so a request for ten million rows gets 100. On line 3, max returns the larger of two numbers, so a request for -5 rows gets 1.

Examples of where limits belong

A number in a request is one place where a limit belongs. The list gives four more places:

  • The size of a request's body, which holds the main data, and the size of an uploaded file.
  • A loop over a list from a request. A list of a thousand order numbers can mean a thousand queries.
  • The time the code waits for a database or for another server.
  • The number of times one person can do something that costs money, such as sending a text message.

One line of Flask sets the largest body the server accepts:

app.config["MAX_CONTENT_LENGTH"] = 1000000  # one million bytes

Flask sends back an error for a request with a larger body.

A timeout is a limit on how long the code waits for something before giving up. Code with no timeout can wait minutes for a slow database or a slow server. Each waiting request holds some of the server's memory until the wait ends.

A rate limit belongs on anything that costs money or that an attacker can guess. Examples are sign-in, password reset, sending an email or a text message, and a paid AI program that writes text.

Regular expressions

A regular expression is a pattern that describes text. Code uses a regular expression to search text or to check input.

^[0-9]+$

  1. ^the start of the text
  2. [0-9]one digit
  3. +one or more times
  4. $the end of the text
A regular expression that matches text made only of digits, such as 2024.

The + is a repeat. A repeat inside a repeat can make a check very slow. The pattern ^(a+)+$ has the repeat a+ inside brackets and a second + after the brackets.

Regular expression denial of service is input that makes a badly written regular expression take seconds or minutes to check. One short piece of text keeps the server busy while other customers wait. The safer pattern ^a+$ accepts the same texts with one repeat. A limit on the length of the text also keeps the work small.

Examples of what to look for

  • A number from a request used as a count, a size or the end of a loop, with no ceiling.
  • Code that reads the body of a request or an uploaded file with no limit on the size.
  • A regular expression with a repeat inside a repeat, such as (a+)+, used on text from a request.
  • A database query, or a request to another server, with no timeout.
  • New code that sends a message or uses a paid service with no rate limit.

Check your understanding

  1. Name three places where a limit belongs.

    Show the answer

    A number in a request, the size of a body and the time the code waits. A loop over a list from a request and an action that costs money are two more.

  2. A request to the order list of the sports shop has limit=10000000. What happens to other customers?

    Show the answer

    Other customers wait a long time for a page, or get an error. The server spends its memory and time on the one request.

  3. Give one reason why the line limit = min(limit, 100) makes the order list safer.

    Show the answer

    min returns the smaller of two numbers, so no request can make the database return more than 100 rows.

Practise it

Challenges with a weakness this lesson teaches.