Lesson 4 of 6
Static code analysis
Listen to an AI summary of Static code analysis
A spellchecker reads a letter and underlines every word the spellchecker thinks is wrong. Programmers have a tool that does the same job for a program.
What is static analysis
A static analysis tool reads the source code of a program without running the software. The tool reports each line that matches code the tool knows is risky.
A risky line can have a weakness.
The spellchecker stands for the static analysis tool, and the letter stands for the source code. A spellchecker cannot tell whether the letter says what the writer meant. A static analysis tool cannot tell what the software is for.
When the tool runs
A team can run a static analysis tool on every merge request. One run of the tool over the code is called a scan. A scan can read every line of the code in a few minutes.
A program from the sports shop
The Python in the example is part of the sports shop. Look at the two variables at the top and the three functions.
admin_password = "football2026" password_label = "Type your password" def order_total(quantity): return eval(quantity + " * 12") def place_order(): quantity = form_field("quantity") # the text the customer typed return order_total(quantity) def show_order(order_id): return find_order(order_id) # looks up one order by number
The first line stores the password of the admin account, which the shop office uses to run the shop. The second line stores the label beside the password box on the sign-in page.
order_total works out the price of an order of ties at 12 pounds each, and calls eval. eval is a Python function that runs a string as Python code.
place_order reads the quantity the customer typed and passes the quantity to order_total. show_order finds one order by the order's number and returns the order.
Two ways the tool finds risky code
A pattern rule is a description of risky code that the tool searches for. One pattern rule describes a variable with password in the name that is set to a string. A second pattern rule describes any call to eval.
A pattern rule is quick to write and misses any code the rule does not describe. The same password in a variable named admin_key matches neither rule.
Data flow analysis is the tool following a value from where the value enters the code to where the value is used. The tool follows the value from one function to the next, and from one file of code to the next.
form_field
the customer's text enters
- returns the text
place_order
stores the text in quantity
- passes the text
order_total
receives quantity
- passes the text
eval
runs the text as Python code
Data flow analysis shows that the text reaching eval came from the customer. A pattern rule for eval cannot show where the text came from. Data flow analysis finds weaknesses a pattern rule cannot describe, and takes longer to run.
What the tool reports
The team scans the program with the two pattern rules. The tool reports three results.
Result 1 is real. Anyone who can read the code can read the admin password.
Result 2 is a false positive, a result that reports a weakness where no weakness exists. The second line matches the pattern rule for a password, but the string is a label for the sign-in page. A spellchecker makes the same kind of mistake by underlining a name that is spelt correctly.
Result 3 is real. A customer can type Python code in place of a number, and eval runs that code on the shop's server.
What the tool misses
show_order returns any order to anyone who asks for the order's number. The shop's rule is that a customer sees only the customer's own orders. The tool does not know the shop's rule, so the tool reports nothing.
A false negative is a real weakness that a tool did not report. The missing check in show_order is a false negative. A password in admin_key would be a false negative too. A scan with no results does not show that the code has no weaknesses.
Check your understanding
State what a static analysis tool reads, and one thing the tool does not know.
Show the answer
A static analysis tool reads the source code without running the software. The tool does not know what the software is for.
Which result in the sports shop example is a false positive, and which function has a false negative?
Show the answer
Result 2 is the false positive, because the string is a label and not a password.
show_orderhas the false negative, because the tool did not report the missing check.Give one reason why a scan with no results does not show that the code has no weaknesses.
Show the answer
A tool can miss a real weakness, which is a false negative. For example, the tool does not know the software's rules about who may see an order.