patterns
A list of conditions; the rule matches the code that satisfies all of them. The items can be:
- positive patterns:
pattern,pattern-regex,pattern-either, or a nestedpatterns; - negative patterns:
pattern-not,pattern-not-regex; - context:
pattern-insideandpattern-not-inside; - filters on metavariables:
metavariable-regex,metavariable-pattern,metavariable-comparison,metavariable-type,metavariable-analysis, andfocus-metavariable.
Code matches when it:
- matches every positive pattern;
- lies inside code matched by each
pattern-insideand outside code matched by anypattern-not-inside; - matches no negative pattern;
- passes every filter.
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: WARNINGeval-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: WARNINGapp.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