Skip to main content

Lightweight static analysis for many languages. Find bug variants with patterns that look like source code.

Project description

Semgrep logo

Lightweight static analysis for many languages.
Find and block bug variants with rules that look like source code.

Getting Started · Examples · Resources
Usage · Contributing · Commercial Support

Homebrew PyPI Issues welcome! Issues welcome! 1500+ GitHub stars

Semgrep tl;dr:

  • A simple, customizable, and fast static analysis tool for finding bugs
  • Combines the speed and customization of grep with the precision of traditional static analysis tools
  • No painful domain-specific language; Semgrep rules look like the source code you’re targeting
  • Batteries included with hundreds of existing community rules for OWASP Top 10 issues and common mistakes
  • Run it in CI, at pre-commit, or in the editor
  • Runs offline on uncompiled code

Semgrep supports:

Go Java JavaScript JSON Python Ruby (beta) C (alpha) OCaml (alpha)

Semgrep is proudly supported by r2c. Learn more about a hosted version of Semgrep with an enterprise feature set at r2c.dev.

Getting Started

The best place to start with Semgrep and rule writing is its Quick Start. For a more in-depth introduction to its syntax and use cases visit the Semgrep Tutorial.

Semgrep can be installed using brew, pip, or docker:

# For macOS
$ brew install semgrep

# On Ubuntu/WSL/linux, we recommend installing via `pip`
$ python3 -m pip install semgrep

# To try Semgrep without installation run via Docker
$ docker run --rm -v "${PWD}:/src" returntocorp/semgrep --help

To confirm installation and get an overview of Semgrep's functionality run with --help:

$ semgrep --help

Once installed, Semgrep can be run with single rule patterns or entire rule packs:

# Check for Python == where the left and right hand sides are the same (often a bug)
$ semgrep -e `$X==$X` --lang=py path/to/src

# Run a rule pack with rules for many languages
$ semgrep --config=https://semgrep.dev/p/r2c-CI path/to/src

Explore the Semgrep Registry of rules and CI integrations at semgrep.dev.

Examples

Use case Semgrep rule
Ban dangerous APIs Prevent use of exec
Search routes and authentiation Extract Spring routes
Enforce the use secure defaults Securely set Flask cookies
Enforce project best-practices Use assertEqual for == checks, Always check subprocess calls
Codify project-specific knowledge Verify transactions before making them
Audit security hotspots Airflow XSS, Hardcoded credentials
Audit configuration files Find S3 ARN uses
Migrate from deprecated APIs DES is deprecated, Deprecated Flask APIs, Deprecated Bokeh APIs
Apply automatic fixes Use listenAndServeTLS

Resources

Learn more:

Get in touch:

Usage

Command Line Options

See semgrep --help for command line options.

Exit Codes

semgrep may exit with the following exit codes:

  • 0: Semgrep ran successfully and found no errors
  • 1: Semgrep ran successfully and found issues in your code
  • >=2: Semgrep failed to run

Upgrading

To upgrade, run the command below associated with how you installed Semgrep:

# Using HomeBrew
$ brew upgrade semgrep

# Using `pip`
$ python3 -m pip install --upgrade semgrep

# Using Docker
$ docker pull returntocorp/semgrep:latest

Contributing

Semgrep is LGPL-licensed, feel free to help out: CONTRIBUTING.

Semgrep is a frontend to a larger program analysis library named pfff. pfff began and was open-sourced at Facebook but is now archived. The primary maintainer now works at r2c. Semgrep was originally named sgrep and was renamed to avoid collisons with existing projects.

Commercial Support

Semgrep is supported by r2c. We're hiring!

Interested in a fully-supported, hosted version of Semgrep? Drop your email and we'll be in touch!

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

semgrep-0.19.1.tar.gz (62.4 kB view details)

Uploaded Source

Built Distributions

semgrep-0.19.1-cp36.cp37.cp38.py36.py37.py38-none-manylinux1_x86_64.whl (28.9 MB view details)

Uploaded CPython 3.6 CPython 3.7 CPython 3.8 Python 3.6 Python 3.7 Python 3.8

semgrep-0.19.1-cp36.cp37.cp38.py36.py37.py38-none-macosx_10_14_x86_64.whl (27.4 MB view details)

Uploaded CPython 3.6 CPython 3.7 CPython 3.8 Python 3.6 Python 3.7 Python 3.8 macOS 10.14+ x86-64

File details

Details for the file semgrep-0.19.1.tar.gz.

File metadata

  • Download URL: semgrep-0.19.1.tar.gz
  • Upload date:
  • Size: 62.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

File hashes

Hashes for semgrep-0.19.1.tar.gz
Algorithm Hash digest
SHA256 8d201769d4bf7787cbf7941d3f0f258055ecfe22bf4cc9869cbba9b29f2b5a95
MD5 78c618c57b0a8a19ff9a06d4a30183d0
BLAKE2b-256 dfe60e6886bd9e8fb0de0d5ca1bb5dd33930f66eb4f5e870a99215a73c557a86

See more details on using hashes here.

File details

Details for the file semgrep-0.19.1-cp36.cp37.cp38.py36.py37.py38-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: semgrep-0.19.1-cp36.cp37.cp38.py36.py37.py38-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 28.9 MB
  • Tags: CPython 3.6, CPython 3.7, CPython 3.8, Python 3.6, Python 3.7, Python 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

File hashes

Hashes for semgrep-0.19.1-cp36.cp37.cp38.py36.py37.py38-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1b6fef26efbc3696dac45ab1f30d8ab5398a610b0065db9519e5b85ab78f0faa
MD5 6d75a9c78cefb2b2f90ecc90d05d8f1f
BLAKE2b-256 decab93f9a43b31744b4b99c1ae16908d700b117a50ad2d91f69134ab883598b

See more details on using hashes here.

File details

Details for the file semgrep-0.19.1-cp36.cp37.cp38.py36.py37.py38-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: semgrep-0.19.1-cp36.cp37.cp38.py36.py37.py38-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.4 MB
  • Tags: CPython 3.6, CPython 3.7, CPython 3.8, Python 3.6, Python 3.7, Python 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

File hashes

Hashes for semgrep-0.19.1-cp36.cp37.cp38.py36.py37.py38-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d3d12d3e93eeb95839ba519e5780ee8a35bacd98e5dcfa5df741ae32f898f16f
MD5 f7626c4ba81f1b6ee147efdf31bf263b
BLAKE2b-256 22354be46d05ab0b1fe3f01d8618bc044fb489a965979f3e3f3764079dfddd19

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page