API key bị lộ = mất tiền thật
Key bị lộ trên GitHub bị bot quét trong 30 giây. Đã có developer mất $12,000 trong 2 giờ. Đây là cách bảo vệ.
Level 1: Environment variables
Bash
# .env (thêm vào .gitignore!)
IZZI_API_KEY=izzi-YOUR_KEY_HERE
Python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["IZZI_API_KEY"],
base_url="https://api.izziapi.com/v1"
)Level 2: .gitignore
Bash
# .gitignore
.env
.env.local
.env.production
*.key
*.pemLevel 3: Pre-commit hook
Bash
#!/bin/sh
# .git/hooks/pre-commit
if git diff --cached -U0 | grep -E '(izzi-|sk-|gsk_)'; then
echo "LỖI: Phát hiện API key trong code!"
exit 1
fiLevel 4: Key rotation mỗi 90 ngày
- Dashboard → API Keys → Tạo key mới
- Cập nhật environment/secret manager
- Verify key mới hoạt động
- Xóa key cũ
Checklist bảo mật
| Check | Mức độ |
|---|---|
| Không hardcode key trong source | Bắt buộc |
| .env trong .gitignore | Bắt buộc |
| Pre-commit hook | Quan trọng |
| Secret manager (production) | Quan trọng |
| Rotate key mỗi 90 ngày | Best practice |
