semgrep-rule-creator

Generate custom Semgrep YAML rules for security vulnerability detection.

Updated Apr 5, 2026
One-click install
npx skills add https://github.com/balic-AI-ML-R-D-Resources/eliza_autonomous_agents --skill semgrep-rule-creator-balic-ai-ml-r-d-resources
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: semgrep-rule-creator
Source: https://github.com/balic-AI-ML-R-D-Resources/eliza_autonomous_agents/tree/main/packages/skills/skills/semgrep-rule-creator
Command: npx skills add https://github.com/balic-AI-ML-R-D-Resources/eliza_autonomous_agents --skill semgrep-rule-creator-balic-ai-ml-r-d-resources

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Create custom Semgrep rules to detect security vulnerabilities and coding patterns, enabling teams to enforce consistent security checks across codebases.

Core Features & Use Cases

  • Rule Template
  • Taint Mode Template
  • Testing Rules
  • Best Practices
  • Resources

Rule Template

rules:
  - id: <org>-<category>-<specific-issue>
    languages: [<language>]
    message: >
      <Clear description of the issue and remediation>
    severity: ERROR  # ERROR | WARNING | INFO
    metadata:
      cwe: "CWE-XXX: Description"
      confidence: HIGH  # HIGH | MEDIUM | LOW
      category: security
    patterns:
      - pattern: <vulnerable-code-pattern>
      - pattern-not: <safe-variant-to-exclude>

Pattern Operators | Operator | Use Case | |----------|----------| | pattern | Match exact code structure | | patterns | All must match (AND) | | pattern-either | Any matches (OR) | | pattern-not | Exclude matches | | pattern-inside | Only match within context | | pattern-not-inside | Only match outside context | | pattern-regex | Regex on source text | | metavariable-regex | Regex on captured variable | | metavariable-comparison | Numeric/string comparison |

Taint Mode Template

rules:
  - id: <taint-rule-id>
    languages: [<language>]
    message: "Untrusted data flows to dangerous sink"
    severity: ERROR
    mode: taint
    pattern-sources:
      - pattern: <source-of-untrusted-data>
    pattern-sinks:
      - pattern: <dangerous-function-call>
    pattern-sanitizers:
      - pattern: <function-that-makes-input-safe>

Testing Rules

Create test files with annotations:

def test_vulnerable():
    # ruleid: my-rule-id
    dangerous_call(user_input)

def test_safe():
    # ok: my-rule-id
    safe_call(sanitized_input)
semgrep --test rules/

Best Practices

  1. Use specific, descriptive rule IDs with org prefix
  2. Include CWE and confidence metadata
  3. Write clear remediation guidance in the message
  4. Always create test cases for true positives AND false positives
  5. Use pattern-not to reduce false positives
  6. Prefer pattern-inside to limit scope rather than overly complex patterns
  7. Test against real codebases before deploying to CI

Resources

  • Rule Syntax — https://semgrep.dev/docs/writing-rules/rule-syntax/
  • Playground — https://semgrep.dev/playground
  • Rule Registry — https://semgrep.dev/explore

Quick Start

Use Semgrep Rule Creator to generate a new YAML rule set for your project by editing the rules template and running semgrep --test to validate the rule against sample code.

Frequently Asked Questions about semgrep-rule-creator

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write a custom Semgrep rule to detect security vulnerabilities?

To write a custom Semgrep rule for security vulnerabilities, you create a YAML file defining the rule id, target languages, severity, vulnerability metadata, and code patterns to match. Use the pattern and pattern-not operators to accurately identify vulnerable code structures.

How does taint tracking work in Semgrep YAML rules?

Taint tracking in Semgrep YAML rules works by defining pattern-sources for untrusted data, pattern-sinks for dangerous functions, and pattern-sanitizers for safe functions. The rule triggers when untrusted data flows from a source to a sink without passing through a sanitizer.

How do I test Semgrep rules to ensure they detect code patterns accurately?

To test Semgrep rules, create test files containing vulnerable code annotated with ruleid comments and safe code annotated with ok comments. Run the semgrep --test command against your rules directory to validate that the rules correctly identify true positives and avoid false positives.

What is the best way to reduce false positives in static analysis rules?

The best way to reduce false positives in static analysis rules is to use the pattern-not operator to exclude safe code variants and the pattern-inside operator to limit matching scope to specific contexts. Always create test cases for both true and false positives to validate accuracy.

Can I port logic from other static analysis tools to Semgrep?

Yes, you can port logic from other static analysis tools to Semgrep by translating existing detection logic into Semgrep YAML rule syntax. Utilize pattern operators, metavariable-regex, and taint-mode templates to replicate complex detection rules across multiple codebases.

When should I use pattern-either versus patterns in Semgrep rule syntax?

Use the patterns operator when all specified code patterns must match simultaneously, acting as a logical AND. Use pattern-either when any single pattern match should trigger the rule, functioning as a logical OR for detecting multiple vulnerable code variants.