Patterns are the expressions Semgrep uses to match code when it scans for vulnerabilities. This article describes the new syntax for Semgrep pattern operators. See Pattern syntax for information on the existing pattern syntax. There is often a one-to-one translation from the existing syntax to the experimental syntax. These changes are marked with . However, some changes are quite different. These changes are marked withDocumentation Index
Fetch the complete documentation index at: https://docs.semgrep.dev/llms.txt
Use this file to discover all available pages before exploring further.
pattern
The pattern operator looks for code matching its expression in the existing syntax. However, pattern is no longer required when using the experimental syntax. For example, you can use ... wherever pattern: "...``` appears. For example, you can omit pattern` and write the following:
pattern key, but note that this can cause YAML parsing issues.
As an example, the following YAML parses:
: is also used to denote a YAML dictionary:
any
Replaces pattern-either. Matches any of the patterns specified.
all
Replaces patterns. Matches all of the patterns specified.
inside
Replaces pattern-inside. Match any of the sub-patterns inside the primary pattern.
not
Replaces pattern-not. Accepts any pattern and does not match on those patterns.
regex
Replaces pattern-regex. Matches based on the regex provided.
Metavariables
Metavariables are an abstraction to match code when you don’t know the value or contents beforehand. They’re similar to capture groups in regular expressions and can track values across a specific code scope. This includes variables, functions, arguments, classes, object methods, imports, exceptions, and more. Metavariables begin with a$ and can only contain uppercase characters, _, or digits. Names like $x or $some_value are invalid. Examples of valid metavariables include $X, $WIDGET, or $USERS_2.
where
Unlike Semgrep’s existing pattern syntax, the following operators no longer occur under pattern or all:
metavariable-patternmetavariable-regexmetavariable-comparisonmetavariable-analysisfocus-metavariable
where clause.
A where clause is required in a pattern where you’re using metavariable operators. It indicates that Semgrep should match based on the pattern if all the conditions are proper.
As an example, take a look at the following:
where clause is on the same indentation level as all, Semgrep understands that everything under where must be paired with the entire all pattern. As such, the results of the ranges matched by the all pattern are modified by the where pattern, and the output includes some final set of ranges that are matched.
metavariable
Replaces:
This operator looks inside the metavariable for a match.
comparison
Replaces metavariable-comparison. Compares metavariables against a basic Python comparison expression.
focus
Replaces focus-metavariable. Puts focus on the code region matched by a single metavariable or a list of metavariables.
as-metavariable
as-metavariable is only available in the new syntax.
as-metavariable is a rule-writing feature that bridges the gap between metavariables and matches. Metavariables gain access to features like metavariable-comparison, metavariable-regex, and metavariable-pattern, but they cannot be used on arbitrary matches. However, the as operator lets you embed arbitrary matches into metavariables or bind arbitrary matches to a name.
The syntax is as follows:
as appears in the same indentation as the pattern, Semgrep couples the two. This augmented pattern operator matches the enclosed pattern, but produces an environment where $DECORATED_FUNC is bound to the match it corresponds to. So, for instance, the following rule:
Syntax search mode
New syntax search mode rules must be nested underneath a top-levelmatch key. For example:
Taint mode
The new syntax supports taint mode, and such roles no longer requiremode: taint in the rule. Instead, everything must be nested under a top-level taint key.
Taint mode key names
The key names for the new syntax taint rules are as follows:pattern-sources—> sourcespattern-sinks—> sinkspattern-propagators—> propagatorspattern-sanitizers—> sanitizers