ตรวจจับปัญหาความปลอดภัยขณะ 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:
{
"enabledPlugins": {
"security-guidance@claude-plugins-official": true
}
}
สิ่งที่ Plugin ตรวจสอบ
Plugin ตรวจสอบงานของ Claude ใน 3 จุด แต่ละจุดมีความลึกต่างกัน:
- เมื่อแก้ไขแต่ละไฟล์: การจับคู่ pattern อย่างรวดเร็วสำหรับ risky calls โดยไม่มี model call
- เมื่อสิ้นสุดแต่ละ turn: background model review ของทุกสิ่งที่เปลี่ยนแปลงใน turn นั้น
- เมื่อ Claude commit หรือ push: agentic review ที่ลึกกว่าซึ่งอ่านโค้ดรอบข้าง
เมื่อแก้ไขแต่ละไฟล์
เมื่อ 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:
# 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:
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."
| Field | Type | Description |
|---|---|---|
rule_name | string | identifier ที่แสดงใน warning |
reminder | string | Warning text ที่เพิ่มใน context ของ Claude |
regex | string | Python regex จับคู่กับเนื้อหาที่แก้ไข |
substrings | list | Literal substrings; ระบุ field นี้หรือ regex |
paths | list | Glob patterns เพื่อจำกัด rule เฉพาะไฟล์ที่ตรงกัน |
exclude_paths | list | Glob 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 แยกกัน:
| Variable | Effect |
|---|---|
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 | วัตถุประสงค์ |
|---|---|
SessionStart | Bootstrap Python environment ของ plugin |
UserPromptSubmit | Capture working-tree baseline |
PostToolUse บน Edit, Write และ NotebookEdit | Per-edit pattern match |
Stop | End-of-turn diff review ใน background |
PostToolUse บน Bash, กรองเฉพาะ git commit และ git push | Commit และ push review |
วิธีที่ Plugin นี้เหมาะกับ Security Tools อื่น ๆ
Plugin นี้เป็นชั้นหนึ่งในแนวทาง defense-in-depth:
| Stage | Tool | สิ่งที่ครอบคลุม |
|---|---|---|
| In session | Security guidance plugin | ช่องโหว่ทั่วไปในโค้ดที่ Claude เขียน แก้ไขใน session เดียวกัน |
| On demand | /security-review | One-time security pass บน branch ปัจจุบัน |
| On pull request | Code Review | Multi-agent review พร้อม context ของ codebase ทั้งหมด |
| In CI | Static analysis และ dependency scanners | Language-specific rules และ supply-chain checks |
การแก้ไขปัญหา
Plugin เขียน runtime diagnostics ไปยัง ~/.claude/security/log.txt ตรวจสอบที่นั่นก่อนถ้า reviews ไม่ปรากฏ
แหล่งข้อมูลที่เกี่ยวข้อง
- Code Review: ตั้งค่า PR-time multi-agent review
- Automate actions with hooks: สร้างการตรวจสอบของคุณเองที่ lifecycle points เดียวกัน
- Discover and install plugins: เรียกดู official plugins อื่น ๆ