Update checkout validation

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Francesco Renzi
2026-11-30 10:53:56 +01:00
parent f548e57e54
commit a4ddf0647e
6 changed files with 268 additions and 74 deletions

43
dist/index.js vendored
View File

@@ -41970,7 +41970,7 @@ async function getGitCommandManager(settings) {
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/;
function assertSafePrCheckout(input) {
async function assertSafePrCheckout(input, authToken) {
if (input.allowUnsafePrCheckout) {
return;
}
@@ -42014,10 +42014,20 @@ function assertSafePrCheckout(input) {
const repositoryMatchesPrHead = typeof prHeadRepoFullName === 'string' &&
input.qualifiedRepository.toLowerCase() === prHeadRepoFullName.toLowerCase();
const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref);
const commitMatchesPrHeadSha = !!input.commit && prShas.includes(input.commit.toLowerCase());
const commit = input.commit?.toLowerCase();
const commitMatchesPrHeadSha = !!commit && prShas.includes(commit);
if (!repositoryMatchesPrHead &&
!refMatchesPullPattern &&
!commitMatchesPrHeadSha) {
if (eventName === 'workflow_run' && !input.ref && commit) {
const { owner, repo } = github_context.repo;
if (input.qualifiedRepository.toLowerCase() ===
`${owner}/${repo}`.toLowerCase() &&
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit))) {
throw new Error('Refusing to check out the requested commit for this fork pull request. ' +
'The commit is not verified to be in the workflow commit history. Use a trusted branch ref.');
}
}
return;
}
throw new Error(`Refusing to check out fork pull request code from a '${eventName}' workflow. ` +
@@ -42027,6 +42037,31 @@ function assertSafePrCheckout(input) {
`at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' ` +
`on the actions/checkout step.`);
}
async function commitIsInWorkflowHistory(authToken, owner, repo, commit) {
const workflowSha = github_context.sha?.toLowerCase() || '';
if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) {
throw new Error('Unable to verify the requested commit: the workflow commit SHA is missing or invalid.');
}
if (commit === workflowSha) {
return true;
}
try {
// Compare immutable commits so the result does not depend on current PR metadata.
const { data } = await getOctokit(authToken)
.rest.repos.compareCommitsWithBasehead({
owner,
repo,
basehead: `${workflowSha}...${commit}`,
per_page: 1
});
return (data?.base_commit?.sha === workflowSha &&
data?.merge_base_commit?.sha === commit);
}
catch (error) {
throw new Error('Unable to verify the requested commit against the workflow commit history. ' +
'Check that the checkout token has contents: read, or use a trusted branch ref.', { cause: error });
}
}
function pushIfSha(target, value) {
if (typeof value === 'string' && value.length > 0) {
target.push(value.toLowerCase());
@@ -42213,12 +42248,12 @@ async function getInputs() {
// repository or ref.
const isDefaultCheckout = isWorkflowRepository && !getInput('ref');
if (!isDefaultCheckout) {
assertSafePrCheckout({
await assertSafePrCheckout({
qualifiedRepository,
ref: result.ref,
commit: result.commit,
allowUnsafePrCheckout: result.allowUnsafePrCheckout
});
}, result.authToken);
}
return result;
}