Skip to content

Lesson 6 of 6

Searching the code

Listen to an AI summary of Searching the code

0:00 / 0:32

What a reviewer searches for

A search finds each source and sink whose name the reviewer knows, and the reviewer then reads the lines between.

  1. Source

    A search finds this line.

  2. data

    The code between

    A reviewer reads these lines.

  3. data

    Sink

    A search finds this line.

The path of data from a source to a sink.

What is grep

A terminal is a window where a person types commands for the computer to run. A command is a typed instruction that tells a computer to run a program. The program grep searches files for a piece of text and prints every line that has the text.

Where the sports shop reads a request

The Python code of the sports shop is in a folder named shop. In the code, the word request stands for the request that came in. Four names read the four parts of a request that hold data (What is in a request):

request.args
The query parameters.
request.form
The body, which holds the form fields.
request.cookies
The cookies.
request.headers
The headers.

All four names start with request., so one search for request. finds every line that reads one of the four parts. Code that gives the request another name, or reads the request inside a function of its own, needs a search for that name as well. A value in the path, such as the 1042 in /orders/1042, does not pass through request. The value arrives as an argument of the function that answers the request, so a reviewer reads those arguments as well.

A search for sources

A grep command that searches the folder shop has four pieces:

grep -rn "request." shop/

  1. grepthe program
  2. -rnthe options
  3. "request."the text to find
  4. shop/the folder to search
The four pieces of one grep command.

The piece -rn is two options, and an option changes how grep works. The option r searches every file in the folder, and n prints the number of each line.

The command is typed into a terminal, followed by Enter. grep prints one result for each line that has the text:

Terminal
grep -rn "request." shop/the command
shop/search.py:8: name = request.args["name"]reads a query parameter
shop/signin.py:6: email = request.form["email"]reads a form in the body
shop/basket.py:5: basket = request.cookies["basket"]reads a cookie
shop/orders.py:11: language = request.headers["Accept-Language"]reads a header
One command and four results. Each result is a source.

Each result has three pieces, split by colons:

shop/search.py:8: name = request.args["name"]

  1. shop/search.pythe file
  2. 8the line number
  3. name = request.args["name"]the text of the line
Line 8 of shop/search.py reads the query parameter name.

A search without a terminal

The search box of a code editor, such as Visual Studio Code, searches every file in a folder:

  1. request.the text typed into the search box
  2. search.pya file with a result
  3. name = request.args["name"]the line that has the text
  4. signin.pythe next file with a result
  5. email = request.form["email"]the line that has the text
Each file with a result, and under the file each line that has the text.

A search for sinks

The code sends each query, such as a SQL SELECT statement, to the shop's database by calling the function execute.

A call is the name of the function and an opening bracket, so the text to find is execute(:

Terminal
grep -rn "execute(" shop/
shop/search.py:10: rows = database.execute(query)
shop/products.py:5: rows = database.execute("SELECT name, price FROM products")

Both results send a query to the database, so both are sinks.

Reading the results

A search result is a place to start reading, not a weakness. The reviewer opens the file at each result and reads the lines around the result.

Lines 7 to 10 of shop/search.py show where name comes from and where name goes:

  1. def search():
  2. name = request.args["name"]# the source
  3. query = "SELECT * FROM products WHERE name = '" + name + "'"# joins name into the query
  4. rows = database.execute(query)# the sink
The highlighted lines are the two lines the searches found.

Line 9 joins untrusted data into the query, so an attacker can change the query. The reviewer has found SQL injection (CWE-89), which a later lesson teaches.

The query on line 5 of shop/products.py is fixed text that the programmer wrote. No data from a request reaches the query, so line 5 has no weakness.

After a weakness is found

A weakness found in one place can be in other places too. A search for the SQL word SELECT finds the other queries in the sports shop. The reviewer reads each query for the same mistake.

Examples of names to search for

Every programming language has its own names for sources and sinks. Each row gives names that read a request and one that runs a SQL query:

JavaScript on a server
req.query, req.body, req.cookies, req.headers and req.params read a request. query( runs a SQL query.
Java
getParameter(, getHeader( and getCookies( read a request. executeQuery( runs a SQL query.
PHP
$_GET, $_POST and $_COOKIE hold request data. mysqli_query( runs a SQL query.
Ruby
params[, cookies[ and request.headers read a request. find_by_sql( runs a SQL query.
Go
r.URL.Query(), r.FormValue( and r.Header read a request. db.Query( runs a SQL query.
C#
Request.Query, Request.Form and Request.Cookies hold request data. FromSqlRaw( runs a SQL query.

Check your understanding

  1. State what grep does.

    Show the answer

    grep searches files for a piece of text and prints every line that has the text.

  2. The search for request. prints shop/basket.py:5: basket = request.cookies["basket"]. Identify the file, the line number and the part of the request the line reads.

    Show the answer

    The file is shop/basket.py and the line number is 5. The line reads a cookie.

  3. Give one reason why a search result is a place to read and not a weakness.

    Show the answer

    A search finds a name and does not show where the data comes from. The fixed query on line 5 of shop/products.py has no weakness.