1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人開発で秘密鍵をgit pushしかけた話 — 二度と事故を起こさない仕組みの作り方

1
Posted at

git pushした後に冷や汗をかいたことはありませんか?

結論: .gitignoreだけでは秘密情報の漏洩は防げません。 pre-commitフックを入れれば、うっかりミスを物理的に不可能にできます。設定は3分。

この記事では、筆者がセッションCookieをpushしかけた体験と、「二度と事故が起きない仕組み」のコードを公開します。

何が起きたか

AIエージェント11体を運用するリポジトリで、noteのセッションCookie(.note-auth/state.json)がgit statusに表示されているのを発見。pushする直前でした。

// .note-auth/state.json の中身(こういうのが漏れる)
{
  "cookies": [
    {
      "name": "_note_session_v5",
      "value": "626f63e8bc9a23c20a455bb4...",
      "httpOnly": true,
      "secure": true
    }
  ]
}

なぜ.gitignoreだけでは不十分か

.gitignore は「知っているファイル」しか除外できません。新しく生成された認証ファイルや、ダウンロードしたService AccountのJSONキーには無力です。

解決策: pre-commitフック

コミット時に秘密情報パターンを自動検出し、コミット自体を止めます。

.git/hooks/pre-commit:

#!/bin/bash
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

BLOCKED=0
FILES=$(git diff --cached --name-only --diff-filter=ACM)

if [ -z "$FILES" ]; then exit 0; fi

# 危険なファイル名パターン
DANGEROUS_PATTERNS=(
  "\.pem$" "\.key$" "\.p12$"
  "credentials\.json" "service.account.*\.json"
  "client_secret.*\.json" "token\.json"
  "state\.json" "_cookies\.json"
  "\.env$" "\.env\."
)

for FILE in $FILES; do
  for PATTERN in "${DANGEROUS_PATTERNS[@]}"; do
    if echo "$FILE" | grep -qE "$PATTERN"; then
      echo -e "${RED}BLOCKED${NC}: $FILE (dangerous filename: $PATTERN)"
      BLOCKED=1
    fi
  done
done

# ファイル内容の秘密情報パターン
SECRET_PATTERNS=(
  "PRIVATE KEY" "BEGIN.*KEY"
  "sk_live_" "sk_test_"
  "ghp_[a-zA-Z0-9]{36}"
  "\"_note_session"
  "\"type\":.*\"service_account\""
)

for FILE in $FILES; do
  if [ ! -f "$FILE" ]; then continue; fi
  if file "$FILE" 2>/dev/null | grep -q "binary"; then continue; fi
  for PATTERN in "${SECRET_PATTERNS[@]}"; do
    if git diff --cached -- "$FILE" | grep -qE "$PATTERN"; then
      echo -e "${RED}BLOCKED${NC}: $FILE contains secret pattern: $PATTERN"
      BLOCKED=1; break
    fi
  done
done

if [ $BLOCKED -ne 0 ]; then
  echo -e "\n${YELLOW}コミットをブロックしました。秘密情報が含まれている可能性があります。${NC}"
  echo "本当にコミットする場合は: git commit --no-verify"
  exit 1
fi
exit 0
chmod +x .git/hooks/pre-commit

動作確認

echo 'sk_live_fake1234567890abcdef' > test-leak.txt
git add test-leak.txt
git commit -m "test"
# → BLOCKED: test-leak.txt contains secret pattern: sk_live_

コミットが止まります。

.gitignoreも強化する

# Auth state / credentials
.note-auth/
*.pem
*.key
*.p12
*-credentials.json
service-account*.json
client_secret*.json
token.json
credentials.json
.env
.env.*
*_cookies.json

まとめ

  • .gitignore → 既知のパターンをブロック(1分)
  • pre-commitフック → 未知のパターンも検出(2分)
  • 2つ合わせて3分で設定完了。今すぐやりましょう。

関連リンク

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?