使用pull_request_target触发的GitHub Workflow安全风险咨询
问题:使用pull_request_target触发的GitHub Action是否存在安全风险?
我正尝试实现一个GitHub Action工作流,为新的Pull Request添加评论。了解到pull_request_target触发器会让工作流在本仓库环境中运行,核心安全原则是避免使用actions/checkout@v2以防执行不可信代码。我的方案是直接从仓库主分支下载用于评论的Markdown文件,请问该方案仍存在安全风险吗?
我的工作流代码如下:
name: Checklist on: pull_request_target: types: - opened jobs: welcome: runs-on: ubuntu-latest steps: - uses: actions/github-script@v6 with: script: | curl https://raw.githubusercontent.com/org/repo/main/markdown-file.md -o markdown-file.md const fs = require('fs'); const body = fs.readFileSync('markdown-file.md') await github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body: body.toString() })
分析与结论
你的方案不存在显著安全风险,原因如下:
- 运行环境与内容可信性:
pull_request_target在基础仓库(而非PR提交的分支)环境中执行,你通过curl拉取的是仓库主分支的可信Markdown文件,完全没有接触PR分支的不可信代码,不会触发PR中的恶意逻辑。 - 规避了核心风险点:没有使用
actions/checkout,避免了将PR分支代码拉取到运行环境,彻底隔绝了PR中恶意脚本、配置文件的执行可能。
额外安全建议
- 可以替换curl操作,直接用GitHub API读取主分支文件,无需本地写入读取,流程更简洁且减少依赖:
name: Checklist on: pull_request_target: types: - opened jobs: welcome: runs-on: ubuntu-latest steps: - uses: actions/github-script@v6 with: script: | const response = await github.rest.repos.getContent({ owner: context.repo.owner, repo: context.repo.repo, path: 'markdown-file.md', ref: 'main' }); const body = Buffer.from(response.data.content, 'base64').toString(); await github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body: body }) - 确保主分支的Markdown文件内容安全,不过GitHub评论会自动过滤危险HTML,这一点风险极低。
- 固定使用的Action版本(如
actions/github-script@v6),避免依赖未固定版本带来的潜在风险。
内容的提问来源于stack exchange,提问作者TedoDec
相关产品推荐
相关产品推荐

