Skip to content
mentoor

Topic 2 of 5 · 5 min read

Static security scanning with SAST tools

Goal: Read a SAST finding on AI-written code and decide whether it is a true positive, a false positive or a gap that needs a custom rule.

SAST (static application security testing) reads your source code without running it and flags patterns that are known to be dangerous; use it on every agent change, and learn to judge each finding. Agents copy common code patterns, and some common patterns are insecure.

Check yourself · 1 of 3

What does a SAST tool do?

Choose one answer

Why it matters

An agent asked for "a search endpoint" may build a SQL query with string concatenation, because that is how a lot of its training examples look. It will not warn you. A SAST tool does: it sees user input flowing into a query and raises a finding. That is a bug caught before a human has read the diff.

The tools

  • Semgrep matches code patterns and is quick to write rules for. Good for local runs and for rules about your own code.
  • CodeQL turns code into a database and queries data flow, such as "does input from a request reach this SQL call?". Slower, deeper, and built into GitHub code scanning.
  • Language-specific tools such as Bandit (Python) or gosec (Go) cover one ecosystem.

How to read a finding

  1. Find the source and the sink. Where does untrusted data enter, and where does it do damage? A finding with both on the page is likely real.
  2. Check for a guard. Is there validation, escaping or a parameterised query the tool could not see? Then it may be a false positive.
  3. Decide: true positive, false positive, or gap. True positives are fixed. False positives get a narrow, explained suppression or a rule tweak. A gap is a risky pattern in your code that no rule covers, so you write one.

Before and after

# Flagged: user input flows into SQL
cur.execute(f"SELECT * FROM orders WHERE user_id = {user_id}")

# Fixed: parameterised query
cur.execute("SELECT * FROM orders WHERE user_id = %s", (user_id,))

Custom rules

Every codebase has its own dangerous calls, such as a helper runShell() or a raw query wrapper. Write a Semgrep rule that flags any use outside an approved folder. Agents will keep reaching for the helper unless something says no.

What goes wrong

  • Alert fatigue. Hundreds of unreviewed findings train everyone to click dismiss.
  • Blanket suppression. An agent silences a finding with a disable comment instead of fixing it. Review every suppression like code.
  • False comfort. SAST finds patterns, not business-logic bugs such as a missing permission check. A clean scan does not mean secure.

Examples

A true positive on agent code

Semgrep flags subprocess.run(cmd, shell=True) where cmd includes a filename from the request. Source: the request. Sink: the shell. No guard. This is real command injection, and the fix is to pass an argument list without shell=True.

A false positive worth a narrow suppression

A rule flags eval in a build script that only reads a constant from the repo. No user input reaches it. The team adds one suppression on that line with a reason: # nosemgrep: build-time constant, no external input. The rule stays on everywhere else.

Key points

  • SAST scans source code for dangerous patterns without running it, so run it on every agent change.
  • Semgrep is fast and easy to extend; CodeQL follows data flow deeper and is slower.
  • Triage each finding by finding the source and sink, then checking for a guard the tool missed.
  • Write custom rules for your own risky helpers so agents cannot reach for them unnoticed.
  • A clean scan is not proof of security: SAST misses business-logic flaws like missing permission checks.

Common mistakes

  • Dismissing findings in bulk because most look like noise, so the real one is missed.
  • Letting the agent add suppression comments to get a green scan without anyone reading them.
  • Assuming a clean scan means the code is secure, including its authorisation logic.
  • Never writing a custom rule, so patterns unique to your codebase are never checked.

Check yourself · 2 of 3

A SAST tool flags a SQL query built by string concatenation with a request parameter. What is the usual correct response?

Choose one answer

Check yourself · 3 of 3

Your team has a helper runShell() that is dangerous if misused, and no built-in rule covers it. What should you do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic