Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
A rule is a code pattern with metavariables ($X) plus a message and severity. Rule sets (registry configs like p/owasp-top-ten, p/python, p/secrets) bundle many rules. Semgrep parses code into a syntax tree, so it understands code structure — not just text like grep.
| Item | Value |
|---|---|
| Install | pipx install semgrep (Python package) or the semgrep/semgrep container |
| Main command | semgrep scan (local), semgrep ci (CI, with or without the Semgrep platform) |
| Rules | registry (p/…), local YAML files, or a folder |
| Ignore | .semgrepignore (paths), # nosemgrep: rule-id (one line) |
| Outputs | text, --json, --sarif, --junit-xml |
Notitie
The open-source engine is Semgrep Community Edition; the company also sells a platform with more rules and cross-file analysis. Opengrep is a community fork of the engine. Everything here works with the free engine and public rules.
Install on RHEL and Ubuntu
dnf -y install python3 pipx
apt-get install -y pipx
pipx ensurepath
pipx install semgrep
semgrep --version(Use the first line on RHEL, the second on Ubuntu.) Or without installing: docker run --rm -v "$PWD:/src" semgrep/semgrep semgrep scan --config p/default /src.
First configuration: choose rule sets and scan
cd shop-api
semgrep scan --config p/default --metrics=off
semgrep scan --config p/owasp-top-ten --config p/python --config p/secrets --metrics=off
semgrep scan --config p/default --severity ERROR --error --metrics=off
semgrep scan --config p/default --sarif --output semgrep.sarif --metrics=off| Option | Why |
|---|---|
--config p/… | Choose rule sets that fit your languages; more is not always better. |
--metrics=off | Do not send usage data. |
--severity ERROR | Start with the important findings only. |
--error | Exit code 1 when there are findings — fails the CI job. |
--sarif | A report for GitHub/GitLab security dashboards. |
Skip folders you do not own:
tests/fixtures/
vendor/
node_modules/
*.min.jsYour own rule
Example: in this company, subprocess with shell=True is not allowed:
rules:
- id: no-subprocess-shell-true
languages: [python]
severity: ERROR
message: >-
subprocess with shell=True can run injected commands.
Pass a list of arguments and keep shell=False.
patterns:
- pattern: subprocess.$FUNC(..., shell=True, ...)
- metavariable-regex:
metavariable: $FUNC
regex: ^(run|call|check_call|check_output|Popen)$
metadata:
cwe: "CWE-78: OS Command Injection"Test it, then use it together with the registry rules:
semgrep scan --config rules/ --metrics=off
semgrep scan --test rules/
semgrep scan --config p/python --config rules/ --metrics=offA reviewed false positive is marked in the code, with the reason in a comment:
# Command is a fixed string from our config, never user input (SEC-210).
subprocess.run(FIXED_CMD, shell=True) # nosemgrep: no-subprocess-shell-trueRun it in CI: only new findings
On merge requests, show only findings in the changed code — old debt does not block new work:
semgrep:
stage: test
image: semgrep/semgrep
variables:
SEMGREP_RULES: "p/default p/secrets rules/"
script:
- semgrep scan --config p/default --config p/secrets --config rules/ --baseline-commit "$CI_MERGE_REQUEST_DIFF_BASE_SHA" --error --sarif --output semgrep.sarif --metrics=off
artifacts:
when: always
paths: [semgrep.sarif]
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"(GitHub Actions: the same command in a job with fetch-depth: 0 and --baseline-commit ${{ github.event.pull_request.base.sha }}.)
Daily use: the commands and the real uses
semgrep scan --config p/default --metrics=off
semgrep scan --config p/default --include '*.py' --exclude 'tests/' --metrics=off
semgrep scan --config p/default --json | jq '.results | length'
semgrep scan -e 'requests.get(..., verify=False)' --lang python .
semgrep scan --config rules/ --autofix --dryrun --metrics=off-e PATTERN searches once without a rule file — great to find all uses of a dangerous function.
Real uses at work:
- A SAST gate on merge requests (new findings only).
- Company guardrails as rules ("use our safe SQL helper", "no
verify=False"). - Fast hunts across all repositories when a new bug class appears.
Make it safe
Security checklist:
- Start with a few strong rule sets and
ERRORseverity; grow step by step. - Diff-aware scans in merge requests (
--baseline-commit); a full scan nightly. - Every
nosemgrephas a reason in the code and is reviewed. - Pin the Semgrep version (pipx/container tag) in CI.
- Combine with secrets scanning (Gitleaks), dependency scanning (Trivy/Grype) and DAST (ZAP).
Update and remove
pipx upgrade semgrep
pipx uninstall semgrepFix common problems
| Problem | Cause | Fix |
|---|---|---|
| Too many findings | Too many rule sets or all severities. | Fewer rule sets, --severity ERROR, diff-aware scans. |
Syntax error / partial parsing warnings | Unusual syntax or generated files. | Exclude generated code; usually safe to ignore for those files. |
| CI scan finds old problems in a merge request | No baseline. | --baseline-commit with the merge base and full Git history. |
| Very slow on a monorepo | Scanning vendor, builds, node_modules. | .semgrepignore; --include per language; --jobs. |
| A custom rule never matches | Pattern too specific (keyword args, imports). | Test with semgrep scan --test; use ... and metavariables. |
Next steps
- Quick how-to: Find bugs in code with Semgrep.
- Command page: semgrep.
- At work: Semgrep — static code analysis (SAST), All scanners in one pipeline.
- In GitHub Actions: Scan code and dependencies. The other gates: SonarQube, Gitleaks, Trivy.
1How do you make Semgrep fail a merge request only for new findings?
Use --baseline-commit with the merge base and --error.
2How do you mark one reviewed false positive?
A # nosemgrep: rule-id comment on that line, with the reason in a comment.