-->, 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.
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:- Is there any user input?
- Do any functions manually build an SQL string using function input?
- Can the user input reach the function that manually builds the SQL string?
on: conditions, in order, read as follows:
- Recursively generate a pseudo callgraph on
$CALLERto$CALLEE. - Match when a method with user input has a
$SINKthat is the$CALLERin the pseudo-callgraph. - Match when the
$CALLEEis the$METHODNAMEof a method that uses a parameter to construct an SQL string.
The join conditions select rows which meet the conditions.
- Match when a method with user input has a CALLER in the pseudo-callgraph.
- Match when 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