Secret scanning とは
公式ページの内容を、ChatGPTで要約
Secret scanning は、APIキーやパスワード、トークンなどの機密情報が誤ってリポジトリに含まれないように検出し防止するセキュリティ機能です。
これを有効にすると、リポジトリ内のコミットをスキャンして、既知のシークレットを発見した場合にアラートがリポジトリ管理者に通知されます。
スキャンは、リポジトリ内のすべてのブランチとGit履歴に対して行われ、GitHubでは新たにサポートされたシークレットタイプが追加されるたびに、パブリックリポジトリの過去のコンテンツも再スキャンされます。
手順
リポジトリを用意
-
GitHubのUI上で、ダミーのPublicリポジトリを作成
-
リポジトリの「Settings」タブを開く
-
左側のサイドバーの「Code Security」をクリック
-
「Secret Protection」及び「Push protection」の項目が有効になっていることを確認
実際にアラートを発生させてみる
-
上記で作成したリポジトリをローカルにclone
$ git clone git@github.com:b-coffin/secret_scanning_sample.git
-
ダミーのシークレット情報を入れたファイルを作成
$ echo 'AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLF' >> secret.txt $ echo 'AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEZ' >> secret.txt
※ ダミーの値はAWSの公式に記載されている値の末尾1文字を変えたもの
-
commit & push
$ git add secret.txt $ git commit -m 'シークレット情報を入れたファイルを作成' $ git push
-
すると、下記のようなアラートが発生
Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 409 bytes | 409.00 KiB/s, done. Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 remote: error: GH013: Repository rule violations found for refs/heads/main. remote: remote: - GITHUB PUSH PROTECTION remote: ————————————————————————————————————————— remote: Resolve the following violations before pushing again remote: remote: - Push cannot contain secrets remote: remote: remote: (?) Learn how to resolve a blocked push remote: https://docs.github.com/code-security/secret-scanning/working-with-secret-scanning-and-push-protection/working-with-push-protection-from-the-command-line#resolving-a-blocked-push remote: remote: remote: —— Amazon AWS Access Key ID —————————————————————————— remote: locations: remote: - commit: **************************************** remote: path: secret.txt:1 remote: remote: (?) To push, remove secret from commit(s) or follow this URL to allow the secret. remote: https://github.com/b-coffin/secret_scanning_sample/security/secret-scanning/unblock-secret/*************************** remote: remote: remote: —— Amazon AWS Secret Access Key —————————————————————— remote: locations: remote: - commit: **************************************** remote: path: secret.txt:2 remote: remote: (?) To push, remove secret from commit(s) or follow this URL to allow the secret. remote: https://github.com/b-coffin/secret_scanning_sample/security/secret-scanning/unblock-secret/*************************** remote: remote: remote: To github.com:b-coffin/secret_scanning_sample.git ! [remote rejected] main -> main (push declined due to repository rule violations) error: failed to push some refs to 'github.com:b-coffin/secret_scanning_sample.git'