Backport checkout validation

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Francesco Renzi
2026-12-01 10:47:26 +01:00
parent fbc6f3992d
commit dc7694f54c
5 changed files with 224 additions and 14 deletions

59
dist/index.js vendored
View File

@@ -2009,12 +2009,12 @@ function getInputs() {
// repository or ref.
const isDefaultCheckout = isWorkflowRepository && !core.getInput('ref');
if (!isDefaultCheckout) {
unsafePrCheckoutHelper.assertSafePrCheckout({
yield unsafePrCheckoutHelper.assertSafePrCheckout({
qualifiedRepository,
ref: result.ref,
commit: result.commit,
allowUnsafePrCheckout: result.allowUnsafePrCheckout
});
}, result.authToken);
}
return result;
});
@@ -2619,12 +2619,21 @@ var __importStar = (this && this.__importStar) || function (mod) {
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.assertSafePrCheckout = assertSafePrCheckout;
const github = __importStar(__nccwpck_require__(5438));
const ref_helper_1 = __nccwpck_require__(8601);
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/;
function assertSafePrCheckout(input) {
function assertSafePrCheckout(input, authToken) {
if (input.allowUnsafePrCheckout) {
return;
}
@@ -2668,10 +2677,23 @@ 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 && input.commit.toLowerCase();
const commitMatchesPrHeadSha = !!commit && prShas.includes(commit);
if (!repositoryMatchesPrHead &&
!refMatchesPullPattern &&
!commitMatchesPrHeadSha) {
if (eventName === 'workflow_run' &&
!input.ref &&
commit &&
input.qualifiedRepository.toLowerCase() ===
`${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase()) {
return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => {
if (!isInHistory) {
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. ` +
@@ -2681,6 +2703,35 @@ function assertSafePrCheckout(input) {
`at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' ` +
`on the actions/checkout step.`);
}
function commitIsInWorkflowHistory(authToken, commit) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const workflowSha = github.context.sha.toLowerCase();
if (!/^[0-9a-f]{40}$/.test(workflowSha)) {
throw new Error('Unable to verify the requested commit: the workflow commit SHA is missing or invalid.');
}
if (commit === workflowSha) {
return true;
}
const { owner, repo } = github.context.repo;
try {
const { data } = yield github
.getOctokit(authToken)
.rest.repos.compareCommitsWithBasehead({
owner,
repo,
basehead: `${workflowSha}...${commit}`,
per_page: 1
});
return (((_a = data === null || data === void 0 ? void 0 : data.base_commit) === null || _a === void 0 ? void 0 : _a.sha) === workflowSha &&
((_b = data === null || data === void 0 ? void 0 : data.merge_base_commit) === null || _b === void 0 ? void 0 : _b.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.');
}
});
}
function pushIfSha(target, value) {
if (typeof value === 'string' && value.length > 0) {
target.push(value.toLowerCase());