Skip to content

Support ref in secure-checkout #1005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 13, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions .github/actions/secure-checkout/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ inputs:
description: "The repository to check out."
required: false
default: ${{ github.repository }}
sha:
description: "The SHA to checkout."
ref:
description: "The ref or SHA to checkout."
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: document that ref should be fully-formed in the form `refs/*, and that it can handle branches and tags.
currently it also handles pull requests. I'm not sure why that would be used in practice

Copy link
Collaborator

Choose a reason for hiding this comment

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

Documenting for later: we'll keep in branches, technically I think we only need it for tags and otherwise we're using sha

required: false
# NOTE: different from actions/checkout which takes any git ref.
# Users must provide a sha1 digest explicitly if not checking out the
# commit that triggered the event.
# NOTE: different from actions/checkout which defaults to GITHUB_REF, GITHUB_SHA, or default branch.
default: ${{ github.sha }}
token:
description: "Token used to fetch the repository."
Expand All @@ -30,19 +28,31 @@ inputs:
runs:
using: "composite"
steps:
- name: Verify input sha
- name: Get expected sha
id: expected
shell: bash
env:
EXPECTED_SHA: "${{ inputs.sha }}"
# Verify that the input sha is a git digest (sha1).
UNTRUSTED_REPO: "${{ inputs.repository }}"
UNTRUSTED_REF: "${{ inputs.ref }}"
run: |
[[ "${EXPECTED_SHA}" =~ ^[a-fA-F0-9]{40}$ ]]
set -euo pipefail
if [[ "${UNTRUSTED_REF}" =~ ^[a-fA-F0-9]{40}$ ]]; then
# sha
sha="${UNTRUSTED_REF}"
elif [[ "${UNTRUSTED_REF}" == refs/* ]]; then
# tag
sha=$(gh api --header 'Accept: application/vnd.github.v3+json' --method GET "/repos/${UNTRUSTED_REPO}/git/${UNTRUSTED_REF}" | jq -r '.object.sha')
else
echo "Unsupported ref: ${UNTRUSTED_REF}"
exit 1
fi
echo "::set-output name=sha::$sha"

- name: Checkout the repository
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3.0.2
with:
fetch-depth: 1
ref: ${{ inputs.sha }}
ref: ${{ inputs.ref }}
persist-credentials: ${{ inputs.persist-credentials }}
repository: ${{ inputs.repository }}
token: ${{ inputs.token }}
Expand All @@ -51,7 +61,7 @@ runs:
shell: bash
env:
CONTEXT: "${{ toJSON(github) }}"
EXPECTED_SHA: "${{ inputs.sha }}"
EXPECTED_SHA: "${{ steps.expected.outputs.sha }}"
run: |
set -euo pipefail

Expand Down