Skip to content

Lesson 5 of 6

Sources and sinks

Listen to an AI summary of Sources and sinks

0:00 / 0:33

Water from a tap to a drain

Data moves through code the way water moves through a basin. Water comes in at a tap and leaves down a drain. A filter over the drain makes the water safe to go down, and some basins have no filter.

No filter

Tapthe source, where data comes in from outside

Nothing in betweenno check on the data

Drainthe sink, where another program acts on the data

A filter

Tapthe source, where data comes in from outside

Filtera check that makes the data safe for the drain

Drainthe sink, where another program acts on the data

Data moves through code as water moves from a tap to a drain. The amber drop is data that an attacker wrote.

The search in the sports shop

A visitor to the sports shop website types a product name, such as hoodie, into the search box. The browser sends the name to the shop's server in a request.

The name is the query parameter at the end of the URL, as /search?name=hoodie.

The server runs six lines of Python for each search. Follow the product name from line 2 to line 5:

def search():
    name = request.args["name"]
    wanted = name.strip()
    query = "SELECT * FROM products WHERE name = '" + wanted + "'"
    rows = database.execute(query)
    return rows

Line 2 reads the query parameter name from the request and stores the text in the variable name. Line 3 removes any spaces from the ends of the text and stores the result in wanted.

Line 4 joins three strings, with wanted in the middle. Code can join a value into a string in other ways too. Python has the f-string, which holds the value as {wanted}, and JavaScript has the template string, which holds the value as ${wanted}. The result is a query, which is one instruction for a database, written in SQL. Line 5 passes the query to the shop's database, and the database runs the query.

What is a source

A source is a place where data from outside enters the code. Line 2 is the source in the search, because line 2 reads the product name from the request. The source is the tap.

A request carries data to the server in the path, the query parameters, the headers, the cookies and the body (What is in a request). Code can read each one as a source.

The sender of a request chooses the text in every one of them. Data that comes from outside the application is called untrusted data, because an attacker could have written the data.

What is a sink

A sink is a place where the code passes data to something that acts on the data. Line 5 is the sink in the search, because line 5 passes the query to the database. The sink is the drain.

The database is an interpreter, which is a program that reads text and carries out the instructions in the text. The Python interpreter reads Python code and runs the code. The database reads SQL and runs the SQL.

Following the value

A reviewer can read forward from a source to every sink, or back from a sink to the source. The next lesson shows how to find sources and sinks by searching for their names.

What is injection

No line between the source and the sink checks what the visitor typed. An attacker types ' OR '1'='1 in place of a product name.

  1. SELECT * FROM products WHERE name = 'hoodie'-- A customer typed hoodie.
  2. SELECT * FROM products WHERE name = '' OR '1'='1'-- The attacker typed ' OR '1'='1
The text from the attacker has become part of the SQL.

The database cannot tell which part of the query the programmer wrote. The database runs the attacker's text as SQL. With OR, a row matches when either test is true. '1'='1' is always true, so every row matches.

Injection is untrusted data reaching an interpreter, which then treats the data as instructions. The lesson on SQL injection shows the attack on the search step by step.

What is a sanitiser

A sanitiser is a step in the code that makes data safe for one kind of sink. The sanitiser is the filter between the tap and the drain. Line 3 is not a sanitiser, because a value with no spaces at the ends can still contain SQL.

Validation, checking that input is the kind of data the program expects, can be a sanitiser. A value turned into a whole number with int() cannot contain SQL.

A product name is text, so the safer search changes lines 4 and 5 instead. Look at the question mark in the query:

    query = "SELECT * FROM products WHERE name = ?"
    rows = database.execute(query, [wanted])

The question mark marks where the value belongs. Line 5 sends the query and the value to the database separately. The database never reads the value as SQL.

The first search

Tapthe source, request.args["name"]

Nothing in betweenwanted is joined into the SQL

Drainthe sink, database.execute(query)

The safer search

Tapthe source, request.args["name"]

Filterthe question mark, which keeps the value apart from the SQL

Drainthe sink, database.execute(query, [wanted])

In the safer search, the value reaches the database but never as SQL.

On every path from a source to a sink, a reviewer asks one question. What made this data safe for this sink?

One sanitiser for each kind of sink

The shop also writes the search word into the results page. The browser reads the HTML of the page as an interpreter does, so the page is a second sink.

The question mark protects the database and does nothing for the page. In SQL, the character ' has a special meaning. In HTML, the characters < and > have a special meaning. The page needs a different sanitiser, which the lesson on cross-site scripting teaches.

Through a database, an attacker can read, change or delete data. Through a web page, an attacker can run code in another person's browser.

Examples of what to look for

  • A new line that reads from a request, such as request.args["name"]. The line is a source.
  • A new line that passes a string to a database, such as database.execute(query). The line is a sink.
  • A string joined with +, an f-string or a template string, where one piece came from a source.
  • A sanitiser made for one kind of sink on a value that goes to a different kind of sink.

Check your understanding

  1. State the question a reviewer asks on every path from a source to a sink.

    Show the answer

    What made this data safe for this sink?

  2. Identify the source and the sink in the six lines of the sports shop search.

    Show the answer

    Line 2 is the source, where the product name comes in from the request. Line 5 is the sink, where the query goes to the database.

  3. Give one reason why a sanitiser made for a database does not protect a web page.

    Show the answer

    A database reads SQL and a browser reads HTML. The two languages give a special meaning to different characters.

Practise it

Challenges with a weakness this lesson teaches.