Lesson 6 of 6
Searching the code
Listen to an AI summary of Searching the code
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.
Source
A search finds this line.
- data
The code between
A reviewer reads these lines.
- data
Sink
A search finds this line.
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/
- grepthe program
- -rnthe options
- "request."the text to find
- shop/the folder to search
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:
Each result has three pieces, split by colons:
shop/search.py:8: name = request.args["name"]
- shop/search.pythe file
- 8the line number
- name = request.args["name"]the text of the line
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:
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(:
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:
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.headersandreq.paramsread a request.query(runs a SQL query.- Java
getParameter(,getHeader(andgetCookies(read a request.executeQuery(runs a SQL query.- PHP
$_GET,$_POSTand$_COOKIEhold request data.mysqli_query(runs a SQL query.- Ruby
params[,cookies[andrequest.headersread a request.find_by_sql(runs a SQL query.- Go
r.URL.Query(),r.FormValue(andr.Headerread a request.db.Query(runs a SQL query.- C#
Request.Query,Request.FormandRequest.Cookieshold request data.FromSqlRaw(runs a SQL query.
Check your understanding
State what
grepdoes.Show the answer
grepsearches files for a piece of text and prints every line that has the text.The search for
request.printsshop/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.pyand the line number is 5. The line reads a cookie.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.pyhas no weakness.