Skip to main content

Custom rules for secure guardrails

You can create custom Semgrep rules and deploy them as guardrails to enforce your organization's secure coding conventions.

Prerequisites

General steps

  1. Create a custom Semgrep rule.
  2. Verify and test that the rule matches the code you want to detect.
  3. Optional: Set the custom Semgrep rule as a secure default.
  4. Deploy the rule as a guardrail in the following developer interfaces: IDE, PR or MR comments, or pre-commit.

The following table lists the relevant documentation for each step:

StepsReferences and notes
Create a custom ruleIn addition to the required fields of a Semgrep rule, the following metadata fields are useful:
  • category
  • confidence
  • likelihood
  • impact
  • subcategory
Filling out confidence and impact in particular is useful for filtering rules within the Semgrep web app.

Read the metadata reference documentation.
Verify that the rule matches as intended
Optional: Set the rule as a secure defaultWhen creating a custom secure default, you must use category: security and subcategory: secure default values in your rule (see Secure default snippet).
Deploy the rule as a guardrailFor PR or MR comments:

For IDEs: Require developers to install the Semgrep extension for their IDE.

For pre-commit: Install and configure Semgrep for pre-commit.

Secure default snippet

When creating a custom secure default, you must use category: security and subcategory: secure default values in your rule:

rules:
- id: some-custom-default
...
metadata:
category: security
subcategory:
- secure default
...

Not finding what you need in this doc? Ask questions in our Community Slack group, or see Support for other ways to get help.