Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- Run in the project folder (quick answer) — Semgrep downloads the rule set and scans the files Git tracks.
- Read a finding:
app/db.py
python.lang.security.audit.formatted-sql-query
Detected possible formatted SQL query. Use parameterized queries instead.
14┆ cur.execute(f"SELECT * FROM orders WHERE id = {order_id}")The fix here: cur.execute("SELECT * FROM orders WHERE id = %s", (order_id,)).
- Choose rule sets for your stack:
| Config | For |
|---|---|
p/default | A good general start. |
p/owasp-top-ten | Web security classics. |
p/python, p/javascript, p/golang | Language-specific. |
p/secrets | Hard-coded keys and passwords. |
p/terraform, p/dockerfile | Infrastructure code. |
- Write your own rule — for a company rule, for example "never disable TLS checks":
rules:
- id: requests-verify-false
languages: [python]
severity: ERROR
message: TLS certificate checks are disabled. Use a CA bundle instead.
pattern: requests.$METHOD(..., verify=False, ...)semgrep scan --config .semgrep/ --metrics=off- False positives — mark one line with a reason, or exclude folders:
result = eval(expr) # nosemgrep: python.lang.security.audit.eval-detected -- input is a fixed constanttests/
vendor/- In CI — scan only what changed on pull requests, block on
ERROR:
semgrep scan --config p/default --config .semgrep/ --baseline-commit origin/main --severity ERROR --error --metrics=off--baseline-commit = report only new findings (old ones do not block every PR).
Check that it worked
The scan runs in seconds to minutes, the findings point to real lines, your own rule finds a test example, and a pull request with a new ERROR finding fails the check.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Too many findings to handle | All severities on an old codebase. | Start with --severity ERROR and --baseline-commit; fix old ones over time. |
semgrep: command not found after pipx | ~/.local/bin not in PATH. | pipx ensurepath, open a new shell. |
| Files are not scanned | They are not tracked by Git, or in .semgrepignore. | git add them; check the ignore file. |
| A rule finds nothing it should | Pattern too strict. | Test the rule with a small example; use ... and metavariables. |
| CI is slow | Full scan of a big repo on every PR. | --baseline-commit, fewer rule sets, cache. |
| Findings in generated or vendored code | Not your code. | Exclude in .semgrepignore. |
Go further
- Command pages: semgrep, git.
- Next how-tos: Find secrets with Gitleaks, Scan a container image for vulnerabilities, Create a CI pipeline in GitHub Actions, Check code quality with SonarQube.
- Full guide: Semgrep. At work: Semgrep, All scanners in one pipeline. Learn it: GitHub Actions — SAST.
1What does --baseline-commit origin/main do?
It reports only findings that are new compared with main, so old findings do not block every PR.
2How do you mark one false positive without turning off the rule everywhere?
A # nosemgrep: RULE-ID comment on that line, with a reason.