mirror of
https://github.com/actions/checkout.git
synced 2026-12-03 19:10:01 +00:00
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
152 lines
4.9 KiB
TypeScript
152 lines
4.9 KiB
TypeScript
import * as github from '@actions/github'
|
|
import {fromPayload} from './ref-helper'
|
|
import {getServerApiUrl, getServerUrl} from './url-helper'
|
|
|
|
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/
|
|
|
|
export interface IUnsafePrCheckoutInput {
|
|
qualifiedRepository: string
|
|
ref: string
|
|
commit: string | undefined
|
|
allowUnsafePrCheckout: boolean
|
|
githubServerUrl?: string
|
|
}
|
|
|
|
export function assertSafePrCheckout(
|
|
input: IUnsafePrCheckoutInput,
|
|
authToken: string
|
|
): void | Promise<void> {
|
|
if (input.allowUnsafePrCheckout) {
|
|
return
|
|
}
|
|
|
|
const eventName = github.context.eventName
|
|
if (eventName !== 'pull_request_target' && eventName !== 'workflow_run') {
|
|
return
|
|
}
|
|
|
|
const baseRepoId = fromPayload('repository.id')
|
|
if (typeof baseRepoId !== 'number') {
|
|
return
|
|
}
|
|
|
|
let prHeadRepoId: unknown
|
|
let prHeadRepoFullName: unknown
|
|
const prShas: string[] = []
|
|
|
|
if (eventName === 'pull_request_target') {
|
|
prHeadRepoId = fromPayload('pull_request.head.repo.id')
|
|
prHeadRepoFullName = fromPayload('pull_request.head.repo.full_name')
|
|
pushIfSha(prShas, fromPayload('pull_request.head.sha'))
|
|
pushIfSha(prShas, fromPayload('pull_request.merge_commit_sha'))
|
|
} else {
|
|
const wrEvent = fromPayload('workflow_run.event')
|
|
if (typeof wrEvent !== 'string' || !wrEvent.startsWith('pull_request')) {
|
|
return
|
|
}
|
|
prHeadRepoId = fromPayload('workflow_run.head_repository.id')
|
|
prHeadRepoFullName = fromPayload('workflow_run.head_repository.full_name')
|
|
pushIfSha(prShas, fromPayload('workflow_run.head_commit.id'))
|
|
// For `pull_request_target`-triggered workflow_run, `head_sha` is the base
|
|
// default branch SHA (not the PR head)
|
|
if (wrEvent !== 'pull_request_target') {
|
|
pushIfSha(prShas, fromPayload('workflow_run.head_sha'))
|
|
}
|
|
}
|
|
|
|
// (A) Fork PR?
|
|
if (typeof prHeadRepoId !== 'number' || prHeadRepoId === baseRepoId) {
|
|
return
|
|
}
|
|
|
|
// (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 repositoryMatchesPrHead =
|
|
typeof prHeadRepoFullName === 'string' &&
|
|
input.qualifiedRepository.toLowerCase() === prHeadRepoFullName.toLowerCase()
|
|
const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref)
|
|
const commit = input.commit && input.commit.toLowerCase()
|
|
const commitMatchesPrHeadSha = !!commit && prShas.includes(commit)
|
|
|
|
if (
|
|
!repositoryMatchesPrHead &&
|
|
!refMatchesPullPattern &&
|
|
!commitMatchesPrHeadSha
|
|
) {
|
|
if (
|
|
eventName === 'workflow_run' &&
|
|
!input.ref &&
|
|
commit &&
|
|
input.qualifiedRepository.toLowerCase() ===
|
|
`${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() &&
|
|
getServerUrl(input.githubServerUrl).origin === getServerUrl().origin
|
|
) {
|
|
return commitIsInWorkflowHistory(
|
|
authToken,
|
|
commit,
|
|
input.githubServerUrl
|
|
).then(isInHistory => {
|
|
if (!isInHistory) {
|
|
throw new Error(
|
|
'Refusing to check out the requested commit for this fork pull request. ' +
|
|
'The commit is not verified to be in the workflow commit history. Use a trusted branch ref.'
|
|
)
|
|
}
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
throw new Error(
|
|
`Refusing to check out fork pull request code from a '${eventName}' workflow. ` +
|
|
`This workflow runs with the base repository's GITHUB_TOKEN, secrets, default-branch ` +
|
|
`cache scope, and runner access. Fetching and executing a fork's code in that trusted ` +
|
|
`context commonly leads to "pwn request" vulnerabilities. To opt in, review the risks ` +
|
|
`at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' ` +
|
|
`on the actions/checkout step.`
|
|
)
|
|
}
|
|
|
|
async function commitIsInWorkflowHistory(
|
|
authToken: string,
|
|
commit: string,
|
|
githubServerUrl?: string
|
|
): Promise<boolean> {
|
|
const workflowSha = github.context.sha.toLowerCase()
|
|
if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) {
|
|
throw new Error(
|
|
'Unable to verify the requested commit: the workflow commit SHA is missing or invalid.'
|
|
)
|
|
}
|
|
if (commit === workflowSha) {
|
|
return true
|
|
}
|
|
|
|
const {owner, repo} = github.context.repo
|
|
try {
|
|
const {data} = await github
|
|
.getOctokit(authToken, {baseUrl: getServerApiUrl(githubServerUrl)})
|
|
.rest.repos.compareCommitsWithBasehead({
|
|
owner,
|
|
repo,
|
|
basehead: `${workflowSha}...${commit}`,
|
|
per_page: 1
|
|
})
|
|
return (
|
|
data?.base_commit?.sha === workflowSha &&
|
|
data?.merge_base_commit?.sha === commit
|
|
)
|
|
} catch (error) {
|
|
throw new Error(
|
|
'Unable to verify the requested commit against the workflow commit history. ' +
|
|
'Check that the checkout token has contents: read, or use a trusted branch ref.'
|
|
)
|
|
}
|
|
}
|
|
|
|
function pushIfSha(target: string[], value: unknown): void {
|
|
if (typeof value === 'string' && value.length > 0) {
|
|
target.push(value.toLowerCase())
|
|
}
|
|
}
|