Skip to main content
Join mode is an extension of Semgrep that runs multiple rules at once and only returns results if certain conditions are met. This is an experimental mode that enables you to cross file boundaries, allowing you to write rules for whole codebases instead of individual files. More information is available in Join mode overview. Recursive join mode has a recursive operator, -->, which executes a recursive query on the given condition. This recursive operator allows you to write a Semgrep rule that effectively crawls the codebase on a condition you specify, letting you build chains such as function call chains or class inheritance chains.

Understanding recursive join mode

In the background, join rules turn captured metavariables into database table columns. For example, a rule with $FUNCTIONNAME, $FUNCTIONCALLED, and $PARAMETER is a table similar to the following: The join conditions then join various tables together and return a result if any rows match the criteria. Recursive join mode conditions use recursive joins to construct a table that recursively joins with itself. For example, you can use a Semgrep rule that gets all function calls and join them recursively to approximate a callgraph. Consider the following Python script and rule.
A join condition such as the following: python-callgraph.$CALLER --> python-callgraph.$CALLEE produces a table below. Notice how function_1 appears with function_4 and function_5 as callees, even though it is not directly called.

Example rule

It’s important to think of a join mode rule as β€œasking questions about the whole project”, rather than looking for a single pattern. For example, to find an SQL injection, you need to understand a few things about the project:
  1. Is there any user input?
  2. Do any functions manually build an SQL string using function input?
  3. Can the user input reach the function that manually builds the SQL string?
Now, you can write individual Semgrep rules that gather information about each of these questions. This example uses Vulnado for finding an SQL injection. Vulnado is a Spring application. The first rule searches for user input into the Spring application. This rule also captures sinks that use a user-inputtable parameter as an argument.
A second rule looks for all methods in the application that build an SQL string with a method parameter.
Finally, the third rule is used to construct a pseudo-callgraph:
The join rule, is displayed as follows:
The on: conditions, in order, read as follows:
  • Recursively generate a pseudo callgraph on $CALLER to $CALLEE.
  • Match when a method with user input has a $SINK that is the $CALLER in the pseudo-callgraph.
  • Match when the $CALLEE is the $METHODNAME of a method that uses a parameter to construct an SQL string.
Running this on Vulnado produces tables that look like this: The join conditions select rows which meet the conditions.
  • Match when a method with user input has a SINKthatistheSINK that is the CALLER in the pseudo-callgraph.
  • Match when the CALLEEistheCALLEE is the METHODNAME of a method that uses a parameter to construct an SQL string.

Limitations

Join mode only works on the metavariable contents, which means it’s fundamentally operating with text strings and not code constructs. There will be some false positives if similarly-named metavariables are extracted.

Use cases

  • Approximating callgraphs in a project
  • Approximating class inheritance