Skip to content

Lesson 4 of 12 · 22m

SAST: Static Analysis That Doesn't Cry Wolf

Static analysis is the workhorse of shift-left and its biggest noise source — the job isn't running Semgrep, it's curating what it's allowed to say.

A fresh Semgrep run on a mature codebase will cheerfully report thousands of "findings," most of them technically correct and practically irrelevant. Running the tool is the easy half — anyone can semgrep scan --config auto and present the number as an accomplishment. The real work is the tuning loop that turns that firehose into ten findings a sprint that people fix.

What SAST actually does

Static analysis pattern-matches your source against known-bad shapes: string-concatenated SQL, eval(), innerHTML fed by user data, weak crypto, dangerous deserialization. The better tools do interprocedural dataflow — Semgrep's taint mode and CodeQL follow a value from req.query into a db.query sink across several files, which is what separates them from grep with anxiety.

What it's good at:

  • "Never do X" rules: eval(userInput), child_process.exec with concatenation, document.write, MD5 for anything but checksums.
  • Framework misuse: missing CSRF middleware, DEBUG = True in a Django settings file, verify=False on a requests call.
  • Custom rules for your codebase — the underused superpower. Your ORM's footguns, your internal auth helper invoked with the wrong scope.

What it's bad at — the wrong mental model is "coverage," as if a clean scan means safe code:

  • Business-logic flaws. No scanner finds "user can refund other users' orders." There is no pattern for your authorization mistake.
  • Anything assembled at runtime: config composed at deploy, reflection, dynamic require, plugin loaders. DAST and review cover that side.

A custom rule is five lines of YAML

# .semgrep/no-sql-concat.yaml
rules:
  - id: sql-concat-query
    patterns:
      - pattern: db.query($A + $B, ...)
      - pattern-not: db.query("..." + "...", ...)
    message: "Use parameterized queries — see security/sql.md"
    severity: ERROR
    languages: [javascript]
semgrep scan --config .semgrep/ --config p/javascript --error

--error makes findings exit non-zero — that's your gate. p/javascript pulls the community pack from the registry; the local directory adds house rules. A rule named after your own codebase's conventions outperforms a generic pack in signal-to-noise every time, because it encodes context a registry author never had. metavariable-regex and pattern-either get you most of what CodeQL's query language does for the common cases, without a PhD in .ql.

Reading the output like an engineer

SAST findings arrive with a severity, a confidence, and a dataflow path — read the path, not the severity. req.params.id reaching db.query through a sanitization layer the tool can't model is a false positive worth suppressing; req.body reaching exec with no validation is a real bug at "medium." --autofix rewrites some finding classes automatically — use it for the mechanical ones (deprecated APIs, unsafe defaults), never for anything touching auth or data.

The tuning loop that works

  1. Run full-repo, advisory only. Read the top twenty findings by hand.
  2. Disable rule families that are pure noise for your stack — a Java deserialization rule in a Node shop earns nobody anything.
  3. Triage the rest: real bugs get tickets; accepted ones get // nosemgrep: rule-id -- reason, reviewed like code.
  4. Only then enable blocking — and only on rules you've vetted.

Field notes

  • nosemgrep suppressions require a justification in the comment and get reviewed like code — they're an audit trail, not an escape hatch.
  • Language coverage is the hidden gotcha: a "multi-language" tool may have deep rules for Java and three for Go. Check rule counts for your stack before buying.
  • Diff-aware scanning (semgrep ci) is what belongs in PRs; full-repo scans are scheduled work. CodeQL's GitHub integration does the same split through codeql-action/analyze.
  • Track false-positive rate per rule, not per tool. One noisy rule family generates most of the pain — delete the rule, keep the tool.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.