Explore our new Cybersecurity Terms Glossary, explained by our experts.

View the Glossary View the Glossary
PRIVIA

Cybersecurity Terms Glossary

What Is Semgrep?

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.

Illustration for the cybersecurity term Semgrep
Home / Blog / Cybersecurity Terms / What Is Semgrep?
17 November 2026

What Is Semgrep?

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.

Writing a Rule

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.

Taint Mode

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.

Where It Earns Its Keep

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.

Fitting It Into a Pipeline

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.

FAQ – Frequently Asked Questions

Is Semgrep Free?

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.

How Does Semgrep Compare With CodeQL?

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.

What Are Metavariables and Ellipses?

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.

Can Semgrep Fix Issues Automatically?

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.

Related Terms

You May Be Interested In These

What Is a Honeynet?

Cybersecurity Terms

17 November 2026

What Is a Honeynet?

Read More
What Is Frida?

Cybersecurity Terms

17 November 2026

What Is Frida?

Read More