Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
Semgrep is a static analysis tool whose rules look like the code they match. It sits between grep and a full semantic engine, and is unusually good at enforcing organisation-specific patterns.

What Is Semgrep?
Semgrep is a static analysis tool built around one idea: a rule should look like the code it is trying to find. Instead of learning a query language, you write a snippet of the pattern you care about, with wildcards where the details do not matter, and Semgrep matches it against a parsed syntax tree rather than against raw text. It handles formatting, whitespace, and the order of arguments without being told.
The engine is open source under the LGPL 2.1 licence and supports more than thirty languages. The company behind it, formerly r2c, sells a hosted product with additional analysis.
Rules are YAML containing patterns:
rules:
- id: subprocess-shell-true
languages: [python]
severity: ERROR
message: >-
subprocess called with shell=True. If any argument is attacker
controlled this is command injection. Pass a list instead.
metadata:
cwe: "CWE-78: OS Command Injection"
patterns:
- pattern: subprocess.$FUNC(..., shell=True, ...)
- pattern-not: subprocess.$FUNC("...", ..., shell=True, ...)
The ellipsis matches any sequence of arguments. $FUNC is a metavariable that binds to whatever name appears and can be referenced again. pattern-not removes the safe case — here, a hardcoded string literal. Further operators cover alternatives, enclosing context, and conditions on metavariable contents.
Most engineers write a working rule in their first half hour. That is the whole point: a static analysis tool whose rules are written by the developers who know the codebase, rather than by whoever owns the security tool.
Pattern matching alone cannot follow a value through a program, so Semgrep adds a dataflow mode:
mode: taint
pattern-sources:
- pattern: flask.request.$ANYTHING
pattern-sanitizers:
- pattern: shlex.quote(...)
pattern-sinks:
- pattern: os.system(...)
This tracks untrusted values from source to sink within a function and, in the open-source engine, within a file. Cross-file and cross-function analysis is a feature of the commercial Semgrep Pro engine. That boundary is the main technical difference from CodeQL, which performs deep interprocedural analysis across an entire compiled project but takes far longer to run and requires a licence for private code.
The registry of community rules is useful and covers the usual categories — injection, insecure deserialisation, weak cryptography, hardcoded credentials, framework-specific mistakes. Running semgrep --config=p/owasp-top-ten gives reasonable coverage in a single command.
But the real value is in rules nobody else can write for you:
A dozen rules of that kind will catch more real problems in your codebase than a thousand generic ones, and they prevent a class of defect rather than reporting instances of it.
Speed is the operational argument. Semgrep scans a large repository in seconds to a couple of minutes, which makes it practical on every pull request where a heavier engine is not. The --baseline-commit option restricts findings to code changed since a given commit, so an existing repository can adopt the tool without failing the build on inherited debt.
Rules can carry an fix field that supplies an automatic correction, applied with --autofix. This works well for mechanical substitutions and badly for anything requiring judgement, so review the diff rather than trusting it.
Output in SARIF renders findings on the pull request diff in GitHub and GitLab. Keep the enabled rule set small and trusted at first: a pull request check that fails on findings nobody intends to act on teaches developers to ignore it, and that habit is difficult to reverse.
The command-line engine and the community rule registry are open source and free, including for commercial use, and many teams never need more. The paid Semgrep offering adds cross-file and cross-function dataflow analysis, a managed findings platform with triage history, supply chain scanning, and secrets detection. Starting with the open-source version and upgrading if interprocedural analysis becomes necessary is a sensible path.
Semgrep is fast, easy to extend, and ideal for enforcing organisation-specific patterns on every pull request, but its free engine analyses within a file. CodeQL builds a semantic database of the whole project and tracks data flow across files and functions, finding deeper issues with traceable paths, at the cost of long build times and a licence for private repositories. They complement each other well.
A metavariable, written with a dollar sign, binds to any expression or identifier at that position and can be referenced again later in the same rule to require that two positions match. An ellipsis matches any sequence of arguments, statements, or elements. Together they let one pattern match many syntactic variations of the same construct without listing them.
Rules can include a fix pattern, and running with autofix applies it. This is reliable for mechanical changes such as swapping a function for its safe equivalent or adding a required argument. It is unreliable where the correct fix depends on context the rule cannot see. Review every automatic change as you would any other code change before merging it.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.