Skip to main content

Semgrep Static Analysis: Custom Rules, CI Integration & SAST Guide

Semgrep is a fast, open-source SAST tool that finds bugs and security vulnerabilities using pattern-based rules. Unlike regex, Semgrep understands code structure — making it easy to write precise custom rules for your codebase and integrate security checks directly into CI/CD pipelines.

✓ 30+ languages supported ✓ 5,000+ community rules ✓ Custom rule authoring in YAML ✓ SARIF output for GitHub Security tab ✓ Runs in seconds on large codebases

What is Semgrep vs Other SAST Tools?

Traditional SAST tools like Fortify or Checkmarx use complex data-flow analysis — powerful but slow and hard to customize. Semgrep takes a different approach: lightweight syntactic pattern matching against the AST. This makes it fast enough to run on every PR, easy to write custom rules for, and flexible enough to catch business-logic bugs alongside security vulnerabilities.

Semgrep is fully open-source (OSS tier). Semgrep Code (cloud) adds cross-file taint analysis and a web dashboard for team management.

Installation and Quickstart

# Install via pip
pip install semgrep

# Or via Homebrew
brew install semgrep

# Run with auto-selected rules for your languages
semgrep --config=auto .

# Run a specific ruleset
semgrep --config=p/nodejs .

# Run against a single file
semgrep --config=auto src/auth.js

Using Community Rulesets

# Auto-detect language and apply best rulesets
semgrep --config=auto .

# Security-focused rulesets
semgrep --config=p/owasp-top-ten .
semgrep --config=p/nodejs .
semgrep --config=p/python .
semgrep --config=p/django .
semgrep --config=p/react .

# Secret detection
semgrep --config=p/secrets .

# Multiple configs
semgrep --config=p/nodejs --config=p/jwt .

Writing Custom Rules

Rules are YAML files. Here's a rule that catches SQL injection via string concatenation in Python:

rules:
  - id: sql-injection-string-concat
    pattern: |
      cursor.execute("..." + $USER_INPUT)
    message: |
      SQL injection risk: do not concatenate user input into queries.
      Use parameterized queries: cursor.execute("SELECT ...", (value,))
    languages: [python]
    severity: ERROR
    metadata:
      category: security
      cwe: CWE-89

  - id: hardcoded-aws-secret
    pattern-either:
      - pattern: |
          $KEY = "AKIA..."
      - pattern: |
          aws_secret_access_key = "..."
    message: Hardcoded AWS credentials detected
    languages: [python, javascript, typescript]
    severity: ERROR

CI Integration — GitHub Actions

name: Semgrep SAST

on:
  push:
    branches: [main]
  pull_request:

jobs:
  semgrep:
    runs-on: ubuntu-latest
    container:
      image: semgrep/semgrep
    steps:
      - uses: actions/checkout@v4

      - name: Run Semgrep
        run: |
          semgrep \
            --config=auto \
            --sarif \
            --output=semgrep.sarif \
            --error
        env:
          SEMGREP_APP_TOKEN: {{ secrets.SEMGREP_APP_TOKEN }}

      - name: Upload SARIF
        uses: github/codeql-action/upload-sarif@v3
        if: always()
        with:
          sarif_file: semgrep.sarif

Detecting Common Security Issues

Vulnerability Ruleset Pattern Example
SQL Injectionp/owasp-top-tenString concat in query
XSSp/react, p/nodejsinnerHTML = userInput
Hardcoded Secretsp/secretspassword = "literal"
Insecure Deserializationp/pythonpickle.loads(data)
Path Traversalp/nodejspath.join(req.params.x)

Semgrep vs SonarQube vs CodeQL

Aspect Semgrep SonarQube CodeQL
Custom rulesEasy YAMLComplex Java APIQL language
SpeedVery fastModerateSlow (deep analysis)
Data-flow analysisCloud tierYesYes (best-in-class)
Free tierOSS fully freeCommunity editionFree on GitHub
Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$9/mo after trial · Cancel anytime

Want the full toolkit?

Get DevPlaybook Pro

Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →