SECURITY ENGINEERING · PART 8 · WRITING SECURE CODE · TESTING · 120 OF 229

SAST in Practice: Semgrep and CodeQL Rules

Static analysis reads code without running it to find security bugs. This episode explains how SAST works, pattern matching (Semgrep) versus deep dataflow and taint tracking (CodeQL), writing your own rules to catch a bug class once and forever, where SAST fits in the pipeline, how to manage false positives and keep developers on side, and the related tools SCA, secret scanning and DAST.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Reads code for bugs
  • Semgrep patterns, CodeQL taint
  • write rules to catch a class forever
  • in the PR, new issues only

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.

From shallow to deep
  • 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.

a Semgrep rule
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.

CodeQL
  • 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.

From one bug to a rule
  • 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.

Fitting it in
  • 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.

Keep developers on side
  • 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 and its siblings
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What is SAST?

Static Application Security Testing: analysing source code, bytecode or binaries without executing them to find security vulnerabilities such as injection, insecure APIs and hard-coded secrets. It provides broad, early, automated coverage in the development pipeline.

Q2What is the difference between Semgrep and CodeQL?

Semgrep matches patterns on the parsed syntax tree, with fast, readable rules that resemble the code, ideal for custom checks. CodeQL treats code as a queryable database and performs deep dataflow and taint analysis across the whole program, giving higher precision on injection-style bugs at the cost of a steeper learning curve.

Q3What is taint analysis?

A dataflow technique that tracks untrusted input (the source) through a program to see whether it reaches a dangerous operation (the sink) without being sanitised. It automates the follow-the-data approach used in manual review and underlies precise SAST tools.

Q4Why write custom SAST rules?

To encode organisation-specific knowledge: forbidden internal functions, required authorization annotations, or a bug class found in review. Once written, the rule checks every repository automatically and forever, which is how a small security team scales across many developers.

Q5How do you manage SAST false positives?

Tune and disable noisy rules, triage each finding to confirm or mark it false with a reason, allow justified in-code suppressions visible in review, and track the true-positive rate. Controlling noise is essential, because it is the main reason teams abandon static analysis.

Q6Where should SAST run in the development process?

In the developer's IDE for instant feedback, and on pull requests scanning the diff and blocking only on new high-severity findings, with full scans run periodically. Existing findings should be baselined so attention focuses on newly introduced issues.

Q7Does a clean SAST scan mean the code is secure?

No. SAST detects known insecure patterns but misses business logic flaws, broken access control and design issues. It must be combined with code review, dependency and secret scanning, and dynamic testing for meaningful assurance.

Q8What is the difference between SAST, SCA, DAST and secret scanning?

SAST analyses your source code, SCA finds known vulnerabilities in dependencies, secret scanning detects credentials in code and history, and DAST tests the running application from the outside. Each covers a different class of issue and they are used together.