reference

Rule syntax

A rule file is a YAML document with a top-level rules list. Each item of the list is a rule:

rules:
  - id: find-eval
    pattern: eval(...)
    message: found eval
    languages: [python]
    severity: WARNING

Required keys

KeyValue
idThe identifier of the rule.
languagesThe languages of the files the rule runs on.
messageThe text reported with each finding.
severityThe severity of the findings.
pattern keysIn the default search mode, one of pattern, patterns, pattern-either and pattern-regex. In taint mode, pattern-sources and pattern-sinks.

Patterns are written in the pattern syntax.

Unknown keys

Opengrep ignores an unknown key at the top level of a rule, logs a warning such as Skipping unknown field 'nosuchkey' in rule "find-eval", and runs the rule. An unknown key where the set of keys is fixed, such as under paths or options, makes the rule invalid.

Index

NameSummary
by-side-effectWhether a taint source, sanitizer or propagator also changes the variable it matches for the code that follows.
controlMake a taint source taint the flow of control, so that any sink reached after it is reported.
exactWhether a taint source, sanitizer or sink is only the code it matches or also the code inside it.
fixThe code that replaces each match, applied by --autofix.
fix-regexFix each match by replacing what a regular expression matches inside it.
focus-metavariableReport only the code a metavariable matched, not the whole match.
idThe identifier of a rule, reported with each of its findings.
labelDistinguish kinds of taint in a taint rule, and require some of them at a sink.
languagesThe languages of the files a rule runs on, and the language its patterns are written in.
messageThe text reported with each finding; metavariables in it are replaced by the code they matched.
metadataData attached to a rule and copied into its findings, such as CWE and OWASP references.
metavariable-analysisKeep the matches where a metavariable passes an analysis: entropy for secrets, redos for regular expressions.
metavariable-comparisonKeep the matches where an expression over metavariables is true.
metavariable-patternKeep the matches where the code a metavariable matched also matches a further pattern.
metavariable-regexKeep the matches where the code bound to a metavariable matches a regular expression.
metavariable-typeKeep the matches where the expression of a metavariable has a given type.
min-versionSkip a rule written for a newer or an older rule format, by Semgrep version number.
modeThe matching mode of a rule: search, the default, or taint.
mode: taintFind data that flows from sources to sinks without passing through a sanitizer.
optionsSettings that change how a rule matches, for that rule only.
pathsLimit a rule to files whose paths match, or do not match, glob patterns.
patternMatch code against a pattern written in the language of the rule.
pattern-eitherMatch code that matches any one of a list of patterns.
pattern-insideKeep only the matches that lie inside code matching this pattern.
pattern-notDrop the matches that also match this pattern.
pattern-not-insideDrop the matches that lie inside code matching this pattern.
pattern-not-regexDrop the matches that contain a match of this regular expression.
pattern-propagatorsIn a taint rule, operations that pass taint from one value to another.
pattern-regexMatch the text of the file with a PCRE regular expression.
pattern-sanitizersIn a taint rule, the code that makes untrusted data safe.
pattern-sinksIn a taint rule, the code that must not receive untrusted data.
pattern-sourcesIn a taint rule, the code that produces untrusted data.
patternsMatch code that satisfies every condition of a list.
rulesThe top-level list of a rule file, one entry per rule.
severityThe severity of the findings: ERROR, WARNING or INFO, or CRITICAL, HIGH, MEDIUM or LOW.

Examples

An unknown key produces a warning

rule.yaml
rules:  - id: find-eval    pattern: eval(...)    message: found eval    languages: [python]    severity: WARNING    nosuchkey: whatever
app.py
eval(1)
Command and result
$ opengrep scan --config rule.yaml app.py 2>&1 | grep Skipping
warning  Skipping unknown field 'nosuchkey' in rule "find-eval"
$ opengrep scan --config rule.yaml app.py > /dev/null 2>&1; echo "exit status: $?"
exit status: 0