LoginSignup
7
1

More than 1 year has passed since last update.

GitHub Actions での Commit Message Injection に気をつける

Last updated at Posted at 2021-12-02

KINTO Technologies Advent Calendar 2021 - Qiita の2日目の記事です。

経緯

GitHub のレポジトリに push して GitHub Actions でビルドを実行して結果を Slack に通知するというユースケースは多いかと思いますが、ある時特定のコミットだけビルドが落ちる現象に遭遇しました。
GitHub Actions のログを調べてみると Slack の API を叩くところで落ちているようでした。

- name: Slack Notification
  run: |
    jq -n '{
      username: "${{ github.event.repository.name }} ",
      attachments: [
        {
          "author_name": "${{ github.actor }}",
          "author_icon": "${{ github.event.sender.avatar_url }}",
          "title": ${{ github.event.head_commit.message }}
        }
      ]
    }' | curl -H 'Content-Type: application/json' -d @- ${{ secrets.SLACK_WEBHOOK_URL }}

やっていることは jq コマンドでリクエストデータを作って curl コマンドでポストしているだけです。しかし、コミットメッセージに特殊記号が含まれていると jq コマンドでエラーになってしまい、ビルドが落ちてしまっているようでした。
ここでふと、コミットメッセージを Slack API のリクエストになるよう書き換えたらインジェクションできてしまうのでは・・?と思い以下のようにリクエストを書き換えて実行すると、なんとインジェクションが成功してしまいました!

Commit Message Injection", title_link: "https://www.kinto-technologies.com/

ということでサニタイズの処理を入れておきましょう。

対応

jq コマンドには --arg オプションというものがあるのでこれを使います。 
(補足: --arg a v set variable $a to value <v>;
jq コマンドの1行目を jq -n --arg msg "${{ github.event.head_commit.message }} と書き換え、 ${{ github.event.head_commit.message }} の箇所を $msg に書き換えます。

- name: Slack Notification
  run: |
    jq -n --arg msg "${{ github.event.head_commit.message }} '{
      username: "${{ github.event.repository.name }} ",
      attachments: [
        {
          "author_name": "${{ github.actor }}",
          "author_icon": "${{ github.event.sender.avatar_url }}",
          "title": $msg
        }
      ]
    }' | curl -H 'Content-Type: application/json' -d @- ${{ secrets.SLACK_WEBHOOK_URL }}

これでコミットメッセージはただの文字列として扱われるようになり安全にコミットメッセージが Slack にポストされるようになりました :tada:

当社では、トヨタ車のサブスク「KINTO」等の企画/開発を行っており、エンジニアを募集中です。
KINTO Technologies コーポレートサイト

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