Pattern

AST and symbolic matching

any/all

FieldDescription
anyMatch when at least one condition is satisfied
allRequire every listed condition

any matches calls to either method_1 or method_2.

yaml
any:
  - pattern: method_1($$$)
  - pattern: method_2($$$)

With all, the method_1 call must be inside both the specified async callback and the for loop.

yaml
all:
  - pattern: method_1($$$)
  - inside:
      pattern: $F(async ($REQ, $RES) => {$$$})
  - inside:
      pattern: for($IN = $S; $$$; $$$) {$$$}

not

Use not to exclude a pattern.

yaml
pattern: $REQ.META
not:
  inside:
    any:
      - pattern: $REQ.META.CONTENT_LENGTH
      - pattern: $REQ.META.CONTENT_TYPE

regex

Use regex to match text against a regular expression.

This rule searches the string captured in STR for a polyfill.io URL.

yaml
pattern: |
  "$STR"
metavar:
  name: STR
  eq:
    regex: '(?:https?:)?//(?:[^/]*\.)?polyfill\.io'
javascript
async function loadPolyfill() {
  await import("https://polyfill.io/v3/polyfill.min.js"); // Matched
}

async function loadPolyfillWithQuote() {
  await import('https://polyfill.io/v3/polyfill.min.js'); // Matched
}

Inside

Use inside to limit where a pattern can match. This rule finds calls to target inside the someMethod method.

yaml
inside:
  pattern: public void someMethod($$$){$$$}
pattern: target($$$)
java
public class Example {
    public void someMethod(String arg) {
        target("something"); // Matched
    }
}

Metavariable

Metavariables start with $ and capture code that matches a pattern.

Metavariable type Description
$VAR Capture one syntax tree (AST) node as VAR.
$$$VARS Capture a sequence of nodes as VARS.
$$$ Match a sequence of nodes, even across lines, without naming it.

Here the two $$$ placeholders match the parameter list and body of someMethod.

yaml
pattern: |
  public void someMethod($$$) {$$$}

This rule matches when the third argument, captured as NOVERIFY, is a string literal or true.

yaml
pattern: $JWT.decode($TOKEN, $SECRET, $NOVERIFY, $$$)
metavar:
  name: NOVERIFY
  eq:
    any:
      - pattern: |
          "$$$_STR"
      - pattern: "true"

matcher

Use matcher to recognize names associated with sensitive data, such as passwords and tokens. These built-in categories let multiple rules share the same name checks.

  • password
  • secret
  • pii
  • sensitive

This example checks whether the name captured as FIELD belongs to the secret category.

yaml
metavar:
  name: FIELD
  eq:
    matcher: secret

matcher strips quotes from names and keys and splits camelCase, snake_case, kebab-case, dotted names, and numeric suffixes into tokens. It checks names and keys only. Whole expressions and literal personal data such as email addresses and phone numbers are outside its scope.

It can also be used in regex_pair.left.

yaml
regex_pair:
  left:
    matcher: secret
  right: "[0-9a-zA-Z\\-_.=\\~@]{10,150}"

constantValue

In JavaScript, TypeScript, and TSX rules, use constantValue to apply a nested rule to a local variable’s initializer.

yaml
constantValue:
  matches: unsafe-origin-value

You can use the same form under metavar. For JavaScript, TypeScript, and TSX, matching works as follows.

  • Apply the nested rule directly to the current node first.
  • If the node is an identifier, find its declaration in the nearest enclosing scope and check its initializer.
  • const (including typed declarations) and initialized let and var are supported. Reassigning a let or var in the same scope rules it out.
  • Supported values include string, number, Boolean, and regex literals, new RegExp(...) with literal arguments, and arrays or objects made up of supported constant expressions. The check can also follow a bounded chain of aliases.

This rule checks the same regex pattern whether a CORS origin is written inline or supplied through a local constant.

yaml
utils:
  unsafe-origin-value:
    any:
      - kind: regex
        regex: "[a-zA-Z0-9\\-]\\."
      - all:
          - pattern: new RegExp($PATTERN, $$$)
          - has:
              kind: string
              regex: "[a-zA-Z0-9\\-]\\."

rule:
  pattern: "cors({ origin: $ORIGIN, $$$ })"
  metavar:
    name: ORIGIN
    eq:
      constantValue:
        matches: unsafe-origin-value

The initializer of originPattern matches the rule’s regex condition.

javascript
const originPattern = /example.test/;
cors({ origin: originPattern });

After reassignment, constantValue can no longer use the original value.

javascript
let originPattern = /example.test/;
originPattern = safeOrigin;
cors({ origin: originPattern });

Use resolvedName to check the package identity of an imported or required function/object. Use constantValue alongside it when checking a local constant initializer used as an argument or configuration value.

compare

Use compare to test Boolean or numeric metavariable values. Numbers can be hexadecimal (0x), binary (0b), or octal (0o).

This rule checks whether the file permissions captured in BIT fall within the specified ranges.

yaml
any:
  - pattern: os.chmod($FILE, $BIT, $$$)
  - pattern: os.fchmod($FILE, $BIT, $$$)
  - pattern: os.lchmod($FILE, $BIT, $$$)
compare: ( $BIT >= 0o650 && $BIT < 0o100000 ) || $BIT >= 0o100650