Semgrep¶
What is Semgrep?¶
- Semgrep CE is an open source tool that performs static analysis on source code.
- It uses rules that detect patterns that represent insecure code.
- It can use community rules or custom rules.
- You can execute it locally through CLI commands and Semgrep can be part of the development workflow.
Why use Semgrep?¶
- It provides an accessible way to start using SAST.
- Semgrep can run fast scans during development stages.
- It has community-provided rules.
- You can create your own rules for specific needs.
- Semgrep can be integrated with your IDE, CI/CD and other workflows.
- It helps to find problems early before they reach production.
Getting started¶
Step 1 - Install Semgrep Community Edition.¶
On MacOS, Semgrep CE can be installed using Homebrew:
brew install semgrep
On Linux or Windows environments with Python:
python3 -m pip install semgrep
Step 2 - Run a scan from the command line.¶
To scan the current project using community rules:
semgrep --config=auto
Semgrep can also scan a specific file:
semgrep --config=auto VulnerableExample.java
To use a specific local rule, you can create a semgrep.yml file in the root of your project:
semgrep --config=semgrep.yml VulnerableExample.java
Step 3 - Review the findings.¶
Semgrep reports matches found by the configured rules. Each finding should be reviewed to determine whether it represents a real security issue.
Rules and patterns¶
- A rule defines what Semgrep should look for in the code.
- A rule can contain one or more patterns.
- A pattern represents a code structure or behavior that Semgrep attempts to identify.
- Rules can detect dangerous function calls, insecure patterns, or specific data flows.
- You can use community-contributed rules or create your own.
- When code matches a rule, Semgrep generates a finding.
- A match—or finding—does not automatically mean that an actual vulnerability exists; it must be reviewed.
Example¶
rules:
- id: java-runtime-exec
languages:
- java
message: Potential command execution using Runtime.exec
severity: WARNING
pattern: Runtime.getRuntime().exec($COMMAND)
ididentifies the rule.languageslimits the language analyzed.patterndefines the structure to be searched for.$COMMANDacts as a metavariable and can represent an expression.- If Semgrep finds a match, it generates a
finding.
Taint analysis¶
- Taint analysis tracks the flow of untrusted data through code.
- This flow is typically modeled as: source → propagation → sink.
- A
sourceis the point where untrusted data enters—for example, an HTTP parameter. propagationrepresents how that data moves through variables, methods, or expressions.- A
sinkis a sensitive or dangerous operation, such as executing a command or constructing an insecure query. - Semgrep can use rules in "taint mode" to detect when untrusted data reaches a sink.
- A
sanitizeris something that the rule recognizes as a validation or transformation that makes the data safe for this context. - The result is a
findingthat requires review; it is not an automatically confirmed vulnerability.
Taint analysis example¶
request.getParameter(...)
↓
command
↓
Runtime.getRuntime().exec(...)
Development workflow integration¶
- It can be integrated into events such as pushes or pull requests.
- It can also be executed within CI/CD pipelines.
- Running it early helps detect findings before later stages.
- Teams can define policies to determine which findings require review and which should block the pipeline.
- Not all findings should automatically block the workflow.
- This integration helps provide faster feedback to developers.
Limitations¶
- Semgrep relies on configured rules; if a rule does not cover a specific pattern, it may fail to detect it.
- It may produce false positives.
- It may produce false negatives.
- Complex data flows, frameworks, reflection, generated code, or dynamic behavior can complicate analysis.
- A finding requires human review and context.
- Semgrep CE is not a substitute for manual review, DAST, SCA, or other security practices.