
semgrep
by umutbasal
SKILL.md
name: semgrep description: Static analysis and security scanning using Semgrep, a fast, open-source static analysis tool for finding bugs and enforcing code standards. Use when scanning code for security vulnerabilities, code quality issues, style violations, or bugs. Triggers include requests to find security issues, detect code patterns, enforce coding standards, scan for vulnerabilities, write custom rules, perform static analysis, or audit code quality. Supports pattern matching, autofix, generic pattern matching, and rule composition.
Semgrep - Static Analysis and Security Scanning
Semgrep is a fast, open-source static analysis tool that finds bugs and enforces code standards using pattern matching. It supports multiple languages and can detect security vulnerabilities, code quality issues, and style violations.
Semgrep Registry
The Semgrep Registry provides thousands of Community Edition and Pro rules maintained by Semgrep and the community. Rules are organized by:
Rule Categories
- Security: Detects vulnerabilities like SQL injection, XSS, insecure deserialization, dangerous APIs, and insecure transport
- Best Practices: Enforces coding standards, secure coding guidelines, and automated style reviews
- Correctness: Identifies logic bugs and common programming errors
- Performance: Addresses code efficiency and optimization issues
OWASP Coverage
Semgrep provides comprehensive coverage for the OWASP Top 10 security risks:
- A01:2021 - Broken Access Control: Authorization bypass, privilege escalation
- A02:2021 - Cryptographic Failures: Weak encryption, insecure hashing
- A03:2021 - Injection: SQL injection, command injection, LDAP injection, XSS
- A04:2021 - Insecure Design: Missing security controls, threat modeling issues
- A05:2021 - Security Misconfiguration: Default credentials, verbose errors, insecure defaults
- A06:2021 - Vulnerable Components: Known CVEs in dependencies (via Semgrep Supply Chain)
- A07:2021 - Authentication Failures: Weak passwords, session management issues
- A08:2021 - Data Integrity Failures: Insecure deserialization, unsigned data
- A09:2021 - Logging Failures: Missing logging, sensitive data in logs
- A10:2021 - SSRF: Server-side request forgery
Rule Sources
- Pro Rules: High-accuracy rules with crossfile and dataflow analysis (commercial)
- Community Rules: Free rules in semgrep/semgrep-rules repository
- Third-party Rules: Contributed by security teams (Trail of Bits, etc.)
Pre-configured Rulesets
p/ci: High-confidence security and correctness bugs for CI/CDp/security-audit: Comprehensive security scanningp/owasp-top-ten: OWASP Top 10 coveragep/default: General security and correctness rules- Language-specific:
p/python,p/javascript,p/java, etc. - Framework-specific:
p/react,p/django,p/spring, etc.
Supported Technologies
30+ languages including Python, JavaScript, TypeScript, Java, Go, Ruby, PHP, C#, Kotlin, Swift, Rust, and more. Framework support includes React, Django, Flask, Spring, Express, Rails, Laravel, and many others.
Installation Check
Verify semgrep is installed:
which semgrep || echo "Install: pip install semgrep or brew install semgrep"
Quick Start
Using Registry Rules
# Scan with auto-detection (recommended for CI/CD)
semgrep --config auto .
# Scan with specific ruleset
semgrep --config "p/ci" .
semgrep --config "p/security-audit" .
semgrep --config "p/owasp-top-ten" .
# Scan with language-specific rules
semgrep --config "p/python" .
semgrep --config "p/javascript" .
# Scan with framework-specific rules
semgrep --config "p/react" .
semgrep --config "p/django" .
# Combine multiple rulesets
semgrep --config "p/ci" --config "p/security-audit" .
Basic Scan
# Scan specific directory
semgrep --config auto src/
# Scan with inline pattern
semgrep -e 'pattern' --lang python .
# Scan with custom rule file
semgrep --config my-rules.yml .
# Search Registry for specific rules
# Visit https://semgrep.dev/r to browse available rules
Common Workflow
- Choose rules - Select from Registry rulesets or write custom rules
- Test pattern - Use
-eflag to test patterns interactively - Run scan - Execute with
--configfor rulesets or-efor inline patterns - Review findings - Check output, filtered by severity if needed
- Apply autofix - Use
--autofixfor automatic fixes (if available) - Integrate CI/CD - Use
p/ciruleset for continuous scanning
Core Concepts
Pattern Matching
Semgrep uses pattern matching to find code that matches a given structure:
# Find all function calls
semgrep -e '$FUNC(...)' --lang python .
# Find specific function with arguments
semgrep -e 'requests.get(...)' --lang python .
# Find with metavariables
semgrep -e '$X = $Y' --lang python .
Metavariables
Metavariables capture code patterns:
$X,$Y,$VAR- Match any expression$FUNC- Match function names$CLASS- Match class names$...ARGS- Match sequence of arguments
# Find useless assignments
semgrep -e '$X = $Y' -e '$X = $Z' --lang python .
Ellipsis Operator
The ... operator matches zero or more items:
# Match function calls with any arguments
semgrep -e 'insecure_function(...)' --lang python .
# Match with specific argument position
semgrep -e 'func(1, ...)' --lang python .
# Match keyword arguments anywhere
semgrep -e 'requests.get(..., verify=False, ...)' --lang python .
See references/pattern-syntax.md for complete pattern syntax reference.
Rule Files
Create YAML rule files for complex patterns:
rules:
- id: use-sys-exit
languages:
- python
message: Use `sys.exit` over the python shell `exit` built-in
pattern: exit($X)
fix: sys.exit($X)
severity: MEDIUM
Run: semgrep --config rule.yml .
Essential Flags
| Flag | Purpose |
|---|---|
-e 'pattern' | Inline pattern (search mode) |
--config <config> | Rule file or registry config |
--lang <lang> | Target language |
--autofix | Apply automatic fixes |
--dryrun | Preview autofix without applying |
--json | JSON output format |
--junit-xml | JUnit XML output |
--sarif | SARIF output format |
--severity <level> | Filter by severity (INFO, WARNING, ERROR, CRITICAL) |
--exclude <pattern> | Exclude files/directories |
--include <pattern> | Include only matching files |
Common Patterns
Security Vulnerabilities
# Find SQL injection risks
semgrep -e 'query("...")' --lang python .
# Find hardcoded secrets
semgrep -e 'password = "..."' --lang python .
# Find unsafe deserialization
semgrep -e 'pickle.loads(...)' --lang python .
Code Quality
# Find unused imports
semgrep -e 'import $X' --lang python .
# Find debug statements
semgrep -e 'print(...)' --lang python .
# Find TODO comments
semgrep -e 'TODO' --lang generic .
API Usage
# Find requests without timeout
semgrep -e 'requests.get(...)' --lang python .
# Find deprecated API calls
semgrep -e 'old_api.call(...)' --lang python .
See references/examples.md for comprehensive pattern examples.
Autofix
Semgrep supports automatic code fixes through the fix key in rules:
rules:
- id: use-sys-exit
languages:
- python
message: Use `sys.exit` over `exit`
pattern: exit($X)
fix: sys.exit($X)
severity: MEDIUM
Apply fixes:
# Preview fixes (dry run)
semgrep --config rule.yml --autofix --dryrun .
# Apply fixes
semgrep --config rule.yml --autofix .
See references/autofix.md for autofix patterns and examples.
Generic Pattern Matching
For unsupported languages or configuration files, use generic pattern matching:
# Scan config files
semgrep -e 'password = ...' --lang generic config.conf
# Scan XML files
semgrep -e '<tag>...</tag>' --lang generic file.xml
See references/generic-patterns.md for generic matching details.
Language Support
Semgrep supports 30+ languages including:
- C-family: C, C++, C#, Java, JavaScript, TypeScript, Go, Rust
- Dynamic: Python, Ruby, PHP, Perl
- Functional: OCaml, Haskell, Elixir, Erlang
- Markup: HTML, XML, JSON, YAML
- Other: SQL, Bash, Dockerfile, Terraform
Use --lang generic for unsupported languages or configuration files.
Rule Composition
Combine patterns with boolean operators:
rules:
- id: dangerous-eval
languages:
- javascript
patterns:
- pattern-either:
- pattern: eval(...)
- pattern: Function(...)
- pattern-not: safe_eval(...)
message: Dangerous use of eval or Function constructor
severity: ERROR
Best Practices
- Start with registry: Use
--config "p/ci"or--config autofor CI/CD - Layer security: Combine
p/owasp-top-tenwith language-specific rulesets - Filter by severity: Use
--severity ERRORor--severity HIGHfor critical issues - Use autofix in CI: Enable
--autofixfor safe, automated fixes - Test patterns: Use
-eflag to test patterns before creating rules - Browse Registry: Visit semgrep.dev/r to discover rules for your stack
- Version control: Commit custom rule files to version control
- Monitor performance: Use
--metrics=offto disable telemetry in sandboxed environments - Incremental adoption: Start with high-confidence rules (
p/ci), then expand - Custom rules: Write custom rules for organization-specific security requirements
Resources
- references/pattern-syntax.md - Complete pattern syntax (metavariables, ellipsis, operators)
- references/autofix.md - Autofix patterns and examples
- references/generic-patterns.md - Generic pattern matching for unsupported languages
- references/examples.md - Common security and quality patterns
Troubleshooting
Pattern doesn't match?
- Check language specification with
--lang - Verify metavariable syntax (
$Xnot$x) - Test with
-eflag first - Use
--lang genericfor unsupported languages
Too many matches?
- Make patterns more specific
- Use
pattern-notto exclude false positives - Add constraints with
metavariable-pattern
Autofix not working?
- Verify
fixkey is present in rule - Check
--dryrunoutput first - Ensure fix syntax matches target language
Score
Total Score
Based on repository quality metrics
SKILL.mdファイルが含まれている
ライセンスが設定されている
100文字以上の説明がある
GitHub Stars 100以上
1ヶ月以内に更新
10回以上フォークされている
オープンIssueが50未満
プログラミング言語が設定されている
1つ以上のタグが設定されている
Reviews
Reviews coming soon
