Nothing ruins a Friday deploy quite like finding a hardcoded API key or an unsafe SQL query in production. That is why Static Application Security Testing (SAST) belongs inside your CI/CD pipeline. Many teams put it off because they expect 20-minute builds and noisy alerts. With Semgrep and a few custom rules, you can catch real bugs in seconds without slowing down merges.
The Real-World Problem: Security Checks Slowing Down Delivery
Growing engineering teams often push security checks to the end of the cycle. A developer opens a pull request, unit tests pass, and a reviewer skims the diff for business logic. Weeks later, a quarterly pentest flags a SQL injection or an open redirect that has been live since last month.
Bolting on a legacy scanner usually creates a worse headache. A single commit triggers a 20-minute scan and dumps 150+ low-value warnings onto the pull request. Engineers quickly learn to mute the bot or force-merge past it. Your pipeline needs feedback that is both fast and accurate enough to earn developer trust.
Root Cause Analysis: Why Traditional SAST Frustrates Developers
Older static analysis tools fail in modern CI/CD pipelines for three predictable reasons:
- Naive text matching: Regex and grep scripts ignore code structure. They flag a string like
password_reset_urlin a unit test or a commented-out example in a README. - Heavy build dependencies: Legacy SAST engines often compile your entire app into an intermediate binary before scanning. That means maintaining a full Java, Go, or Rust toolchain inside every security job.
- One-size-fits-all rules: Public rulesets catch known CVEs, yet they miss your internal conventions. They will not notice a missing auth decorator on a new Flask route or a call to a deprecated helper.
Tip: Good SAST relies on Abstract Syntax Tree (AST) parsing instead of plain text search. AST-aware tools tell the difference between a string literal and a function call—without compiling a single file.
Solutions Compared: Choosing the Right SAST Approach
Before picking a tool, weigh the three main ways teams run static analysis:
- Custom regex in pre-commit hooks: Free and instant, but brittle. Maintaining regex patterns across Python, TypeScript, and Go soon turns into a chore, and false positives kill adoption.
- Enterprise SAST platforms: Strong at cross-file taint tracking and compliance reporting. However, they are expensive, slow on pull requests, and awkward to extend for team-specific rules.
- Semgrep: An open-source, AST-based analyzer that scans code in seconds with zero compilation. You write rules in YAML using syntax that looks almost identical to the target language.
For most platform and DevOps teams, Semgrep strikes the right balance between scan speed, low noise, and easy customization.
The Best Approach: Step-by-Step Semgrep Integration and Custom Rules
Roll out Semgrep in four stages: test locally, run diff-aware CI scans, add custom rules, and enforce blocking only after tuning noise.
Tip 1: Validate Rules Locally Before Touching CI
Never drop an untested security gate onto your main branch. Install Semgrep locally and run a baseline scan first so you know what it finds:
# Install Semgrep CLI
pip install semgrep
# Run a quick scan using community OWASP and security-audit rulesets
semgrep scan --config p/owasp-top-ten --config p/security-audit .
Does a community rule flood legacy files you cannot refactor this sprint? Add those paths to a .semgrepignore file at the repo root. A clean initial signal makes or breaks developer buy-in.
Tip 2: Run Fast, Diff-Aware Scans in GitHub Actions
Scanning only changed lines keeps pull request checks between 15 and 45 seconds on most repositories. Create .github/workflows/semgrep.yml using the official Semgrep container:
name: Semgrep SAST
on:
pull_request:
branches: [main]
jobs:
semgrep:
name: Static Security Scan
runs-on: ubuntu-latest
container:
image: semgrep/semgrep
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run Semgrep scan
run: |
semgrep scan \
--config p/owasp-top-ten \
--config .semgrep/rules/ \
--baseline-commit ${{ github.event.pull_request.base.sha }} \
--error \
--sarif --output=semgrep.sarif
- name: Upload SARIF report
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: semgrep.sarif
Three flags do the heavy lifting here. --baseline-commit restricts findings to code introduced in the pull request. --error fails the job when issues appear, and --sarif lets GitHub render inline annotations right on the diff.
Tip 3: Write Custom Semgrep Rules for Your Own Standards
Public rulesets cover common vulnerabilities, but custom rules deliver the biggest win for your team. Suppose your Python services must never call subprocess.run(..., shell=True) because of command injection risks.
Save a rule at .semgrep/rules/no-shell-true.yaml instead of catching it manually in code review:
rules:
- id: python-no-subprocess-shell-true
languages:
- python
severity: ERROR
message: >
Avoid using shell=True in subprocess calls as it can lead to command injection.
Pass a list of arguments instead.
pattern-either:
- pattern: subprocess.run(..., shell=True, ...)
- pattern: subprocess.Popen(..., shell=True, ...)
- pattern: subprocess.call(..., shell=True, ...)
That ... ellipsis operator matches any arguments before or after shell=True, regardless of line breaks. Here is how it behaves on real Python code:
import subprocess
# Flagged by our custom Semgrep rule
subprocess.run(f"ls -la {user_dir}", shell=True)
# Safe alternative
subprocess.run(["ls", "-la", user_dir], check=True)
Tip: Put a concrete fix in every rule’s message field. When developers see both the risk and the safe replacement in the CI log, they fix the bug in one commit.
Tip 4: Start in Audit Mode, Then Enforce
Avoid turning on --error on day one if your repo already has 40 or 50 legacy warnings. Run the workflow in non-blocking mode for a two-week sprint while the team triages findings. For true false positives, add an inline # nosemgrep: python-no-subprocess-shell-true comment with a brief reason.
Once the noise is gone, enable --error for ERROR-level rules on new pull requests and leave WARNING rules informational. Builds stay under a minute, real bugs get caught before merge, and releases keep moving.
Quick Checklist for Production Readiness
- Pin your Semgrep container tag or CLI version in CI (for example,
semgrep/semgrep:1.90.0) so upstream rule updates never break builds mid-sprint. - Store custom rules in
.semgrep/rules/and require peer review on rule changes just like production code. - Pair each custom rule with a test file and run
semgrep --test .semgrep/rules/in CI to catch silent regressions. - Keep high-confidence rules blocking and experimental rules non-blocking to protect developer trust.

