2026-03-146 phút đọcVI
Bảo mật config và secret
English title: Security — Config and Secrets
Bài 14 trong chuỗi 20 bài về Claude Code. Config, rule, skill có thể vô tình chứa API key hoặc token; prompt gửi lên model có thể bị log; repo public = lộ. Bài này: rủi ro, scan và phát hiện, best practices.
Mở đầu: API key, token — đừng để lộ trong config hay prompt
Dev paste key vào rule "dùng API X với key Y" để Claude gọi API — key đi vào context và có thể bị log hoặc commit. Giải pháp: Không bao giờ hardcode secret trong rule/skill/config. Dùng env var hoặc secret manager; .gitignore .env. Checklist: grep pattern key/token trong repo; đảm bảo rule/skill không chứa giá trị thật.
1. Đi sâu: Rủi ro và nguồn lộ
Nguồn lộ: File config, .env commit nhầm, prompt history (user paste key vào chat). Hậu quả: abuse, tốn tiền, mất uy tín. Solution: Luôn dùng biến môi trường; template .env.example không có giá trị thật; docs nội bộ "cách lấy key"; không ghi key vào transcript/summary.
2. Scan và phát hiện
Pre-commit hoặc CI: scan file config, rule, skill cho pattern (API key, password, token). Regex pattern (vd. key=..., token=...); dùng secret scanner nếu có. Hook PreToolUse có thể cảnh báo khi prompt chứa từ khóa "key", "password". Pre-commit fail nếu tìm thấy pattern secret trong thư mục agents/rules. False positive: placeholder (vd. YOUR_KEY_HERE) có thể whitelist.
3. Best practices
Dùng env; secret manager cho production; rotate key; quyền file (config không world-readable). Onboard: .env.example + doc "cách lấy key". Review config khi thêm rule mới. Bài 15 nói MCP — Model Context Protocol và tool.
Bài tiếp: MCP — Model Context Protocol và tool (bài 15).