Notes
How SAST works
SAST works at levels of depth. The simplest is a regular expression over the text, noisy and understanding nothing. Better is matching on the parsed syntax tree, so the tool understands structure, not characters; that is Semgrep. The deepest is dataflow and taint analysis, which follows values through functions and files to see whether untrusted input can reach a dangerous sink. Taint tracking is the follow-the-data technique from last episode, automated, and it makes the serious tools powerful.
- simplest: regex on text (noisy, no understanding)
- better: match on the parsed syntax tree (Semgrep)
- deepest: dataflow and taint analysis, across functions and files (CodeQL)
- taint = follow untrusted input to a dangerous sink, automatically
Semgrep
Semgrep is fast, runs locally and in the pipeline, and its rules are readable: a rule looks like the code it matches, with a few wildcards. Here is one that flags subprocess calls with shell equals true. You can write a useful rule in minutes, and there is a large community ruleset. Semgrep is the easiest way to start with SAST and to encode your team's patterns. Its limit is that pure pattern matching cannot always tell whether input is really attacker-controlled, which is where taint analysis helps.
rules:
- id: python-shell-true
pattern: subprocess.$F(...,
shell=True)
message: shell=True with a
variable is command
injection
severity: ERROR
languages: [python]
# the pattern looks like the code CodeQL
CodeQL turns your codebase into a database you query in a language called QL. Because it builds a full dataflow model, it answers deep questions, like whether untrusted input from any request handler can reach a particular SQL sink anywhere in the program. That makes it precise for injection bugs, with fewer false positives. It is free for open source and built into GitHub code scanning. It is steeper to learn than Semgrep, so many teams use both: Semgrep for quick rules, CodeQL for depth.
- treats code as a database you query (QL language)
- deep dataflow and taint: does untrusted input reach this sink, across the whole program?
- fewer false positives on injection-style bugs
- free for open source; built into GitHub code scanning
- steeper to learn than Semgrep
Write your own rules
Here is the real power of modern SAST, and the point of this episode. When you find a bug in review or a pentest, write a rule for it. From then on, every repository is checked for that pattern automatically, forever, with no reviewer time. You can encode things specific to you: a forbidden internal function, a controller method missing its authorization annotation, an unsafe helper. This is how a small security team scales across hundreds of developers: by turning knowledge into rules instead of repeating reviews.
- found a bug in review or a pentest? write a rule for it
- now every repo is checked for that pattern, forever
- examples: a forbidden function, a missing authz annotation, an internal unsafe helper
- this is how security scales without more reviewers
In the pipeline
Where SAST runs decides whether it helps. Best is in the developer's editor, instant feedback as code is written. Next is on pull requests, commenting on changed lines and blocking only on new high-severity findings. Scan just the diff for speed, run full scans nightly. Crucially, baseline existing findings and focus on new issues from this change, not ten thousand old warnings. And do not fail the build on old or low issues, which only teaches people to bypass the tool.
- in the IDE: instant feedback as code is written
- on pull requests: comment on the diff, block on new highs
- scan only the diff for speed; full scans nightly
- baseline existing findings; focus on NEW issues
- do not fail the build on old or low issues
False positives
False positives are what kill a SAST programme. If the tool cries wolf, developers stop reading it, and then it catches nothing. So manage them. Tune rules to your stack and disable noisy ones. Triage every finding: confirm and fix, or mark it a false positive with a reason. Allow in-code suppression with a comment and justification, visible in review. Track the true-positive rate and fix rules that are mostly wrong. Noise is the biggest reason teams abandon static analysis, so treat signal quality as the goal.
- tune rules to your stack; disable the noisy ones
- triage: confirm, fix, or mark false positive with a reason
- suppress in code with a comment and justification
- track the true-positive rate; if it is low, fix the rules
- noise is the number one reason teams abandon SAST
The wider toolkit
SAST is one of a family of automated checks, and you use several because each finds a different class of bug. SAST reads your source code. Software composition analysis (Part nine) finds known vulnerabilities in dependencies. Secret scanning finds keys and tokens in code and git history. Dynamic analysis tests the running app from outside, catching runtime-only issues. Interactive analysis instruments a running app from inside. No single tool is enough; together they cover far more.
- SAST: your source code
- SCA: known vulns in dependencies (Part 9)
- secret scanning: keys and tokens in code and history
- DAST: tests the running app from outside
- IAST: instruments a running app from inside
- use several; each finds a different class
Common mistakes
Mistakes. Dumping thousands of old findings on developers instead of baselining and focusing on new ones. Failing the build on every low issue, which trains people to bypass the tool. Never writing custom rules, missing the point. Ignoring the output. A clean run read as proof of security, when SAST misses logic and authorization. No triage, so false positives pile up and trust erodes. And running only SAST, with no dependency scanning, secret scanning or dynamic testing.
- dumping thousands of old findings on developers
- failing the build on every low issue
- never writing custom rules
- ignoring the output
- a clean SAST run read as "secure"
- no triage; false positives pile up
- SAST only, no SCA/secrets/DAST
Say it like this
In the interview: Semgrep for fast, readable rules I can write myself, CodeQL for deep taint analysis that traces untrusted input to sinks with few false positives. I run SAST in the IDE and on pull requests, scanning the diff, blocking only on new high-severity findings and baselining the rest. When we find a bug in review or a pentest, I write a rule so it never returns. I manage false positives hard, because noise kills adoption, and pair SAST with dependency scanning, secret scanning and dynamic testing.
- "Semgrep for fast, readable
- custom rules, CodeQL for deep
- taint analysis. Run it in the IDE and on PRs, scan the diff, block only on new highs, baseline the rest. I write
- rules for bugs we find so they
- never return, manage false
- positives hard, and pair it
- with SCA, secrets and DAST."