Push protection
誤って token や secret を push しようとしたときに、GitHub 側で push をブロックしてくれる機能です。
secret scanning の一部として提供されているため、使用するにはsecret scanning も有効にしておく必要があります。
GitHub Advanced Securityの一部であり、プライベートリポジトリではGitHub Advanced Securityという有料ライセンス契約が必要です。ただし、パブリックリポジトリでは無料利用できます。
有効化
対象 repository で以下の 2 つを有効にします。
Secret scanning
Secret scanning push protection
GitHub API では、それぞれ以下の項目に対応します。
security_and_analysis.secret_scanning.status
security_and_analysis.secret_scanning_push_protection.status
どちらも enabled になっていれば有効化されています。
方法1:GitHub UI から確認・変更する場合
repository の画面から以下を開きます。
Settings → Code security → Secret scanning
この中で secret scanning と push protection の状態を確認・変更できます。
方法2:API
GitHub CLI で対象 repository に対して PATCH します。操作には repository の admin 権限が必要です。
OWNER と REPO は対象 repository に置き換えます。
gh api --method PATCH "repos/${OWNER}/${REPO}" -f 'security_and_analysis[secret_scanning][status]=enabled' -f 'security_and_analysis[secret_scanning_push_protection][status]=enabled'
zsh では [] が glob として解釈されるため、-f の値は quote しておくのが安全です。
設定は repository の security_and_analysis で確認できます。
gh api "repos/${OWNER}/${REPO}" --jq '.security_and_analysis'
その他セキュリティ機能との関係
push protection は「secret の push を止める」ための設定です。
通常の開発フローを制御する branch protection とは役割が違います。
main への直接 push 禁止、PR 必須化、required checks などを設定していなくても、push protection だけを有効化できます。
必要に応じて別途、以下のような設定を行うことが重要となります。
default branch への直接 push 禁止
PR 必須化
required review
required status checks
force push 禁止
参考
プッシュ保護について - GitHub Enterprise Cloud Docs
マネーフォワードのGitHub不正アクセス事件をエンジニア視点で読み解く — なぜソースコードに本番カード情報と認証キーが入っていたのか