home / skills / multiversx / mx-ai-skills / mvx_semgrep_creator
This skill guides you in writing Semgrep rules to enforce MultiversX patterns and catch unsafe code early.
npx playbooks add skill multiversx/mx-ai-skills --skill mvx_semgrep_creatorReview the files below or copy the command above to add this skill to your agents.
---
name: mvx_semgrep_creator
description: Writing custom Semgrep rules to enforce MultiversX best practices.
---
# Semgrep Rule Creator (MX)
This skill guides you in writing Semgrep rules to catch MultiversX-specific patterns automatically.
## 1. Common Patterns
- **Unsafe Math**: `x + y` where `x` is `u64`.
- **Floating Point**: `f64`.
- **Endpoint without Payment Check**: `#[payable]` function without `call_value()`.
## 2. Template
```yaml
rules:
- id: mvx-unsafe-addition
languages: [rust]
message: "Potential arithmetic overflow. Use checked_add or BigUint."
severity: ERROR
patterns:
- pattern: $X + $Y
- pattern-not: $X.checked_add($Y)
- pattern-inside: |
#[multiversx_sc::contract]
trait Contract {
...
}
```
## 3. Workflow
1. **Identify Pattern**: See `mvx_variant_analysis`.
2. **Write Rule**: Use the template.
3. **Test**: Run on the codebase using `semgrep --config rules.yaml .`
4. **Refine**: Reduce false positives.
This skill helps you write custom Semgrep rules that enforce MultiversX-specific best practices across Rust smart contracts. It packages common detection patterns, a reusable rule template, and a short workflow to create, test, and refine rules that reduce security and correctness issues. Use it to automate code review checks and catch risky patterns early in CI.
The skill inspects source code patterns and generates Semgrep YAML rules tailored for MultiversX contracts. It highlights patterns such as unsafe integer arithmetic, use of floating point types, and payable endpoints that lack explicit payment handling. You can seed a rule from the template, run semgrep locally or in CI, and iterate to lower false positives.
How do I reduce false positives?
Narrow patterns, add pattern-not exclusions for known safe helpers, and test rules on representative files before enabling in CI.
Can these rules run in CI?
Yes. Add semgrep to your build pipeline and point it at the rules.yaml; start with non-blocking reporting before enforcing failures.