GitHub Actions 安全加固 #

安全是CI/CD的重要考虑因素。本节介绍GitHub Actions的安全最佳实践。

安全威胁 #

常见威胁 #

威胁 描述
密钥泄露 敏感信息暴露
代码注入 恶意代码执行
供应链攻击 依赖被篡改
权限滥用 过度权限

攻击向量 #

  • 恶意PR
  • 被入侵的依赖
  • 不安全的Actions
  • 配置错误

密钥安全 #

不要硬编码密钥 #

yaml
# 错误
env:
  API_KEY: my-secret-key

# 正确
env:
  API_KEY: ${{ secrets.API_KEY }}

不要打印密钥 #

yaml
# 错误
- run: echo ${{ secrets.API_KEY }}

# 正确
- env:
    API_KEY: ${{ secrets.API_KEY }}
  run: curl -H "Authorization: Bearer $API_KEY" https://api.example.com

使用掩码 #

yaml
- run: |
    echo "::add-mask::${{ secrets.MY_SECRET }}"

使用环境密钥 #

yaml
jobs:
  deploy:
    environment: production
    steps:
      - env:
          DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}

代码注入防护 #

避免使用用户输入 #

yaml
# 危险
- run: echo "${{ github.event.issue.title }}"

# 安全
- env:
    TITLE: ${{ github.event.issue.title }}
  run: echo "$TITLE"

使用环境变量 #

yaml
# 危险
- run: ./script.sh ${{ github.event.inputs.param }}

# 安全
- env:
    PARAM: ${{ github.event.inputs.param }}
  run: ./script.sh "$PARAM"

验证输入 #

yaml
- name: Validate input
  run: |
    if [[ ! "${{ github.event.inputs.version }}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
      echo "Invalid version format"
      exit 1
    fi

Actions安全 #

使用固定版本 #

yaml
# 推荐
- uses: actions/checkout@v4

# 最安全
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9

# 不推荐
- uses: actions/checkout@main

审查Actions #

使用前检查:

  • 作者信誉
  • Stars和使用量
  • 源代码
  • 权限需求
  • 更新频率

限制Actions来源 #

在仓库设置中:

  1. 进入 Settings → Actions → General
  2. 选择 “Allow actions created by GitHub”
  3. 或指定允许的Actions

使用Dependabot #

yaml
# .github/dependabot.yml
version: 2
updates:
  - package-ecosystem: "github-actions"
    directory: "/"
    schedule:
      interval: "weekly"

工作流安全 #

使用最小权限 #

yaml
permissions:
  contents: read

使用并发控制 #

yaml
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

设置超时 #

yaml
jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 30

使用环境保护 #

yaml
jobs:
  deploy:
    environment: production

Fork PR安全 #

了解风险 #

来自Fork的PR可能包含恶意代码。

安全措施 #

yaml
# 不要在PR上使用pull_request_target
on:
  pull_request:
    branches: [main]

# 如果必须使用pull_request_target
on:
  pull_request_target:
    branches: [main]

jobs:
  build:
    if: github.event.pull_request.head.repo.full_name == github.repository
    # 只处理同仓库的PR

检查PR来源 #

yaml
jobs:
  build:
    runs-on: ubuntu-latest
    if: github.event.pull_request.head.repo.full_name == github.repository
    steps:
      - uses: actions/checkout@v4

供应链安全 #

锁定依赖版本 #

yaml
# 使用lock文件
- run: npm ci  # 使用package-lock.json
- run: pip install -r requirements.txt

验证依赖完整性 #

yaml
- name: Verify dependencies
  run: npm audit --audit-level=high

使用SBOM #

yaml
- uses: anchore/sbom-action@v0
  with:
    artifact-name: sbom.spdx.json

漏洞扫描 #

代码扫描 #

yaml
- uses: github/codeql-action/init@v2
  with:
    languages: javascript
- uses: github/codeql-action/analyze@v2

依赖扫描 #

yaml
- uses: snyk/actions/node@master
  env:
    SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}

容器扫描 #

yaml
- uses: aquasecurity/trivy-action@master
  with:
    image-ref: myapp:latest
    severity: CRITICAL,HIGH

密钥扫描 #

yaml
- uses: trufflesecurity/trufflehog@main
  with:
    path: ./
    base: ${{ github.event.repository.default_branch }}

安全配置示例 #

安全的CI工作流 #

yaml
name: Secure CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  security-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Run Trivy vulnerability scanner
        uses: aquasecurity/trivy-action@master
        with:
          scan-type: 'fs'
          scan-ref: '.'
          severity: 'CRITICAL,HIGH'

  build:
    needs: security-check
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4
      
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      
      - run: npm ci
      - run: npm audit --audit-level=high
      - run: npm test

安全的部署工作流 #

yaml
name: Secure Deploy

on:
  push:
    branches: [main]

permissions:
  contents: read
  id-token: write
  deployments: write

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://example.com
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4
      
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: arn:aws:iam::123456789012:role/myRole
          aws-region: us-east-1
      
      - name: Deploy
        run: ./deploy.sh

最佳实践清单 #

密钥管理 #

  • [ ] 使用Secrets存储敏感信息
  • [ ] 不要硬编码密钥
  • [ ] 不要打印密钥
  • [ ] 使用环境密钥

权限控制 #

  • [ ] 使用最小权限
  • [ ] 配置工作流权限
  • [ ] 使用OIDC认证
  • [ ] 使用环境保护

代码安全 #

  • [ ] 避免代码注入
  • [ ] 验证用户输入
  • [ ] 使用环境变量

Actions安全 #

  • [ ] 使用固定版本
  • [ ] 审查第三方Actions
  • [ ] 限制Actions来源
  • [ ] 使用Dependabot

供应链安全 #

  • [ ] 锁定依赖版本
  • [ ] 扫描漏洞
  • [ ] 使用SBOM

下一步学习 #

小结 #

  • 使用最小权限原则
  • 保护密钥安全
  • 防止代码注入
  • 审查第三方Actions
  • 使用环境保护
  • 定期扫描漏洞
  • 锁定依赖版本
最后更新:2026-03-28