reference Commands

opengrep scan

See also
opengrep test, --config, opengrep ci, opengrep lsp, opengrep validate
opengrep scan [FLAGS] [TARGETS...]

Runs rules on the files under TARGETS and reports the findings. TARGETS are files or directories; the default is the current directory. scan is the default command: opengrep --config rule.yaml . is equivalent to opengrep scan --config rule.yaml ..

The rules come from --config or OPENGREP_RULES, or from a single pattern given with -e/--pattern and -l/--lang. Without any of them, opengrep scan uses --config auto, which downloads rules from the Semgrep registry.

In a directory target, Opengrep skips the files that .gitignore and .semgrepignore exclude. When the project has no .semgrepignore, a default list applies, which includes node_modules/, vendor/, test/ and tests/. Files named on the command line are always scanned. See --exclude.

The findings go to standard output, in the text format unless another format is selected, such as --json or --sarif. The progress and the summary go to standard error. When standard output is a terminal, the scan starts with a banner that shows the Opengrep logo and version; --quiet leaves out everything on standard error.

Exit status

StatusMeaning
0The scan completed. Findings do not change the status unless --error is given.
1--error was given and there are findings.
2A fatal error, including an invalid command line or an invalid value of an environment variable.
3A target file could not be parsed.
4A rule pattern could not be parsed.
5A rule file is not valid YAML.
7No valid configuration could be loaded, for example because of an invalid rule.
8A rule names an unsupported language.
141The program that reads the output closed the pipe.

Flags

FlagSummary
--allow-rule-timeout-controlLet rules set their own time limits, which are otherwise ignored.
--autofixApply the fixes of the rules to the scanned files.
--baseline-commitReport only the findings that are not present in the given commit.
--configLoad rules from a file, a directory, a URL, a git repository or the registry.
--dataflow-tracesShow the path from source to sink in the findings of taint rules.
--debugLog the messages of --verbose and the diagnostics of the engine.
--developListed in Internal and debugging interfaces.
--disable-intrafileAnalyze every taint rule one function at a time, including rules that set taint_intrafile or taint_interfile.
--dryrunShow the fixes --autofix would make, without writing them to the files.
--dump-astListed in Internal and debugging interfaces.
--dynamic-timeoutScale the time limit of each rule with the size of the file.
--dynamic-timeout-max-multiplierThe largest factor by which --dynamic-timeout multiplies the timeout.
--dynamic-timeout-unit-kbThe file size, in KB, of one step of --dynamic-timeout.
--emacsPrint one line per finding, in the format of Emacs compilation buffers.
--emacs-outputWrite the findings in the Emacs format to a file as well.
--enable-interfileRun cross-file taint analysis for the rules that set the taint_interfile option.
--enable-nosemWhether nosem comments suppress findings; on by default.
--errorExit with status 1 when there are findings.
--excludeSkip files and directories whose path matches a gitignore-style pattern.
--exclude-minified-filesSkip files that look minified.
--exclude-ruleSkip the rule with this id.
--files-with-matchesPrint only the paths of the files that have findings.
--force-colorStyle the output even when it does not go to a terminal.
--force-excludeApply --include and --exclude to files named on the command line as well.
--gitlab-sastPrint the findings as a GitLab SAST report.
--gitlab-sast-outputWrite the findings as a GitLab SAST report to a file as well.
--gitlab-secretsPrint the findings as a GitLab secret detection report.
--gitlab-secrets-outputWrite the findings as a GitLab secret detection report to a file as well.
--guarded-taint-signaturesDrop a cross-function taint finding when the condition leading to the sink cannot hold for the call.
--includeScan only the files whose path matches one of these patterns.
--incremental-outputPrint each finding as soon as it is found.
--incremental-output-postprocess
--inline-metavariablesReplace the metavariables in the metadata of a rule with the code they matched.
--interfile-dedup-byWhether a sink reached by several sources across files produces one finding or one per source.
--interfile-depthThe longest call chain the cross-file taint analysis follows.
--interfile-timeoutThe time a rule may spend on the cross-file taint analysis.
--jobsThe number of workers that run rules in parallel.
--jsonPrint the findings as a JSON document.
--json-outputWrite the findings as JSON to a file as well.
--junit-xmlPrint the findings as a JUnit XML report, one failed test per finding.
--junit-xml-outputWrite the findings as a JUnit XML report to a file as well.
--langThe language of a pattern given on the command line.
--matching-explanationsAdd to the JSON output a record of how each part of a rule matched.
--max-chars-per-lineThe width at which lines of code are wrapped in the text output.
--max-lines-per-findingThe number of lines of code shown for a finding in the text output.
--max-log-list-entriesThe number of items above which a list in the logs is replaced by a note.
--max-match-per-fileThe number of matches in a file, across all rules, above which all its findings are dropped.
--max-memoryThe memory that the analysis of one file may use before it is abandoned.
--max-target-bytesSkip files larger than this size.
--no-progress-barDo not draw the status line that shows the progress of a scan.
--opengrep-ignore-patternAn additional comment prefix that suppresses the findings on a line.
--optimizationsTurn the optimizations of the engine, such as the prefilter, on or off.
--outputWrite the output to a file instead of standard output.
--output-enclosing-contextRecord the function or class that contains each finding.
--patternSearch with a pattern given on the command line instead of a rule file.
--profileListed in Internal and debugging interfaces.
--project-rootThe project root, where .gitignore and .semgrepignore are read.
--quietPrint only the findings, and nothing on standard error.
--replacementThe fix of a pattern given on the command line.
--rewrite-rule-idsWhether the ids of rules from a subdirectory are prefixed with its path; on by default.
--sarifPrint the findings as a SARIF 2.1.0 document.
--sarif-outputWrite the findings as SARIF to a file as well, including suppressed findings.
--scan-unknown-extensionsScan a file named on the command line even when its extension belongs to no language.
--semgrepignore-filenameRead the ignored paths from a file with a name other than .semgrepignore.
--severityReport only the findings of rules with these severities.
--show-supported-languagesPrint the supported languages and exit.
--skinThe layout of the text output.
--skip-invalid-configsSkip invalid rule files in a directory or a git repository instead of stopping.
--strictFail the run when a file cannot be parsed or another warning-level error occurs.
--taint-intrafileFollow taint through calls to functions in the same file, for every taint rule.
--testRun the rule tests instead of scanning, as opengrep test does.
--textPrint the text output, the default format.
--text-outputWrite the text output to a file as well.
--timeReport the time the scan took, per rule and per file.
--timeoutThe time limit of a rule on a file, in seconds; 0 sets no limit.
--timeout-thresholdThe number of rules that may time out on a file before the file is skipped.
--use-git-ignoreWhether files ignored by git are skipped; on by default.
--validateCheck the rules instead of scanning, as opengrep validate does.
--verboseLog the progress of the run, at the info level.
--versionPrint the Opengrep version and exit.
--vimPrint one line per finding, in the format of Vim quickfix lists.
--vim-outputWrite the findings in the Vim quickfix format to a file as well.
--x-ignore-semgrepignore-filesListed in Internal and debugging interfaces.
--x-lsListed in Internal and debugging interfaces.
--x-ls-longListed in Internal and debugging interfaces.

Examples

Scan a file

rule.yaml
rules:  - id: find-eval    pattern: eval(...)    message: found eval    languages: [python]    severity: WARNING
app.py
def run(expr):    return eval(expr)
Command and result
$ opengrep scan -q --config rule.yaml app.py
app.py

  warn  find-eval
  found eval

     2: return eval(expr)

Fail when there are findings

rule.yaml
rules:  - id: find-eval    pattern: eval(...)    message: found eval    languages: [python]    severity: WARNING
app.py
eval(input())
Command and result
$ opengrep scan --config rule.yaml app.py > /dev/null 2>&1; echo "exit status: $?"
exit status: 0
$ opengrep scan --config rule.yaml --error app.py > /dev/null 2>&1; echo "exit status: $?"
exit status: 1