mirror of
https://github.com/actions/checkout.git
synced 2026-12-05 03:52:55 +08:00
Ignore a .git suffix when matching checkout repositories
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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(
|
||||
|
||||
11
dist/index.js
vendored
11
dist/index.js
vendored
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user