Safeguard
Security Guides•

Should you write a custom static analysis rule or use an existing one?

Use an existing rule when one covers the bug class. Write a custom rule for internal APIs and house patterns only. A five question decision guide.

Safeguard Security Team
3 min read

Use an existing rule when one already covers the bug class, and write a custom rule only for something specific to your code, such as an internal API that is unsafe in certain uses. A custom rule is code you must test, document and maintain, so it should earn its place.

The cost nobody budgets

Writing the first version of a rule takes an afternoon. The cost is the following years: language upgrades, false positive reports, new developers asking why the build failed, and the day the author leaves. ESLint's own documentation makes a related point when it warns that building on core rules is fragile because they are not part of the public API. Anything you depend on can move.

Five questions

  1. Is the bug class common? Injection, unsafe deserialization and hard-coded secrets are covered by existing rulesets. Check there first.
  2. Is the pattern specific to your code? A wrapper function, an internal client library or a naming convention is invisible to generic rules. This is where custom rules shine.
  3. Can you state it in one sentence? "Never call legacyAuth outside the auth module" is a good rule. "Find insecure design" is not.
  4. Can you test it? If you cannot write both a flagged and a safe example, you cannot tell whether the rule works.
  5. Who will own it? If nobody can answer, do not ship it.

Decision table

SituationBetter choice
Well known weakness classExisting rule, tuned
Existing rule is noisy in your frameworkTune or scope it first, then consider custom
Banned internal functionCustom rule
Architectural boundary, such as no database calls from controllersCustom rule or a dependency boundary check
Needs data flow across servicesProbably beyond a simple rule
Business logic flawReview and tests, not a rule

Middle options

Before writing code, look at configuration. Many engines let you turn a rule on for some directories only, change its severity, or ban a function with a built-in restricted syntax setting. Those options carry far less maintenance than a rule you wrote.

A short pilot

  1. Write the rule for one pattern.
  2. Run it on the full repository in warning mode.
  3. Review every result, or a sample if there are many.
  4. If most results are real, keep it. If most are noise, rewrite or drop it.

OWASP's source code analysis page lists high false positive counts as a known weakness of static tools in general. A custom rule inherits that problem, so plan the feedback loop before the rule ships.

Quick check

Is a custom rule better than code review?

For a repeatable mechanical pattern, yes, because it never gets tired. For judgment calls about design or business logic, review is better.

How many custom rules is too many?

There is no magic number. A useful limit is the number your team can name an owner for and review at least once a quarter.

Safeguard's scanning covers dependencies and SBOMs, and its reachability analysis helps show which findings your code can actually call. You can run a free scan on a repository.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.