Skip to main content

ตรวจจับปัญหาความปลอดภัยขณะ Claude เขียนโค้ด

ติดตั้ง security-guidance plugin เพื่อให้ Claude ตรวจสอบ code changes ของตัวเองสำหรับช่องโหว่และแก้ไขใน session เดียวกัน

Security guidance plugin ทำให้ Claude ตรวจสอบ code changes ของตัวเองสำหรับช่องโหว่ทั่วไปขณะทำงาน และแก้ไขสิ่งที่พบใน session เดียวกัน Plugin นี้จับปัญหาเช่น injection, unsafe deserialization และ unsafe DOM APIs ก่อนที่โค้ดจะถึง pull request

เมื่อติดตั้งแล้ว plugin จะทำงานอัตโนมัติ ไม่ต้องเรียกใช้หรือจำคำสั่งแยกต่างหาก

Prerequisites

  • Claude Code CLI เวอร์ชัน 2.1.144 หรือใหม่กว่า
  • Python 3.8 หรือใหม่กว่าบน PATH ของคุณ Plugin ลอง python3, python และ py -3 ตามลำดับ
  • Git repository สำหรับ directory ที่คุณทำงาน

ติดตั้ง Plugin

ใน Claude Code session ติดตั้งจาก official Anthropic marketplace:

/plugin install security-guidance@claude-plugins-official

จากนั้น activate ใน session ปัจจุบัน:

/reload-plugins

เปิดใช้งานใน Cloud Sessions และ Shared Repositories

User-scoped plugins ไม่ถ่ายโอนไปยัง Claude Code บนเว็บ เพื่อเปิดใช้งาน plugin ที่นั่น หรือสำหรับทุกคนที่ clone repository ให้ประกาศใน checked-in settings ของ project:

.claude/settings.json
{
"enabledPlugins": {
"security-guidance@claude-plugins-official": true
}
}

สิ่งที่ Plugin ตรวจสอบ

Plugin ตรวจสอบงานของ Claude ใน 3 จุด แต่ละจุดมีความลึกต่างกัน:

เมื่อแก้ไขแต่ละไฟล์

เมื่อ Claude เขียนไฟล์ plugin สแกนเนื้อหาใหม่สำหรับ risky patterns ที่รู้จัก นี่คือ pattern match โดยไม่มี model call จึงไม่มีค่าใช้จ่าย

ตัวอย่างหมวดหมู่ pattern:

  • Dynamic code execution: eval(, new Function, os.system, child_process.exec
  • Unsafe deserialization: pickle
  • DOM injection: dangerouslySetInnerHTML, .innerHTML =, document.write
  • Workflow files: การแก้ไขใน .github/workflows/

เมื่อสิ้นสุดแต่ละ Turn

หลังแต่ละ turn plugin คำนวณ git diff ของทุกสิ่งที่เปลี่ยนแปลงใน working tree ระหว่าง turn นั้น และส่งไปยัง Claude review แยกต่างหาก review รันใน background จึงไม่ทำให้ response ของ Claude ล่าช้า

จับปัญหาที่ string match ไม่สามารถจับได้ เช่น:

  • Authorization bypass
  • Insecure direct object references
  • Injection
  • Server-side request forgery
  • Weak cryptography

เมื่อ Claude Commit หรือ Push

เมื่อ Claude รัน git commit หรือ git push ผ่าน Bash tool plugin รัน agentic review ที่ลึกกว่าใน background review นี้อ่านโค้ดรอบข้าง รวมถึง callers, sanitizers และไฟล์ที่เกี่ยวข้อง เพื่อตัดสินว่า finding นั้นเป็นจริงก่อนรายงาน

ความเป็นอิสระของ Review และข้อจำกัด

Plugin ไม่ขอให้ Claude instance เดิมที่เขียนโค้ดตรวจสอบตัวเอง:

  • การตรวจสอบ per-edit คือ string match แบบ deterministic
  • End-of-turn และ commit reviews รันเป็น Claude call แยกต่างหากพร้อม fresh context

ไม่มี layer ใดที่บล็อกการเขียนหรือ commit Findings ถึงการเขียน Claude เป็นคำสั่ง Claude แก้ไขใน conversation ถือว่า plugin เป็นชั้นหนึ่งของ defense in depth

เพิ่มกฎของคุณเอง

เพิ่ม Guidance สำหรับ Model-backed Reviews

สร้าง .claude/claude-security-guidance.md ใน project ของคุณและอธิบาย threat model และ review checklist ใน plain language:

.claude/claude-security-guidance.md
# Security guidance for this repo

- Do not log `customer_id` or `account_number` at INFO level or above.
- All routes under `/admin` must call `require_role("admin")` before any database read.
- Use `crypto.timingSafeEqual` for token comparison instead of `===`.

เพิ่ม Custom Per-edit Patterns

สร้าง .claude/security-patterns.yaml เพื่อเพิ่ม regex หรือ substring rules:

.claude/security-patterns.yaml
patterns:
- rule_name: internal_api_key
substrings: ["sk_live_", "AKIA"]
reminder: "Hardcoded API key prefix. Load credentials from the secret manager."
- rule_name: tenant_unfiltered_query
regex: "\\.objects\\.all\\(\\)"
paths: ["**/src/tenants/**"]
reminder: "Multi-tenant code must filter by org_id."
FieldTypeDescription
rule_namestringidentifier ที่แสดงใน warning
reminderstringWarning text ที่เพิ่มใน context ของ Claude
regexstringPython regex จับคู่กับเนื้อหาที่แก้ไข
substringslistLiteral substrings; ระบุ field นี้หรือ regex
pathslistGlob patterns เพื่อจำกัด rule เฉพาะไฟล์ที่ตรงกัน
exclude_pathslistGlob patterns เพื่อข้าม

ค่าใช้จ่าย

  • Per-edit pattern check ไม่มี model call และไม่มีค่าใช้จ่าย
  • End-of-turn และ commit reviews ใช้ model usage เพิ่มเติม

Reviews ทั้งสองใช้ Claude Opus 4.7 โดยค่าเริ่มต้น ตั้งค่า SECURITY_REVIEW_MODEL เพื่อเลือก model อื่น

ปิดใช้งานหรือ Uninstall

ปิดใช้งาน layers แยกกัน:

VariableEffect
ENABLE_PATTERN_RULES=0ปิด per-edit pattern check
ENABLE_STOP_REVIEW=0ปิด end-of-turn diff review
ENABLE_COMMIT_REVIEW=0ปิด commit และ push review
ENABLE_CODE_SECURITY_REVIEW=0ปิด model-backed reviews ทั้งหมด
SECURITY_GUIDANCE_DISABLE=1ปิด plugin ทั้งหมดโดยไม่ uninstall
/plugin disable security-guidance@claude-plugins-official
/plugin uninstall security-guidance@claude-plugins-official

วิธีที่ Plugin ทำงานกับ Claude Code

Plugin สร้างขึ้นทั้งหมดบน hooks ซึ่งเป็นกลไกสำหรับรันโค้ดของคุณเองที่จุดเฉพาะใน loop ของ Claude:

Hook eventวัตถุประสงค์
SessionStartBootstrap Python environment ของ plugin
UserPromptSubmitCapture working-tree baseline
PostToolUse บน Edit, Write และ NotebookEditPer-edit pattern match
StopEnd-of-turn diff review ใน background
PostToolUse บน Bash, กรองเฉพาะ git commit และ git pushCommit และ push review

วิธีที่ Plugin นี้เหมาะกับ Security Tools อื่น ๆ

Plugin นี้เป็นชั้นหนึ่งในแนวทาง defense-in-depth:

StageToolสิ่งที่ครอบคลุม
In sessionSecurity guidance pluginช่องโหว่ทั่วไปในโค้ดที่ Claude เขียน แก้ไขใน session เดียวกัน
On demand/security-reviewOne-time security pass บน branch ปัจจุบัน
On pull requestCode ReviewMulti-agent review พร้อม context ของ codebase ทั้งหมด
In CIStatic analysis และ dependency scannersLanguage-specific rules และ supply-chain checks

การแก้ไขปัญหา

Plugin เขียน runtime diagnostics ไปยัง ~/.claude/security/log.txt ตรวจสอบที่นั่นก่อนถ้า reviews ไม่ปรากฏ

แหล่งข้อมูลที่เกี่ยวข้อง