mirror of
https://github.com/actions/checkout.git
synced 2026-12-03 18:00:06 +00:00
Backport checkout validation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot App
parent
fbc6f3992d
commit
dc7694f54c
@@ -1,10 +1,19 @@
|
||||
import * as github from '@actions/github'
|
||||
import {assertSafePrCheckout} from '../lib/unsafe-pr-checkout-helper'
|
||||
import {assertSafePrCheckout as assertSafePrCheckoutImpl} from '../lib/unsafe-pr-checkout-helper'
|
||||
|
||||
// Shallow clone original @actions/github context
|
||||
const originalContext = {...github.context}
|
||||
const originalEventName = github.context.eventName
|
||||
const originalPayload = github.context.payload
|
||||
const originalSha = github.context.sha
|
||||
const mockCompareCommits = jest.fn()
|
||||
|
||||
function assertSafePrCheckout(
|
||||
input: Parameters<typeof assertSafePrCheckoutImpl>[0],
|
||||
authToken = 'checkout-token'
|
||||
): void | Promise<void> {
|
||||
return assertSafePrCheckoutImpl(input, authToken)
|
||||
}
|
||||
|
||||
const BASE_REPO_ID = 100
|
||||
const FORK_REPO_ID = 200
|
||||
@@ -63,11 +72,16 @@ describe('unsafe-pr-checkout-helper', () => {
|
||||
owner: 'some-owner',
|
||||
repo: 'some-repo'
|
||||
})
|
||||
jest.spyOn(github, 'getOctokit').mockReturnValue(
|
||||
{rest: {repos: {compareCommitsWithBasehead: mockCompareCommits}}} as any
|
||||
)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
;(github.context as {eventName: string}).eventName = originalEventName
|
||||
;(github.context as {payload: object}).payload = originalPayload
|
||||
github.context.sha = originalSha
|
||||
mockCompareCommits.mockReset()
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
@@ -264,4 +278,89 @@ describe('unsafe-pr-checkout-helper', () => {
|
||||
})
|
||||
).not.toThrow()
|
||||
})
|
||||
|
||||
it('refuses workflow_run checkout of an unverified merge SHA', 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,
|
||||
ref: '',
|
||||
commit: PR_MERGE_SHA,
|
||||
allowUnsafePrCheckout: false
|
||||
},
|
||||
'checkout-token'
|
||||
)
|
||||
).rejects.toThrow(/Refusing to check out the requested commit/)
|
||||
|
||||
expect(mockCompareCommits).toHaveBeenCalledWith({
|
||||
owner: 'some-owner',
|
||||
repo: 'some-repo',
|
||||
basehead: `${SAFE_BASE_SHA}...${PR_MERGE_SHA}`,
|
||||
per_page: 1
|
||||
})
|
||||
})
|
||||
|
||||
it('allows a workflow_run pin verified in workflow history', async () => {
|
||||
setContext('workflow_run', forkWorkflowRunPayload())
|
||||
github.context.sha = SAFE_BASE_SHA
|
||||
mockCompareCommits.mockResolvedValue({
|
||||
data: {
|
||||
base_commit: {sha: SAFE_BASE_SHA},
|
||||
merge_base_commit: {sha: PR_MERGE_SHA}
|
||||
}
|
||||
})
|
||||
|
||||
await expect(
|
||||
assertSafePrCheckout(
|
||||
{
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: '',
|
||||
commit: PR_MERGE_SHA,
|
||||
allowUnsafePrCheckout: false
|
||||
},
|
||||
'checkout-token'
|
||||
)
|
||||
).resolves.toBeUndefined()
|
||||
})
|
||||
|
||||
it('fails closed when workflow_run history cannot be checked', async () => {
|
||||
setContext('workflow_run', forkWorkflowRunPayload())
|
||||
github.context.sha = SAFE_BASE_SHA
|
||||
mockCompareCommits.mockRejectedValue(new Error('API unavailable'))
|
||||
|
||||
await expect(
|
||||
assertSafePrCheckout(
|
||||
{
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: '',
|
||||
commit: PR_MERGE_SHA,
|
||||
allowUnsafePrCheckout: false
|
||||
},
|
||||
'checkout-token'
|
||||
)
|
||||
).rejects.toThrow(/Unable to verify the requested commit/)
|
||||
})
|
||||
|
||||
it('allows an opted-out workflow_run merge SHA without a comparison', () => {
|
||||
setContext('workflow_run', forkWorkflowRunPayload())
|
||||
|
||||
expect(() =>
|
||||
assertSafePrCheckout({
|
||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||
ref: '',
|
||||
commit: PR_MERGE_SHA,
|
||||
allowUnsafePrCheckout: true
|
||||
})
|
||||
).not.toThrow()
|
||||
expect(mockCompareCommits).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user