Skip to content

fix(ci): upload fork PR coverage safely - #2008

Merged
josecelano merged 14 commits into
torrust:developfrom
josecelano:2006-fix-fork-pr-coverage-upload-workflow
Jul 21, 2026
Merged

josecelano merged 14 commits into
torrust:developfrom
josecelano:2006-fix-fork-pr-coverage-upload-workflow

Conversation

@josecelano

Copy link
Copy Markdown
Member

Summary

Moves the privileged coverage uploader checkout before fork-produced artifact retrieval and removes the artifact-derived checkout ref.

Codecov still receives the downloaded report with explicit pull request and source commit overrides, while the upload job checks out only trusted default-branch code.

Validation

  • linter all
  • Pre-push checks: nightly format, nightly check, nightly docs, stable full tests
  • Workflow diff review and focused YAML lint

Remaining Verification

The required fork pull request and Codecov upload verification will run through this PR after CI starts.

Closes #2006

Copilot AI review requested due to automatic review settings July 20, 2026 17:32
@josecelano
josecelano requested a review from a team as a code owner July 20, 2026 17:32
@josecelano josecelano self-assigned this Jul 20, 2026

Copilot AI 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.

Pull request overview

This PR addresses #2006 by changing the privileged workflow_run coverage uploader workflow to avoid checking out fork-controlled refs while still uploading the fork-produced coverage report to Codecov.

Changes:

  • Adds a trusted default-branch checkout step early in upload_coverage_pr.yaml and removes the artifact-derived ref checkout.
  • Adds an issue specification document tracking scope, risks, and verification for fixing fork PR coverage uploads.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
docs/issues/open/2006-fix-fork-pr-coverage-upload-workflow.md Adds the implementation/verification spec for issue #2006.
.github/workflows/upload_coverage_pr.yaml Reorders checkout ahead of artifact retrieval and removes fork SHA checkout for safer fork PR coverage uploads.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

josecelano added a commit to josecelano/torrust-tracker that referenced this pull request Jul 20, 2026
Copilot AI review requested due to automatic review settings July 20, 2026 18:53

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Copilot AI review requested due to automatic review settings July 20, 2026 20:16

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

josecelano added a commit to josecelano/torrust-tracker that referenced this pull request Jul 21, 2026
Copilot AI review requested due to automatic review settings July 21, 2026 06:52

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

docs/pr-reviews/pr-2008-copilot-suggestions.md:47

  • Thread ID in row 3 has stray * characters, so it no longer matches the real review thread ID format (and won’t match the cspell ignore entry above).
| 3   | PRRT*kwDOGp2yqc6SWYB* | `.github/workflows/upload_coverage_pr.yaml`                     | https://github.com/torrust/torrust-tracker/pull/2008#discussion_r3616793119 | Reject symlinked files extracted from fork-produced artifact archives.                      | action: require the three expected artifact paths to be regular files and reject symlinks before reading or uploading them.                          | DONE   | RESOLVED     |

Copilot AI review requested due to automatic review settings July 21, 2026 07:21

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

josecelano added a commit to josecelano/torrust-tracker that referenced this pull request Jul 21, 2026
Copilot AI review requested due to automatic review settings July 21, 2026 07:36

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

.github/workflows/upload_coverage_pr.yaml:130

  • pr_number.txt and commit_sha.txt come from fork-produced artifacts. Writing their raw contents into $GITHUB_OUTPUT via echo "name=$(<file)" is vulnerable to output injection if the file contains newlines (an attacker can append extra output keys). Sanitize/validate these values (numeric PR number, 40-hex SHA) and write them with printf after stripping newlines.
          # Make the params available as step output
          echo "override_pr=$(<"$artifact_dir/pr_number.txt")" >> "$GITHUB_OUTPUT"
          echo "override_commit=$(<"$artifact_dir/commit_sha.txt")" >> "$GITHUB_OUTPUT"

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 21, 2026 08:53
josecelano added a commit to josecelano/torrust-tracker that referenced this pull request Jul 21, 2026
@josecelano
josecelano force-pushed the 2006-fix-fork-pr-coverage-upload-workflow branch from 409c493 to ff0c335 Compare July 21, 2026 09:00
@josecelano

Copy link
Copy Markdown
Member Author

ACK ff0c335

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

docs/pr-reviews/pr-2008-copilot-suggestions.md:55

  • The Thread ID in this row is wrapped in * characters, which alters the opaque ID (Markdown renders it as emphasis) and breaks auditability/searchability. Record the exact thread ID value (optionally wrapped in backticks to keep it literal).
| 3   | PRRT*kwDOGp2yqc6SWYB* | `.github/workflows/upload_coverage_pr.yaml`                     | https://github.com/torrust/torrust-tracker/pull/2008#discussion_r3616793119 | Reject symlinked files extracted from fork-produced artifact archives.                      | action: require the three expected artifact paths to be regular files and reject symlinks before reading or uploading them.                               | DONE   | RESOLVED     |

| 9 | PRRT_kwDOGp2yqc6SfBQ1 | `docs/pr-reviews/pr-2008-copilot-suggestions.md` | https://github.com/torrust/torrust-tracker/pull/2008#discussion_r3620043755 | Add the tracker skill-link marker. | action: added `<!-- skill-link: process-copilot-suggestions -->` for the governing review workflow. | DONE | RESOLVED |
| 10 | PRRT_kwDOGp2yqc6SfecS | `.github/workflows/upload_coverage_pr.yaml` | https://github.com/torrust/torrust-tracker/pull/2008#discussion_r3620210121 | Validate untrusted artifact metadata before writing step outputs. | action: require a numeric PR number and 40-character hexadecimal SHA before emitting Codecov metadata outputs. | DONE | RESOLVED |
| 11 | PRRT_kwDOGp2yqc6SgaMi | `docs/pr-reviews/pr-2008-copilot-suggestions.md` | https://github.com/torrust/torrust-tracker/pull/2008#discussion_r3620551890 | Remove the duplicate processing-log entry. | action: removed the repeated event so each review-batch milestone appears once. | DONE | RESOLVED |
| 12 | PRRT*kwDOGp2yqc6SgaM* | `.github/workflows/upload_coverage_pr.yaml` | https://github.com/torrust/torrust-tracker/pull/2008#discussion_r3620551932 | Clean temporary extraction directories on all paths. | action: run extraction in a subshell with an `EXIT` trap that removes its temporary directory. | DONE | RESOLVED |
Copilot AI review requested due to automatic review settings July 21, 2026 09:01

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

docs/pr-reviews/pr-2008-copilot-suggestions.md:55

  • Row 3 still records the thread ID with * characters (PRRT*kwDOGp2yqc6SWYB*), which won’t match the real opaque GitHub thread ID format and contradicts row 8’s decision text (“restored PRRT_kwDOGp2yqc6SWYB_”). Update the table entry to the exact ID so the audit log remains searchable and consistent.
| 3   | PRRT*kwDOGp2yqc6SWYB* | `.github/workflows/upload_coverage_pr.yaml`                     | https://github.com/torrust/torrust-tracker/pull/2008#discussion_r3616793119 | Reject symlinked files extracted from fork-produced artifact archives.                      | action: require the three expected artifact paths to be regular files and reject symlinks before reading or uploading them.                               | DONE   | RESOLVED     |

docs/pr-reviews/pr-2008-copilot-suggestions.md:64

  • Row 12’s Thread ID still contains * characters (PRRT*kwDOGp2yqc6SgaM*). Since this file is meant to be an audit log keyed by opaque thread IDs, the table should record the exact ID (matching the cspell:ignore list above).
| 12  | PRRT*kwDOGp2yqc6SgaM* | `.github/workflows/upload_coverage_pr.yaml`                     | https://github.com/torrust/torrust-tracker/pull/2008#discussion_r3620551932 | Clean temporary extraction directories on all paths.                                        | action: run extraction in a subshell with an `EXIT` trap that removes its temporary directory.                                                            | DONE   | RESOLVED     |

@josecelano
josecelano merged commit 3f03996 into torrust:develop Jul 21, 2026
16 checks passed

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a post-merge review of the change from #2008 as it stands at f72f93c5c on the default branch.

What still runs: .github/workflows/upload_coverage_pr.yaml keeps the logic this pull request introduced. The default branch is checked out first, with no ref taken from any artifact. Each fork-produced archive must hold exactly the one expected entry, which is extracted into a temporary directory with paths junked and accepted only as a regular non-symlink file. The pull request number and commit SHA must match ^[0-9]+$ and ^[0-9a-f]{40}$ before they reach $GITHUB_OUTPUT, and the upload sends coverage_artifacts/codecov.json with both overrides. Since the merge, 833441940 and f86a8e1fe have only pinned the three actions to patch tags, and no later commit up to f72f93c5c touches the file. The description said the fork upload would be verified through this pull request's own CI, but a workflow_run workflow always runs from the default branch's copy, so that CI exercised the previous uploader. The archived issue, docs/issues/closed/2006-fix-fork-pr-coverage-upload-workflow/ISSUE.md, still leaves AC1 (fork upload) and AC4 (pull request and commit reach Codecov) open.

[Minor] The uploader no longer runs for pull requests. .github/workflows/upload_coverage_pr.yaml:10 triggers on workflows: ["Generate Coverage Report (PR)"], but 198c0ba57 renamed the producer at .github/workflows/generate_coverage_pr.yaml:1 to Generate Coverage Reports (PR), and no workflow on the default branch carries the old name. When a pull request's producer run completes, no workflow_run event matches, so its report never reaches Codecov. No run fails, so the gap is silent. A rename of a workflow that a workflow_run trigger names is expected to update that trigger in the same change. Smallest fix: set line 10, and the comment at line 8, to the producer's current name.

[Minor] As hardening, narrow the job's token. The workflow-level block at .github/workflows/upload_coverage_pr.yaml:14-18 grants write on actions, contents, issues and pull-requests. The job only lists and downloads artifacts, checks out the default branch and uploads a report, so it needs actions: read and contents: read. The checkout at line 28 also keeps the default persist-credentials: true. No code from the pull request runs in this job, so the extra scopes matter only if an action the job uses were itself compromised, which is the case least privilege exists for; .github/workflows/AGENTS.md:15 asks workflows to use least-privilege permissions. #2495 plans explicit permissions but lists only workflows that have no permissions: block, so this file is not covered there. Smallest fix: permissions: { actions: read, contents: read } and persist-credentials: false on the checkout.

[Minor] As hardening, bind the pull request number and commit SHA to the run that triggered the upload; today they are checked only for shape. The three artifacts come from the triggering pull_request run, which for a fork pull request executes the fork's own workflow files, so their contents are whatever that run stores. Values naming a different pull request or a develop commit pass the checks at .github/workflows/upload_coverage_pr.yaml:128-135, and lines 155-156 then attribute the uploaded report to that pull request and commit. The issue's AC4 expects Codecov to receive the pull request and commit that belong to the report. Two things limit this to hardening: runs from external contributors need maintainer approval, and coverage gates no merge here. Smallest fix: take the commit from github.event.workflow_run.head_sha, which GitHub sets for the run, and accept the artifact's pull request number only when pulls.get shows that pull request's head.sha equal to it, failing otherwise.

[Nit] As hardening, extract quietly. .github/workflows/upload_coverage_pr.yaml:112 runs unzip -j without -q, and Info-ZIP UnZip prints an archive's comment verbatim during extraction. The listing at line 106 (unzip -Z1) never shows the comment, so the entry check does not see it. A comment in a fork-produced archive therefore reaches the job log, where the runner reads ::-prefixed lines as workflow commands and can add annotations to this job's run. A job that holds a secret is expected to keep text from untrusted archives out of its log. The consumed outputs are rewritten afterwards at lines 141-142 from checked values, so the effect stays cosmetic. Smallest fix: unzip -q -j, which suppresses the comment in UnZip 6.00.

[Nit] Lines 11-12 subscribe to types: completed with no if: github.event.workflow_run.conclusion == 'success', although the comment at line 7 says the trigger is every successful execution. A failed or cancelled producer run that stored no codecov_report artifact makes the job fail at line 52 with a count of 0, adding a red run that reports nothing new. Smallest fix: add that if: to the job.

[Nit] listWorkflowRunArtifacts at line 41 uses the API's default page size of 30. Besides the three artifacts this job needs, the producer stores one package-coverage-<package> artifact per changed package (.github/workflows/generate_coverage_pr.yaml:114-120), and the workspace has 37 members. A pull request touching 28 or more packages yields more than 30 artifacts. If the needed ones fall past the first page, the job fails at line 52, 68 or 84 with a count of 0. Smallest fix: pass name: for each of the three lookups, or per_page: 100.

Checked and found sound: the checkout runs first and takes no artifact-derived ref. The entry allowlist rejects extra entries, nested paths, directory entries, names split by newlines and symlinks. The EXIT trap in the subshell function removes the extraction directory on every path, and a failing extraction stops the job under the default bash -e. The shape checks keep newlines and other injected text out of $GITHUB_OUTPUT and the log. The files: path matches where the report is moved, and fail_ci_if_error: true keeps upload failures visible.

This branch was previously deployed

1 inactive deployment
coverage — ff0c335a Deployed Jul 21, 2026 by josecelano via Generate Coverage Report #1620
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.

Fix coverage upload for fork pull requests

3 participants