Chuyển đến nội dung
Skillpedia
Bảo mật35k · #8119

implementing-semgrep-for-custom-sast-rules

Skill này làm gì

Công cụ này giúp tìm các vấn đề trong mã của bạn, như lỗi bảo mật hoặc cách viết mã không tuân theo quy tắc. Bạn có thể tự tạo quy tắc để tìm những điều cụ thể trong mã.

Khi nào nên dùng

  • ✓Khi bạn muốn tìm các lỗ hổng bảo mật cụ thể trong mã.
  • ✓Khi bạn muốn bảo đảm mã tuân theo một tiêu chuẩn đã định.
  • ✓Khi bạn muốn tự động kiểm tra mã trong các dự án lớn.

Liên kết skill

https://github.com/mukul975/Anthropic-Cybersecurity-Skills/blob/main/skills/implementing-semgrep-for-custom-sast-rules/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/mukul975/Anthropic-Cybersecurity-Skills/blob/main/skills/implementing-semgrep-for-custom-sast-rules/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Tìm các mật khẩu được ghi cứng trực tiếp trong mã.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Bạn viết quy tắc theo định dạng đặc biệt (YAML) mô tả điều cần tìm trong mã. Công cụ đọc các quy tắc này và kiểm tra mã để tìm chỗ khớp.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Implementing Semgrep for Custom SAST Rules

Overview

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.

When to Use

  • When deploying or configuring implementing semgrep for custom sast rules capabilities in your environment
  • When establishing security controls aligned to compliance requirements
  • When building or improving security architecture for this domain
  • When conducting security assessments that require this implementation

Prerequisites

  • Python 3.8+ or Docker
  • Semgrep CLI installed
  • Target codebase in a supported language

Installation

# 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

Running Semgrep

# 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 .

Writing Custom Rules

Basic Pattern Matching

# 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,))

Pattern Operators

rules:
  - 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

Taint Analysis

rules:
  - 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"]

Multiple Language Rule

rules:
  - 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"]

Enforce Coding Standards

rules:
  - 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.*"

JWT Security Rules

rules:
  - id: jwt-none-algorithm

…
Câu hỏi thường gặp

Tôi có cần biết cách máy tính biên dịch mã để dùng công cụ này không?

Không. Bạn không cần hiểu về biên dịch mã. Các quy tắc dùng hệ thống “so khớp mẫu”, dễ học hơn.

Công cụ hỗ trợ những ngôn ngữ lập trình nào?

Công cụ hỗ trợ nhiều ngôn ngữ phổ biến như Python, JavaScript, Go, Java và C.

Tôi có thể chỉ dùng quy tắc có sẵn, hay phải tự tạo?

Bạn có thể dùng quy tắc có sẵn từ cộng đồng hoặc tự tạo quy tắc tùy chỉnh cho nhu cầu riêng.

Chi tiết

Mô tả gốc của tác giả

Write custom Semgrep SAST rules in YAML to detect application-specific vulnerabilities, enforce coding standards, and integrate into CI/CD pipelines.

repo: mukul975/Anthropic-Cybersecurity-Skills

Độ phổ biến

Sao GitHub
35k
Vị trí xếp hạng
#8119
Giấy phép
Apache-2.0
Cập nhật
31/8/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự