reference Rule syntax

patterns

See also
pattern, pattern-either, pattern-inside, pattern-not, pattern-not-inside, pattern-regex, focus-metavariable, metavariable-regex, --matching-explanations, --pattern, metavariable-analysis, metavariable-comparison, metavariable-pattern, metavariable-type

A list of conditions; the rule matches the code that satisfies all of them. The items can be:

Code matches when it:

The order of the items does not matter. A metavariable bound in one item must match the same code in the others.

A patterns list made only of negative patterns and filters is an invalid rule.

Examples

Inside a function, and not a string literal

eval-in-function.yaml
rules:  - id: eval-in-function    patterns:      - pattern-inside: |          def $F(...):              ...      - pattern: eval($X)      - pattern-not: eval("...")    message: eval of a non-literal inside a function    languages: [python]    severity: WARNING
eval-in-function.py
# ok: eval-in-functioneval(user_input)def handler(expr):    # ruleid: eval-in-function    eval(expr)    # ok: eval-in-function    eval("1 + 1")
Command and result
$ opengrep test .
1/1: ✓ All tests passed
No tests for fixes found.

Only negative patterns

rule.yaml
rules:  - id: only-negative    patterns:      - pattern-not: eval("...")    message: a rule that cannot work    languages: [python]    severity: WARNING
app.py
eval(x)
Command and result
$ opengrep scan --config rule.yaml app.py 2>&1 | grep positive
         you need at least one positive term (not just negations or conditions)
$ opengrep scan --config rule.yaml app.py > /dev/null 2>&1; echo "exit status: $?"
exit status: 7