Scanning
semgrep scan
Scan the current directory
Needs a --config, or a local semgrep config file is not picked
up automatically.
semgrep scan --config auto
Registry rules chosen for your code
Requires login-free network access; sends project metadata.
semgrep scan --config <file|dir>
Run local rule files
semgrep scan --config p/<ruleset>
Run a registry ruleset
For example p/security-audit or p/python.
semgrep scan <path>
Scan only a file or folder
semgrep scan --severity <level>
Filter by INFO, WARNING, ERROR
Repeat the flag for several levels.
semgrep scan --exclude-rule <id>
Skip a rule
semgrep scan --include <glob>
Only paths matching glob
semgrep scan --exclude <glob>
Skip paths matching glob
semgrep scan -j <n>
Number of parallel jobs
semgrep scan --timeout <sec>
Per-rule per-file time limit
semgrep scan --max-target-bytes <n>
Raise the file size limit
Files over 1 MB are skipped by default.
semgrep scan --metrics off
Disable metrics
Ad-hoc patterns
semgrep -e '<pattern>' -l <lang>
<path>
Search with a pattern
semgrep -e 'eval(...)' -l python .
Find any eval call
... matches any arguments.
semgrep -e '$X == $X' -l <lang> .
Metavariable, same value twice
$X binds to any expression; repeating it requires equality.
semgrep -e '<pat>' --replacement
'<new>' -l <lang> --autofix
Rewrite matches
Add --dryrun to preview the change.
Pattern syntax
| Syntax |
Matches |
... |
any arguments or statements |
$X |
any expression (metavariable) |
<... p ...> |
p nested anywhere inside |
CI & baseline
semgrep ci
Scan in CI, fail on blocking findings
Works with a Semgrep account; reports to the platform.
semgrep ci --baseline-commit <ref>
Only findings new since ref
semgrep scan --baseline-commit <ref>
Same for plain scan
Compares against the given commit; needs a git repo.
semgrep scan --error
Exit 1 when findings exist
semgrep ci --subdir <dir>
Scan only a subdirectory
semgrep login
Authenticate with semgrep.dev
semgrep ci --code
Run only code (SAST) scans
semgrep scan --strict
Fail on invalid target code
Ignoring
# nosemgrep
Ignore this line or the next
Put it as a comment on the matching line or the line above.
# nosemgrep: <rule-id>
Ignore one rule only
Use the full rule id with namespace; separate several with
commas.
.semgrepignore
Paths to skip, gitignore syntax
Replaces the default ignore list, which skips tests/, vendor/,
node_modules/ and more.
:include .gitignore
Reuse gitignore in .semgrepignore
semgrep scan --no-git-ignore
Also scan gitignored files
semgrep scan --disable-nosem
Report nosemgrep lines anyway