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?

Code scanning results に "No new alerts" と出るのはなぜ?PR 差分とアラート表示の関係を検証

Posted at

はじめに

GitHub の Advanced Security を利用して tflint などの静的解析を行った際、PR の Checks タブに表示される Code scanning results に疑問が生じました。

具体的には、tflint が警告を出しているにもかかわらず、Checks タブには「No new alerts」と表示されるケースがあるのはなぜなのか?
公式ドキュメントの記述と実際の挙動をもとに検証した結果を整理しました。

ドキュメントでの記載

参考にしたのはこちらの GitHub Docs です:

Code scanning displays alerts in pull requests only when all the lines of code identified by the alert exist in the pull request diff.

和訳:

コードスキャンは、プルリクエストの差分にアラートで特定されたコード行がすべて存在する場合にのみ、プルリクエストにアラートを表示します。

なるほど、そういうものなのでしょうか?
実際に試してみましょう。

検証

ワークフローの準備

tflint を実行して SARIF ファイルを生成・アップロードするワークフローを作成します。

name: code-scanning-example

on: pull_request

jobs:
  tflint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup tflint
        uses: terraform-linters/setup-tflint@v4
        with:
          tflint_version: v0.57.0

      - name: tflint
        run: |
          tflint --init
          tflint --disable-rule=terraform_required_version --disable-rule=terraform_required_providers -f sarif > tflint-results.sarif

      - name: Upload SARIF file
        uses: github/codeql-action/upload-sarif@v3
        if: success() || failure()
        with:
          sarif_file: "tflint-results.sarif"

PR 作成前のコード

デフォルトブランチには、以下のような S3 バケットを定義する Terraform コードを用意しておきます。

main.tf
locals {
  bucket_prefix = "test"
}

resource "aws_s3_bucket" "dummy" {
  bucket_prefix = local.bucket_prefix
}

terraform apply は不要です。

パターン 1:PR の差分にアラート対象の行が含まれていない場合

以下のように resource ブロックを削除し、ローカル変数 bucket_prefix の使用をやめたとします。

main.tf
locals {
  bucket_prefix = "test"
}

# resource "aws_s3_bucket" "dummy" {
#   bucket_prefix = local.bucket_prefix
# }

tflint は「未使用の変数」として警告しますが、PR の 差分には locals の行が含まれているだけです。

結果として、Checks タブの Code scanning results には:

✅「No new alerts」

と表示されました。

image.png

ただし、「View all branch alerts」リンクから遷移すると、実際にはアラートが記録されていることが確認できます。

image.png

パターン 2:PR の差分にアラート対象の行が含まれている場合

次に、未使用変数 unused を新たに追加したパターンを検証します。

main.tf
locals {
  bucket_prefix = "test"
  unused        = "dummy" ### 新規未使用変数
}

resource "aws_s3_bucket" "dummy" {
  bucket_prefix = local.bucket_prefix
}

この場合、未使用変数の定義が PR の差分に含まれるため、Code scanning results に以下のように表示されました:

⚠️ New alerts found

image.png

まとめ

  • PR の Checks に表示されるアラートは、「アラート対象のコード行すべてが PR の差分に含まれている場合」に限られる

  • 既存のコードが PR によって間接的に問題のある状態になっても、その行が変更されていなければアラートは表示されない

  • 実際のアラートは Security タブの「Code scanning」から確認可能

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?