Skip to content

Conversation

jeffwidman
Copy link
Member

When running this workflow manually (signalled via the workflow_dispatch event), we need to ensure that we are operating on commits that have been reviewed/approved. This is a safety measure to prevent:

  1. Accidental pushes that might cause bugs.
  2. Malicious pushes that might do nefarious things.

I considered implementing this by re-checking for approval at the time we checkout the code, but because the gh call to check for approval is a separate call from the checkout, we will always need to do this SHA comparison to avoid a race. So the simplest thing was to memoize the SHA as part of the output from the first job and avoid another gh call.

Lastly, this validation relies on Dismiss stale pull request approvals when new commits are pushed being set in the repo settings. It's already configured that way here in dependabot-core.

When running this workflow manually (signalled via the `workflow_dispatch` event),
we need to ensure that we are operating on commits that have been
reviewed/approved. This is a safety measure to prevent:

1. Accidental pushes that might cause bugs.
2. Malicious pushes that might do nefarious things.

I considered implementing this by re-checking for approval at the time
we checkout the code, but because the `gh` call to check for approval is
a separate call from the checkout, we will always need to do this SHA
comparison to avoid a race. So the simplest thing was to memoize the SHA
as part of the output from the first job and avoid another `gh` call.

Lastly, this validation relies on `Dismiss stale pull request approvals
when new commits are pushed` being set in the repo settings. It's already
configured that way here in `dependabot-core`.
@jeffwidman jeffwidman requested a review from a team as a code owner January 3, 2025 20:49
@jeffwidman jeffwidman force-pushed the ensure-PR-commit-matches branch from fd35568 to 798d3a1 Compare January 3, 2025 20:55
Comment on lines +101 to +104
# Ensure the commit we've checked out matches our expected SHA from when we checked the PR's approval status above.
# This is a security measure to prevent any unreviewed commits from getting pulled into this action workflow.
# The format is "APPROVED:OPEN:<PR_COMMIT_SHA>", so compare the end of the string to the current commit.
[[ ${{needs.approval.outputs.decision}} =~ $(git rev-parse HEAD)$ ]]
Copy link
Contributor

Choose a reason for hiding this comment

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

💅 This is my preference, not a requirement.

I'd prefer we use two outputs instead of matching against a long string multiple times. I find exact matches quite a bit easier to reason about.

Copy link
Member Author

Choose a reason for hiding this comment

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

I hear you... I actually wrote it that way originally--I also prefer explicit over implicit, but it got kinda verbose, so I decided it was more readable/simpler this route.

@jeffwidman jeffwidman merged commit 74c174a into main Jan 3, 2025
131 of 150 checks passed
@jeffwidman jeffwidman deleted the ensure-PR-commit-matches branch January 3, 2025 21:20
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.

4 participants