PAYATHON 2026

workflow_run 中安全发布 fork PR 报告,避免 checkout fork 代码

数据小陈

结论

不要在具有 pull-requests: write 的 workflow_run job 中 checkout 或执行 fork PR 的代码。发布报告时,只需要从原 workflow run 下载 artifact,然后用可信的 base 仓库代码或内联脚本调用 GitHub API 写评论。把原来的 Checkout PR source 删掉;如果必须使用 tools/publish_report.py,只 checkout base 仓库的默认分支或固定 SHA,并设置 persist-credentials: false。

原因

workflow_run 工作流通常位于 base 仓库的默认分支上下文中,并且带有写权限。它不需要 PR 的源代码也能发布评论。workflow_run.head_repository.full_name 和 workflow_run.head_sha 指向 fork 的 head commit,按这两个值 checkout,就等于把不受信任的代码带进有特权的 job,还可能把它执行了。artifact 里的 report.json 也不能直接信任:只解析 JSON,不执行其中的命令或脚本;发评论前做转义并限制长度。

推荐改法

  1. 把 Checkout PR source 里针对 fork 的 repository 和 ref 设置删掉。
  2. 发布脚本确实需要时,只 checkout 当前 base 仓库的默认分支或固定 SHA,不要 checkout head_repository.full_name。
  3. actions/download-artifact@v8 继续用来下载原 run 的 artifact。
  4. PR 编号先取 github.event.workflow_run.pull_requests[0].number;为空时,用 head_repository.full_name 和 head_branch 调 API 查打开的 PR,并确认 head.sha 等于 workflow_run.head_sha。artifact 里的 PR 编号不能直接信。
  5. 通过 GitHub API 创建 issue comment;报告内容做 HTML 转义,防止 Markdown 或 shell 注入。
name: Publish PR report

on:
  workflow_run:
    workflows: ['PR validation']
    types: [completed]

permissions:
  actions: read
  contents: read
  pull-requests: write

jobs:
  publish:
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout trusted base repository
        uses: actions/checkout@v7
        with:
          ref: ${{ github.event.repository.default_branch }}
          persist-credentials: false

      - name: Download report
        uses: actions/download-artifact@v8
        with:
          name: validation-report
          run-id: ${{ github.event.workflow_run.id }}
          github-token: ${{ secrets.GITHUB_TOKEN }}
          path: out

      - name: Publish report
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
          HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
        run: python tools/publish_report.py out/report.json

PR_NUMBER 在 pull_requests 为空时可能为空,发布脚本要显式处理,并回退查询。下面这个 Python 示例只读取 JSON,校验 PR head SHA,再调用 API 发评论:

import html
import json
import os
import sys
import urllib.request

report_path = sys.argv[1]
with open(report_path, encoding='utf-8') as f:
    report = json.load(f)

token = os.environ['GITHUB_TOKEN']
repo = os.environ['GITHUB_REPOSITORY']
pr_number = os.environ.get('PR_NUMBER', '')
head_sha = os.environ.get('HEAD_SHA', '')

if not pr_number:
    raise SystemExit('PR_NUMBER is empty; query and validate the PR by head_repository/head_branch')

headers = {
    'Authorization': f'Bearer {token}',
    'Accept': 'application/vnd.github+json',
    'X-GitHub-Api-Version': '2022-11-28',
}

def api_get(url):
    req = urllib.request.Request(url, headers=headers)
    with urllib.request.urlopen(req) as resp:
        return json.load(resp)

def api_post(url, payload):
    data = json.dumps(payload).encode('utf-8')
    req = urllib.request.Request(url, data=data, method='POST', headers={**headers, 'Content-Type': 'application/json'})
    with urllib.request.urlopen(req) as resp:
        return json.load(resp)

pr = api_get(f'https://api.github.com/repos/{repo}/pulls/{pr_number}')
if pr['head']['sha'] != head_sha:
    raise SystemExit('PR head SHA does not match workflow_run.head_sha')

report_text = html.escape(json.dumps(report, ensure_ascii=False, indent=2))
body = f'''## PR validation report

<details><summary>report.json</summary>

<pre>{report_text[:60000]}</pre>

</details>'''

api_post(f'https://api.github.com/repos/{repo}/issues/{pr_number}/comments', {'body': body})

注意事项

  • 不要为了绕过 checkout 失败就启用不安全的 checkout 选项,也不要在有特权的 workflow_run 里执行 fork 的测试或脚本。
  • actions/checkout@v7 只负责取 base 仓库里的发布脚本;用不到脚本时,这一步可以直接删掉。
  • artifact 下载下来仍然不可信;不要 eval,不要拼 shell 命令,也不要根据报告内容动态执行代码。
  • 想更严格的话,可以把发布脚本固定到受保护分支的某个 SHA,或者直接用 actions/github-script 内联发布逻辑。

备注:内容仅供参考。