CodeQL 文档

Actions 中的表达式注入

ID: js/actions/command-injection
Kind: problem
Security severity: 9.3
Severity: warning
Precision: high
Tags:
   - actions
   - security
   - external/cwe/cwe-094
Query suites:
   - javascript-code-scanning.qls
   - javascript-security-extended.qls
   - javascript-security-and-quality.qls

点击查看 CodeQL 代码库中的查询

在 GitHub Actions 中使用用户控制的输入可能会导致代码注入,例如在 run:script: 上下文中。

GitHub Actions 中的代码注入可能允许攻击者窃取工作流程中使用的任何密钥以及临时 GitHub 代码库授权令牌。该令牌可能具有对代码库的写入权限,允许攻击者使用该令牌对代码库进行更改。

建议

避免 GitHub 工作流程中出现代码注入漏洞的最佳实践是将表达式的不可信输入值设置为中间环境变量,然后使用 shell/脚本解释器的原生语法(即,不是 ${{ env.VAR }})使用环境变量。

还建议限制工作流程使用的任何令牌(如 GITHUB_TOKEN)的权限。

示例

以下示例允许用户注入任意的 shell 命令

on: issue_comment

jobs:
  echo-body:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo '${{ github.event.comment.body }}'

以下示例使用环境变量,但仍然允许注入,因为使用了表达式语法

on: issue_comment

jobs:
  echo-body:
    runs-on: ubuntu-latest
    steps:
    -  env:
        BODY: ${{ github.event.issue.body }}
      run: |
        echo '${{ env.BODY }}'

以下示例使用 shell 语法读取环境变量,将阻止攻击

on: issue_comment

jobs:
  echo-body:
    runs-on: ubuntu-latest
    steps:
    - env:
        BODY: ${{ github.event.issue.body }}
      run: |
        echo "$BODY"

参考资料

  • ©GitHub, Inc.
  • 条款
  • 隐私