Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Write custom Semgrep SAST rules in YAML to detect application-specific vulnerabilities, enforce coding standards, and integrate into CI/CD pipelines.
.claude/skills/implementing-semgrep-for-custom-sast-rules/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-07 | ✗→✓ | ▲ Improved | — | — |
| case-15 | ✗→✓ | ▲ Improved | — | — |
| case-01 | ✗→✓ | ▲ Improved | — | — |
| case-13 | ✗→✓ | ▲ Improved | — | — |
| case-06 | ✗→✓ | ▲ Improved | — | — |
Semgrep is an open-source static analysis tool that uses pattern-matching to find bugs, enforce code standards, and detect security vulnerabilities. Custom rules are written in YAML using Semgrep's pattern syntax, making it accessible without requiring compiler knowledge. It supports 30+ languages including Python, JavaScript, Go, Java, and C.
bash# Install via pip pip install semgrep # Install via Homebrew brew install semgrep # Run via Docker docker run -v "${PWD}:/src" returntocorp/semgrep semgrep --config auto /src # Verify semgrep --version
bash# Auto-detect rules for your code semgrep --config auto . # Use Semgrep registry rules semgrep --config r/python.lang.security # Use custom rule file semgrep --config my-rules.yaml . # Use multiple configs semgrep --config auto --config ./custom-rules/ . # JSON output semgrep --config auto --json . > results.json # SARIF output for GitHub semgrep --config auto --sarif . > results.sarif # Filter by severity semgrep --config auto --severity ERROR .
yaml# rules/sql-injection.yaml rules: - id: sql-injection-string-format languages: [python] severity: ERROR message: | Potential SQL injection via string formatting. Use parameterized queries instead. pattern: | cursor.execute(f"..." % ...) metadata: cwe: ["CWE-89"] owasp: ["A03:2021"] category: security fix: | cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
yamlrules: - id: hardcoded-secret-in-code languages: [python, javascript, typescript] severity: ERROR message: Hardcoded secret detected in source code patterns: - pattern-either: - pattern: $VAR = "..." - pattern: $VAR = '...' - metavariable-regex: metavariable: $VAR regex: (?i)(password|secret|api_key|token|aws_secret) - pattern-not: $VAR = "" - pattern-not: $VAR = "changeme" - pattern-not: $VAR = "PLACEHOLDER" metadata: cwe: ["CWE-798"] category: security
yamlrules: - id: xss-taint-tracking languages: [python] severity: ERROR message: User input flows to HTML response without sanitization mode: taint pattern-sources: - pattern: request.args.get(...) - pattern: request.form.get(...) - pattern: request.form[...] pattern-sinks: - pattern: return render_template_string(...) - pattern: Markup(...) pattern-sanitizers: - pattern: bleach.clean(...) - pattern: escape(...) metadata: cwe: ["CWE-79"] owasp: ["A03:2021"]
yamlrules: - id: insecure-random languages: [python, javascript, go, java] severity: WARNING message: | Using insecure random number generator. Use cryptographically secure alternatives for security-sensitive operations. pattern-either: # Python - pattern: random.random() - pattern: random.randint(...) # JavaScript - pattern: Math.random() # Go - pattern: math/rand.Intn(...) # Java - pattern: new java.util.Random() metadata: cwe: ["CWE-330"]
yamlrules: - id: require-error-handling languages: [go] severity: WARNING message: Error return value not checked pattern: | $VAR, _ := $FUNC(...) fix: | $VAR, err := $FUNC(...) if err != nil { return fmt.Errorf("$FUNC failed: %w", err) } - id: no-console-log-in-production languages: [javascript, typescript] severity: WARNING message: Remove console.log before merging to production pattern: console.log(...) paths: exclude: - "tests/*" - "*.test.*"
yamlrules: - id: jwt-none-algorithm languages: [python] severity: ERROR message: JWT decoded without algorithm verification - allows token forgery patterns: - pattern: jwt.decode($TOKEN, ..., algorithms=["none"], ...) metadata: cwe: ["CWE-347"] - id: jwt-no-verification languages: [python] severity: ERROR message: JWT decoded with verification disabled patterns: - pattern: jwt.decode($TOKEN, ..., options={"verify_signature": False}, ...) metadata: cwe: ["CWE-345"]
yaml# rules/test-sql-injection.yaml rules: - id: sql-injection-format-string languages: [python] severity: ERROR message: SQL injection via format string pattern: | cursor.execute(f"...{$VAR}...") # Test annotation in test file: # test-sql-injection.py def bad_query(user_id): # ruleid: sql-injection-format-string cursor.execute(f"SELECT * FROM users WHERE id = {user_id}") def good_query(user_id): # ok: sql-injection-format-string cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
bash# Run rule tests semgrep --test rules/ # Test specific rule semgrep --config rules/sql-injection.yaml --test
yamlname: Semgrep SAST on: [pull_request] jobs: semgrep: runs-on: ubuntu-latest container: image: returntocorp/semgrep steps: - uses: actions/checkout@v4 - name: Run Semgrep run: | semgrep --config auto \ --config ./custom-rules/ \ --sarif --output results.sarif \ --severity ERROR \ . - name: Upload SARIF uses: github/codeql-action/upload-sarif@v3 with: sarif_file: results.sarif
yamlsemgrep: stage: test image: returntocorp/semgrep script: - semgrep --config auto --config ./custom-rules/ --json --output semgrep.json . artifacts: reports: sast: semgrep.json
yaml# .semgrep.yaml rules: - id: my-org-rules # ... rules here # .semgrepignore tests/ node_modules/ vendor/ *.min.js
# ruleid: and # ok: annotationsfix key where possible| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-07 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-16 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-08 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-04 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-05 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-15 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-17 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-12 | pass→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-14 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-19 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-01 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-13 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-18 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-21 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-02 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-20 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-06 | fail→pass | — | — | — | — | — | — | — | — | — | — | — | — |
case-09 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-03 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-10 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-11 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
case-22 | fail→fail | — | — | — | — | — | — | — | — | — | — | — | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +23 percentage points is the difference between those two pass rates over the 22 comparable cases.
The per-case answers from this run were removed by the retention sweep, so the case table below shows the verdicts without the text either arm produced. The counts above were recorded at the time and are unaffected. Answers are now kept for 180 days.
Other measured skills in the registry, with their headline benchmark lift.