Secure code review with AI
AI can review pull requests for security problems in seconds. Learn what it catches, what it misses, and how to check that a fix really works.
In 60 seconds
Secure code review with AI
AI can review pull requests for security problems in seconds. Learn what it catches, what it misses, and how to check that a fix really works.
What AI tends to catch, and what it tends to miss
- Clear injection patterns (SQL built by joining strings)
- Secrets pasted into code
- Missing input checks in the changed lines
- Risky functions and old crypto
- Authorisation bugs that need knowledge of business rules
- Problems that span many files or services
- Issues in code it was not shown
- Whether a fix really works when run
A vulnerable and a fixed example
def get_user(conn, username):
cur = conn.cursor()
# BAD: user input is joined into the SQL text
query = "SELECT id, email FROM users WHERE name = '" + username + "'"
cur.execute(query)
return cur.fetchone()def get_user(conn, username):
cur = conn.cursor()
# GOOD: the value is passed separately; the database treats it as data
cur.execute("SELECT id, email FROM users WHERE name = ?", (username,))
return cur.fetchone()' OR '1'='1 changes the meaning of the SQL. In the fixed version, the database driver sends the value separately, so it can never become part of the command. The placeholder style depends on the driver: sqlite3 uses ?, some other Python drivers use %s.A security review checklist
| Area | Questions to ask (you and the AI) |
|---|---|
| Injection | Does any input reach SQL, shell commands, file paths or templates without safe handling? |
| Authorisation | Does every route check that this user may do this action on this object? |
| Authentication | Are sessions, tokens and password handling done with standard libraries? |
| Secrets | Are any keys, passwords or tokens in the code or config files? |
| Data exposure | Does any response or log include more personal data than needed? |
| Dependencies | Are new libraries needed, maintained and from the right source? |
Verify the fix
- 1
Write a test
Add a test that sends the attack input and expects safe behaviour. - 2
See it fail
Run it on the old code; it should fail (or show the bug). - 3
See it pass
Run it on the fixed code; it should pass. - 4
Look for siblings
Search for the same pattern elsewhere in the codebase.
Watch and read more
Lab
An AI-assisted review of a small made-up function, with a verified fix and a test.
The problem
Work on your own machine only. Use the starter code (fictional).
- Ask an AI to review the code for security problems using the checklist in this lesson.
- List each issue it found, and mark whether you agree. Add any issue it missed.
- Write a fixed version.
- Write a short test (or a clear manual test plan) that shows the injection no longer works and that users cannot read other users' notes.
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1The note_id comes from the URL and is converted with str(). Does that make it safe?Reveal
No. str() just turns any value into text; it does not check that the value is a number. If the caller passes a crafted string, it goes straight into the SQL. Converting with int() would reject non-numbers, but parameterised queries are still the correct fix.
Q2Why is the missing ownership check easy for an AI reviewer to miss, even though the parameter is right there?Reveal
The function works and has no obviously dangerous call. Noticing the bug needs a rule the code does not state: users may only read their own notes. A hint helps (the unused current_user_id), but reviewers must think about who is allowed to do what, which is why a checklist with an authorisation question helps both people and AI.
Questions people ask
Can an AI reviewer replace a human security review?
No. It is a useful first pass that catches many common mistakes quickly. Human reviewers bring knowledge of the business rules, the design and the threats, which is where many serious bugs hide.
Is it safe to send our code to an AI reviewer?
Only if the tool is approved for your code. Code can contain secrets and business logic. Check where the code is sent, whether it is stored, and whether it is used for training.
Why are authorisation bugs hard for AI?
The code may look perfectly normal. The bug is that a user can see or change something that belongs to someone else, and only someone who knows the rules ("a customer may only see their own orders") can tell that a check is missing.
What if the AI reviewer produces too many comments?
Tune it to focus on security-relevant issues, and track which comments developers accept. If most comments are ignored, people stop reading them, and real issues get lost.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.