ast-grep security rules: a curated pack and its honest limits

In September 2026, scanning the nginx echo module with our ast-grep security rules took 0.83 seconds and produced 31 findings across 4,661 lines of C. Twenty-three of those 31 came from one rule, nginx-unchecked-palloc, and that rule cannot tell you whether a single one of them is a bug. It was built that way on purpose, and it’s the most useful thing in the pack.

In short: this is a curated pack of ast-grep rules that we run across every C, Go, PHP, Python and Lua tree we build. You get what a structural match can and can’t prove, why most rules ship as warnings, the config traps that silently skip files, and the fixture discipline that keeps each rule honest.

The pack is public at myguard-labs/ast-grep-essentials, and it is the least glamorous piece of tooling we own. It grows fast: when this post first went out in September it held 469 rule files with 938 fixtures. On 27 November 2026 it held 2,573 rule files, each with a mirrored fixture: 2,479 active rules plus 94 deprecated aliases. It still covers 18 languages.

What does ast-grep actually match?

ast-grep parses source with tree-sitter and matches patterns against the syntax tree instead of the bytes. You write $L || $R and it matches a binary expression, not a string containing a pipe character.

Comments don’t match. String literals don’t match. A line that someone broke across two lines still matches, where grep would miss it, because the tree doesn’t care about your newlines.

That last point is the whole pitch. Here’s a real rule from the pack:

rule:
  kind: binary_expression
  all:
    - pattern: $L || $R
    - any:
        - has: {field: right, pattern: $A && $B}
        - has: {field: left, pattern: $A && $B}

That’s c-and-or-mixed-without-parens. C binds && tighter than ||, so a || b && c means a || (b && c). That is often not the grouping the author read off the page. A guard chain meant to reject unless every clause holds instead accepts on the first disjunct alone.

Try writing that check in grep. You can’t, because the finding is a tree relationship: an && node sitting as a direct child of a || node. Wrap either operand in parentheses and it becomes a parenthesized_expression. It is no longer a direct child, and the finding clears without changing a single instruction the compiler emits.

That is also the rule’s honest limitation, and it says so in its own note field: it cannot know which grouping you meant. The finding is not “your precedence is wrong”. The finding is “your code doesn’t say which precedence you wanted, and the next person to read it will guess”.

Why are most rules only warnings?

In September the severity split was 85 error, 370 warning, 13 info and one off. On 27 November 2026 the 2,479 active rules split 101 error, 2,359 warning and 19 info. All 94 deprecated aliases are off.

If you’ve used a linter that ships everything at error and then asks you to suppress the noise, that ratio probably looks cowardly. It’s the honest answer to a question most static analysis tools dodge: does a match prove a bug?

Usually not. Take c-memcmp-on-secret:

rule:
  kind: call_expression
  all:
    - has:
        field: function
        regex: '^(memcmp|strcmp|strncmp|ngx_memcmp|ngx_strcmp|ngx_strncmp)$'
    - has:
        field: arguments
        has:
          stopBy: end
          kind: identifier
          regex: '(?i)(secret|token|key|hmac|mac|password|passwd|digest|signature|sig|hash|nonce)'

memcmp stops at the first differing byte. How long it takes leaks the length of the common prefix the attacker supplied. That is enough to recover a forged authenticator one byte at a time. Real bug class, real CVEs, thoroughly documented.

And the detection is an identifier-name regex, nothing more. It sees hash in a hash-table lookup and reports it. It reports a config string compared against a variable somebody called key. It misses key material held in a buffer named buf entirely.

Every rule in the pack carries a note: field that states its limits in plain language. The alternative is a reviewer rediscovering the limitation from scratch after twenty minutes on a hash-table lookup.

That note is what makes the finding actionable. “Variable-time comparison on possible key material, gate is a name regex, dismiss if the compared value isn’t an authenticator” is a thing a junior triages in ten seconds. A bare SEVERITY: HIGH with no stated gate is a thing they escalate to you at 3 a.m., and they’re right to.

ast-grep rule pack as of September 2026: 469 rule files, 938 fixtures, 18 languages, 31 nginx-internals rules, and a severity split of 370 warnings, 85 errors and 13 info
The pack as it stood in September 2026. The counts have grown since (see the text); the warning-heavy split has not changed.

The 23 findings that are deliberately not findings

Back to nginx-unchecked-palloc, the rule that fired 23 times on one small module. Here it is, entire:

rule:
  kind: call_expression
  has:
    field: function
    regex: ^(ngx_palloc|ngx_pnalloc|ngx_pcalloc)$

It matches every pool allocation. All of them. Its severity is info, and its note opens with “Candidate only”: the rule flags every pool-alloc call and “does NOT prove the guard is missing”.

We tried to write the real rule first. ngx_palloc returns NULL under memory pressure, and dereferencing without if (p == NULL) return NGX_ERROR; is a crash you’ll meet on the worst possible day.

But the guard comes in many shapes: a declaration with an initialiser, an assignment followed by an if, or a goto. It can be a check three statements later, after two unrelated assignments, or an early return with NGX_HTTP_INTERNAL_SERVER_ERROR. Each variant we encoded made the rule wrong in the other direction. We shipped the enumerator instead.

Twenty-three candidates in a 4,661-line module is a ten-minute review. It’s a review that actually happens, because nobody is arguing with the tool about whether it’s right. Compare the version of this rule that tries to be clever. It misses the goto form, so you trust it, so the one real NULL deref in the tree never gets looked at.

People do disagree with this position: a static analysis rule that can’t prove a bug should still ship, provided it says so out loud. Common practice is the opposite. Suppress anything under some confidence threshold, keep the signal-to-noise ratio pretty for the dashboard, and quietly ship the NULL deref. Give me 23 honest candidates over one confident rule that skips the guard three statements down.

How the deprecated-alias invariant broke

A rule ID is a contract. Once it’s in someone’s CI config, renaming it breaks their build, so renamed rules keep a deprecated alias pointing at the replacement.

Those aliases only work while the alias’s matcher stays identical to its target’s. We maintained that by hand, and it broke exactly the way hand-maintained invariants break. Enriching c-string-sizeof-includes-nul to also catch the pointer form left nginx-string-sizeof-includes-nul on the old single-pattern matcher.

The alias still existed. It still ran. It just stopped catching half of what its target caught, and nothing anywhere said so.

The fix (commit 02b2f82, 23 September) removes the remembering. An alias declares metadata.deprecated_alias_of, and ci/sync_deprecated_aliases.py rebuilds its matcher from the target. It preserves only the fields an alias legitimately owns. --check fails on drift.

The part worth stealing: the check runs from the trusted base checkout, not from the PR’s tree. A pull request that edits the sync script cannot disable the gate validating that same pull request. Five-minute change in the workflow file. It’s the difference between a gate and a suggestion, and most repositories I’ve read have the suggestion.

Running the scan without stepping on the config traps

npm ci
npx ast-grep scan -c sgconfig.yml /path/to/source

On 27 November 2026 the engine was pinned at @ast-grep/cli 0.51.3 in the pack’s package.json; in September it was 0.45.3. Pin yours too. Rule semantics drift when tree-sitter grammars change under you. An unpinned engine means a CI verdict that flips because somebody else shipped a release on a Tuesday.

If you consume the pack itself, the same logic applies one level up. The README asks consumers who need a reviewed pack to pin a commit carrying a signed reviewed-YYYYMMDD-N tag, not an arbitrary main commit.

Two config details cost us time. First, sgconfig.yml lists every native language directory explicitly rather than pointing at the parent rules/, because rules/powershell/ needs a custom parser configured separately in sgconfig.powershell.yml. Importing the parent tries to load those rules with a parser that isn’t there. The pack README adds that with ast-grep 0.50.0 the PowerShell config needs --custom-languages allow on each invocation, or ast-grep ignores the native parser.

Second, PHP needs the languageGlobs mapping copied across too, or .phtml files go unparsed:

languageGlobs:
  php:
    - '*.php'
    - '*.phtml'

Unparsed and silent. The scan comes back clean on a directory where half the templates were never read. That is worse than an error, because a clean scan is something you act on.

The languages are Bash, C, C++, C#, Go, HTML, Java, JavaScript, Kotlin, Lua, PHP, Python, Ruby, Rust, Scala, Swift and TypeScript, plus PowerShell through that separate parser config. C has the most active rules (888 on 27 November 2026), then PHP (769) and Python (203). The pinned release has no built-in Perl parser, so the pack doesn’t pretend to cover Perl.

The nginx-internals rules

The nginx-internals rules are the ones we wrote rather than curated. There were 31 in September; on 27 November 2026, 45 active rule IDs start with nginx-. They encode conventions no general C linter knows about.

  • nginx-str-data-passed-to-libc: an ngx_str_t .data pointer isn’t NUL-terminated, so handing it to strlen reads off the end of your buffer into whatever the pool allocated next.
  • nginx-table-missing-sentinel.
  • nginx-use-after-finalize.
  • nginx-conf-return-code-confusion, which catches the config handler returning NGX_ERROR where the caller expects a char *.

nginx-finalize-plus-return-rc is my favourite. Call ngx_http_finalize_request() and then return a status code, and you’ve double-finalised the request. The symptom is a worker that dies under load with nothing useful in the error log.

You find it once by reading a core dump at an hour you’d rather not discuss. After that, the scan finds it every time. If you’re writing a module from scratch, the same class of mistake is why we keep a skeleton nginx module with the boilerplate already correct and a test harness that exercises it under load.

184 rules are copied from CodeRabbit’s ast-grep essentials at commit 73120109. Each records its source and Apache-2.0 notice in its leading comments, and the upstream licence stays under LICENSES/. We didn’t write those and don’t claim them. The pack README records that upstream project as archived, so those rules are carried forward in this pack.

Fixtures, or the rule doesn’t ship

Every rule has a mirrored fixture under tests/<language>/<category>/<id>.yml. Here is an excerpt from the fixture for c-memcmp-on-secret:

valid:
  - 'int f(const unsigned char *hmac, const unsigned char *e, size_t n) { return CRYPTO_memcmp(hmac, e, n) == 0; }'
  - 'void f(void) { /* memcmp(hmac, expected, n); */ }'
  - 'const char *s = "memcmp(hmac, expected, n)";'
invalid:
  - 'int f(const unsigned char *hmac, const unsigned char *e, size_t n) { return memcmp(hmac, e, n) == 0; }'

The first valid entry is the corrected form, using a constant-time compare. Look at the third. It’s a string literal containing what looks exactly like a match. The second is the same code sitting in a comment.

Neither may match. Both are in the fixture because a grep-shaped implementation of this rule fires on both. A future edit that quietly makes the rule grep-shaped again needs to fail loudly rather than pass.

Near misses are the point of the fixture. Positive examples prove the rule fires; only the near misses prove it fires for the right reason. A fixture with four positives and no lookalikes tells you nothing you didn’t already know while writing the pattern. It’s the same discipline that keeps a YARA corpus honest: without a negative control you’re measuring your own optimism.

CI validates only changed rules. Each is copied into an isolated config alongside its fixture, so one rule cannot pass because another language happened to load cleanly. For that check, CI installs the latest ast-grep from npm, not the pinned engine used for scanning.

Pinning gives reproducible verdicts. Testing on latest tells us a grammar change is coming before it lands on everyone. A second job loads every non-PowerShell rule on ast-grep 0.45.2, the lowest consumer version the pack supports, so a rule that older engines cannot load fails CI.

Authoring, enrichment and scheduling live in a separate private harness repository. It is part of a larger workflow that keeps rule creation reviewable. The public pack is rules, fixtures, config and licences. Those are the parts anyone consuming the rules actually needs, and a rule that only works inside our tooling isn’t a rule we should be shipping.

Where ast-grep rules don’t help at all

There’s no dataflow here, and none is coming. A rule sees one syntax tree and matches shapes in it. If your bug is “this attacker-controlled value reaches that sink four functions away”, ast-grep is the wrong tool: you want CodeQL, or Semgrep with taint mode. Runtime request inspection is a different layer again, which is what a WAF in front of nginx is for.

What it’s genuinely good at is the local, visible, known bug: the precedence trap, the wrong sizeof operand, the off-by-one in strncat, the getenv result going straight into a %s. Those ship constantly, in code written by people who know better, at 17:40 on a Friday. The echo module scan took 0.83 seconds in September. That is cheap enough to run on every save, and cheap enough that nobody negotiates about whether to run it.

The pack is under the MyGuard Internal Use License 1.0. It allows internal use, including commercial use, and forbids distribution to third parties outside GitHub, with a carve-out for forks and branches used to prepare pull requests.

Anyway. Go count how many ngx_palloc calls in your own module have a NULL check under them. I’ll wait.

Related reading