Skip to content

fix: add allow-unsafe-pr-checkout: true to checkout steps#794

Merged
BLumia merged 1 commit into
masterfrom
fix/allow-unsafe-pr-checkout
Jul 21, 2026
Merged

fix: add allow-unsafe-pr-checkout: true to checkout steps#794
BLumia merged 1 commit into
masterfrom
fix/allow-unsafe-pr-checkout

Conversation

@deepin-ci-robot

@deepin-ci-robot deepin-ci-robot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Problem

When using pull_request_target with repository: ${{ github.event.pull_request.head.repo.full_name }} to checkout from a fork, the actions/checkout action requires allow-unsafe-pr-checkout: true to be explicitly set.

Without this option, the checkout may fail for fork PRs due to security restrictions.

Changes

Add allow-unsafe-pr-checkout: true to all actions/checkout steps that use ref: ${{ github.event.pull_request.head.sha }} and repository: ${{ github.event.pull_request.head.repo.full_name }}.

File Changes
auto-tag.yml +1
build-distribution.yml +4
commitlint.yml +1
doc-check.yml +1
dtk-unittest.yml +1
license-check.yml +3

Summary by Sourcery

CI:

  • Set allow-unsafe-pr-checkout: true on actions/checkout steps that use the pull request head ref and repository in CI workflows for fork PRs.

When checking out from fork repos, the allow-unsafe-pr-checkout option
must be set to true to allow actions/checkout to fetch from the fork
repository.
@sourcery-ai

sourcery-ai Bot commented Jul 21, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR updates multiple GitHub Actions workflows to explicitly enable unsafe PR checkouts when using forked repositories with pull_request_target, ensuring actions/checkout works with refs from fork PRs.

Sequence diagram for GitHub Actions fork PR checkout with allow-unsafe-pr-checkout

sequenceDiagram
  actor Developer
  participant GitHubActions
  participant actions_checkout
  participant ForkRepo

  Developer->>GitHubActions: open pull_request_target from fork
  GitHubActions->>actions_checkout: run with ref head_sha
  GitHubActions->>actions_checkout: set allow-unsafe-pr-checkout true
  actions_checkout->>ForkRepo: checkout repository head_repo_full_name at head_sha
  actions_checkout-->>GitHubActions: repository checked out for workflow job
Loading

File-Level Changes

Change Details Files
Enable unsafe PR checkout for fork-based pull_request_target workflows using actions/checkout with head SHA and fork repository.
  • Add allow-unsafe-pr-checkout: true to actions/checkout steps that use github.event.pull_request.head.sha and github.event.pull_request.head.repo.full_name in build-distribution workflow.
  • Add allow-unsafe-pr-checkout: true to all fork-based actions/checkout steps in license-check workflow, including those with fetch-depth: 0.
  • Add allow-unsafe-pr-checkout: true to the fork-based actions/checkout step in auto-tag workflow while keeping persist-credentials: false.
  • Add allow-unsafe-pr-checkout: true to the fork-based actions/checkout step in commitlint workflow alongside fetch-depth: 0.
  • Add allow-unsafe-pr-checkout: true to the fork-based actions/checkout step in doc-check workflow with persist-credentials: false.
  • Add allow-unsafe-pr-checkout: true to the fork-based actions/checkout step in dtk-unittest workflow while leaving push-based checkout unchanged.
.github/workflows/build-distribution.yml
.github/workflows/license-check.yml
.github/workflows/auto-tag.yml
.github/workflows/commitlint.yml
.github/workflows/doc-check.yml
.github/workflows/dtk-unittest.yml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown
Contributor Author

deepin pr auto review

★ 总体评分:40分

■ 【总体评价】

代码通过添加参数解决了CI拉取PR代码的安全限制阻断问题,但引入了严重的密钥泄露和恶意代码执行风险
逻辑正确但因全局绕过安全检查导致存在高危漏洞,强制扣分至上限

■ 【详细分析】

  • 1.语法逻辑(正确)✓

代码修改在 YAML 文件中正确添加了 allow-unsafe-pr-checkout: true 参数,缩进和层级关系完全符合 YAML 语法规范。
潜在问题:无
建议:无

  • 2.代码质量(一般)✕

在多个 workflow 文件中机械式地添加同一参数,未根据不同 job 的实际安全需求进行差异化配置。虽然解决了当前 CI 失败的问题,但属于治标不治本的做法。
潜在问题:缺乏对不同 job 执行环境和所需权限的区分;后续维护时可能难以追踪哪些 job 真正需要此权限。
建议:评估每个 workflow 的实际需求,仅在确实需要构建和运行 fork 代码且不涉及敏感操作的 job 中开启此参数;对于涉及发布或部署的 job,应严格限制。

  • 3.代码性能(无性能问题)✓

修改仅涉及 CI 配置参数的添加,不会对代码运行时的算法复杂度或资源占用产生任何影响。
潜在问题:无
建议:无

  • 4.代码安全(存在1个安全漏洞)✕

漏洞对比统计:新增漏洞 1 个,减少漏洞 0 个,持平 0 个
在所有拉取 PR 代码的步骤中启用 allow-unsafe-pr-checkout: true,使得来自 fork 仓库的未受信任代码能够在 CI 环境中完全执行。如果这些 workflow 的 job 中注入了任何 secrets(如 npm token、docker credentials、github token 等),攻击者只需提交一个包含恶意构建脚本或测试脚本的 PR,即可在 CI 运行期间窃取这些机密信息,甚至篡改构建产物。

  • 安全漏洞1:[高危] 在 [actions/checkout 步骤] 中,[不安全的 PR checkout 配置导致敏感信息泄露与恶意代码执行] ——非常重要

  • 建议:移除不必要的 allow-unsafe-pr-checkout: true。对于需要访问 secrets 的 job,应使用 pull_request_target 事件触发,并严格控制 checkout 的代码执行权限;如果必须运行 fork 的代码进行测试,应确保该 job 环境中不注入任何敏感 secrets,或使用隔离的沙箱环境。

■ 【改进建议代码示例】

# 错误示例(当前代码):
#       - uses: actions/checkout@v7
#         if: ${{ github.event_name == 'pull_request' }}
#         with:
#           ref: ${{ github.event.pull_request.head.sha }}
#           repository: ${{ github.event.pull_request.head.repo.full_name }}
#           allow-unsafe-pr-checkout: true
#           persist-credentials: false

# 修复建议:针对不同场景分离处理,避免在含有 secrets 的流程中直接执行 fork 代码
# 对于仅做代码检查的 job,可以保留拉取,但绝对不能在此 job 中注入任何 secrets
      - uses: actions/checkout@v7
        if: ${{ github.event_name == 'pull_request' }}
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          persist-credentials: false
          # 移除 allow-unsafe-pr-checkout: true
          
# 对于需要 secrets 的构建或发布 job,应基于基座分支检出,并手动合并 PR 代码,或使用 pull_request_target
      - uses: actions/checkout@v7
        if: ${{ github.event_name == 'pull_request_target' }}
        with:
          ref: ${{ github.base_ref }}
          persist-credentials: false
      - name: Merge PR
        run: |
          git fetch origin pull/${{ github.event.pull_request.number }}/head:pr-head
          git merge --no-ff pr-head

@deepin-ci-robot

Copy link
Copy Markdown
Contributor Author

@deepin-ci-robot: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
github-pr-review-ci 6f5190f link true /test github-pr-review-ci

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@deepin-ci-robot

Copy link
Copy Markdown
Contributor Author

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: BLumia, deepin-ci-robot

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@BLumia
BLumia merged commit 8b4e60f into master Jul 21, 2026
7 of 10 checks passed

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Given how often this actions/checkout configuration is repeated, consider extracting it into a reusable workflow or composite action to reduce duplication and keep future changes (e.g., to allow-unsafe-pr-checkout) in one place.
  • It may help future maintainers if you add a brief inline comment near the allow-unsafe-pr-checkout: true lines explaining that this is required for fork PRs using pull_request_target with repository overrides, so it’s not mistaken for an unnecessary security relaxation.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Given how often this `actions/checkout` configuration is repeated, consider extracting it into a reusable workflow or composite action to reduce duplication and keep future changes (e.g., to `allow-unsafe-pr-checkout`) in one place.
- It may help future maintainers if you add a brief inline comment near the `allow-unsafe-pr-checkout: true` lines explaining that this is required for fork PRs using `pull_request_target` with `repository` overrides, so it’s not mistaken for an unnecessary security relaxation.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants