Back to Security & Vulnerability Analysis

semgrep-rule-variant-creator

Semgrepstatic analysiscode securityrule generationlanguage portingAI assistantcode qualitydeveloper tools
⭐ 5.7kπŸ“„ CC-BY-SA-4.0πŸ•’ 2026-06-15Source β†—

Install this skill

npx skills add trailofbits/skills

Works across Claude Code, Cursor, Codex, Copilot & Antigravity

The semgrep-rule-variant-creator skill automates the expansion of security detection logic by porting existing Semgrep rules across different programming languages. Rather than simply translating syntax, this workflow enforces a strict four-phase methodology to ensure that ported rules account for language-specific AST structures, data-flow idioms, and distinct library behaviors. By requiring an initial applicability analysis followed by a mandatory test-first development loop, the skill ensures each variant remains accurate and functional. It prevents the common pitfall of assuming that vulnerability patterns translate identically across different runtime environments. Users receive a structured output directory for each target language, containing both the refined YAML rule and the necessary test files to validate the detection mechanism against language-specific edge cases.

When to Use This Skill

  • β€’Expanding an existing SQL injection rule to support both Java and Go
  • β€’Translating web framework security rules from Python to Ruby
  • β€’Adapting internal compliance rules to support polyglot microservice environments
  • β€’Creating language-specific variants for universal vulnerability patterns

How to Invoke This Skill

Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:

  • β€œPort this Semgrep rule to support Java
  • β€œCreate a variant of this rule for Golang
  • β€œAdapt my existing rule for another target language
  • β€œCheck if this pattern applies to a different runtime
  • β€œGenerate a ported rule variant with test cases

Pro Tips

  • πŸ’‘Before porting, thoroughly assess if the vulnerability pattern or coding standard is truly applicable and has equivalent constructs in the target language.
  • πŸ’‘Always review the generated test cases and expand them with language-specific edge cases to ensure robust validation of the new rule variant.
  • πŸ’‘Combine this skill with `semgrep-rule-creator` when you need to build a new rule from scratch before generating its language-specific variants.
  • πŸ’‘Provide clear and concise rule YAML content or a precise file path to ensure the skill accurately parses the original rule.

What this skill does

  • β€’Performs deep AST-based applicability analysis for target languages
  • β€’Generates language-specific YAML rule files from source patterns
  • β€’Forces test-first development with required vulnerable and safe test cases
  • β€’Produces organized output structures for individual rule variants
  • β€’Validates API semantic equivalence across different platform ecosystems

When not to use it

  • βœ•Authoring original security rules from the ground up
  • βœ•Running active scans against production codebases
  • βœ•Addressing simple syntax changes within a single language context

Example workflow

  1. Analyze target language vulnerability vectors via applicability check
  2. Draft test files containing explicit vulnerable and safe patterns
  3. Construct the Semgrep YAML rule targeting the new language AST
  4. Execute tests to verify detection accuracy
  5. Debug and refine rule patterns until all test cases pass

Prerequisites

  • –An existing functional Semgrep rule
  • –Target language specifications
  • –Basic knowledge of Semgrep AST patterns

Pitfalls & limitations

  • !Assuming identical data-flow behavior across different languages
  • !Skipping the mandatory test-first cycle for specific variants
  • !Overlooking language-specific standard library differences during porting

FAQ

Why can't I just translate the syntax directly?
Semgrep rules rely on AST structures which vary wildly between languages; 1:1 translation often misses the specific ways languages handle data flow.
What happens if a rule is not applicable to a target language?
The workflow requires you to document why it is not applicable, preventing the creation of noisy or broken rules.
Do I need to run tests for every variant?
Yes, every variant must go through a full test-first cycle to ensure that the detection logic remains accurate within that specific language's context.

How it compares

While manual porting often ignores language-specific edge cases, this skill enforces a rigorous test-first validation loop that ensures portability without sacrificing detection precision.

Source & trust

⭐ 5.7k starsπŸ“„ CC-BY-SA-4.0πŸ•’ Updated 2026-06-15
πŸ“„ Full skill instructions β€” original source: trailofbits/skills
# Semgrep Rule Variant Creator

Port existing Semgrep rules to new target languages with proper applicability analysis and test-driven validation.

## When to Use

**Ideal scenarios:**
- Porting an existing Semgrep rule to one or more target languages
- Creating language-specific variants of a universal vulnerability pattern
- Expanding rule coverage across a polyglot codebase
- Translating rules between languages with equivalent constructs

## When NOT to Use

Do NOT use this skill 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 skill requires:
1. **Existing Semgrep rule** - YAML file path or YAML rule content
2. **Target languages** - One or more languages to port to (e.g., "Golang and Java")

## Output Specification

For each applicable target language, produces:
<original-rule-id>-<language>/
β”œβ”€β”€ <original-rule-id>-<language>.yaml # Ported Semgrep rule
└── <original-rule-id>-<language>.<ext> # Test file with annotations


Example output for porting sql-injection to Go and Java:
sql-injection-golang/
β”œβ”€β”€ sql-injection-golang.yaml
└── sql-injection-golang.go

sql-injection-java/
β”œβ”€β”€ sql-injection-java.yaml
└── sql-injection-java.java


## Rationalizations to Reject

When porting Semgrep rules, reject these common shortcuts:

| Rationalization | Why It Fails | Correct Approach |
|-----------------|--------------|------------------|
| "Pattern structure is identical" | Different ASTs across languages | Always dump AST for target language |
| "Same vulnerability, same detection" | Data flow differs between languages | Analyze target language idioms |
| "Rule doesn't need tests since original worked" | Language edge cases differ | Write NEW test cases for target |
| "Skip applicability - it obviously applies" | Some patterns are language-specific | Complete applicability analysis first |
| "I'll create all variants then test" | Errors compound, hard to debug | Complete full cycle per language |
| "Library equivalent is close enough" | Surface similarity hides differences | Verify API semantics match |
| "Just translate the syntax 1:1" | Languages have different idioms | Research target language patterns |

## Strictness Level

This workflow is **strict** - do not skip steps:
- **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

## Overview

This skill guides the creation of language-specific variants of existing Semgrep rules. Each target language goes through an independent 4-phase cycle:

FOR EACH target language:
Phase 1: Applicability Analysis β†’ Verdict
Phase 2: Test Creation (Test-First)
Phase 3: Rule Creation
Phase 4: Validation
(Complete full cycle before moving to next language)


## Foundational Knowledge

**The semgrep-rule-creator skill is the authoritative reference for Semgrep rule creation fundamentals.** While this skill focuses on porting existing rules to new languages, the core principles of writing quality rules remain the same.

Consult semgrep-rule-creator for guidance on:
- **When to use taint mode vs pattern matching** - Choosing the right approach for the vulnerability type
- **Test-first methodology** - Why tests come before rules and how to write effective test cases
- **Anti-patterns to avoid** - Common mistakes like overly broad or overly specific patterns
- **Iterating until tests pass** - The validation loop and debugging techniques
- **Rule optimization** - Removing redundant patterns after tests pass

When porting a rule, you're applying these same principles in a new language context. If uncertain about rule structure or approach, refer to semgrep-rule-creator first.

## Four-Phase Workflow

### Phase 1: Applicability Analysis

Before porting, determine if the pattern applies to the target language.

**Analysis criteria:**
1. Does the vulnerability class exist in the target language?
2. Does an equivalent construct exist (function, pattern, library)?
3. 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

See [applicability-analysis.md]({baseDir}/references/applicability-analysis.md) for detailed guidance.

### Phase 2: Test Creation (Test-First)

**Always write tests before the rule.**

Create test file with target language idioms:
- Minimum 2 vulnerable cases (ruleid:)
- Minimum 2 safe cases (ok:)
- Include language-specific edge cases

// ruleid: sql-injection-golang
db.Query("SELECT * FROM users WHERE id = " + userInput)

// ok: sql-injection-golang
db.Query("SELECT * FROM users WHERE id = ?", userInput)


### Phase 3: Rule Creation

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

See [language-syntax-guide.md]({baseDir}/references/language-syntax-guide.md) for translation guidance.

### Phase 4: Validation

# Validate YAML
semgrep --validate --config rule.yaml

# Run tests
semgrep --test --config rule.yaml test-file


**Checkpoint**: Output MUST show All tests passed.

For taint rule debugging:
semgrep --dataflow-traces -f rule.yaml test-file


See [workflow.md]({baseDir}/references/workflow.md) for detailed workflow and troubleshooting.

## Quick Reference

| Task | Command |
|------|---------|
| Run tests | semgrep --test --config rule.yaml test-file |
| Validate YAML | semgrep --validate --config rule.yaml |
| Dump AST | semgrep --dump-ast -l <lang> <file> |
| Debug taint flow | semgrep --dataflow-traces -f rule.yaml file |


## Key Differences from Rule Creation

| Aspect | semgrep-rule-creator | This skill |
|--------|---------------------|------------|
| Input | Bug pattern description | Existing rule + target languages |
| Output | Single rule+test | Multiple rule+test directories |
| Workflow | Single creation cycle | Independent cycle per language |
| Phase 1 | Problem analysis | Applicability analysis per language |
| Library research | Always relevant | Optional (when original uses libraries) |

## Documentation

**REQUIRED**: Before porting rules, read relevant Semgrep documentation:

- [Rule Syntax](https://semgrep.dev/docs/writing-rules/rule-syntax) - YAML structure and operators
- [Pattern Syntax](https://semgrep.dev/docs/writing-rules/pattern-syntax) - Pattern matching and metavariables
- [Pattern Examples](https://semgrep.dev/docs/writing-rules/pattern-examples) - Per-language pattern references
- [Testing Rules](https://semgrep.dev/docs/writing-rules/testing-rules) - Testing annotations
- [Trail of Bits Testing Handbook](https://appsec.guide/docs/static-analysis/semgrep/advanced/) - Advanced patterns

## Next Steps

- For applicability analysis guidance, see [applicability-analysis.md]({baseDir}/references/applicability-analysis.md)
- For language translation guidance, see [language-syntax-guide.md]({baseDir}/references/language-syntax-guide.md)
- For detailed workflow and examples, see [workflow.md]({baseDir}/references/workflow.md)

How to Use This Skill Unit

Option A: Project-Specific (Recommended)

  1. Click "Download" above
  2. In your project, create the directory: .agent/skills/semgrep-rule-variant-creator/
  3. Save the file as SKILL.md
  4. The agent will automatically discover the skill based on its description.

Option B: Global Installation (All Agents)

Save the file to these locations to make it available across all projects:

  • Claude Code: ~/.claude/skills/trailofbits/skills/semgrep-rule-variant-creator/SKILL.md
  • Cursor: ~/.cursor/skills/trailofbits/skills/semgrep-rule-variant-creator/SKILL.md
  • Antigravity: ~/.gemini/antigravity/skills/trailofbits/skills/semgrep-rule-variant-creator/SKILL.md

πŸš€ Install with CLI:
npx skills add trailofbits/skills

Read the Master Guide: Mastering Agent Skills β†’

Related Skill Units

Recommended Rules

View more rules β†’

Recommended Workflows

View more workflows β†’

Recommended MCP Servers

View more MCP servers β†’

Take It Further

Maximize your productivity with these powerful resources

πŸ“‹

Define Your Standards

Set up coding standards to ensure this workflow produces consistent, high-quality results.

Browse Rules Library
πŸ“–

Master Workflows

Learn how to create custom workflows, use Turbo Mode, and build your automation library.

Complete Guide

How to use this Skill in Claude Code & Cursor

For Claude Code (CLI)

To use this skill in Claude Code, copy the rule content into your project's custom instructions or follow our Add-Skill CLI guide. This ensures Claude follows your standards during every code generation.

For Cursor & Windsurf

For Cursor or Windsurf, individual skills are best used in the "Rules for AI" section. This specific unit helps the agent avoid security & vulnerability analysis issues, leading to cleaner, more efficient code.

Why the skill format matters: the standardized Agent Skills format lets your AI agent load detailed instructions only when they are relevant, keeping your prompt clean while improving results.

Source & attribution

This skill is categorized under Security & Vulnerability Analysis and is published by Trail of Bits, maintained in trailofbits/skills.

← Browse All Agent Skills
Sponsored AI assistant. Recommendations may be paid.