Code Scanning & Secret Scanning
この章では、コードの脆弱性を検出するCode Scanningと、シークレットの漏洩を防ぐSecret Scanningについて学びます。
Code Scanning
Section titled “Code Scanning”Code Scanningとは
Section titled “Code Scanningとは”Code Scanningは、コード内のセキュリティ脆弱性を自動検出する機能です。主にCodeQLを使用して静的解析を行います。
CodeQLが対応する言語:
| 言語 | サポート状況 |
|---|---|
| JavaScript/TypeScript | ✅ フルサポート |
| Python | ✅ フルサポート |
| Java/Kotlin | ✅ フルサポート |
| C/C++ | ✅ フルサポート |
| C# | ✅ フルサポート |
| Go | ✅ フルサポート |
| Ruby | ✅ フルサポート |
| Swift | ✅ サポート |
有効化(デフォルト設定)
Section titled “有効化(デフォルト設定)”Settings → Security → Code security and analysis:
Code scanning → Set up → Default自動で .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 }}"クエリスイート
Section titled “クエリスイート”| スイート | 説明 |
|---|---|
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 │└─────────────────────────────────────────┘アラートへの対応
Section titled “アラートへの対応”- Dismiss: 誤検知の場合
- Won’t fix: 対応しない
- False positive: 誤検知
- Used in tests: テストコード
- 修正: コードを修正してPR
- コメント: 調査結果を記録
Secret Scanning
Section titled “Secret Scanning”Secret Scanningとは
Section titled “Secret Scanningとは”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(プッシュ保護)アラートの確認
Section titled “アラートの確認”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 │└─────────────────────────────────────────┘アラートへの対応
Section titled “アラートへの対応”- シークレットを無効化: 漏洩したキーをすぐに無効化
- 新しいシークレットを発行: 新しいキーを生成
- 履歴から削除: git filter-branch等で履歴から削除
- 環境変数に移行: ハードコードをやめる
Push Protection
Section titled “Push Protection”Push Protectionとは
Section titled “Push Protectionとは”シークレットを含むコミットのプッシュを事前にブロックする機能です。
Settings → Security → Code security and analysis:
✅ Push protection$ 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:5remote:remote: To push, you must remove the secret from your commits.バイパス方法
Section titled “バイパス方法”正当な理由がある場合、バイパス可能:
- WebUIでバイパス理由を選択
- 理由を記録してプッシュ
バイパス理由:
- False positive: 誤検知
- Used in tests: テスト用
- Will fix later: 後で修正
Q1. GitHub Code Scanningで使用されるデフォルトの解析エンジンは何ですか?
Q2. Secret Scanningの主な目的は何ですか?
Q3. Push Protectionが有効な場合、シークレットを含むプッシュはどうなりますか?