Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 66Semgrep · Security

Install and set up Semgrep (static code analysis)

Semgrep is a fast SAST tool: it reads source code in 30+ languages and finds security bugs (SQL injection, command injection, weak crypto, unsafe deserialization), risky patterns and project-specific mistakes. Its rules look like the code they search for, so writing your own rules is easy. This guide installs Semgrep on RHEL and Ubuntu, chooses rule sets, runs it in CI so only new problems fail a merge request, writes a custom rule, and handles false positives.

  • 10 Lessen
  • 5 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

66.1

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.

ItemValue
Installpipx install semgrep (Python package) or the semgrep/semgrep container
Main commandsemgrep scan (local), semgrep ci (CI, with or without the Semgrep platform)
Rulesregistry (p/…), local YAML files, or a folder
Ignore.semgrepignore (paths), # nosemgrep: rule-id (one line)
Outputstext, --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.

66.2

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.

66.3

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
OptionWhy
--config p/…Choose rule sets that fit your languages; more is not always better.
--metrics=offDo not send usage data.
--severity ERRORStart with the important findings only.
--errorExit code 1 when there are findings — fails the CI job.
--sarifA report for GitHub/GitLab security dashboards.

Skip folders you do not own:

.semgrepignore
tests/fixtures/
vendor/
node_modules/
*.min.js

66.4

Your own rule

Example: in this company, subprocess with shell=True is not allowed:

rules/no-shell-true.yaml
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=off

A reviewed false positive is marked in the code, with the reason in a comment:

app/tasks.py
# Command is a fixed string from our config, never user input (SEC-210).
subprocess.run(FIXED_CMD, shell=True)  # nosemgrep: no-subprocess-shell-true

66.5

Run it in CI: only new findings

On merge requests, show only findings in the changed code — old debt does not block new work:

.gitlab-ci.yml
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 }}.)

66.6

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.

66.7

Make it safe

Security checklist:

  • Start with a few strong rule sets and ERROR severity; grow step by step.
  • Diff-aware scans in merge requests (--baseline-commit); a full scan nightly.
  • Every nosemgrep has 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).

66.8

Update and remove

pipx upgrade semgrep
pipx uninstall semgrep

66.9

Fix common problems

ProblemCauseFix
Too many findingsToo many rule sets or all severities.Fewer rule sets, --severity ERROR, diff-aware scans.
Syntax error / partial parsing warningsUnusual syntax or generated files.Exclude generated code; usually safe to ignore for those files.
CI scan finds old problems in a merge requestNo baseline.--baseline-commit with the merge base and full Git history.
Very slow on a monorepoScanning vendor, builds, node_modules..semgrepignore; --include per language; --jobs.
A custom rule never matchesPattern too specific (keyword args, imports).Test with semgrep scan --test; use ... and metavariables.

66.10

Next steps

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.

Alle commando's in dit hoofdstuk