semgrep-rule-creator – skill Bảo mật cho agent AI
Tạo rule Semgrep tùy chỉnh để phát hiện lỗ hổng bảo mật, lỗi và mẫu code. Dùng khi viết rule Semgrep hoặc xây dựng phát hiện cho static analysis tùy chỉnh.
Cài skill semgrep-rule-creator bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/semgrep-rule-creator-a4794.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /semgrep-rule-creator – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/semgrep-rule-creator-a4794.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $semgrep-rule-creator – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/semgrep-rule-creator-a4794.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /semgrep-rule-creator – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/semgrep-rule-creator-a4794.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /semgrep-rule-creator – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
Semgrep Rule Creator
Create production-quality Semgrep rules with proper testing and validation.
When to Use
Ideal scenarios:
- Writing Semgrep rules for specific bug patterns
- Writing rules to detect security vulnerabilities in your codebase
- Writing taint mode rules for data flow vulnerabilities
- Writing rules to enforce coding standards
When NOT to Use
Do NOT use this skill for:
- Running existing Semgrep rulesets
- General static analysis without custom rules (use
static-analysisskill)
Rationalizations to Reject
When writing Semgrep rules, reject these common shortcuts:
- "The pattern looks complete" → Still run
semgrep --test --config <rule-id>.yaml <rule-id>.<ext>to verify. Untested rules have hidden false positives/negatives. - "It matches the vulnerable case" → Matching vulnerabilities is half the job. Verify safe cases don't match (false positives break trust).
- "Taint mode is overkill for this" → If data flows from user input to a dangerous sink, taint mode gives better precision than pattern matching.
- "One test is enough" → Include edge cases: different coding styles, sanitized inputs, safe alternatives, and boundary conditions.
- "I'll optimize the patterns first" → Write correct patterns first, optimize after all tests pass. Premature optimization causes regressions.
- "The AST dump is too complex" → The AST reveals exactly how Semgrep sees code. Skipping it leads to patterns that miss syntactic variations.
Anti-Patterns
Too broad - matches everything, useless for detection:
# BAD: Matches any function call
pattern: $FUNC(...)
# GOOD: Specific dangerous function
pattern: eval(...) # security-allowlist: Semgrep sink patternMissing safe cases in tests - leads to undetected false positives:
# BAD: Only tests vulnerable case
# ruleid: my-rule
dangerous(user_input)
# GOOD: Include safe cases to verify no false positives
# ruleid: my-rule
dangerous(user_input)
# ok: my-rule
dangerous(sanitize(user_input))
# ok: my-rule
dangerous("hardcoded_safe_value")Overly specific patterns - misses variations:
# BAD: Only matches exact format
pattern: os.system("rm " + $VAR)
# GOOD: Matches all os.system calls with taint tracking
mode: taint
pattern-sinks:
- pattern: os.system(...)Strictness Level
This workflow is strict - do not skip steps:
- Read documentation first: See [Documentation](#documentation) before writing Semgrep rules
- Test-first is mandatory: Never write a rule without tests
- 100% test pass is required: "Most tests pass" is not acceptable
- Optimization comes last: Only simplify patterns after all tests pass
- Avoid generic patterns: Rules must be specific, not match broad patterns
- Prioritize taint mode: For data flow vulnerabilities
- One YAML file - one Semgrep rule: Each YAML file must contain only one Semgrep rule; don't combine multiple rules in a single file
- No generic rules: When targeting a specific language for Semgrep rules - avoid generic pattern matching (
languages: generic) - Forbidden
todookandtodoruleidtest annotations:todoruleid: <rule-id>andtodook: <rule-id>annotations in tests files for future rule improvements are forbidden
Overview
This skill guides creation of Semgrep rules that detect security vulnerabilities and code patterns. Rules are created iteratively: analyze the problem, write tests first, analyze AST structure, write the rule, iterate until all tests pass, optimize the rule.
Approach selection:
- Taint mode (prioritize): Data flow issues where untrusted input reaches dangerous sinks
- Pattern matching: Simple syntactic patterns without data flow requirements
Why prioritize taint mode? Pattern matching finds syntax but misses context. A pattern eval($X) matches both eval(user_input) (vulnerable) and eval("safe_literal") (safe). Taint mode tracks data flow, so it only alerts when untrusted data actually reaches the sink—dramatically reducing false positives for injection vulnerabilities. <!-- security-allowlist: Semgrep taint-mode explanation -->
Iterating between approaches: It's okay to experiment. If you start with taint mode and it's not working well (e.g., taint doesn't propagate as expected, too many false positives/negatives), switch to pattern matching. Conversely, if pattern matching produces too many false positives on safe cases, try taint mode instead. The goal is a working rule—not rigid adherence to one approach.
Output structure - exactly 2 files in a directory named after the rule-id:
<rule-id>/
├── <rule-id>.yaml # Semgrep rule
└── <rule-id>.<ext> # Test file with ruleid/ok annotationsQuick Start
rules:
- id: insecure-eval
languages: [python]
severity: HIGH
message: User input passed to eval() allows code execution # security-allowlist: Semgrep finding message
mode: taint
pattern-sources:
- pattern: request.args.get(...)
pattern-sinks:
- pattern: eval(...) # security-allowlist: Semgrep sink patternTest file (insecure-eval.py):
# ruleid: insecure-eval
eval(request.args.get('code')) # security-allowlist: intentionally vulnerable Semgrep fixture
# ok: insecure-eval
eval("print('safe')") # security-allowlist: safe-literal Semgrep fixtureRun tests (from rule directory): semgrep --test --config <rule-id>.yaml <rule-id>.<ext>
Supporting scope
Only SKILL.md is bundled; quick-reference.md and workflow.md are unavailable. Use the inline checklist plus current official rule/test documentation, report missing support and never claim an absent workflow was followed. Pattern snippets omitting sources/languages/message are fragments, not complete standalone rules.
Quick Reference
- For commands, pattern operators, and taint mode syntax, see quick-reference.md.
- For detailed workflow and examples, you MUST see workflow.md
Workflow
Copy this checklist and track progress:
Semgrep Rule Progress:
- [ ] Step 1: Analyze the Problem
- [ ] Step 2: Write Tests First
- [ ] Step 3: Analyze AST structure
- [ ] Step 4: Write the rule
- [ ] Step 5: Iterate until all tests pass (semgrep --test)
- [ ] Step 6: Optimize the rule (remove redundancies, re-test)
- [ ] Step 7: Final RunDocumentation
REQUIRED: Before writing any rule, use WebFetch to read the relevant current documentation from these links with Semgrep documentation:
1. [Rule Syntax](https://semgrep.dev/docs/writing-rules/rule-syntax)
2. [Pattern Syntax](https://semgrep.dev/docs/writing-rules/pattern-syntax)
3. [ToB Testing Handbook - Semgrep](https://appsec.guide/docs/static-analysis/semgrep/advanced/)
4. [Constant propagation](https://semgrep.dev/docs/writing-rules/data-flow/constant-propagation)
5. [Writing Rules Index](https://github.com/semgrep/semgrep-docs/tree/main/docs/writing-rules/)
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
Skill liên quan
- sharp-edges – Rà soát các API bảo mật dễ bị dùng sai, cấu hình mặc định, giao diện mật mã và trải…
- skill-audit – Khung rà soát trước khi cài đặt cho các skill của AI agent: kiểm tra toàn bộ nội dung…
- skill-security-audit – Kiểm toán một Agent Skill, máy chủ MCP, connector hoặc desktop extension trước khi cài…
- skill-skill – Viết, nâng cấp hoặc kiểm toán skill của Claude để thay đổi nội dung được tạo ra: forcing…
- soc2-compliance – Triển khai các Tiêu chí Dịch vụ Tin cậy SOC 2 (Trust Services Criteria). Cấu hình kiểm…
- solidity-security – Nắm vững các best practice bảo mật smart contract, phòng ngừa lỗ hổng và các mẫu phát…
- soroban-contract-audit – Sổ đăng ký kiểm toán bảo mật hợp đồng thông minh Soroban: kiểm tra phân quyền, đường dẫn…
- soroban-oracle-data-feed-audit – Sổ đăng ký kiểm toán tích hợp và an toàn của oracle giá DeFi: giới hạn heartbeat, hoàn…