Skip to main content
Port existing Semgrep rules to new target languages with proper applicability analysis and independent test-driven validation for each language variant.

Overview

The Semgrep Rule Variant Creator plugin takes an existing Semgrep rule and one or more target languages, then generates independent rule variants for each applicable language. Each variant goes through a complete 4-phase cycle ensuring quality and correctness. Key capabilities:
  • Applicability analysis before porting
  • Independent 4-phase cycle per language
  • Test-first methodology for each variant
  • Language-specific idiom adaptation
  • Proper validation before proceeding

Installation

Prerequisites

  • Semgrep installed and available in PATH
  • Existing Semgrep rule to port (in YAML format)
  • Target languages specified

When to Use

Use this plugin when you need to:
  • Port an existing Semgrep rule to one or more target languages
  • Create language-specific variants of a universal vulnerability pattern
  • Expand rule coverage across a polyglot codebase
  • Translate rules between languages with equivalent constructs

When NOT to Use

Do NOT use this plugin for:
  • Creating a new Semgrep rule from scratch (use semgrep-rule-creator instead)
  • Running existing rules against code
  • Languages where the vulnerability pattern fundamentally doesn’t apply
  • Minor syntax variations within the same language

Input Specification

This plugin requires:
  1. Existing Semgrep rule - YAML file path or YAML rule content
  2. Target languages - One or more languages to port to
Example invocations:

Output Structure

For each applicable target language, produces:

Example Output

Input:
  • Rule: python-command-injection.yaml
  • Target languages: Go, Java
Output:

Four-Phase Workflow

Each target language goes through an independent 4-phase cycle:
1

Phase 1: Applicability Analysis

Determine if the vulnerability pattern applies to the target language before proceeding.Analysis criteria:
  • Does the vulnerability class exist in the target language?
  • Does an equivalent construct exist (function, pattern, library)?
  • Are the semantics similar enough for meaningful detection?
Verdict options:
  • APPLICABLE → Proceed with variant creation
  • APPLICABLE_WITH_ADAPTATION → Proceed but significant changes needed
  • NOT_APPLICABLE → Skip this language, document why
2

Phase 2: Test Creation

Write tests BEFORE the rule using target language idioms.Create test file with:
  • Minimum 2 vulnerable cases (ruleid:)
  • Minimum 2 safe cases (ok:)
  • Language-specific edge cases
3

Phase 3: Rule Creation

Translate the original rule to the target language.
  1. Analyze AST: semgrep --dump-ast -l <lang> test-file
  2. Translate patterns to target language syntax
  3. Update metadata: language key, message, rule ID
  4. Adapt for idioms: Handle language-specific constructs
4

Phase 4: Validation

Validate YAML and run tests to ensure correctness.
Checkpoint: Output MUST show All tests passed.For taint rule debugging:
Complete the full 4-phase cycle for each language before moving to the next. Do not batch languages together.

Applicability Analysis Details

Before porting, perform thorough analysis:

Does the Vulnerability Class Exist?

Examples:
  • Buffer overflow: Applies to C/C++, may apply to Rust (in unsafe blocks), does NOT apply to Python/Java
  • SQL injection: Applies to any language with database access
  • XSS: Applies to any language generating HTML output

Does an Equivalent Construct Exist?

Parse the original rule to identify:
  • Sinks: What dangerous functions/methods does it detect?
  • Sources: Where does tainted data originate?
  • Pattern type: Is it taint-mode or pattern-matching?
Then research the target language:
  • What are the equivalent dangerous functions?
  • What are the common source patterns?
  • Are there language-specific idioms to consider?

Example Analysis

Example Variant Creation

Let’s port a Python SQL injection rule to Go:

Key Differences from Rule Creator

Rationalizations to Reject

When porting Semgrep rules, reject these common shortcuts:

Strictness Principles

Non-negotiable requirements:
  • Applicability analysis is mandatory: Don’t assume patterns translate
  • Each language is independent: Complete full cycle before moving to next
  • Test-first for each variant: Never write a rule without test cases
  • 100% test pass required: “Most tests pass” is not acceptable

Commands

Foundational Knowledge

The semgrep-rule-creator plugin is the authoritative reference for Semgrep rule creation fundamentals. Consult it for guidance on:
  • When to use taint mode vs pattern matching
  • Test-first methodology
  • Anti-patterns to avoid
  • Iterating until tests pass
  • Rule optimization
When porting a rule, you’re applying these same principles in a new language context.
  • semgrep-rule-creator - Create new Semgrep rules from scratch
  • static-analysis - Run existing Semgrep rules against code
  • variant-analysis - Find similar vulnerabilities across codebases

Additional Resources

Author

Maciej Domanski (opensource@trailofbits.com)