3
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?

Firebaseの秘密鍵がGitHubのPush Protectionに引っかかった

Posted at

発生した問題

Firebaseの設定後にGitHubへgit push origin mainを実行したところ、以下のエラーが発生。

! [remote rejected] main -> main (push declined due to repository rule violations)
error: failed to push some refs to 'https://github.com/username/repository.git'

一時的にpushができなくなりました。

原因

GitHubの Push Protection(プッシュ保護) により、Firebaseの秘密鍵(Service Account JSON)がリポジトリに含まれていることが検出され、Pushがブロックされた。

  • GitHubのPush Protectionは手動で設定していなかったのにブロックされた
  • .gitignore に追加しても 過去の履歴に秘密鍵が含まれているためブロックが続く
  • Firebaseの設定変更がPush Protectionに影響した可能性がある

発生の流れ

  1. Firebaseの設定時に、秘密鍵 study-app-a701b-firebase-adminsdk-xxx.json をダウンロード。
  2. .gitignore に追加し忘れたまま git add .git commit -m "Update" を実行し、秘密鍵を誤ってコミット。
  3. GitHubの Secret Scanning(シークレットスキャン) が秘密鍵を検出し、Pushを拒否。
  4. Firebaseの設定変更が影響し、リポジトリのPush Protectionの動作が変わった可能性。

🛠 解決方法

Gitの履歴から完全に削除する必要があるため、以下の手順を実行。

1️⃣ git filter-repo を使用して秘密鍵を履歴から削除

このコマンドは、指定したファイルを過去のすべてのコミット履歴から削除する。

git filter-repo --path study-app-a701b-firebase-adminsdk-xxx.json --invert-paths --force

実行後、GitHubのリモートリポジトリの情報が削除されるため、再設定が必要。

2️⃣ GitHubのリモートリポジトリを再設定

git remote add origin https://github.com/username/repository.git

3️⃣ 強制Push(履歴を書き換えるため --force を使用)

git push origin main --force

🚀 今後の防止策

.gitignore にFirebaseの秘密鍵を追加

echo "study-app-a701b-firebase-adminsdk-*.json" >> .gitignore

環境変数を使用して秘密鍵を管理

  1. Firebaseの秘密鍵をBase64エンコードして環境変数に設定
    cat study-app-a701b-firebase-adminsdk-xxx.json | base64
    
  2. GitHub Actionsの secretsFIREBASE_KEY として登録
  3. main.yml にデコード処理を追加
    - name: Decode Firebase service account key
      run: echo "${{ secrets.FIREBASE_KEY }}" | base64 --decode > ./firebase-key.json
    
  4. Firebaseのデプロイ時に環境変数を利用
    env:
      GOOGLE_APPLICATION_CREDENTIALS: "./firebase-key.json"
    

GitHubのPush Protectionを事前に確認

GitHubの Push Protection が有効になっているか確認し、シークレットスキャンに引っかからないか意識する。

✅ まとめ

  • Firebaseの秘密鍵がGitの履歴に残っていると、Pushが拒否される
  • .gitignore だけでは解決せず、git filter-repo を使って履歴から削除する必要がある。
  • 環境変数を活用し、秘密鍵をGitに含めない運用を徹底することが重要!
  • Firebaseの設定変更がPush Protectionに影響する可能性があるため、事前に認識しておくべき

Firebaseの秘密鍵は生の状態で絶対にGitにコミットしない🔥

3
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
3
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?