From 830bba2f1cc1b527ff6dc92c77bff8bff9435eda Mon Sep 17 00:00:00 2001 From: Francesco Renzi Date: Fri, 4 Dec 2026 11:13:15 +0100 Subject: [PATCH] Ignore a .git suffix when matching checkout repositories Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- __test__/unsafe-pr-checkout-helper.test.ts | 51 ++++++++++++++++++++++ dist/index.js | 10 ++++- src/unsafe-pr-checkout-helper.ts | 11 ++++- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/__test__/unsafe-pr-checkout-helper.test.ts b/__test__/unsafe-pr-checkout-helper.test.ts index 6629666..d3a8982 100644 --- a/__test__/unsafe-pr-checkout-helper.test.ts +++ b/__test__/unsafe-pr-checkout-helper.test.ts @@ -187,6 +187,30 @@ describe('unsafe-pr-checkout-helper', () => { ).toThrow() }) + it('refuses pull_request_target when repository points at the fork with a .git suffix', () => { + setContext('pull_request_target', forkPullRequestTargetPayload()) + expect(() => + assertSafePrCheckout({ + qualifiedRepository: `${FORK_QUALIFIED_REPO}.git`, + ref: 'refs/heads/main', + commit: '', + allowUnsafePrCheckout: false + }) + ).toThrow() + }) + + it('strips only one .git suffix from the repository', () => { + setContext('pull_request_target', forkPullRequestTargetPayload()) + expect(() => + assertSafePrCheckout({ + qualifiedRepository: `${FORK_QUALIFIED_REPO}.git.git`, + ref: 'refs/heads/main', + commit: '', + allowUnsafePrCheckout: false + }) + ).not.toThrow() + }) + it('allows pull_request_target checkout of an unrelated third-party repo', () => { setContext('pull_request_target', forkPullRequestTargetPayload()) expect(() => @@ -309,6 +333,33 @@ describe('unsafe-pr-checkout-helper', () => { }) }) + it('verifies a workflow_run pin on the workflow repository with a .git suffix', async () => { + setContext('workflow_run', forkWorkflowRunPayload()) + github.context.sha = SAFE_BASE_SHA + mockCompareCommits.mockResolvedValue({ + data: { + base_commit: {sha: SAFE_BASE_SHA}, + merge_base_commit: {sha: '5555555555555555555555555555555555555555'} + } + }) + + await expect( + assertSafePrCheckout( + { + qualifiedRepository: `${BASE_QUALIFIED_REPO}.git`, + ref: '', + commit: PR_MERGE_SHA, + allowUnsafePrCheckout: false + }, + 'checkout-token' + ) + ).rejects.toThrow(/Refusing to check out the requested commit/) + + expect(mockCompareCommits).toHaveBeenCalledWith( + expect.objectContaining({owner: 'some-owner', repo: 'some-repo'}) + ) + }) + it('allows a workflow_run pin verified in workflow history', async () => { setContext('workflow_run', forkWorkflowRunPayload()) github.context.sha = SAFE_BASE_SHA diff --git a/dist/index.js b/dist/index.js index 53097fe..9ef590e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -13404,8 +13404,9 @@ function assertSafePrCheckout(input, authToken) { } // (B) We cannot check for all fork PR refs so check to see // if the resolved input points to the fork PR sha we have in the payload + const repository = repositoryIdentity(input.qualifiedRepository); const repositoryMatchesPrHead = typeof prHeadRepoFullName === 'string' && - input.qualifiedRepository.toLowerCase() === prHeadRepoFullName.toLowerCase(); + repository === prHeadRepoFullName.toLowerCase(); const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref); const commit = input.commit && input.commit.toLowerCase(); const commitMatchesPrHeadSha = !!commit && prShas.includes(commit); @@ -13415,7 +13416,7 @@ function assertSafePrCheckout(input, authToken) { if (eventName === 'workflow_run' && !input.ref && commit && - input.qualifiedRepository.toLowerCase() === + repository === `${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase()) { return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => { if (!isInHistory) { @@ -13434,6 +13435,11 @@ function assertSafePrCheckout(input, authToken) { `on the actions/checkout step.`); } exports.assertSafePrCheckout = assertSafePrCheckout; +// Git transports resolve 'owner/repo.git' to 'owner/repo', so compare +// repositories without one terminal '.git' suffix. +function repositoryIdentity(qualifiedRepository) { + return qualifiedRepository.replace(/\.git$/, '').toLowerCase(); +} function commitIsInWorkflowHistory(authToken, commit) { return __awaiter(this, void 0, void 0, function* () { const workflowSha = github.context.sha.toLowerCase(); diff --git a/src/unsafe-pr-checkout-helper.ts b/src/unsafe-pr-checkout-helper.ts index ae4a56b..320ee04 100644 --- a/src/unsafe-pr-checkout-helper.ts +++ b/src/unsafe-pr-checkout-helper.ts @@ -59,9 +59,10 @@ export function assertSafePrCheckout( // (B) We cannot check for all fork PR refs so check to see // if the resolved input points to the fork PR sha we have in the payload + const repository = repositoryIdentity(input.qualifiedRepository) const repositoryMatchesPrHead = typeof prHeadRepoFullName === 'string' && - input.qualifiedRepository.toLowerCase() === prHeadRepoFullName.toLowerCase() + repository === prHeadRepoFullName.toLowerCase() const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref) const commit = input.commit && input.commit.toLowerCase() const commitMatchesPrHeadSha = !!commit && prShas.includes(commit) @@ -75,7 +76,7 @@ export function assertSafePrCheckout( eventName === 'workflow_run' && !input.ref && commit && - input.qualifiedRepository.toLowerCase() === + repository === `${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() ) { return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => { @@ -100,6 +101,12 @@ export function assertSafePrCheckout( ) } +// Git transports resolve 'owner/repo.git' to 'owner/repo', so compare +// repositories without one terminal '.git' suffix. +function repositoryIdentity(qualifiedRepository: string): string { + return qualifiedRepository.replace(/\.git$/, '').toLowerCase() +} + async function commitIsInWorkflowHistory( authToken: string, commit: string