今日目標
• 避免敏感資訊外洩
• 學會 Commit 簽署
• 設定基本安全防護
• 知道緊急處理方法
一、絕對不要 Commit 的東西
❌ 絕對禁止:
憑證和金鑰:
配置檔案:
個人/內部資訊:
二、完善的 .gitignore
.env
.env.local
.env.production
secrets.json
*.key
*.pem
*.db
*.sqlite
database.yml.production
*.log
logs/
node_modules/
vendor/
.DS_Store
Thumbs.db
.vscode/
.idea/
三、檢查敏感資訊
brew install gitleaks # macOS
gitleaks detect --source . --verbose
gitleaks detect --source . --verbose --log-opts="--all"
四、不小心 Commit 了敏感資訊 - 緊急處理
情況 1:還沒推送(簡單)
git rm --cached .env
git commit --amend --no-edit
git reset --soft HEAD~1
rm .env
echo ".env" >> .gitignore
git add .
git commit -m "Remove sensitive file"
情況 2:已經推送(嚴重!)
java -jar bfg.jar --delete-files .env
git reflog expire --expire=now --all
git gc --prune=now --aggressive
git push --force --all
五、預防措施 - Pre-commit Hook
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
if git diff --cached --name-only | grep -E ".env$|secrets.|.key$|.pem$"; then
echo "❌ 錯誤:不能 commit 敏感檔案"
exit 1
fi
if git diff --cached | grep -iE "password|api_key|secret_key" > /dev/null; then
echo "⚠️ 警告:發現可能的敏感資訊"
git diff --cached | grep -iE "password|api_key|secret_key"
read -p "確定要繼續嗎?(y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
echo "✅ Pre-commit 檢查通過"
EOF
chmod +x .git/hooks/pre-commit
六、Commit 簽署
為什麼要簽署?
問題:任何人都可以偽造作者
git config user.name "Linus Torvalds"
git commit -m "I am Linus" # 看起來像 Linus 的 commit!
簽署的好處:
✅ 驗證身分
✅ 防止偽造
✅ GitHub 顯示 "Verified" 標誌
方法 1:GPG 簽署(傳統)
brew install gnupg # macOS
gpg --full-generate-key
gpg --list-secret-keys --keyid-format=long
git config --global user.signingkey 3AA5C34371567BD2
git config --global commit.gpgsign true
gpg --armor --export 3AA5C34371567BD2
git commit -m "feat: 新增功能" # 自動簽署
方法 2:SSH 簽署(較簡單,推薦)
ssh-keygen -t ed25519 -C "your.email@example.com"
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign true
echo "your.email@example.com $(cat ~/.ssh/id_ed25519.pub)" >> ~/.ssh/allowed_signers
git config --global gpg.ssh.allowedSignersFile ~/.ssh/allowed_signers
git commit -m "feat: 新增功能" # 自動簽署
七、GitHub 安全設定
步驟:
注意:
啟用 2FA 後,push 需要用 Personal Access Token
2. Personal Access Token
git clone https://@github.com/user/repo.git
git config --global credential.helper osxkeychain # macOS
必須設定:
✅ Require pull request (至少 1 個 approve)
✅ Require status checks to pass
✅ Require signed commits
✅ Include administrators
✅ Do not allow force pushes
✅ Do not allow deletions
八、自動化安全掃描
name: Security Scan
on: [push, pull_request]
jobs:
gitleaks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Gitleaks 掃描
uses: gitleaks/gitleaks-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
npm-audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm ci
- run: npm audit --audit-level=moderate
九、緊急處理流程
發現敏感資訊外洩:
□ 立即行動(5 分鐘內)
□ 如果還沒推送
□ 如果已推送
□ 事後檢討
十、快速檢查清單
□ 建立完整的 .gitignore
□ 設定 pre-commit hook
□ 啟用 commit 簽署
□ 設定分支保護
□ 掃描敏感資訊(gitleaks)
□ 檢查依賴漏洞(npm audit)
□ 審查大型 commits
□ 輪換 tokens(每季)
gitleaks detect --source .
git secrets --scan-history
npm audit
今日重點回顧
✅ 核心概念:
預防外洩
Commit 簽署
基本防護
緊急處理
記住:預防 >> 補救
立即行動
□ 檢查 .gitignore
□ 安裝 gitleaks
□ 啟用 GitHub 2FA
□ 掃描一次現有 repo
□ 設定 commit 簽署
□ 建立 pre-commit hook
□ 設定分支保護
□ 建立安全規範
常見問題
Q: GPG 太複雜,有更簡單的方法嗎?
A: 用 SSH 簽署!
實用工具
brew install gitleaks git-secrets gnupg gh
gitleaks version
git secrets --list
git secrets --install
git secrets --register-aws