Pattern syntax

Info

Getting started with rule writing? Try the Semgrep Tutorial 🎓

This document describes Semgrep’s pattern syntax. You can also see pattern examples by language. In the command line, patterns are specified with the flag --pattern (or -e). Multiple coordinating patterns may be specified in a configuration file. See rule syntax for more information.

Expression matching

Expression matching searches code for a given pattern. For example, the pattern 1 + func(42) can match a full expression or be part of a subexpression:

foo(1 + func(42)) + bar()

String matching

Search string literals within code with Perl Compatible Regular Expressions (PCRE).

The pattern requests.get("=~/dev\./i") matches:

requests.get("api.dev.corp.com")  # Oops, development API left in

To search for specific strings, use the syntax "=~/<regexp>/". Advanced regexp features are available, such as case-insensitive regexps with '/i' (e.g., "=~/foo/i"). Matching occurs anywhere in the string unless the regexp ^ anchor character is used: "=~/^foo.*/" checks if a string begins with foo.

Ellipsis operator

The ellipsis operator (...) abstracts away a sequence of zero or more arguments, statements, or characters.

Function calls

Use the ellipsis operator to search for function calls or function calls with specific arguments. For example, the pattern insecure_function(...) finds calls regardless of its arguments.

insecure_function("MALICIOUS_STRING", arg1, arg2)

Functions and classes must be referenced by their fully qualified name, e.g.,

  • django.utils.safestring.mark_safe(...) not mark_safe(...)
  • System.out.println(...) not println(...)

You can also search for calls with arguments after a match. The pattern func(1, ...) will match both:

func(1, "extra stuff", False)
func(1)  # Matches no arguments as well

Or find calls with arguments before a match with func(..., 1):

func("extra stuff", False, 1)
func(1)  # Matches no arguments as well

The pattern requests.get(..., verify=False, ...) finds calls where an argument appears anywhere:

requests.get(verify=False, url=URL)
requests.get(URL, verify=False, timeout=3)
requests.get(URL, verify=False)

Match the keyword argument value with the pattern $FUNC(..., $KEY=$VALUE, ...).

Method calls

The ellipsis operator can be used to search for method calls on a specific object type. For example, the pattern $OBJECT.extractall(...) matches:

tarball.extractall('/path/to/directory')  # Oops, potential arbitrary file overwrite

Function definitions

The ellipsis operator can be used in function argument lists or in the function body. To find function definitions with mutable default arguments:

pattern: |
  def $FUNC(..., $ARG={}, ...):
      ...
def parse_data(parser, data={}):  # Oops, mutable default arguments
    pass

Info

The YAML | operator allows for multiline strings.

Class definitions

The ellipsis operator can be used in class definitions. To find classes that inherit from a certain parent:

pattern: |
  class $CLASS(InsecureBaseClass):
      ...
class DataRetriever(InsecureBaseClass):
    def __init__(self):
        pass

Info

The YAML | operator allows for multiline strings.

Strings

The ellipsis operator can be used to search for strings containing any data. The pattern crypto.set_secret_key("...") matches:

crypto.set_secret_key("HARDCODED SECRET")

Binary operations

The ellipsis operator can match any number of arguments to binary operations. The pattern $X = 1 + 2 + ... matches:

foo = 1 + 2 + 3 + 4

Containers

The ellipsis operator can match inside container data structures like lists, arrays, and key-value stores.

The pattern pattern: user_list = [..., 10] matches:

user_list = [8, 9, 10]

The pattern pattern: user_dict = {...} matches:

user_dict = {'username': 'password'}

The pattern pattern: user_dict = {..., $KEY: $VALUE, ...} matches the following and allows for further metavariable queries:

user_dict = {'username': 'password'}

Conditionals and loops

The ellipsis operator can be used inside conditionals or loops. The pattern:

pattern: |
  if $CONDITION:
      ...

Info

The YAML | operator allows for multiline strings.

matches:

if can_make_request:
    check_status()
    make_request()
    return

A metavariable can match a conditional or loop body if the body statement information is re-used later. The pattern:

pattern: |
  if $CONDITION:
      $BODY

matches:

if can_make_request:
    single_request_statement()

Note

Half or partial statements can't be matches; both of the examples above must specify the contents of the condition’s body (e.g., $BODY or ...), otherwise they are not valid patterns.

Metavariables

Metavariables are an abstraction to match code when you don’t know the value or contents ahead of time, similar to capture groups in regular expressions.

Metavariables can be used to track values across a specific code scope. This includes variables, functions, arguments, classes, object methods, imports, exceptions, and more.

Metavariables look like $X, $WIDGET, or $USERS_2. They begin with a $ and can only contain uppercase characters, _, or digits. Names like $x or $some_value are invalid.

The pattern $X + $Y matches the following code examples:

foo() + bar()
current + total

Metavariables can also be used to match imports. For example, import $X matches:

import random

Re-using metavariables shows their true power. Detect useless assignments:

pattern: |
  $X = $Y
  $X = $Z

Useless assignment detected:

initial_value = 10  # Oops, useless assignment
initial_value = get_initial_value()

Info

The YAML | operator allows for multiline strings.

Typed Metavariables

Typed metavariables only match a metavariable if it’s declared as a specific type. For example, you may want to specifically check that == is never used for strings.

Go:

pattern: "$X == ($Y : string)"
func main() {
    var x string
    var y string
    var a int
    var b int

    // Matched
    if x == y {
       x = y
    }

    // Not matched
    if a == b {
       a = b
    }
}

Java:

pattern: $X == (String $Y)
public class Example {
    public int foo(String a, int b) {
        // Matched
        if (a == "hello") {
            return 1;
        }

        // Not matched
        if (b == 2) {
            return -1;
        }
    }
}

Warning

Since matching happens within a single file, this is only guaranteed to work for local variables and arguments. Additionally, Semgrep currently understands types on a shallow level. For example, if you have int[] A, it will not recognize A[0] as an integer. If you have a class with fields, you will not be able to use typechecking on field accesses, and it will not recognize the class’s field as the expected type. Literal types are understood to a limited extent. Expanded type support is under active development.

Warning

For Go, Semgrep currently does not recognize the type of all variables that are declared on the same line. That is, the following will not take both a and b as ints: var a, b = 1, 2

Equivalences

Semgrep automatically searches for code that is semantically equivalent.

Imports

Equivalent imports using aliasing or submodules are matched.

The pattern subprocess.Popen(...) matches:

import subprocess.Popen as sub_popen
sub_popen('ls')

The pattern foo.bar.baz.qux(...) matches:

from foo.bar import baz
baz.qux()

Constants

Semgrep performs constant propagation.

The pattern set_password("password") matches:

HARDCODED_PASSWORD = "password"

def update_system():
    set_password(HARDCODED_PASSWORD)

Basic constant propagation support like in the example above is a stable feature. Experimentally, Semgrep also supports intra-procedural flow-sensitive constant propagation.

The pattern set_password("...") also matches:

def update_system():
    if cond():
        password = "abc"
    else:
        password = "123"
    set_password(password)

Deep expression operator

Use the deep expression operator <... [your_pattern] ...> to match an expression that could be deeply nested within another expression. An example is looking for a pattern anywhere within an if statement. The deep expression operator matches your pattern in the current expression context and recursively in any subexpressions.

For example, this pattern:

pattern: |
  if <... $USER.is_admin() ...>:
    ...

matches:

if user.authenticated() and user.is_admin() and user.has_group(gid):
  [ CONDITIONAL BODY ]

The deep expression operator works in:

  • if statements: if <... $X ...>:
  • nested calls: sql.query(<... $X ...>)
  • operands of a binary expression: "..." + <... $X ...>
  • any other expression context

Limitations

Statements types

Semgrep handles some statement types differently than others, particularly when searching for fragments inside statements. For example, the pattern foo will match these statements:

x += foo()
return bar + foo
foo(1, 2)

But foo will not match the following statement (import foo will match it though):

import foo

Statements as expressions

Many programming languages differentiate between expressions and statements. Expressions can appear inside if conditions, in function call arguments, etc. Statements can not appear everywhere; they are sequence of operations (in many languages using ; as a separator/terminator) or special control flow constructs (if, while, etc.).

foo() is an expression (in most languages).

foo(); is a statement (in most languages).

If your search pattern is a statement, Semgrep will automatically try to search for it as both an expression and a statement.

When you write the expression foo() in a pattern, Semgrep will visit every expression and sub-expression in your program and try to find a match.

Many programmers don't really see the difference between foo() and foo();. This is why when one looks for foo(); Semgrep thinks the user wants to match statements like a = foo();, or print(foo());.

Info

Note that in some programming languages such as Python, which does not use semicolons as a separator or terminator, the difference between expressions and statements is even more confusing. Indentation in Python matters, and a newline after foo() is really the same than foo(); in other programming languages such as C.

Partial statements

Partial statements are not valid patterns. For example, the following are invalid:

pattern: 1+
pattern: if $CONDITION:

In both cases, a complete statement is needed (like 1 + $X)