コンテンツにスキップ

Code Scanning & Secret Scanning

この章では、コードの脆弱性を検出するCode Scanningと、シークレットの漏洩を防ぐSecret Scanningについて学びます。

Code Scanningは、コード内のセキュリティ脆弱性を自動検出する機能です。主にCodeQLを使用して静的解析を行います。

CodeQLが対応する言語:

言語サポート状況
JavaScript/TypeScript✅ フルサポート
Python✅ フルサポート
Java/Kotlin✅ フルサポート
C/C++✅ フルサポート
C#✅ フルサポート
Go✅ フルサポート
Ruby✅ フルサポート
Swift✅ サポート

Settings → Security → Code security and analysis:

Code scanning → Set up → Default

自動で .github/workflows/codeql.yml が作成されます。

.github/workflows/codeql.yml
name: "CodeQL"
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
schedule:
- cron: '0 0 * * 1' # 毎週月曜日
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: ['javascript', 'python']
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
queries: +security-extended
- name: Autobuild
uses: github/codeql-action/autobuild@v3
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{ matrix.language }}"
スイート説明
default標準的なセキュリティクエリ
security-extendedより多くのセキュリティクエリ
security-and-qualityセキュリティ + コード品質

Security → Code scanning alerts で確認:

┌─────────────────────────────────────────┐
│ High (3) │
├─────────────────────────────────────────┤
│ ⚠️ SQL Injection │
│ src/db/query.js:45 │
│ CWE-89 │
├─────────────────────────────────────────┤
│ ⚠️ Cross-site Scripting (XSS) │
│ src/components/Comment.jsx:23 │
│ CWE-79 │
├─────────────────────────────────────────┤
│ ⚠️ Insecure Randomness │
│ src/utils/token.js:12 │
│ CWE-330 │
└─────────────────────────────────────────┘
  1. Dismiss: 誤検知の場合
    • Won’t fix: 対応しない
    • False positive: 誤検知
    • Used in tests: テストコード
  2. 修正: コードを修正してPR
  3. コメント: 調査結果を記録

Secret Scanningは、リポジトリ内のシークレット(APIキー、トークン等)を検出する機能です。

100種類以上のシークレットパターンを検出:

  • AWS Access Key
  • GitHub Personal Access Token
  • Google API Key
  • Stripe API Key
  • Slack Webhook URL
  • その他多数

Settings → Security → Code security and analysis:

✅ Secret scanning
✅ Push protection(プッシュ保護)

Security → Secret scanning alerts:

┌─────────────────────────────────────────┐
│ Active (2) │
├─────────────────────────────────────────┤
│ 🔑 AWS Access Key │
│ config/aws.js:5 │
│ Detected: 2024-01-15 │
│ Partner: Amazon Web Services │
├─────────────────────────────────────────┤
│ 🔑 GitHub Personal Access Token │
│ scripts/deploy.sh:12 │
│ Detected: 2024-01-14 │
└─────────────────────────────────────────┘
  1. シークレットを無効化: 漏洩したキーをすぐに無効化
  2. 新しいシークレットを発行: 新しいキーを生成
  3. 履歴から削除: git filter-branch等で履歴から削除
  4. 環境変数に移行: ハードコードをやめる

シークレットを含むコミットのプッシュを事前にブロックする機能です。

Settings → Security → Code security and analysis:

✅ Push protection
Terminal window
$ git push origin main
remote: error: GH013: Repository rule violations found.
remote:
remote: Secret scanning detected the following secrets:
remote: - AWS Access Key ID in config/aws.js:5
remote:
remote: To push, you must remove the secret from your commits.

正当な理由がある場合、バイパス可能:

  1. WebUIでバイパス理由を選択
  2. 理由を記録してプッシュ

バイパス理由:

  • False positive: 誤検知
  • Used in tests: テスト用
  • Will fix later: 後で修正

Q1. GitHub Code Scanningで使用されるデフォルトの解析エンジンは何ですか?

Q2. Secret Scanningの主な目的は何ですか?

Q3. Push Protectionが有効な場合、シークレットを含むプッシュはどうなりますか?