Ignore a .git suffix when matching checkout repositories

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Francesco Renzi
2026-12-04 11:08:08 +01:00
parent 152ac2d544
commit e3c9da6753
4 changed files with 59 additions and 6 deletions

View File

@@ -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([ it.each([
['a PR merge commit outside workflow history', workflowSha, ancestorSha], ['a PR merge commit outside workflow history', workflowSha, ancestorSha],
['a comparison against an unexpected base', ancestorSha, mergeSha] ['a comparison against an unexpected base', ancestorSha, mergeSha]

View File

@@ -196,6 +196,30 @@ describe('unsafe-pr-checkout-helper', () => {
).rejects.toThrow() ).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 () => { it('allows pull_request_target checkout of an unrelated third-party repo', async () => {
setContext('pull_request_target', forkPullRequestTargetPayload()) setContext('pull_request_target', forkPullRequestTargetPayload())
await expect( await expect(

11
dist/index.js vendored
View File

@@ -42012,8 +42012,9 @@ async function assertSafePrCheckout(input, authToken) {
} }
// (B) We cannot check for all fork PR refs so check to see // (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 // 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' && const repositoryMatchesPrHead = typeof prHeadRepoFullName === 'string' &&
input.qualifiedRepository.toLowerCase() === prHeadRepoFullName.toLowerCase(); repository === prHeadRepoFullName.toLowerCase();
const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref); const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref);
const commit = input.commit?.toLowerCase(); const commit = input.commit?.toLowerCase();
const commitMatchesPrHeadSha = !!commit && prShas.includes(commit); const commitMatchesPrHeadSha = !!commit && prShas.includes(commit);
@@ -42022,8 +42023,7 @@ async function assertSafePrCheckout(input, authToken) {
!commitMatchesPrHeadSha) { !commitMatchesPrHeadSha) {
if (eventName === 'workflow_run' && !input.ref && commit) { if (eventName === 'workflow_run' && !input.ref && commit) {
const { owner, repo } = github_context.repo; const { owner, repo } = github_context.repo;
if (input.qualifiedRepository.toLowerCase() === if (repository === `${owner}/${repo}`.toLowerCase() &&
`${owner}/${repo}`.toLowerCase() &&
fetchesFromWorkflowServer(input) && fetchesFromWorkflowServer(input) &&
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit))) { !(await commitIsInWorkflowHistory(authToken, owner, repo, commit))) {
throw new Error('Refusing to check out the requested commit for this fork pull request. ' + 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' ` + `at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' ` +
`on the actions/checkout step.`); `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 // SSH fetch URLs keep only the hostname, so the scheme and port cannot
// distinguish the checkout server from the workflow server. // distinguish the checkout server from the workflow server.
function fetchesFromWorkflowServer(input) { function fetchesFromWorkflowServer(input) {

View File

@@ -62,9 +62,10 @@ export async function assertSafePrCheckout(
// (B) We cannot check for all fork PR refs so check to see // (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 // if the resolved input points to the fork PR sha we have in the payload
const repository = repositoryIdentity(input.qualifiedRepository)
const repositoryMatchesPrHead = const repositoryMatchesPrHead =
typeof prHeadRepoFullName === 'string' && typeof prHeadRepoFullName === 'string' &&
input.qualifiedRepository.toLowerCase() === prHeadRepoFullName.toLowerCase() repository === prHeadRepoFullName.toLowerCase()
const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref) const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref)
const commit = input.commit?.toLowerCase() const commit = input.commit?.toLowerCase()
const commitMatchesPrHeadSha = !!commit && prShas.includes(commit) const commitMatchesPrHeadSha = !!commit && prShas.includes(commit)
@@ -77,8 +78,7 @@ export async function assertSafePrCheckout(
if (eventName === 'workflow_run' && !input.ref && commit) { if (eventName === 'workflow_run' && !input.ref && commit) {
const {owner, repo} = github.context.repo const {owner, repo} = github.context.repo
if ( if (
input.qualifiedRepository.toLowerCase() === repository === `${owner}/${repo}`.toLowerCase() &&
`${owner}/${repo}`.toLowerCase() &&
fetchesFromWorkflowServer(input) && fetchesFromWorkflowServer(input) &&
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit)) !(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 // SSH fetch URLs keep only the hostname, so the scheme and port cannot
// distinguish the checkout server from the workflow server. // distinguish the checkout server from the workflow server.
function fetchesFromWorkflowServer(input: IUnsafePrCheckoutInput): boolean { function fetchesFromWorkflowServer(input: IUnsafePrCheckoutInput): boolean {