Repository navigation
Match report targets to a unique current PR without provenance - #24
Conversation
Danger ReportNo issues found. |
b85b5dd to
b63bc47
Compare
Resolve open PRs independently of untrusted artifacts and recheck current heads and allowed bases before comment access. Document the weaker current-state routing guarantee and cover it with workflow regression tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
b63bc47 to
6cbe82e
Compare
|
@numbata when you have a chance |
numbata
left a comment
There was a problem hiding this comment.
A few follow-ups on the workflow logic. The main one is about failure handling for routine races.
| head: `${headOwner}:${run.head_branch}`, | ||
| per_page: 100, | ||
| }); | ||
| const matches = pulls.filter(pr => pr.state === 'open' && matchesHead(pr)); |
There was a problem hiding this comment.
Nit: pr.state === 'open' is redundant. The pulls.list call already passes state: 'open', so every entry here is open. Dropping it keeps the filter to the head-match check.
There was a problem hiding this comment.
Agreed. Removed the redundant state check from the list filter in c8a5b10; the API request already restricts that list to open PRs. The separate fetch still checks state because the PR can close between requests. Updated the tests to stop modeling a closed PR returned by an open-only list.
— GitHub Copilot (AI), on behalf of @dblock.
| pull_number: matches[0].number, | ||
| }); | ||
| const prCreatedAt = typeof pr.created_at === 'string' ? Date.parse(pr.created_at) : NaN; | ||
| if (pr.number !== prNumber || pr.state !== 'open' || !matchesHead(pr) || |
There was a problem hiding this comment.
Nit: pr.number !== prNumber can't be true here. matches[0].number was already checked against prNumber at line 119, and pulls.get was called with that same number. It's harmless, but it reads as a defense against something the code has already ruled out. Consider removing it or leaving a comment that says why it's there.
There was a problem hiding this comment.
Agreed. Removed the redundant fetched-number comparison and its impossible-response test in c8a5b10. The report number is still checked against the independently resolved candidate before pulls.get, which uses that candidate number. The second fetch retains checks for state, head, creation time, and allowed base, which can matter independently.
— GitHub Copilot (AI), on behalf of @dblock.
| !Number.isFinite(prCreatedAt) || prCreatedAt > runCreatedAt || | ||
| typeof pr.base?.ref !== 'string' || !pr.base.ref || | ||
| (process.env.BASE_BRANCH !== '*' && pr.base.ref !== process.env.BASE_BRANCH)) { | ||
| core.setFailed('Current PR no longer matches the run or allowed base'); |
There was a problem hiding this comment.
This path fails the job for expected races: a new push during the run changes the head SHA, or the PR is closed or merged before the commenter runs. Each one produces a red run and a failure email for normal activity, and a forged pr_number produces the same message, so maintainers can't tell them apart.
Suggestion: use core.notice with the expected and current values for the expected states (head moved, PR no longer open). Keep core.setFailed for real integrity problems, such as a report number that doesn't match the resolved PR, an ambiguous match, or a disallowed base. Line 120 has the same issue for the ambiguous-match case, though that one is a legitimate failure.
There was a problem hiding this comment.
Agreed; addressed in the additional commit c8a5b10. No current open match now produces a notice and returns. If the independently fetched PR has closed/merged or its head SHA moved, it also produces a notice and returns, provided integrity checks still pass. The moved-head notice includes the run and current SHAs; the closed-PR notice includes expected/current state.
Zero matches cannot distinguish a push from a closure or inconsistent data, so that notice does not claim a specific cause and does not fall back to fetching the artifact’s claimed PR. Multiple matches, a report/resolved-number mismatch, disallowed bases, invalid metadata/timestamps, source branch/repository mismatches, and API errors remain failures with more specific diagnostics. All skip/failure paths return before comment listing or writes.
Updated SECURITY.md and regression tests to distinguish notices from failures and assert no comment access in either case. Validation: 116 targeted examples passed; RuboCop, actionlint, Node syntax, and whitespace checks passed.
— GitHub Copilot (AI), on behalf of @dblock.
Separate no-match, moved-head, and closed-PR notices from integrity failures. Remove redundant API checks, improve diagnostics, and update regression tests and security documentation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
## Summary Fix the main-branch [Test failure](https://github.com/danger/danger-pr-comment/actions/runs/44210523732) introduced when #23 and #24 were combined. Twelve installed-consumer scenarios failed with `Triggering run is missing required PR matching metadata`: the consumer commenter harness still supplied the old run-only context and had no PR API stubs. - Supply a separate workflow/PR fixture with caller repository metadata, fork source/head information, timestamps, and an independently defined PR response. The scenario's original event fixture supplies its PR number; the uploaded report does not determine the API response. - Evaluate the actual job condition and environment expressions, including the default-base fallback, rather than only substituting simple input expressions. - Stub PR listing and fetching alongside comment operations. Require consumer scenarios to reach PR lookup, re-fetch, and comment listing without failures, notices, or job skips, so quiet-mode scenarios cannot accidentally pass by skipping validation. No production workflows, permissions, matching rules, dependencies, or package APIs change. ## Validation - `CI=true GITHUB_ACTIONS=true bundle exec rake spec` — **161 examples, 0 failures**, including all 13 installed-gem consumer scenarios. - `bundle exec rubocop` — **34 files, no offenses**. - `node --check spec/fixtures/publish_consumer_report.js` — passed. - `git diff --check` — passed. The consumer scenarios build and install the real gem in temporary consumers and run the actual Danger and preview executables. GitHub API calls remain stubbed; no remote comments or publication are performed. Authored by GitHub Copilot (AI), on behalf of @dblock. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
Independent alternative to #20, based directly on
main. This implements the smaller API-only approach rather than adding authenticated provenance. It leaves #20 and #23 unchanged and does not supersede or close either PR.pull_requestruns delivered throughworkflow_run. Preserve existing sticky comments, empty-report behavior, writer serialization, and permissions.No producer changes, additional jobs, signing permissions, attestations, dependencies, PR checkout, or PR-controlled code execution are added. Fork support uses the PR API lookup, not potentially empty
workflow_run.pull_requestsassociations.Base policy and compatibility
base-branchomitted/empty allows only the caller repository default branch. A literal value such asreleaseis an exact restriction. Explicitbase-branch: "*"allows changing stacked targets within the caller repository.These are intentional restrictions compared with current
main, which does not bind report targets: non-default-target callers must configure a literal base or"*", closed/merged PRs are rejected, and multiple same-head open PRs are rejected even if a literal base would distinguish them. Existing default-branch callers normally need no configuration change.What's the worst that can happen?
Someone could make Danger post a misleading report on the wrong PR, or replace its existing Danger comment with a fake “all clear.” The destination is still restricted to an open PR in this repository with the exact source repository, branch, and commit from the run, and an allowed target branch. It cannot be just any unrelated PR.
For example, PRs A and B could use the same source branch and commit but target different branches. Danger runs for A. If A closes before the comment is posted, someone who controls the uploaded report could change its PR number to B. B could then pass the checks—even if the original workflow would not normally run for B's target branch. This version checks what GitHub shows now; unlike the signed-record option in #20, it cannot prove which PR originally started the run.
An old report can also be posted after a PR changes its target branch without changing its code, or overwrite a newer report from another run at the same commit. That can make the current PR look checked when its current target was not. Run Danger again after changing the target, but this version cannot guarantee that happened.
These are risks to the accuracy of the PR comments. This workflow does not merge PRs, change code, or execute code from the uploaded report. If GitHub's data is missing, inaccessible, or shows multiple matching open PRs, it refuses to post rather than guessing. The creation-time check blocks later-created PRs, but does not prove which PR started the run.
The new
SECURITY.mddocuments these limits, and tests demonstrate both the rejected cases and the misleading-routing cases this simpler approach still allows.Validation
bundle exec rspec spec/scripts/current_pr_workflow_spec.rb spec/scripts/sticky_comment_workflow_spec.rb spec/scripts/empty_comment_workflow_spec.rb spec/scripts/install_workflows_spec.rb— 116 examples, 0 failures.bundle exec rubocop— 16 files, no offenses.actionlint .github/workflows/danger-comment.yml— passed.node --checkfor all three commenter workflow harnesses — passed.git diff --check— passed.The tests execute the actual workflow JavaScript and job/environment expressions with paginated API fixtures. They cover default and explicit bases, upper stacked targets, forks, stale heads, re-fetch races, absent/ambiguous/inaccessible matches, forged report targets, run creation timestamps, and rejection before all comment access. No live privileged workflow-run integration test was performed.
Authored by GitHub Copilot (AI), on behalf of @dblock.