Generic SAST rulesets are good at generic bugs. What they cannot know is that your organization has a blessed way to build queries, a deprecated auth helper nobody should call, and an internal template function that must always escape its input. Those are the flaws that actually bite mature teams, and catching them means writing rules that encode your own conventions. Semgrep has become the common tool for this because its rules read almost like the code they match.

Semgrep matches patterns against the abstract syntax tree rather than raw text, so it understands code structure: whitespace, variable names and formatting do not matter, but the shape of an expression does. A rule is YAML: an id, the languages it applies to, a pattern to find, a message to show, and a severity. The pattern language mirrors the target language, which means you can often write a rule by pasting the bad code and generalizing the parts that vary.

The building blocks

Writing Custom Semgrep Rules for Your CodebaseDiagram with these labels, in reading order: architecture misuse; org-wide guardrail; Your code; AST; Pattern; metavariables; pattern-not; exclude safe usage; Custom rule; + test fixtures; Shared org; rule repo; Architecture-specific; misuse the generic; ruleset walks; straight past.architecture misuseorg-wide guardrailYour codeASTPatternmetavariablespattern-notexclude safe usageCustom rule+ test fixturesShared orgrule repoArchitecture-specificmisuse the genericruleset walksstraight past
A custom Semgrep rule: paste the bad code, generalise, exclude the safe cases, roll out.
  • Metavariables ($X) match any expression and bind it, so the same value can be referred to across the pattern. Think of a wildcard that remembers what it matched.
  • pattern-either matches any of several patterns; pattern-not excludes the safe cases so you don't flag correct usage.
  • pattern-inside scopes a match to a surrounding context, such as only inside a request handler or only within a certain class.
  • Taint mode (mode: taint) declares pattern-sources, pattern-sinks, and pattern-sanitizers, giving you full source-to-sink data-flow rules tuned to your own wrappers.
A rule banning a dangerous internal APIrules:
  - id: no-raw-query-builder
    pattern: db.rawQuery("..." + $INPUT)
    message: Use the parameterized queryBuilder; rawQuery with concatenation is SQLi-prone.
    languages: [java]
    severity: ERROR
Why it matters: The most valuable rules are the ones no vendor could ship, because they encode knowledge that exists only inside your team, your safe patterns, your banned APIs, your architectural invariants.

Rules worth writing first

Start where you already know the pain. If a past incident traced to a specific misuse, write the rule that would have caught it. That is among the highest-confidence rules you can write. Then encode your paved road: flag anyone bypassing the sanctioned auth check, the approved crypto wrapper, or the mandatory output encoder. These rules don't just find bugs; they enforce the architecture, keeping the safe path the default path as the codebase and the team both grow.

Test your rules like code

A rule that never fires is worse than useless: it manufactures false confidence. Semgrep supports test files that annotate which lines should and should not match (comments such as ruleid: and ok:, run with semgrep --test), and those tests belong in version control beside the rule. Write a fixture with both a vulnerable case and a safe case, and confirm the rule catches the first and stays silent on the second. When you tune the rule later, the tests tell you whether you fixed the noise or broke the detection.

From one repo to the whole org

Custom rules gain their power at scale. A rule proven in one service can roll out across every repository, turning a single team's hard-won lesson into an organization-wide guardrail. Keep them in a shared, reviewed rule repository, version them, and treat a new rule with the same care as a code change, because a noisy rule that floods the pipeline gets the whole ruleset ignored.

The generic scanner tells you what every codebase gets wrong. A custom rule tells you what yours gets wrong, and turns each discovered weakness into a permanent tripwire, so the same flaw is caught if it quietly reappears.

Back to the knowledge baseRequest an assessment