Semgrep pattern examples by language

Bash

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo <... bar ...>
Matching code
baz() {
  # MATCH:
  foo bar

  foo bar baz

  # MATCH:
  foo "$(echo 'hello' | bar)"
}

Expression and Statement

Pattern
foo
bar
Matching code
foo() {
  foo
  bar
  foo
  x=$(bar)
  foo
  foo2 "$(bar)"
  foo
  bloo | bar > /dev/null
}

Exact Matches

Single Statements

Pattern
foo bar
Matching code
foo bar

foo "bar"

foo "$bar"

foo 'bar'

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Argument

Pattern
foo $X 2
Matching code
foo 1 2

foo "$(date)" 2

foo 2

Class Definitions

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Function Call

Pattern
$CMD foo bar
Matching code
something foo bar

"$thing" foo bar

Function Definitions

Pattern
function $F() {
  ...
}
Matching code
foo() {
  echo hello
}

function foo() {
  echo hello
}


Imports

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Reoccurring Expressions

Expressions

Pattern
cp ... $X ... $X
Matching code
cp a b c b

cp a b c d

Statement

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Variables

Pattern
$FILE=''
touch "${$FILE}"
Matching code
out=''
touch "$out"

Wildcard Matches (...)

Arguments

Pattern
foo ... 5
Matching code
foo

foo 5

foo 1 2 3 4 5

foo 5 6

Method Chaining

This example is missing, or this feature isn't supported yet for Bash in Semgrep.

Nested Statements

Pattern
if ...; then
  ...
else
  ...
fi
Matching code
# MATCH:
if [[ -e foo ]]; then
  echo "foo exists"
else
  echo "error: foo is missing"
  exit 1
fi

Statements

Pattern
$V=$(cat)
...
eval ${$V}
Matching code
CMD=$(cat)
eval $CMD

cmd=$(cat)
echo "User entered: $cmd"
eval $cmd

Strings

Pattern
foo "..."
Matching code
# simple strings
foo bar
foo "bar"
foo 'bar'

# string made of multiple fragments
foo "${HOME}/bar"


C

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo(<... 42 ...>);
Matching code
int main() {
    int bar = 0;
    foo(bar + 42);
}

Expression and Statement

Pattern
foo();
bar();
Matching code
int main(int argc, char *argv[]) {
        foo();
	bar();
        foo();
	int x = bar();
        foo();
	foo(bar());
        foo();
	return bar();
}

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
void foo() {
    foo(1,2);

 foo(1,
     2);

 foo (1, // comment
      2);

 foo(2,1);
}



Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for C in Semgrep.

Named Placeholders ($X)

Argument

Pattern
foo($X, 2)
Matching code
void foo() {
    foo(1,2);

    foo(a_very_long_constant_name,
        2);

    foo (unsafe(), // indeed
         2);

    foo(bar(1,3), 2);

    foo(2,1);
}


Conditionals

Pattern
if ($E)
   foo();
Matching code
void foo() {
    x = 1;
    if (x > 2)
        foo();
}


Function Call

Pattern
$F(1,2)
Matching code
void foo() {
    foo(1,2);

    return 1;
}


Function Definitions

This example is missing, or this feature isn't supported yet for C in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for C in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for C in Semgrep.

Statement

Pattern
if ($X > $Y)
   $S;
Matching code
void foo() {
    v = 1;
    if (v > 2)
        return 1;
}


Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for C in Semgrep.

Typed Metavariables

Pattern
(char *$X) == $Y
Matching code
int test_equal() {
    int a = 1;
    int b = 2;
    if (a == b) return 1;
    if (3 == 4) return 2;

    int *p = malloc(sizeof(int));
    int *q = malloc(sizeof(int));
    if (p == q) return 1;

    char *x = "hello";
    char *y = "bye";
    if (x == y) return 2;
    if (x == "nope") return 3;
    if ("lit1" == "lit2") return 4;
    return 0;
}


Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for C in Semgrep.

Strings

Pattern
$X = "=~//[lL]ocation.*/" 

Matching code
void foo() {
    char path = "/location/1";
}


Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
void test_equal() {
    a = 1;
    b = 2;
    if (a+b == a+b)
        return 1;
    return 0;
}


Statement

Pattern
if ($E)
  $S;
else
  $S;

Matching code
void foo() {
    if (x > 2) {
        foo();
        bar();
    } else {
        foo();
        bar();
    }

    if (x > 2) {
        foo();
        bar();
    } else {
        foo();
    }
}




Variables

Pattern
$V = open();
close($V);

Matching code
void foo() {
    myfile = open();
    close(myfile);
}




Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
void foo() {
    foo(1,2,3,4,5);
    foo(5);
}


Method Chaining

This example is missing, or this feature isn't supported yet for C in Semgrep.

Nested Statements

This example is missing, or this feature isn't supported yet for C in Semgrep.

Statements

Pattern
$V = get();
...
eval($V);

Matching code
void foo() {

    user_data = get();
    print("do stuff");
    foobar();
    eval(user_data);
}



Strings

Pattern
foo("...")
Matching code
void foo() {
    foo("whatever sequence of chars");
}


Cairo

No pattern examples are available for this language yet.

Clojure

No pattern examples are available for this language yet.

C++

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo(<... 42 ...>);
Matching code
int main() {
    int bar = 0;
    foo(bar + 42);
}

Expression and Statement

Pattern
foo();
bar();
Matching code
int main(int argc, char *argv[]) {
        foo();
	bar();
        foo();
	int x = bar();
        foo();
	foo(bar());
        foo();
	return bar();
}

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
void foo() {
    foo(1,2);

 foo(1,
     2);

 foo (1, // comment
      2);

 foo(2,1);
}



Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Argument

Pattern
foo($X, 2)
Matching code
void foo() {
    foo(1,2);

    foo(a_very_long_constant_name,
        2);

    foo (unsafe(), // indeed
         2);

    foo(bar(1,3), 2);

    foo(2,1);
}


Class Definitions

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Conditionals

Pattern
if ($E)
   foo();
Matching code
void foo() {
    x = 1;
    if (x > 2)
        foo();
}


Function Call

Pattern
$F(1,2)
Matching code
void foo() {
    foo(1,2);

    return 1;
}


Function Definitions

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Statement

Pattern
if ($X > $Y)
   $S;
Matching code
void foo() {
    v = 1;
    if (v > 2)
        return 1;
}


Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Strings

Pattern
$X = "=~//[lL]ocation.*/" 

Matching code
void foo() {
    char path = "/location/1";
}


Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
void test_equal() {
    a = 1;
    b = 2;
    if (a+b == a+b)
        return 1;
    return 0;
}


Statement

Pattern
if ($E)
  $S;
else
  $S;

Matching code
void foo() {
    if (x > 2) {
        foo();
        bar();
    } else {
        foo();
        bar();
    }

    if (x > 2) {
        foo();
        bar();
    } else {
        foo();
    }
}




Variables

Pattern
$V = open();
close($V);

Matching code
void foo() {
    myfile = open();
    close(myfile);
}




Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
void foo() {
    foo(1,2,3,4,5);
    foo(5);
}


Method Chaining

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Nested Statements

This example is missing, or this feature isn't supported yet for C++ in Semgrep.

Statements

Pattern
$V = get();
...
eval($V);

Matching code
void foo() {

    user_data = get();
    print("do stuff");
    foobar();
    eval(user_data);
}



Strings

Pattern
foo("...")
Matching code
void foo() {
    foo("whatever sequence of chars");
}


C#

Deep (Recursive) Matching

Deep Expression Operator

Pattern
<... 12 ...>
Matching code
public class Test
{
    public static void Test()
    {
        int x = 13;
        Console.WriteLine(x + 12 + x);
    }
}

Expression and Statement

Pattern
foo();
bar();
Matching code
using System;

class HelloWorldFuncCall
{
    public static string Test()
    {
        foo();
        bar();

        foo();
        Console.WriteLine(bar());

        foo();
        var x = bar();

        foo();
	return bar();
    }

    private static string bar()
    {
        return "hello world";
    }
}

Exact Matches

Single Statements

Pattern
Foo(1, 2)
Matching code
public class ConcreteSyntax
{
    public static void Main()
    {
        Foo(1, 2);

        Foo(1,
            2);

        Foo(1, // comment
            2);

        Foo(2, 1);

        Foo(1, 2, 3);
    }

    private static void Foo(int a, int b, int c = 3)
    {
    }
}

Helpful Features

Constant Propagation

Pattern
Console.WriteLine("world");
Matching code
public class Test
{
    public static void Test()
    {
        String x = "world";
        Console.WriteLine(x);
    }
}

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for C# in Semgrep.

Named Placeholders ($X)

Annotations

Pattern
[$ANNO]
class $CLASS{ ... }
Matching code
[Serializable]
public class Test {
}

Argument

Pattern
Foo($X, 2)
Matching code
public class MetaVar
{
    public static void Main()
    {
        Foo(1, 2);

        Foo(int.MaxValue,
            2);

        Foo(int.Parse("3"), // comment
            2);

        Foo(Bar(1, 3), 2);

	// OK:
        Foo(2, 1);

	// OK:
	Foo(1, 2, 3);
    }
}

Class Definitions

Pattern
class $KLASS {}
Matching code
class Foo {
}

interface Foo {
}

namespace Foo {
}

Conditionals

Pattern
if ($COND) { ... }
Matching code
public class Test
{
    public static void Test()
    {
        if (x == some_cond)
            Console.WriteLine("matched");
        else
            Console.WriteLine("not matched");
    }
}

Function Call

Pattern
$F(1,2)
Matching code
public class Test
{
    public static int Test()
    {
        Foo(1, 2);
        return 1;
    }
}

Function Definitions

Pattern
$RETURNTYPE $FUNC(...) {...}
Matching code
public class Test
{
    public static int Test()
    {
        Foo(1, 2);
        return 1;
    }

    int Test2() {
        return 1;
    }

    public static void Main()
    {
        Foo(1, 2);
        return 1;
    }
}

Imports

Pattern
using $X;
Matching code
using System;
using System.IO;

class Test {}

Object or Dictionary Key Value Pairs

Pattern
Foo(..., bar: 42, ...);
Matching code
class Test 
{
    public static void Main()
    {
        // OK:
        Foo(42, "baz");

        Foo(bar: 42, baz: "baz");
        Foo(baz: "baz", bar: 42);
    }
}

Statement

Pattern
if ($E) {
    $S;
}
Matching code
public class Test
{
    public static void Test()
    {
        if (true)
            Console.WriteLine("same");
    }
}

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for C# in Semgrep.

Typed Metavariables

Pattern
$X == (String $Y)
Matching code
public class Foo {
    public static void main() {
        String x;
        String y;
        int a;
        int b;
        if (x == y) x = y;
        if (a == b) a = b;
   }
}

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for C# in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for C# in Semgrep.

Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
public class Test
{
    public static void Test()
    {
        if (1+2 == 1+2)
            Console.WriteLine("matched");
        else
            Console.WriteLine("not matched");
    }
}

Statement

Pattern
if ($E) {
    $S;
} else {
    $S;
}
Matching code
public class Test
{
    public static void Test()
    {
        if (true)
            Console.WriteLine("same");
        else
            Console.WriteLine("same");
    }
}

Variables

Pattern
$V = Open();
Close($V);

Matching code
public class Test
{
    public static void Test()
    {
        myFile = Open();
        Close(myFile);
    }
}

Wildcard Matches (...)

Arguments

Pattern
Foo(..., 5)
Matching code
public class Test
{
    public static void Test()
    {
        Foo(1, 2, 3, 4, 5);
        Foo(5);
    }
}

Method Chaining

Pattern
$X = $O.foo(). ... .bar(). ...
Matching code
class Foo {

  void test() {

    f = this.foo().m().h().bar().z();

    f = this.foo().bar();

    f = this.foo().m().h().z();

    f = this.before().foo().m().h().bar().z();
  }
}


Nested Statements

Pattern
if (...)
{
    ...
}
Matching code
public class Test
{
    public static void Test()
    {
        if (x == some_cond)
            Console.WriteLine("matched");
        else
            Console.WriteLine("not matched");
    }
}

Statements

Pattern
$X = Get();
...
Eval($X)
Matching code
public class Test
{
    public static void Test()
    {
        userData = Get();
        Console.WriteLine("do stuff");
        FooBar();
        Eval(userData);
        FooBar();
    }
}

Strings

Pattern
Foo("...")
Matching code
public static void Bar(input) {
    Foo("whatever sequence of chars");

    //OK:
    Foo("not a constant string: " + input);
}


Dockerfile

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Exact Matches

Single Statements

Pattern
FROM ubuntu
RUN echo hello
Matching code
FROM ubuntu
RUN echo hello

FROM debian
RUN echo hello

FROM ubuntu:testing
RUN echo hello

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Argument

Pattern
foo $X 2
Matching code
RUN foo 1 2

RUN foo "$(date)" 2

RUN foo 2

Class Definitions

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Function Call

Pattern
$CMD foo bar
Matching code
RUN something foo bar

CMD "$thing" foo bar

Function Definitions

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Variables

Pattern
RUN $FILE='' && touch "${$FILE}"
Matching code
RUN out='' && touch "$out"

Wildcard Matches (...)

Arguments

Pattern
foo ... 5
Matching code
RUN foo

RUN foo 5

RUN foo 1 2 3 4 5

RUN foo 5 6

Method Chaining

This example is missing, or this feature isn't supported yet for Dockerfile in Semgrep.

Nested Statements

Pattern
if ...; then
  ...
else
  ...
fi
Matching code
# MATCH:
RUN if [[ -e foo ]]; then \
      echo "foo exists"; \
    else \
      echo "error: foo is missing"; \
      exit 1; \
    fi

Statements

Pattern
FROM debian
...
RUN apt-get ...
Matching code
FROM debian:testing
RUN echo hello
RUN apt-get update && apt-get install -y fortune

Strings

Pattern
foo "..."
Matching code
# simple strings
RUN foo bar
RUN foo "bar"
RUN foo 'bar'

# string made of multiple fragments
RUN foo "${HOME}/bar"

Fga

No pattern examples are available for this language yet.

Elixir

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Exact Matches

Single Statements

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Argument

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Class Definitions

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Function Call

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Function Definitions

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Variables

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Wildcard Matches (...)

Arguments

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Method Chaining

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Nested Statements

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Statements

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Elixir in Semgrep.

Go

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo(<... 42 ...>);
Matching code
package Foo

func bar() {
	baz := 0
	foo(baz + 42)
}

Expression and Statement

Pattern
foo();
bar();
Matching code
package Foo

func foo() {
        foo()
	bar()
        foo()
	x = bar()
        foo()
	foo2(bar())
        foo()
	return bar()
}

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
package Foo

func foo() {
    foo(1,2);
    
    foo(1,
        2);
    
    foo (1, // comment
         2)
    
    foo(2,1)
    
}


Helpful Features

Constant Propagation

Pattern
$F("password")
Matching code
package Foo

const Bar = "password"

func foo() {
     dangerous1("password");

     dangerous2(Bar);
}

Import Renaming/Aliasing

Pattern
subprocess.open(...)
Matching code
package Foo

import "a/subprocess"

import sub "subprocess"

func foo() {
  result = subprocess.open("ls")
  result = sub.open("ls")

  result = sub.not_open("ls")

}

Named Placeholders ($X)

Argument

Pattern
foo($X, 2)
Matching code
package Foo

func bar() {
    foo(1,2)

    foo(a_very_long_constant_name,
        2)

    foo (unsafe(), // indeed
         2)

    foo(bar(1,3), 2)

    foo(2,1)
}



Conditionals

Pattern
if ($E) { 
  foo() 
}
Matching code
package Foo

func foo() {
    x = 1
    if (x > 2) {
        foo()
    }
}

Function Call

Pattern
$F(1,2)
Matching code
package Foo

func foo() {
    foo(1,2)

    return 1
}




Function Definitions

Pattern
func $X(...) {
    ...
}
Matching code
package Foo

func foo() {
    foo (1,2)
}

func bar(bar1, bar2, bar3) {
    return 1
}

func foobar(bar1) int {
    bar(1, 2, 3)
    foo()
    return 2
}

Imports

This example is missing, or this feature isn't supported yet for Go in Semgrep.

Object or Dictionary Key Value Pairs

Pattern
config{..., $KEY:$VALUE, ...}
Matching code
package Foo 

func foo() {
    foo = config {
        key: value,
        key2: value2,
        key3: value3
    }

    bar = config2 {
        key: value,
    }
}

Statement

Pattern
if ($X > $Y) {
   $S;
}
Matching code
package Foo

func bar() {
	var a = 1
	var b = 2
	var c = 3
	if a > b {
		c = 0
	}
}

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Go in Semgrep.

Typed Metavariables

Pattern
$X == ($Y : string)
Matching code
package Foo

func main() {
    var x string
    var y string
    var a int
    var b int
    if x == y {
       x = y
    }
    if a == b {
       a = b
    }
}

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Go in Semgrep.

Strings

Pattern
$X = "=~//[lL]ocation.*/" 

Matching code
package Foo

func foo() {
	var path = "/location/1"
	var d1 = "notamatch"
}

Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
package Foo

func test_equal() {
    a = 1
    b = 2
    if (a+b == a+b) {
        return 1
    }
    return 0
}

Statement

Pattern
if $E {
  $S;
} else {
  $S;
}
Matching code
package main

import "fmt"

func main() {
	if x > 2 {
		fmt.Println("hello world")
	} else {
		fmt.Println("hello world")
	}

	if x > 2 {
		fmt.Println("hello world")
	} else {
		fmt.Println("goodbye world")
	}
}

Variables

Pattern
$V = open();
close($V);

Matching code
package Foo

func foo() {
    myfile = open()
    close(myfile)
}

Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
package Foo

func bar() {
    foo(1,2,3,4,5)
    foo(5)
}




Method Chaining

Pattern
$X = $O.foo(). ... .bar(). ...
Matching code
func foo() {
    
  f = o.foo().m().h().bar().z()

  f = o.foo().bar()

  // this one does not contain the bar()
  f = o.foo().m().h().z()

  f = o.before().foo().m().h().bar().z()

}

Nested Statements

Pattern
if $E {
  ...
}
Matching code
package foo

func foo() {
     if 1 {
        return 1
     }
}

Statements

Pattern
$X = get()
...
eval($X)
Matching code
package Foo

func foo() {
    user_data = get()
    print("do stuff")
    foobar()
    eval(user_data)
    foobar()
}

Strings

Pattern
foo("...")
Matching code
package Foo

func bar () {
    foo("whatever sequence of chars")
}


HTML

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Exact Matches

Single Statements

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Argument

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Class Definitions

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Function Call

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Function Definitions

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Variables

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Wildcard Matches (...)

Arguments

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Method Chaining

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Nested Statements

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Statements

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for HTML in Semgrep.

Java

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo(<... 42 ...>);
Matching code
class Foo {
    void bar() {
        int baz = 0;
        foo(baz + 42);
    }
}

Expression and Statement

Pattern
foo();
bar();
Matching code
class Foo {
    void foo() {
        foo();
        bar();
        foo();
        x = bar();
        foo();
        foo2(bar());
        foo();
        return bar();
      }
  }
      

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
class Foo {
    void foo() {
        foo(1,2);

        foo(1,
            2);

        foo (1, // comment
             2);

        foo(2,1);
    }
}

Helpful Features

Constant Propagation

Pattern
foo("password")
Matching code
class Foo {
	final String pwd = "password";	
 	void main()	{
		foo(pwd);
	}

    void diff_word() {
        foo("hello");
    }
    void diff_func() {
        bar("password");
    }

    void same_const() {
        foo("password");
    }

    void all_diff() {
        bar("hello");
    }
}

Named Placeholders ($X)

Annotations

Pattern
@$X
class $CLASS {
    ...
}
Matching code
@AnnoFoo
class Foo {
    int foo = 1;
}

@Anno1
@Anno2
class FooBar {
    int foobar = 2;
}

@AnnoBar(bar = "bar")
class Bar {
    int bar = 1;
}

class NoAnno {
    int noanno = 2;
}

Argument

Pattern
foo($X, 2)
Matching code
class Foo {
  void bar() {
    foo(1,2);

    foo(a_very_long_constant_name,
        2);

    foo (unsafe(), // indeed
         2);

    foo(bar(1,3), 2);

    foo(2,1);
  }
}


Class Definitions

Pattern
class $X {
    ...
}
Matching code
public class Foo {
    int foo = 5;

    public static void foo() {
        int foo1 = 4;
    }
}

private class Bar extends Foo {
    int bar = 3;
}

abstract class Foobar {
    public abstract void foobar();
}


Conditionals

Pattern
if ($E)
   foo();
Matching code
class Foo {
void foo() {
    x = 1;
    if (x > 2)
        foo();
}

}

Function Call

Pattern
$F(1,2)
Matching code
class Foo {
void foo() {
    foo(1,2);

    return 1;
}
}



Function Definitions

Pattern
void $X(...) {
    ...
}
Matching code
class Foo {
    void foo() {
        foo(1,2);
    }

    public static void bar(int bar1, int bar2, int bar3) {
        foo(1,2,3);
        bar(1,2);
    } 

    public void foobar(int bar1) {
        foo();
    }

    public static String str() {
        return "hello";
    }
}

Imports

Pattern
import java.util.$X;
Matching code
import java.util.ArrayList;
import java.util.List;

class Foo {

}

Statement

Pattern
if ($X > $Y)
   $S;
Matching code
class Foo {
void foo() {
    v = 1;
    if (v > 2)
        return 1;
}
}



Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Java in Semgrep.

Typed Metavariables

Pattern
$X == (String $Y)
Matching code
public class Foo {
    public static void main() {
        String x;
        String y;
        int a;
        int b;
        if (x == y) x = y;
        if (a == b) a = b;
   }
}

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Java in Semgrep.

Strings

Pattern
$X = "=~//[lL]ocation.*/" 

Matching code
class Foo {
    void foo() {
        path = "/location/1";
    }
}

Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
class Foo {
void test_equal() {
    a = 1;
    b = 2;
    if (a+b == a+b)
        return 1;
    return 0;
}

}

Statement

Pattern
if ($E)
  $S;
else
  $S;

Matching code
class foo {

void foo() {
    if (x > 2) {
        foo();
        bar();
    } else {
        foo();
        bar();
    }

    if (x > 2) {
        foo();
        bar();
    } else {
        foo();
    }
}


}

Variables

Pattern
$V = open();
close($V);

Matching code
class Foo {
void foo() {
    myfile = open();
    close(myfile);
}
}





Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
class Foo {
  void bar() {
    foo(1,2,3,4,5);
    foo(5);
  }
}



Method Chaining

Pattern
$X = $O.foo(). ... .bar(). ...
Matching code
class Foo {

  void test() {

    f = this.foo().m().h().bar().z();

    f = this.foo().bar();

    f = this.foo().m().h().z();

    f = this.before().foo().m().h().bar().z();
  }
}


Nested Statements

Pattern
if (...)
  ...

Matching code
class Foo {
  void main() {

    if (x == 1) 
        return 2; 
  }
}


Statements

Pattern
$V = get();
...
eval($V);

Matching code
class Foo {
void foo() {

    user_data = get();
    print("do stuff");
    foobar();
    eval(user_data);
}

}



Strings

Pattern
foo("...")
Matching code
class Foo { 
  void bar () {
    foo("whatever sequence of chars");
  }
}

JavaScript

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo(<... 42 ...>);
Matching code
function bar() {
    baz = 0;
    foo(baz + 42);
}

Expression and Statement

Pattern
foo();
bar();
Matching code
function foo() {
    foo();
    bar();
    foo();
    x = bar();
    foo();
    print(bar());
    foo();
    return bar();
}

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
function foo() {
    foo(1, 2);
    foo(1,2);
    foo (1, 2);
 foo(1,
     2);
 foo(1, // comment
     2);

 foo(2,1)
}



Helpful Features

Constant Propagation

Pattern
$F("password")
Matching code
const Bar = "password";

function foo() {
     password(Bar);
}

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for JavaScript in Semgrep.

Named Placeholders ($X)

Annotations

Pattern
@$X
class $CLASS {
    ...
}
Matching code
@Anno1
class Foo {
    foo() {
        var foo = 1;
    }
}

@Anno1()
@Anno2()
class FooBar {
    foo() {
        var foo = 2;
    }
}

@AnnoVar("Bar")
class Bar {
    bar() {
        var bar = 1;
    }
}

class NoAnno {
    noanno() {
        var no_anno = 1;
    }
}

Argument

Pattern
foo($X, 2)
Matching code
function foo() {
    foo(1,2);

    foo(a_very_long_constant_name,
        2);

    foo (unsafe(), // indeed
         2);

    foo(bar(1,3), 2);

    foo(2,1);
}


Class Definitions

Pattern
class $X {
    ...
}
Matching code
class Foo {
    constructor(foo) {
        this.foo = foo;
    }

    get foo() {
        return this.foo;
    }
}

class Bar extends Foo {
    constructor (bar) {
        this.bar = bar;
    }
}

Conditionals

Pattern
if ($E)
   foo();
Matching code
function foo() {
    x = 1;
    if (x > 2)
        foo();
}


Function Call

Pattern
$F(1,2)
Matching code
function foo() {
    foo(1,2);

    return 1;
}


Function Definitions

Pattern
function $X(...) {
    ...
}
Matching code
function foo() {
    foo(1,2);
}

function bar(bar1, bar2, bar3) {
    foo(1,2);
    bar(1,2,3);
}


Imports

Pattern
import $X from 'foo';
Matching code
import jwt from 'foo';
let a = jwt.sign({}, 'a');

Object or Dictionary Key Value Pairs

Pattern
{..., $KEY: $VALUE, ...}
Matching code
function foo() {
    var foo = {key: value, key2: value2, key3: value3};

    var bar = {foo: bar, foo2: bar2, foo3: bar3};
}

Statement

Pattern
if ($X > $Y)
   $S;
Matching code
function foo() {
    v = 1;
    if (v > 2)
        return 1;
}


Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for JavaScript in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

Pattern
var $X = {"=~/[lL]ocation/": $Y};
Matching code
var x = {"Location": 1};

Strings

Pattern
$X = "=~//[lL]ocation.*/" 

Matching code
path = "/location/1";

Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
function test_equal() {
    a = 1;
    b = 2;
    if (a+b == a+b)
        return 1;
    return 0;
}


Statement

Pattern
if ($E)
  $S;
else
  $S;

Matching code
function foo() {
    if (x > 2) {
        foo();
        bar();
    } else {
        foo();
        bar();
    }

    if (x > 2) {
        foo();
        bar();
    } else {
        foo();
    }
}




Variables

Pattern
$V = open();
close($V);

Matching code
function foo() {
    myfile = open();
    close(myfile);
}




Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
function foo() {
    foo(1,2,3,4,5);
    foo(5);
}


Method Chaining

Pattern
$X = $O.foo(). ... .bar(). ...
Matching code
f = o.foo().m().h().bar().z();

f = o.foo().bar();

f = o.foo().m().h().z();

f = o.before().foo().m().h().bar().z();

Nested Statements

Pattern
if (...)
  ...

Matching code
function foo() {

    if (x == 1) 
        return 2;
}

Statements

Pattern
$V = get();
...
eval($V);

Matching code
function foo() {

    user_data = get();
    print("do stuff");
    foobar();
    eval(user_data);
}



Strings

Pattern
foo("...")
Matching code
function foo() {
    foo("whatever sequence of chars");
    foo('whatever sequence of chars');
}


JSON

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Exact Matches

Single Statements

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Argument

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Class Definitions

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Function Call

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Function Definitions

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Variables

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Wildcard Matches (...)

Arguments

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Method Chaining

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Nested Statements

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Statements

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for JSON in Semgrep.

Jsonnet

No pattern examples are available for this language yet.

Julia

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Exact Matches

Single Statements

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Argument

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Class Definitions

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Function Call

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Function Definitions

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Variables

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Wildcard Matches (...)

Arguments

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Method Chaining

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Nested Statements

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Statements

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Julia in Semgrep.

Kotlin

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Exact Matches

Single Statements

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Argument

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Class Definitions

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Function Call

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Function Definitions

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Variables

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Wildcard Matches (...)

Arguments

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Method Chaining

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Nested Statements

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Statements

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Kotlin in Semgrep.

Lisp

No pattern examples are available for this language yet.

Lua

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Expression and Statement

Pattern
foo();
bar();
Matching code
function foo()
    foo()
    bar()
    foo()
    x = bar()
    foo()
    print(bar())
    foo()
    return bar()
end

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
function foo()
    foo(1,2)

 foo(1,
     2)

 foo (1, -- comment
      2)

 foo(2,1)
end

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Argument

Pattern
foo($X, 2)
Matching code
function foo()
    foo(1,2)

    foo(a_very_long_constant_name,
        2)

    foo (unsafe(), -- indeed
         2)

    foo(bar(1,3), 2)

    foo(2,1)
end


Class Definitions

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Function Call

Pattern
$F(1,2)
Matching code
function foo()
    foo(1,2)

    return 1
end



Function Definitions

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Variables

Pattern
$V = open();
close($V);

Matching code
function foo()
    myfile = open()
    close(myfile)
end





Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
function foo()
    foo(1,2,3,4,5)
    foo(5)
end



Method Chaining

This example is missing, or this feature isn't supported yet for Lua in Semgrep.

Nested Statements

Pattern
if (...) then
  ...
end


Matching code
function foo()

    if (x == 1) then
        return 2
    end
end


Statements

Pattern
$V = get()
...
eval($V)


Matching code
function foo()

    user_data = get()
    print("do stuff")
    foobar()
    eval(user_data)
end

Strings

Pattern
foo("...")
Matching code
function foo()
    foo("whatever sequence of chars")
    foo('whatever sequence of chars')
end

OCaml

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Exact Matches

Single Statements

Pattern
foo 1 2
Matching code
let foo = foo 1 2

let foo1 = foo 1
               2

let foo2 = foo 1 (* comment *)
              2

let foo3 = foo 2 1

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Named Placeholders ($X)

Argument

Pattern
foo $X 2
Matching code
let foo = foo 1 2

let foo1 = foo a_very_long_constant_name 2

let foo2 = foo unsafe (* indeed *) 2

let foo3 = foo (bar 1 3) 2

let foo4 = foo 2 1

Class Definitions

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Conditionals

Pattern
if $E then foo else $X
Matching code
let foo = 
  let x = 1 in
  if x > 2 then foo else bar

Function Call

Pattern
$F 1 2
Matching code
let foo = foo 1 2

Function Definitions

Pattern
let $X ... = ...
Matching code
let foo = foo 1 2

let bar bar1 bar2 = bar 1 2

let rec foobar bar1 = foobar 1 2

Imports

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Statement

Pattern
if $X > $Y then $S else $M
Matching code
let foo = 
  let v = 1 in
  if v > 2 then 1 else 2

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Strings

Pattern
let $X = "=~//[lL]ocation.*/"
Matching code
let foo = "/location/l"

Reoccurring Expressions

Expressions

Pattern
$X = $X
Matching code
let foo = 
  let a = 1 in
  let b = 2 in
  if a+b = a+b then 1 else 2

Statement

Pattern
if $E then $S else $S
Matching code
let foo = 
  if x > 2 then 
    foo
  else 
    foo

let foo2 = 
  if x > 2 then
    bar
  else 
    foo

Variables

Pattern
let $A = open_file "file" in
close $A
Matching code
let foo = 
  let myfile = open_file "file" in
  close myfile

Wildcard Matches (...)

Arguments

Pattern
foo ... 5
Matching code
let foo = foo 1 2 3 4 5

let foo2 = foo 5




Method Chaining

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Nested Statements

Pattern
if ... then ... else ...
Matching code
let foo = if x = 1 then 2 else 2

Statements

This example is missing, or this feature isn't supported yet for OCaml in Semgrep.

Strings

Pattern
foo "..."
Matching code
let bar = foo "whatever sequence of chars"

PHP

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo(<... 42 ...>);
Matching code
<?php

foo($bar + 42);

Expression and Statement

Pattern
foo();
bar();
Matching code
<?php

class Foo
{
    public function foo()
    {
        foo();
        bar();
        foo();
        $x = bar();
        foo();
        foo2(bar());
        foo();
        return bar();
    }
}

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
<?php
function foo()
{
    foo(1,2);

    foo(1,
        2);

    foo (1, // comment
        2);

    foo (2,1);
}
?>

Helpful Features

Constant Propagation

Pattern
$F("password")
Matching code
<?php

const glob1 = "password";

$glob2 = "password";
$glob2 = "bar";

function foo()
{
    bar(glob1);

    bar($glob2);
}

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for PHP in Semgrep.

Named Placeholders ($X)

Annotations

Pattern
#[$ATTR]
class $F { ... }
Matching code
<?php
#[AnnoFoo]
class Foo {
}

#[Anno1, Anno2]
class FooBar {
}

#[AnnoBar("bar")]
class Bar {
}

class NoAnno {
}

Argument

Pattern
foo($X, 2)
Matching code
<?php
function foo() {
    foo(1,2);

    foo(a_very_long_constant_name,
        2);

    foo (unsafe(), //comment
         2);

    foo(bar(1,3),  2);

    foo(2,1);
}
?>

Class Definitions

Pattern
class $X {
    ...
}
Matching code
<?php

class Foo
{
    public function member()
    {
        echo 'Member function';
    }
}

Conditionals

Pattern
if ($E)
   foo();
Matching code
<?php
function foo() {
    $x = 1;
    if ($x > 2) {
        foo();
    }
}
?>

Function Call

Pattern
$F(1,2)
Matching code
<?php
function foo() {
    foo(1,2);

    return 1;
}
?>

Function Definitions

Pattern
function $X(...) {
    ...
}
Matching code
<?php

function foo()
{
    return 'This is a function';
}

Imports

Pattern
require $A;
include $B;
require_once $C;
include_once $D;
Matching code
<?php

require "a.php";
include "b.php";
require_once "c.php";
include_once "d.php";

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for PHP in Semgrep.

Statement

Pattern
if ($X > $Y)
   $S;
Matching code
<?php
function foo() {
    $v = 1;
    if ($v > 2)
        return 1;
}
?>

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for PHP in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for PHP in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for PHP in Semgrep.

Strings

Pattern
$X = "=~//[lL]ocation.*/" 

Matching code
<?php
function foo() {
    $path = "/location/1";
}
?>

Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
<?php
function foo() {
    $a = 1;
    $b = 2;
    if ($a+$b == $a+$b) {
        return 1;
    }
    return 0;
}

?>

Statement

Pattern
if ($E)
  $S;
else
  $S;

Matching code
<?php
function foo() {
    if ($x > 2) {
        foo();
        bar();
    } else {
        foo();
        bar();
    }

    if ($x > 2) {
        foo();
        bar();
    }
    else {
        foo();
    }
}
?>

Variables

Pattern
$V = open();
close($V);

Matching code
<?php
function foo() {
    $myfile = open();
    close($myfile);
}
?>

Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
<?php
function foo() {
    foo(1,2,3,4,5);
    foo(5);
}
?>

Method Chaining

Pattern
$X = $O->foo()-> ... ->bar()-> ...;

Matching code
<?php

function test() {

    $f = this->foo()->m()->h()->bar()->z();

    $f = this->foo()->bar();

    $f = this->foo()->m()->h()->z();

    $f = this->before()->foo()->m()->h()->bar()->z();
}



Nested Statements

Pattern
if (...) {
    ...
}
Matching code
<?php

function foo()
{
    $var = 10;

    if ($var === 42) {
        echo('matched');
    }

    if ($var === 42) {
        echo('matched');
    } else {
        echo('not matched');
    }
}

Statements

Pattern
$V = get();
...
eval($V);

Matching code
<?php
function foo() {
    $user_data = get();
    print("do stuff");
    foobar();
    eval($user_data);
}
?>

Strings

Pattern
foo("...")
Matching code
<?php
function foo() {
    foo("whatever sequence of chars");
}
?>

Powershell

No pattern examples are available for this language yet.

Prometheus Query Language

No pattern examples are available for this language yet.

Python

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo(<... 42 ...>)
Matching code
bar = 0
foo(bar + 42)

Expression and Statement

Pattern
foo()
bar()
Matching code
def foo():
    foo()
    bar()
    foo()
    x = bar()
    foo()
    foo2(bar())
    foo()
    return bar()

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
def foo():
    foo(1,2)

    foo(1, 
        2)

    foo(1,   # comment 
        2)

    foo(2, 1)

Helpful Features

Constant Propagation

Pattern
bar("secret")
Matching code
CONST_GLOBAL = "secret"

# this is modifed somewhere, so not inferred as a constant
GLOBAL = "secret"

def foo():
    bar(CONST_GLOBAL)


def bar():
    global GLOBAL
    GLOBAL="x"

Import Renaming/Aliasing

Pattern
subprocess.open(...)
Matching code
import subprocess.open
from subprocess import open as sub_open

import subprocess as sub


# TODO
# import os as subprocess


def foo():
    result = subprocess.open("ls")
    result = sub_open("ls")
    result = sub.open("ls")

    result = sub.not_open("ls")

Named Placeholders ($X)

Annotations

Pattern
@$X
def $FUNC(...):
    ...
Matching code
@anno1
def bar(foo: 'foo'):
    x = 2

@anno1
@anno2
def foobar():
    x = 3

@app.route('/foo')
def foo():
    x = 1

def no_anno():
    x = 2



Argument

Pattern
foo($X, 2)
Matching code
def foo():
    foo(1, 2)

    foo(a_very_long_constant_name, 2)

    foo(unsafe(), 2)  # indeed

    foo(bar(1, 3), 2)

    foo(2, 1)

Class Definitions

Pattern
class $X:
    ...
Matching code
class Foo:
    def foo ():
        foo (3)
class Anything(Foo):
    def foos ():
        foo (4)

def foo(var):
    foo (3)



Conditionals

Pattern
if $E:
   foo()
Matching code
def foo():
    x = 1
    if x > 2:
        foo()

Function Call

Pattern
$F(1,2)
Matching code
def foo():
    foo(1, 2)

    return 1

Function Definitions

Pattern
def $X(...):
    ...
Matching code
def foo():
    foo(1,2)

def bar(bar1, bar2, bar3):
    bar (1,2,3)

def foobar(bar1) -> int:
    bar(1,2,3)
    foo()
    return 3

Imports

Pattern
import foo.$BAR
Matching code
import foo.something

Object or Dictionary Key Value Pairs

Pattern
{..., $KEY: $VALUE, ...}
Matching code
def foo():
    thisdict = {
        key:value,
        key2:value2,
        key3:value3
    }

    foo = {
        foo:bar
    }

Statement

Pattern
if var > 2:
   $S
Matching code
def foo():
    var = 1
    if var > 2:
        return 1

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Python in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Python in Semgrep.

Strings

Pattern
$X = "=~//[lL]ocation.*/" 

Matching code
path = "/location/1";

Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
def test_equal():
    a = 1
    b = 2
    if a + b == a + b:
        return 1
    return 0

Statement

Pattern
if $E:
  $S
else:
  $S
Matching code
def foo():
    if x > 2:
        foo()
        bar()
    else:
        foo()
        bar()

    if x > 2:
        foo()
        bar()
    else:
        foo()

Variables

Pattern
$V = open()
close($V)
Matching code
def foo():
    myfile = open()
    close(myfile)

Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
def foo():
    foo(1, 2, 3, 4, 5)
    foo(5)

Method Chaining

Pattern
$X = $O.foo(). ... .bar(). ...
Matching code
def foo():
    
  f = o.foo().m().h().bar().z()

  f = o.foo().bar()

  # this one does not contain the bar()
  f = o.foo().m().h().z()

  f = o.before().foo().m().h().bar().z()

Nested Statements

Pattern
if (...):
  ...

Matching code
def foo():
    if (x == some_cond):
        print('matched')
    else:
        print('not matched')
    


Statements

Pattern
$V = get()
...
eval($V)
Matching code
def foo():

    user_data = get()
    print("do stuff")
    foobar()
    eval(user_data)

Strings

Pattern
foo("...")
Matching code
def foo():
    foo("whatever sequence of chars")
    # foo('whatever sequence of chars')

    foo(f'constant string')

    # this string is not a constant, and therefore will not be matched.
    foo(f'string {var} interpolation')

QL

No pattern examples are available for this language yet.

R

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for R in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for R in Semgrep.

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
foo <- function () {
    foo(1,2)

 foo(1,
     2)

 foo (1, #comment
      2)

 foo(2,1)
}

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for R in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for R in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for R in Semgrep.

Argument

Pattern
foo($X, 2)
Matching code
foo <- function () {
    foo(1,2)

    foo(a_very_long_constant_name,
        2)

    foo (unsafe(), #indeed
         2)

    foo(bar(1,3), 2)

    foo(2,1)
}


Class Definitions

This example is missing, or this feature isn't supported yet for R in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for R in Semgrep.

Function Call

Pattern
$F(1,2)
Matching code
foo <- function () {
    foo(1,2)

    return 1
}



Function Definitions

This example is missing, or this feature isn't supported yet for R in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for R in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for R in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for R in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for R in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for R in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for R in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for R in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for R in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for R in Semgrep.

Variables

Pattern
$V = open();
close($V);

Matching code
foo <- function () {
    myfile = open()
    close(myfile)
}





Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
foo <- function () {
    foo(1,2,3,4,5)
    foo(5)
}



Method Chaining

This example is missing, or this feature isn't supported yet for R in Semgrep.

Nested Statements

Pattern
if (...)
  ...

Matching code
foo <- function () {
    if (x == 1) then
        return 2
    end
}


Statements

Pattern
$V <- get()
...
eval($V)

Matching code
foo <- function () {
    user_data <- get()
    print("do stuff")
    foobar()
    eval(user_data)
}

Strings

Pattern
foo("...")
Matching code
foo <- function() {
    foo("whatever sequence of chars")
    foo('whatever sequence of chars')
}

Ruby

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo(<... 42 ...>)
Matching code
def bar()
    baz = 0
    foo(baz + 42)
    foo(/hello #{world + 42}/)
end

Expression and Statement

Pattern
foo()
bar()
Matching code
def foo()
    foo()
    bar()
    foo()
    x = bar()
    foo()
    print(bar())
    foo()
    return bar()
end

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
def foo()
    foo(1,2)

    foo(1,
        2)

    foo(1, # comment 
        2)
 
    foo(2, 1)
end

Helpful Features

Constant Propagation

Pattern
$F("password")
Matching code
PASSWORD = "password"
def foo()
    func(PASSWORD)
end

# we even handle this case!
A=PASSWORD
def foo2()
    func(A)
end

Named Placeholders ($X)

Argument

Pattern
foo($X, 2)
Matching code
def foo()
    foo(1, 2)

    foo(a_very_long_constant_name, 2)

    foo(unsafe(), # indeed
        2)

    foo(bar(1, 3), 2)

    foo(2, 1)
end

Class Definitions

Pattern
class $X
    ...
end
Matching code
class Foo
  def foo()
    foo(1,2)
  end 
end

class Foo::Bar
end

Conditionals

Pattern
if $E
   foo()
end
Matching code
def foo()
    x = 1
    if x > 2
        foo()
    end
end


Function Call

Pattern
$F(1,2)
Matching code
def foo()
    foo(1, 2)

    return 1
end


Function Definitions

Pattern
def $X(...)
    ...
end
Matching code
def foo()
  foo(1,2)
end

def bar(bar1, bar2, bar3)
  foo(1,2)
  bar(1,2,3)
end


Imports

Pattern
require $R
Matching code
require 'foo'

Object or Dictionary Key Value Pairs

Pattern
{..., $KEY: $VALUE, ...}
Matching code
options = { key1: 41, key2: 42, key3: 43 }
options[:key2]  # => 42

Statement

Pattern
if $X > $Y
   $S
end

Matching code
def foo()
    var = 1
    if var > 2
        return 1
    end
end


Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Ruby in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Ruby in Semgrep.

Strings

Pattern
$X = "=~/\/[lL]ocation.*/"
Matching code
path = "/location/1"

Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
def test_equal()
    a = 1
    b = 2
    if a + b == a + b
        return 1
    end
    return 0
end


Statement

Pattern
if $E
  $S
else
  $S
end

Matching code
def foo()
    if x > 2
        foo()
        bar()
    else
        foo()
        bar()
    end

    if x > 2
        foo()
        bar()
    else
        foo()
    end
end


Variables

Pattern
$V = open()
close($V)
Matching code
def foo()
    myfile = open()
    close(myfile)
end


Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
def foo()
    foo(1, 2, 3, 4, 5)
    foo(5)
end


Method Chaining

Pattern
$X = $O.foo(). ... .bar(). ...
Matching code
def foo()
    
  f = o.foo().m().h().bar().z()

  f = o.foo().bar()

  # this one does not contain the bar()
  f = o.foo().m().h().z()

  f = o.before().foo().m().h().bar().z()
end

Nested Statements

Pattern
if ... then
  ...
end
Matching code
def foo
    if 1 != 3 then
        foo()
    end

    if 1 != 3
        foo()
    end

    if 1 == 1
        foo()
        bar()
        baz()
    end
end


Statements

Pattern
$V = get_data user
...
eval $V

Matching code
def foo(user)
    user_data = get_data user
    puts "... more stuff here ..."
    eval user_data

end

Strings

Pattern
foo("...")
Matching code
def foo()
    foo("whatever sequence of chars")
    # foo('whatever sequence of chars')

    # this string is not a constant, and therefore will not be matched.
    foo("string #{var} interpolation")
end


Rust

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Exact Matches

Single Statements

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Argument

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Class Definitions

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Function Call

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Function Definitions

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Variables

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Wildcard Matches (...)

Arguments

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Method Chaining

Pattern
Command::new(...). ... .expect(...)
Matching code
use std::process::Command;

fn main() -> io::Result<()> {
    let proc = Command::new("semgrep-core")
        .args(["-l", "rust"])
        .args(["-rules", "test.yaml"])
        .output()
        .expect("failed to execute process");

    Ok(())
}

Nested Statements

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Statements

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Rust in Semgrep.

Scala

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo(<... 42 ...>);
Matching code
def bar() = {
	return foo(baz + 42)
}

Expression and Statement

Pattern
foo();
bar();
Matching code
object Foo {

def foo() {
    foo()
    bar()
    foo()
    x = bar()
    foo()
    print(bar())
    foo()
    return bar()
}
}

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
object Foo { 

def foo() {
    foo(1,2)

 foo(1,
     2)

 foo (1, // comment
      2)

 foo(2,1)
}
}

Helpful Features

Constant Propagation

Pattern
$F("password")
Matching code

def ohno = {
    val bad = "password"

    f("password");

    f(bad);
}

Import Renaming/Aliasing

Pattern
subprocess.open(...)
Matching code

import subprocess.open
import subprocess.{open => sub_open}

def foo = {
    result = subprocess.open("ls")

    result = sub_open("ls")

}

Named Placeholders ($X)

Annotations

Pattern
@$X
class $CLASS {
    ...
}
Matching code

@Anno
class Foo1 {}

@Anno1
@Anno2
class Foo2 {}

//OK:
@Anno(x)
class Foo3 {}

//OK:
class Foo4 {}

Argument

Pattern
foo($X, 2)
Matching code
object Foo {
def foo() {
    foo(1,2)

    foo(a_very_long_constant_name,
        2)

    foo (unsafe(), // indeed
         2)

    foo(bar(1,3), 2)

    foo(2,1)
}
}


Class Definitions

Pattern
class $X {
    ...
}
Matching code
class Foo {
    val x = 0

    def bar (x : Int) : Int = x
}

Conditionals

Pattern
if ($E) {
    foo()
}
Matching code
object Foo {
    def f(x : Bool) : Int = {
        if (x) {
            return foo()
        }
    }
}

Function Call

Pattern
$F(1,2)
Matching code
object Foo {
def foo() {
    foo(1,2)

    return 1
}
}




Function Definitions

Pattern
def $FUNC(...) : $T = ...
Matching code
object Test {
    def foo() : Int = 1

    def bar(bar1 : Int, bar2 : Bool, bar3 : String) : Bool = {
        return bar2
    }

}

Imports

Pattern
import $A._
import $B.Thingy
import $C.{ $X => $Y }
Matching code
import Lib._
import Lib.Thingy
import Lib.{ ThingA => ThingB, ThingC => ThingD }

Object or Dictionary Key Value Pairs

Pattern
Map(..., $KEY -> $VALUE, ...)
Matching code
def x = Map("x" -> 1, "y" -> 2)

//OK:
def y = Map2("x" -> 1, "y" -> 2)

Statement

Pattern
if ($E) {
    $S
}
Matching code
object Foo {
    def foo () : Int = {
        if (cond) {
            return 1
        }
    }
}

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Scala in Semgrep.

Typed Metavariables

Pattern
foo($X : Int)
Matching code
def foo = {
  val x: Int = 0
  val y: String = ""
  foo(x);

  //OK:
  foo(y);
}

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Scala in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Scala in Semgrep.

Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
object Foo {
    val x = (a + b - foo()) == (a + b - foo())
}

Statement

Pattern
if ($E) {
    $S
}
else {
    $S
}
Matching code
object Foo {
    def foo() = {

        if (cond) {
            bar()
        }
        else {
            bar()
        }

        if (cond) {
            bar()
        }
        else {
            baz()
        }

    }
}

Variables

Pattern
$V = open();
close($V);

Matching code
object Foo {
def foo() {
    myfile = open()
    close(myfile)
}
}






Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
object Foo {

def foo() {
  foo(1,2,3,4,5)
  foo(5)
}
}




Method Chaining

Pattern
def $X = $O.foo(). ... .bar(). ...
Matching code
    
def f = o.foo().m().h().bar().z()

def f = o.foo().bar()

  // this one does not contain the bar()
def f = o.foo().m().h().z()

def f = o.before().foo().m().h().bar().z()


Nested Statements

Pattern
if (...)
  ...

Matching code
object Foo {

def foo() {

    if (x == 1) {
        return 2
    }
}
}



Statements

Pattern
$V = get();
...
eval($V);

Matching code
object Foo {
def foo() {

    user_data = get()
    print("do stuff")
    foobar()
    eval(user_data)
}
}


Strings

Pattern
foo("...")
Matching code
object Foo {
def foo() {
    foo("whatever sequence of chars")
    foo(s"whatever sequence of chars")
    foo("""whatever sequence of chars""")
}
}

Scheme

No pattern examples are available for this language yet.

Solidity

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Expression and Statement

Pattern
foo();
bar();
Matching code
function foo() {
    foo();
    bar();
    foo();
    x = bar();
    foo();
    print(bar());
    foo();
    return bar();
}

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
function foo() {
    foo(1,2);

 foo(1,
     2);

 foo (1, // comment
      2);

 foo(2,1);
}

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Argument

Pattern
foo($X, 2)
Matching code
function foo() {
    foo(1,2);

    foo(a_very_long_constant_name,
        2);

    foo (unsafe(), // indeed
         2);

    foo(bar(1,3), 2);

    foo(2,1);
}


Class Definitions

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Function Call

Pattern
$F(1,2)
Matching code
function foo() {
    foo(1,2);

    return 1;
}



Function Definitions

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Variables

Pattern
$V = open();
close($V);

Matching code
function foo() {
    myfile = open();
    close(myfile);
}





Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
function foo() {
    foo(1,2,3,4,5);
    foo(5);
}



Method Chaining

This example is missing, or this feature isn't supported yet for Solidity in Semgrep.

Nested Statements

Pattern
if (...)
  ...

Matching code
function foo() {

    if (x == 1) {
        return 2;
    }
}


Statements

Pattern
$V = get();
...
eval($V);

Matching code
function foo() {

    user_data = get();
    print("do stuff");
    foobar();
    eval(user_data);
}

Strings

Pattern
foo("...")
Matching code
function foo() {
    foo("whatever sequence of chars");
    foo('whatever sequence of chars');
}

Swift

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Expression and Statement

Pattern
foo();
bar();
Matching code
func foo() {
    foo();
    bar();
    foo();
    let x = bar();
    foo()
    print(bar());
    foo();
    return bar();
}

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
func foo() {
    foo(1, 2);
    foo(1,2);
    foo (1, 2);
    foo(1,
      2);
    foo(1, // comment
      2);

    foo(2,1)
}



Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Argument

Pattern
foo($X, 2)
Matching code
func foo() {
    foo(1,2);

    foo(a_very_long_constant_name,
        2);

    foo (unsafe(), // indeed
         2);

    foo(bar(1,3), 2);

    foo(2,1);
}


Class Definitions

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Function Call

Pattern
$F(1,2)
Matching code
func foo() {
    foo(1,2);

    return 1;
}


Function Definitions

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Variables

Pattern
$V = open();
close($V);

Matching code
func foo() {
    myfile = open();
    close(myfile);
}

Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
func foo() {
    foo(1,2,3,4,5);
    foo(5);
    foo(1, 5);
}


Method Chaining

This example is missing, or this feature isn't supported yet for Swift in Semgrep.

Nested Statements

Pattern
if ... {
  ...
}
Matching code
func foo() {
    if (x == 1) {
        return 2;
    }
}

Statements

Pattern
$V = get();
...
eval($V);

Matching code
func foo() {
    let user_data = get();
    print("do stuff");
    foobar();
    eval(user_data);
}

Strings

Pattern
foo("...")
Matching code
func foo() {
    foo("whatever sequence of chars");
}


Terraform

No pattern examples are available for this language yet.

TypeScript

Deep (Recursive) Matching

Deep Expression Operator

Pattern
foo(<... 42 ...>);
Matching code
function bar() {
    baz = 0;
    foo(baz + 42);
}

Expression and Statement

Pattern
foo();
bar();
Matching code
function foo() {
    foo();
    bar();
    foo();
    x = bar();
    foo();
    print(bar());
    foo();
    await bar();
    foo();
    bar().then(other => stuff());
    foo();
    return bar();
}

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
function foo() {
    foo(1,2);

    foo(1,
        2);

    foo (1, //comment
         2);

    foo (2,1);
}

Helpful Features

Constant Propagation

Pattern
$F("password")
Matching code
const Bar = "password";

function foo() {
     password(Bar);
}

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for TypeScript in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for TypeScript in Semgrep.

Argument

Pattern
foo($X, 2)
Matching code
function foo() {
    foo(1,2);

    foo(a_very_long_constant_name,
        2);

    foo (unsafe(), // indeed
         2);

    foo(bar(1,3), 2);

    foo(2,1);
}


Class Definitions

This example is missing, or this feature isn't supported yet for TypeScript in Semgrep.

Conditionals

Pattern
if ($E)
   foo();
Matching code
function foo() {
    x = 1;
    if (x > 2)
        foo();
}


Function Call

Pattern
$F(1,2)
Matching code
function foo() {
    foo(1,2);

    return 1;
}


Function Definitions

Pattern
function $X(...) {
    ...
}
Matching code
function foo() {
    foo(1,2);
}

function bar(bar1:number,bar2:number,bar2:number) {
    foo(1,2);
    bar(1,2,3);
}

function foobar(bar1: number): number {
    foo();
}

Imports

Pattern
import $X from 'foo';
Matching code
import bar from 'foo';
let x = bar.func_call('a');

Object or Dictionary Key Value Pairs

Pattern
{..., $KEY: $VALUE, ...}
Matching code
function foo(): number {
    var config = {
        key: value,
        key2: value2,
        key3: value3,
    };

    var invoke = function(obj: {key: number, key2: string}) {
        return 2;
    };
}

Statement

Pattern
if ($X > $Y)
   $S;
Matching code
function foo() {
    v = 1;
    if (v > 2)
        return 1;
}


Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for TypeScript in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for TypeScript in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

Pattern
var $X = {"=~/[lL]ocation/": $Y};
Matching code
var x = {"Location": 1};

Strings

Pattern
$X = "=~//[lL]ocation.*/" 

Matching code
path = "/location/1";

Reoccurring Expressions

Expressions

Pattern
$X == $X
Matching code
function test_equal() {
    a = 1;
    b = 2;
    if (a+b == a+b)
        return 1;
    return 0;
}


Statement

Pattern
if ($E)
  $S;
else
  $S;

Matching code
function foo() {
    if (x > 2) {
        foo();
        bar();
    } else {
        foo();
        bar();
    }

    if (x > 2) {
        foo();
        bar();
    } else {
        foo();
    }
}




Variables

Pattern
$V = open();
close($V);

Matching code
function foo() {
    myfile = open();
    close(myfile);
}




Wildcard Matches (...)

Arguments

Pattern
foo(..., 5)
Matching code
function foo() {
    foo(1,2,3,4,5);
    foo(5);
}


Method Chaining

Pattern
$X = $O.foo(). ... .bar(). ...
Matching code
f = o.foo().m().h().bar().z();

f = o.foo().bar();

f = o.foo().m().h().z();

f = o.before().foo().m().h().bar().z();

Nested Statements

Pattern
if (...)
  ...

Matching code
function foo() {

    if (x == 1) 
        return 2;
}

Statements

Pattern
$V = get();
...
eval($V);

Matching code
function foo() {

    user_data = get();
    print("do stuff");
    foobar();
    eval(user_data);
}



Strings

Pattern
foo("...")
Matching code
function foo() {
    foo("whatever sequence of chars");
    foo('whatever sequence of chars');
}


XML

No pattern examples are available for this language yet.

YAML

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Exact Matches

Single Statements

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Argument

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Class Definitions

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Function Call

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Function Definitions

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Variables

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Wildcard Matches (...)

Arguments

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Method Chaining

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Nested Statements

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Statements

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for YAML in Semgrep.

Generic

Deep (Recursive) Matching

Deep Expression Operator

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Expression and Statement

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Exact Matches

Single Statements

Pattern
foo(1, 2)
Matching code
// Generic mode sees words, punctuation, and indented blocks.
class Foo {
  void foo() {
    foo(1,2);

    foo(1,
        2);  // whitespace is ignored.

    foo (1,  // comments aren't ignored :-(
         2);

    foo (1,
  2);  // nonsensical indentation.

    foo (1,
    2);  // acceptable indentation.

    foo(2,1);
  }
}

Helpful Features

Constant Propagation

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Import Renaming/Aliasing

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Named Placeholders ($X)

Annotations

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Argument

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Class Definitions

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Conditionals

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Function Call

Pattern
$F(1,2)
Matching code
class Foo {
  void foo() {
    foo(1,2);

    Bar.Baz(
     1,
     2
    );

    return 1;
  }
}

Function Definitions

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Imports

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Object or Dictionary Key Value Pairs

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Statement

Pattern
if ($X > $Y)
  ...;
Matching code
class Foo {
  void foo() {
    v = 1;

    if (v > 2)
      return 1;

    if (v > 2)
      x++;

    // If the pattern contains an indented block, it must match an indented
    // block in the program. Usually, indentation in the pattern is
    // best avoided.
    //
    if (v > 2) return 1;

    // Another tricky case that doesn't match. This is due to indentation
    // in the pattern after the closing parenthesis ')', but no indentation
    // in the program between ')' and '{'.
    //
    if (v > 2) {
      x++;
    }

    return 0;
  }
}

Typed Metavariable Field Access

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Typed Metavariables

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Regular Expressions '=~/regexp/'

Field Names

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Strings

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Reoccurring Expressions

Expressions

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Statement

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Variables

Pattern
$V = open();
...
close($V);
Matching code
class Foo {
  void foo() {
    myfile = open();
    try {
      foo(myfile);
    }
    finally {
      close(myfile);
    }
  }
}

Wildcard Matches (...)

Arguments

Pattern
foo(
  ... 5 ...
)
Matching code
class Foo {
  void bar() {
    foo(1,2,3,4,5);

    foo ( 5 );

    foo("5");  // this matches too.

    foo(5, ")");  // quoted strings are not understood.

    foo(5.5);  // '.' is generic punctuation.

    foo(55);  // '55' is a single word of the form [A-Za-z0-9_]+

    /*
       Matching nested parentheses requires an indented pattern:

       Do:
           foo(
             ...
           )

       Don't:

           foo(...)
    */

    foo(bar(baz(5)));

    foo(
      bar(
        baz(5)
      )
    );

    foo(
      5
      );  // strange indentation fails to match indentation in the pattern.

    // Dots ('...') can match at most 10 lines.
    // Use '... ...' to match up to 20 lines.
    foo(
     5,
     6,
     7,
     8,
     9,
     10,
     11,
     12,
     13,
     14,
     15,
     16,
     17
    );
  }
}

Method Chaining

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Nested Statements

This example is missing, or this feature isn't supported yet for Generic in Semgrep.

Statements

Pattern
$V = get();
...
eval($V);

Matching code
class Foo {
  void foo() {

    user_data = get();
    print("do stuff");
    foobar();
    eval(user_data);
  }
}


Strings

Pattern
foo(
  "..."
)
Matching code
class Foo {
  void bar () {
    foo("whatever sequence of chars");

    foo("abc", "def");  // generic mode doesn't understand strings.

    foo("hello \"world\"");

    foo("broken);
  }
}