From e3c9da67538f391b21808a3caf3a00f13c7fc712 Mon Sep 17 00:00:00 2001 From: Francesco Renzi Date: Fri, 4 Dec 2026 11:08:08 +0100 Subject: [PATCH] Ignore a .git suffix when matching checkout repositories Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- __test__/input-helper.test.ts | 18 ++++++++++++++++ __test__/unsafe-pr-checkout-helper.test.ts | 24 ++++++++++++++++++++++ dist/index.js | 11 +++++++--- src/unsafe-pr-checkout-helper.ts | 12 ++++++++--- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index 44c4afd..89dbbf4 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -378,6 +378,24 @@ describe('input-helper tests', () => { }) }) + it('verifies a pin on the workflow repository with a .git suffix', async () => { + inputs.repository = 'some-owner/some-repo.git' + inputs.ref = mergeSha + mockCompareCommits.mockResolvedValue({ + data: { + base_commit: {sha: workflowSha}, + merge_base_commit: {sha: ancestorSha} + } + }) + + await expect(inputHelper.getInputs()).rejects.toThrow( + /Refusing to check out the requested commit/ + ) + expect(mockCompareCommits).toHaveBeenCalledWith( + expect.objectContaining({owner: 'some-owner', repo: 'some-repo'}) + ) + }) + it.each([ ['a PR merge commit outside workflow history', workflowSha, ancestorSha], ['a comparison against an unexpected base', ancestorSha, mergeSha] diff --git a/__test__/unsafe-pr-checkout-helper.test.ts b/__test__/unsafe-pr-checkout-helper.test.ts index 70aeea2..c39553f 100644 --- a/__test__/unsafe-pr-checkout-helper.test.ts +++ b/__test__/unsafe-pr-checkout-helper.test.ts @@ -196,6 +196,30 @@ describe('unsafe-pr-checkout-helper', () => { ).rejects.toThrow() }) + it('refuses pull_request_target when repository points at the fork with a .git suffix', async () => { + setContext('pull_request_target', forkPullRequestTargetPayload()) + await expect( + check({ + qualifiedRepository: `${FORK_QUALIFIED_REPO}.git`, + ref: 'refs/heads/main', + commit: '', + allowUnsafePrCheckout: false + }) + ).rejects.toThrow() + }) + + it('strips only one .git suffix from the repository', async () => { + setContext('pull_request_target', forkPullRequestTargetPayload()) + await expect( + check({ + qualifiedRepository: `${FORK_QUALIFIED_REPO}.git.git`, + ref: 'refs/heads/main', + commit: '', + allowUnsafePrCheckout: false + }) + ).resolves.toBeUndefined() + }) + it('allows pull_request_target checkout of an unrelated third-party repo', async () => { setContext('pull_request_target', forkPullRequestTargetPayload()) await expect( diff --git a/dist/index.js b/dist/index.js index f837b71..1a3bdfc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -42012,8 +42012,9 @@ async 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?.toLowerCase(); const commitMatchesPrHeadSha = !!commit && prShas.includes(commit); @@ -42022,8 +42023,7 @@ async function assertSafePrCheckout(input, authToken) { !commitMatchesPrHeadSha) { if (eventName === 'workflow_run' && !input.ref && commit) { const { owner, repo } = github_context.repo; - if (input.qualifiedRepository.toLowerCase() === - `${owner}/${repo}`.toLowerCase() && + if (repository === `${owner}/${repo}`.toLowerCase() && fetchesFromWorkflowServer(input) && !(await commitIsInWorkflowHistory(authToken, owner, repo, commit))) { throw new Error('Refusing to check out the requested commit for this fork pull request. ' + @@ -42039,6 +42039,11 @@ async function assertSafePrCheckout(input, authToken) { `at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' ` + `on the actions/checkout step.`); } +// 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(); +} // SSH fetch URLs keep only the hostname, so the scheme and port cannot // distinguish the checkout server from the workflow server. function fetchesFromWorkflowServer(input) { diff --git a/src/unsafe-pr-checkout-helper.ts b/src/unsafe-pr-checkout-helper.ts index 3edb8d5..7a909fb 100644 --- a/src/unsafe-pr-checkout-helper.ts +++ b/src/unsafe-pr-checkout-helper.ts @@ -62,9 +62,10 @@ export async 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?.toLowerCase() const commitMatchesPrHeadSha = !!commit && prShas.includes(commit) @@ -77,8 +78,7 @@ export async function assertSafePrCheckout( if (eventName === 'workflow_run' && !input.ref && commit) { const {owner, repo} = github.context.repo if ( - input.qualifiedRepository.toLowerCase() === - `${owner}/${repo}`.toLowerCase() && + repository === `${owner}/${repo}`.toLowerCase() && fetchesFromWorkflowServer(input) && !(await commitIsInWorkflowHistory(authToken, owner, repo, commit)) ) { @@ -101,6 +101,12 @@ export async 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() +} + // SSH fetch URLs keep only the hostname, so the scheme and port cannot // distinguish the checkout server from the workflow server. function fetchesFromWorkflowServer(input: IUnsafePrCheckoutInput): boolean {