AI for security defendersMiddleLesson 1656 min read

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.

Lesson in motion

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.

1/5
In simple words
When you write an exam answer, a friend can read it and spot silly mistakes fast. But the friend may miss a big mistake, and may point to something that is actually fine. You still read the answer again yourself.
In a pull request (PR), a developer proposes code changes and others review them before they are merged. AI reviewers can comment on every PR, looking for security problems early, when they are cheapest to fix.

What AI tends to catch, and what it tends to miss

Often catches
  • Clear injection patterns (SQL built by joining strings)
  • Secrets pasted into code
  • Missing input checks in the changed lines
  • Risky functions and old crypto
Often misses
  • 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

Vulnerable: SQL injection (Python, sqlite3)python
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()
Fixed: parameterised querypython
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()
In the first version, a username like ' 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

AreaQuestions to ask (you and the AI)
InjectionDoes any input reach SQL, shell commands, file paths or templates without safe handling?
AuthorisationDoes every route check that this user may do this action on this object?
AuthenticationAre sessions, tokens and password handling done with standard libraries?
SecretsAre any keys, passwords or tokens in the code or config files?
Data exposureDoes any response or log include more personal data than needed?
DependenciesAre new libraries needed, maintained and from the right source?

Verify the fix

  1. 1

    Write a test

    Add a test that sends the attack input and expects safe behaviour.
  2. 2

    See it fail

    Run it on the old code; it should fail (or show the bug).
  3. 3

    See it pass

    Run it on the fixed code; it should pass.
  4. 4

    Look for siblings

    Search for the same pattern elsewhere in the codebase.
Watch out
AI-suggested fixes can look right but be incomplete, for example escaping quotes by hand instead of using parameters, or fixing one route and missing another. Review AI fixes as carefully as the original code.
Try this. Ask an AI to review the vulnerable function above. Then ask it for a test that proves the fix works. Run the test yourself if you can.

Watch and read more

Lab

An AI-assisted review of a small made-up function, with a verified fix and a test.

~30 min

The problem

Work on your own machine only. Use the starter code (fictional).

  1. Ask an AI to review the code for security problems using the checklist in this lesson.
  2. List each issue it found, and mark whether you agree. Add any issue it missed.
  3. Write a fixed version.
  4. 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.

Output

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.

Please sign in to continue.

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.

Sign in to take the test and save your progress