mirror of
https://github.com/actions/checkout.git
synced 2026-12-03 18:00:06 +00:00
Update checkout validation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot App
parent
f548e57e54
commit
a4ddf0647e
@@ -5,6 +5,7 @@ import {
|
||||
expect,
|
||||
beforeAll,
|
||||
beforeEach,
|
||||
afterEach,
|
||||
afterAll
|
||||
} from '@jest/globals'
|
||||
import * as path from 'path'
|
||||
@@ -52,9 +53,12 @@ jest.unstable_mockModule('@actions/core', () => ({
|
||||
}))
|
||||
|
||||
// Mock @actions/github before loading input-helper
|
||||
const mockCompareCommits = jest.fn<() => Promise<unknown>>()
|
||||
jest.unstable_mockModule('@actions/github', () => ({
|
||||
context: mockGithubContext,
|
||||
getOctokit: jest.fn()
|
||||
getOctokit: jest.fn(() => ({
|
||||
rest: {repos: {compareCommitsWithBasehead: mockCompareCommits}}
|
||||
}))
|
||||
}))
|
||||
|
||||
// Mock fs-helper
|
||||
@@ -265,4 +269,94 @@ describe('input-helper tests', () => {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('explicit checkout pins on a fork PR workflow_run', () => {
|
||||
const workflowSha = '1234567890123456789012345678901234567890'
|
||||
const ancestorSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
|
||||
const mergeSha = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
|
||||
|
||||
beforeEach(() => {
|
||||
inputs.token = 'checkout-token'
|
||||
mockGithubContext.eventName = 'workflow_run'
|
||||
mockGithubContext.payload = {
|
||||
repository: {id: 100},
|
||||
workflow_run: {
|
||||
event: 'pull_request',
|
||||
head_repository: {id: 200, full_name: 'fork-owner/some-repo'},
|
||||
head_sha: 'cccccccccccccccccccccccccccccccccccccccc'
|
||||
}
|
||||
}
|
||||
mockCompareCommits.mockReset()
|
||||
mockCompareCommits.mockRejectedValue(new Error('API unavailable'))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
mockGithubContext.eventName = ''
|
||||
mockGithubContext.payload = {}
|
||||
})
|
||||
|
||||
it.each([
|
||||
['a PR merge commit outside workflow history', workflowSha, ancestorSha],
|
||||
['a comparison against an unexpected base', ancestorSha, mergeSha]
|
||||
])('refuses %s without PR metadata', async (_, base, mergeBase) => {
|
||||
inputs.ref = mergeSha
|
||||
mockCompareCommits.mockResolvedValue({
|
||||
data: {
|
||||
base_commit: {sha: base},
|
||||
merge_base_commit: {sha: mergeBase}
|
||||
}
|
||||
})
|
||||
|
||||
await expect(inputHelper.getInputs()).rejects.toThrow(
|
||||
/Refusing to check out the requested commit/
|
||||
)
|
||||
})
|
||||
|
||||
it('allows a pin verified to be in workflow history', async () => {
|
||||
inputs.ref = ancestorSha.toUpperCase()
|
||||
mockCompareCommits.mockResolvedValue({
|
||||
data: {
|
||||
base_commit: {sha: workflowSha},
|
||||
merge_base_commit: {sha: ancestorSha}
|
||||
}
|
||||
})
|
||||
|
||||
await expect(inputHelper.getInputs()).resolves.toMatchObject({
|
||||
ref: '',
|
||||
commit: inputs.ref
|
||||
})
|
||||
})
|
||||
|
||||
it('refuses a pin if the comparison is unavailable', async () => {
|
||||
inputs.ref = mergeSha
|
||||
|
||||
await expect(inputHelper.getInputs()).rejects.toThrow(
|
||||
/Unable to verify the requested commit/
|
||||
)
|
||||
})
|
||||
|
||||
it('allows a pin when the workflow explicitly opts out', async () => {
|
||||
inputs.ref = mergeSha
|
||||
inputs['allow-unsafe-pr-checkout'] = 'true'
|
||||
|
||||
await expect(inputHelper.getInputs()).resolves.toMatchObject({
|
||||
ref: '',
|
||||
commit: mergeSha
|
||||
})
|
||||
expect(mockCompareCommits).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it.each([
|
||||
['default checkout', '', 'refs/heads/some-ref', workflowSha],
|
||||
['named branch', 'refs/heads/main', 'refs/heads/main', undefined],
|
||||
['workflow commit', workflowSha, '', workflowSha]
|
||||
])('allows %s without a comparison', async (_, inputRef, ref, commit) => {
|
||||
inputs.ref = inputRef
|
||||
|
||||
const settings = await inputHelper.getInputs()
|
||||
expect(settings.ref).toBe(ref)
|
||||
expect(settings.commit).toBe(commit)
|
||||
expect(mockCompareCommits).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -34,6 +34,12 @@ jest.unstable_mockModule('@actions/github', () => ({
|
||||
const {assertSafePrCheckout} =
|
||||
await import('../src/unsafe-pr-checkout-helper.js')
|
||||
|
||||
function check(
|
||||
input: Parameters<typeof assertSafePrCheckout>[0]
|
||||
): Promise<void> {
|
||||
return assertSafePrCheckout(input, 'checkout-token')
|
||||
}
|
||||
|
||||
const originalEventName = mockContext.eventName
|
||||
const originalPayload = mockContext.payload
|
||||
|
||||
@@ -94,191 +100,191 @@ describe('unsafe-pr-checkout-helper', () => {
|
||||
mockContext.payload = originalPayload
|
||||
})
|
||||
|
||||
it('allows pull_request events untouched', () => {
|
||||
it('allows pull_request events untouched', async () => {
|
||||
setContext('pull_request', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: 'attacker/fork',
|
||||
ref: 'refs/pull/1/merge',
|
||||
commit: '',
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).not.toThrow()
|
||||
).resolves.toBeUndefined()
|
||||
})
|
||||
|
||||
it('allows pull_request_target default checkout (base branch)', () => {
|
||||
it('allows pull_request_target default checkout (base branch)', async () => {
|
||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: 'refs/heads/main',
|
||||
commit: SAFE_BASE_SHA,
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).not.toThrow()
|
||||
).resolves.toBeUndefined()
|
||||
})
|
||||
|
||||
it('allows same-repo pull_request_target checkout of PR head', () => {
|
||||
it('allows same-repo pull_request_target checkout of PR head', async () => {
|
||||
setContext('pull_request_target', sameRepoPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: '',
|
||||
commit: PR_HEAD_SHA,
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).not.toThrow()
|
||||
).resolves.toBeUndefined()
|
||||
})
|
||||
|
||||
it('refuses pull_request_target fork PR head SHA checkout', () => {
|
||||
it('refuses pull_request_target fork PR head SHA checkout', async () => {
|
||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: '',
|
||||
commit: PR_HEAD_SHA,
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).toThrow(/Refusing to check out fork pull request code/)
|
||||
).rejects.toThrow(/Refusing to check out fork pull request code/)
|
||||
})
|
||||
|
||||
it('refuses pull_request_target fork PR merge_commit_sha checkout', () => {
|
||||
it('refuses pull_request_target fork PR merge_commit_sha checkout', async () => {
|
||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: '',
|
||||
commit: PR_MERGE_SHA,
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).toThrow(/allow-unsafe-pr-checkout/)
|
||||
).rejects.toThrow(/allow-unsafe-pr-checkout/)
|
||||
})
|
||||
|
||||
it('refuses pull_request_target fork PR ref pattern (head)', () => {
|
||||
it('refuses pull_request_target fork PR ref pattern (head)', async () => {
|
||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: 'refs/pull/42/head',
|
||||
commit: '',
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).toThrow()
|
||||
).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('refuses pull_request_target fork PR ref pattern (merge)', () => {
|
||||
it('refuses pull_request_target fork PR ref pattern (merge)', async () => {
|
||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: 'refs/pull/42/merge',
|
||||
commit: '',
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).toThrow()
|
||||
).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('refuses pull_request_target when repository points at the fork', () => {
|
||||
it('refuses pull_request_target when repository points at the fork', async () => {
|
||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: FORK_QUALIFIED_REPO,
|
||||
ref: 'refs/heads/main',
|
||||
commit: '',
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).toThrow()
|
||||
).rejects.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', async () => {
|
||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: 'some-other/unrelated',
|
||||
ref: 'refs/heads/main',
|
||||
commit: '',
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).not.toThrow()
|
||||
).resolves.toBeUndefined()
|
||||
})
|
||||
|
||||
it('refuses pull_request_target ignoring repository case differences', () => {
|
||||
it('refuses pull_request_target ignoring repository case differences', async () => {
|
||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: FORK_QUALIFIED_REPO.toUpperCase(),
|
||||
ref: '',
|
||||
commit: '',
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).toThrow()
|
||||
).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('refuses pull_request_target ignoring commit SHA case differences', () => {
|
||||
it('refuses pull_request_target ignoring commit SHA case differences', async () => {
|
||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: '',
|
||||
commit: PR_HEAD_SHA.toUpperCase(),
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).toThrow()
|
||||
).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('allows pull_request_target fork PR checkout when opted in', () => {
|
||||
it('allows pull_request_target fork PR checkout when opted in', async () => {
|
||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: 'refs/pull/42/merge',
|
||||
commit: '',
|
||||
allowUnsafePrCheckout: true
|
||||
})
|
||||
).not.toThrow()
|
||||
).resolves.toBeUndefined()
|
||||
})
|
||||
|
||||
it('refuses workflow_run fork PR head_commit.id checkout', () => {
|
||||
it('refuses workflow_run fork PR head_commit.id checkout', async () => {
|
||||
setContext('workflow_run', forkWorkflowRunPayload())
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: '',
|
||||
commit: WORKFLOW_RUN_HEAD_COMMIT_SHA,
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).toThrow()
|
||||
).rejects.toThrow(/Refusing to check out fork pull request code/)
|
||||
})
|
||||
|
||||
it('refuses workflow_run with pull_request_target underlying event', () => {
|
||||
it('refuses workflow_run with pull_request_target underlying event', async () => {
|
||||
const payload = forkWorkflowRunPayload() as {
|
||||
workflow_run: {event: string}
|
||||
}
|
||||
payload.workflow_run.event = 'pull_request_target'
|
||||
setContext('workflow_run', payload)
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: '',
|
||||
commit: WORKFLOW_RUN_HEAD_COMMIT_SHA,
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).toThrow()
|
||||
).rejects.toThrow(/Refusing to check out fork pull request code/)
|
||||
})
|
||||
|
||||
it('allows workflow_run same-repo PR (head_repository.id matches base)', () => {
|
||||
it('allows workflow_run same-repo PR (head_repository.id matches base)', async () => {
|
||||
const payload = forkWorkflowRunPayload() as {
|
||||
workflow_run: {head_repository: {id: number}}
|
||||
}
|
||||
payload.workflow_run.head_repository.id = BASE_REPO_ID
|
||||
setContext('workflow_run', payload)
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
await expect(
|
||||
check({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: '',
|
||||
commit: WORKFLOW_RUN_HEAD_COMMIT_SHA,
|
||||
allowUnsafePrCheckout: false
|
||||
})
|
||||
).not.toThrow()
|
||||
).resolves.toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user