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:09:30 +01:00
co-authored by Copilot App
parent 15c98b676d
commit decbdeb99c
3 changed files with 68 additions and 4 deletions
@@ -187,6 +187,30 @@ describe('unsafe-pr-checkout-helper', () => {
).toThrow() ).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', () => { it('allows pull_request_target checkout of an unrelated third-party repo', () => {
setContext('pull_request_target', forkPullRequestTargetPayload()) setContext('pull_request_target', forkPullRequestTargetPayload())
expect(() => 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 () => { it('allows a workflow_run pin verified in workflow history', async () => {
setContext('workflow_run', forkWorkflowRunPayload()) setContext('workflow_run', forkWorkflowRunPayload())
github.context.sha = SAFE_BASE_SHA github.context.sha = SAFE_BASE_SHA
+8 -2
View File
@@ -2857,8 +2857,9 @@ 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 && input.commit.toLowerCase(); const commit = input.commit && input.commit.toLowerCase();
const commitMatchesPrHeadSha = !!commit && prShas.includes(commit); const commitMatchesPrHeadSha = !!commit && prShas.includes(commit);
@@ -2868,7 +2869,7 @@ function assertSafePrCheckout(input, authToken) {
if (eventName === 'workflow_run' && if (eventName === 'workflow_run' &&
!input.ref && !input.ref &&
commit && commit &&
input.qualifiedRepository.toLowerCase() === repository ===
`${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() && `${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() &&
fetchesFromWorkflowServer(input)) { fetchesFromWorkflowServer(input)) {
return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => { return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => {
@@ -2896,6 +2897,11 @@ function fetchesFromWorkflowServer(input) {
? checkoutServer.hostname === workflowServer.hostname ? checkoutServer.hostname === workflowServer.hostname
: checkoutServer.origin === workflowServer.origin; : checkoutServer.origin === workflowServer.origin;
} }
// 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) { function commitIsInWorkflowHistory(authToken, commit) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
var _a, _b; var _a, _b;
+9 -2
View File
@@ -62,9 +62,10 @@ export 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 && input.commit.toLowerCase() const commit = input.commit && input.commit.toLowerCase()
const commitMatchesPrHeadSha = !!commit && prShas.includes(commit) const commitMatchesPrHeadSha = !!commit && prShas.includes(commit)
@@ -78,7 +79,7 @@ export function assertSafePrCheckout(
eventName === 'workflow_run' && eventName === 'workflow_run' &&
!input.ref && !input.ref &&
commit && commit &&
input.qualifiedRepository.toLowerCase() === repository ===
`${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() && `${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() &&
fetchesFromWorkflowServer(input) fetchesFromWorkflowServer(input)
) { ) {
@@ -114,6 +115,12 @@ function fetchesFromWorkflowServer(input: IUnsafePrCheckoutInput): boolean {
: checkoutServer.origin === workflowServer.origin : checkoutServer.origin === workflowServer.origin
} }
// 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( async function commitIsInWorkflowHistory(
authToken: string, authToken: string,
commit: string commit: string