Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 122Security · Code

How to find bugs in code with Semgrep

SQL injection, command injection, unsafe deserialization, hard-coded secrets, verify=False — many security bugs have a typical code pattern. Semgrep finds these patterns in Python, JavaScript, Go, Java, Terraform and more, fast enough to run on every pull request. This how-to scans a project with ready rule sets, reads a finding, writes one own rule, and puts it into CI so new problems block the merge.

  • 5 Lessen
  • 2 Commando's

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

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

122.1

The quick answer

pipx install semgrep
semgrep scan --config p/default --metrics=off
semgrep scan --config p/owasp-top-ten --config p/python --severity ERROR --error --metrics=off

--error = exit code 1 when there are findings (for CI). --metrics=off = send no usage data.

122.2

Step by step

  1. Run in the project folder (quick answer) — Semgrep downloads the rule set and scans the files Git tracks.
  2. 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,)).

  1. Choose rule sets for your stack:
ConfigFor
p/defaultA good general start.
p/owasp-top-tenWeb security classics.
p/python, p/javascript, p/golangLanguage-specific.
p/secretsHard-coded keys and passwords.
p/terraform, p/dockerfileInfrastructure code.
  1. Write your own rule — for a company rule, for example "never disable TLS checks":
.semgrep/no-verify-false.yml
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
  1. False positives — mark one line with a reason, or exclude folders:
example line in code
result = eval(expr)  # nosemgrep: python.lang.security.audit.eval-detected -- input is a fixed constant
.semgrepignore
tests/
vendor/
  1. 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).

122.3

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.

122.4

Common problems

ProblemCauseFix
Too many findings to handleAll 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 scannedThey are not tracked by Git, or in .semgrepignore.git add them; check the ignore file.
A rule finds nothing it shouldPattern too strict.Test the rule with a small example; use ... and metavariables.
CI is slowFull scan of a big repo on every PR.--baseline-commit, fewer rule sets, cache.
Findings in generated or vendored codeNot your code.Exclude in .semgrepignore.

Commando's in deze les

122.5

Go further

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.

Alle commando's in dit hoofdstuk