متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 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 الدروس
  • 2 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع 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.

الأوامر في هذا الدرس

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.

كل الأوامر في هذا الفصل