0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHubの Secret scanning を使ってみる

Posted at

Secret scanning とは

公式ページの内容を、ChatGPTで要約

Secret scanning は、APIキーやパスワード、トークンなどの機密情報が誤ってリポジトリに含まれないように検出し防止するセキュリティ機能です。
これを有効にすると、リポジトリ内のコミットをスキャンして、既知のシークレットを発見した場合にアラートがリポジトリ管理者に通知されます。
スキャンは、リポジトリ内のすべてのブランチとGit履歴に対して行われ、GitHubでは新たにサポートされたシークレットタイプが追加されるたびに、パブリックリポジトリの過去のコンテンツも再スキャンされます。

手順

リポジトリを用意

  1. GitHubのUI上で、ダミーのPublicリポジトリを作成

    スクリーンショット 2025-04-15 22.26.22.png

  2. リポジトリの「Settings」タブを開く

    スクリーンショット 2025-04-15 18.50.09.png

  3. 左側のサイドバーの「Code Security」をクリック

  4. 「Secret Protection」及び「Push protection」の項目が有効になっていることを確認

    スクリーンショット 2025-04-15 23.46.27.png
    上記のように、ボタンが「Disable」になっていればOK
    「Enable」になっていたらそれをクリックして有効にする

実際にアラートを発生させてみる

  1. 上記で作成したリポジトリをローカルにclone

    $ git clone git@github.com:b-coffin/secret_scanning_sample.git
    

  2. ダミーのシークレット情報を入れたファイルを作成

    $ echo 'AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLF' >> secret.txt
    $ echo 'AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEZ' >> secret.txt
    

    ※ ダミーの値はAWSの公式に記載されている値の末尾1文字を変えたもの

  3. commit & push

    $ git add secret.txt
    $ git commit -m 'シークレット情報を入れたファイルを作成'
    $ git push
    

  4. すると、下記のようなアラートが発生

    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'
    

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?