Skip to content

Match report targets to a unique current PR without provenance - #24

Merged
dblock merged 2 commits into
danger:mainfrom
dblock:fix/match-current-pr
Nov 18, 2026
Merged

dblock merged 2 commits into
danger:mainfrom
dblock:fix/match-current-pr

Conversation

@dblock

@dblock dblock commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

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.

  • Resolve the destination independently of the untrusted report: paginate open PRs by the triggering run's source owner/branch, then match the exact head SHA, head branch, source repository ID, and caller base repository ID.
  • Require exactly one match across all bases before applying the base policy. The report's positive safe-integer PR number must equal that independently selected PR; it cannot choose between candidates.
  • Fetch the selected PR again and recheck its number, open state, head, creation timestamp, and current base before any comment listing, creation, or update. Reject PRs created after the original run creation timestamp, not the rerun start time.
  • Accept only successful/failed pull_request runs delivered through workflow_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_requests associations.

Base policy and compatibility

base-branch omitted/empty allows only the caller repository default branch. A literal value such as release is an exact restriction. Explicit base-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.md documents 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 --check for 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.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Danger Report

No issues found.

View run

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>
@dblock
dblock force-pushed the fix/match-current-pr branch from b63bc47 to 6cbe82e Compare November 15, 2026 15:23
@dblock

dblock commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

@numbata when you have a chance

@numbata numbata left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A few follow-ups on the workflow logic. The main one is about failure handling for routine races.

Comment thread .github/workflows/danger-comment.yml Outdated
head: `${headOwner}:${run.head_branch}`,
per_page: 100,
});
const matches = pulls.filter(pr => pr.state === 'open' && matchesHead(pr));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Comment thread .github/workflows/danger-comment.yml Outdated
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) ||

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Comment thread .github/workflows/danger-comment.yml Outdated
!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');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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>
@dblock
dblock merged commit 0441db6 into danger:main Nov 18, 2026
3 checks passed
@dblock
dblock deleted the fix/match-current-pr branch November 18, 2026 00:46
dblock added a commit that referenced this pull request Nov 18, 2026
## 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>
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.

2 participants