Scope workflow history verification to the checkout server

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Francesco Renzi
2026-12-03 18:09:48 +01:00
parent a4ddf0647e
commit 8b33a4c30e
4 changed files with 78 additions and 8 deletions

View File

@@ -76,6 +76,7 @@ jest.unstable_mockModule('../src/workflow-context-helper.js', () => ({
// Dynamic imports after mocking // Dynamic imports after mocking
const core = await import('@actions/core') const core = await import('@actions/core')
const github = await import('@actions/github')
const inputHelper = await import('../src/input-helper.js') const inputHelper = await import('../src/input-helper.js')
type IGitSourceSettings = type IGitSourceSettings =
import('../src/git-source-settings.js').IGitSourceSettings import('../src/git-source-settings.js').IGitSourceSettings
@@ -271,11 +272,13 @@ describe('input-helper tests', () => {
}) })
describe('explicit checkout pins on a fork PR workflow_run', () => { describe('explicit checkout pins on a fork PR workflow_run', () => {
const originalServerUrl = process.env['GITHUB_SERVER_URL']
const workflowSha = '1234567890123456789012345678901234567890' const workflowSha = '1234567890123456789012345678901234567890'
const ancestorSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' const ancestorSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
const mergeSha = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' const mergeSha = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
beforeEach(() => { beforeEach(() => {
process.env['GITHUB_SERVER_URL'] = 'https://github.com'
inputs.token = 'checkout-token' inputs.token = 'checkout-token'
mockGithubContext.eventName = 'workflow_run' mockGithubContext.eventName = 'workflow_run'
mockGithubContext.payload = { mockGithubContext.payload = {
@@ -293,8 +296,61 @@ describe('input-helper tests', () => {
afterEach(() => { afterEach(() => {
mockGithubContext.eventName = '' mockGithubContext.eventName = ''
mockGithubContext.payload = {} mockGithubContext.payload = {}
if (originalServerUrl === undefined) {
delete process.env['GITHUB_SERVER_URL']
} else {
process.env['GITHUB_SERVER_URL'] = originalServerUrl
}
}) })
it.each([
'https://other.example.com',
'http://github.com',
'https://github.com:8443'
])(
'skips workflow history on another server origin: %s',
async serverUrl => {
inputs.ref = mergeSha
inputs['github-server-url'] = serverUrl
await expect(inputHelper.getInputs()).resolves.toMatchObject({
commit: mergeSha
})
expect(github.getOctokit).not.toHaveBeenCalled()
expect(mockCompareCommits).not.toHaveBeenCalled()
}
)
it.each([
[
'https://github.example.com',
'https://GITHUB.EXAMPLE.COM:443/',
'https://github.example.com/api/v3'
],
['https://org.ghe.com', 'https://org.ghe.com/', 'https://api.org.ghe.com']
])(
'uses the checkout API URL for workflow history on %s',
async (workflowServer, checkoutServer, apiUrl) => {
process.env['GITHUB_SERVER_URL'] = workflowServer
inputs['github-server-url'] = checkoutServer
inputs.ref = ancestorSha
mockCompareCommits.mockResolvedValue({
data: {
base_commit: {sha: workflowSha},
merge_base_commit: {sha: ancestorSha}
}
})
await expect(inputHelper.getInputs()).resolves.toMatchObject({
commit: ancestorSha
})
expect(github.getOctokit).toHaveBeenCalledWith('checkout-token', {
baseUrl: apiUrl
})
expect(mockCompareCommits).toHaveBeenCalledTimes(1)
}
)
it.each([ it.each([
['a PR merge commit outside workflow history', workflowSha, ancestorSha], ['a PR merge commit outside workflow history', workflowSha, ancestorSha],
['a comparison against an unexpected base', ancestorSha, mergeSha] ['a comparison against an unexpected base', ancestorSha, mergeSha]

11
dist/index.js vendored
View File

@@ -41969,6 +41969,7 @@ async function getGitCommandManager(settings) {
;// CONCATENATED MODULE: ./src/unsafe-pr-checkout-helper.ts ;// CONCATENATED MODULE: ./src/unsafe-pr-checkout-helper.ts
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/; const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/;
async function assertSafePrCheckout(input, authToken) { async function assertSafePrCheckout(input, authToken) {
if (input.allowUnsafePrCheckout) { if (input.allowUnsafePrCheckout) {
@@ -42023,7 +42024,8 @@ async function assertSafePrCheckout(input, authToken) {
const { owner, repo } = github_context.repo; const { owner, repo } = github_context.repo;
if (input.qualifiedRepository.toLowerCase() === if (input.qualifiedRepository.toLowerCase() ===
`${owner}/${repo}`.toLowerCase() && `${owner}/${repo}`.toLowerCase() &&
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit))) { getServerUrl(input.githubServerUrl).origin === getServerUrl().origin &&
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit, input.githubServerUrl))) {
throw new Error('Refusing to check out the requested commit for this fork pull request. ' + 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.'); 'The commit is not verified to be in the workflow commit history. Use a trusted branch ref.');
} }
@@ -42037,7 +42039,7 @@ async function assertSafePrCheckout(input, authToken) {
`at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' ` + `at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' ` +
`on the actions/checkout step.`); `on the actions/checkout step.`);
} }
async function commitIsInWorkflowHistory(authToken, owner, repo, commit) { async function commitIsInWorkflowHistory(authToken, owner, repo, commit, githubServerUrl) {
const workflowSha = github_context.sha?.toLowerCase() || ''; const workflowSha = github_context.sha?.toLowerCase() || '';
if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) { 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.'); throw new Error('Unable to verify the requested commit: the workflow commit SHA is missing or invalid.');
@@ -42047,7 +42049,7 @@ async function commitIsInWorkflowHistory(authToken, owner, repo, commit) {
} }
try { try {
// Compare immutable commits so the result does not depend on current PR metadata. // Compare immutable commits so the result does not depend on current PR metadata.
const { data } = await getOctokit(authToken) const { data } = await getOctokit(authToken, { baseUrl: getServerApiUrl(githubServerUrl) })
.rest.repos.compareCommitsWithBasehead({ .rest.repos.compareCommitsWithBasehead({
owner, owner,
repo, repo,
@@ -42252,7 +42254,8 @@ async function getInputs() {
qualifiedRepository, qualifiedRepository,
ref: result.ref, ref: result.ref,
commit: result.commit, commit: result.commit,
allowUnsafePrCheckout: result.allowUnsafePrCheckout allowUnsafePrCheckout: result.allowUnsafePrCheckout,
githubServerUrl: result.githubServerUrl
}, result.authToken); }, result.authToken);
} }
return result; return result;

View File

@@ -196,7 +196,8 @@ export async function getInputs(): Promise<IGitSourceSettings> {
qualifiedRepository, qualifiedRepository,
ref: result.ref, ref: result.ref,
commit: result.commit, commit: result.commit,
allowUnsafePrCheckout: result.allowUnsafePrCheckout allowUnsafePrCheckout: result.allowUnsafePrCheckout,
githubServerUrl: result.githubServerUrl
}, },
result.authToken result.authToken
) )

View File

@@ -1,5 +1,6 @@
import * as github from '@actions/github' import * as github from '@actions/github'
import {fromPayload} from './ref-helper.js' import {fromPayload} from './ref-helper.js'
import {getServerApiUrl, getServerUrl} from './url-helper.js'
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/ const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/
@@ -8,6 +9,7 @@ export interface IUnsafePrCheckoutInput {
ref: string ref: string
commit: string | undefined commit: string | undefined
allowUnsafePrCheckout: boolean allowUnsafePrCheckout: boolean
githubServerUrl?: string
} }
export async function assertSafePrCheckout( export async function assertSafePrCheckout(
@@ -76,7 +78,14 @@ export async function assertSafePrCheckout(
if ( if (
input.qualifiedRepository.toLowerCase() === input.qualifiedRepository.toLowerCase() ===
`${owner}/${repo}`.toLowerCase() && `${owner}/${repo}`.toLowerCase() &&
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit)) getServerUrl(input.githubServerUrl).origin === getServerUrl().origin &&
!(await commitIsInWorkflowHistory(
authToken,
owner,
repo,
commit,
input.githubServerUrl
))
) { ) {
throw new Error( throw new Error(
'Refusing to check out the requested commit for this fork pull request. ' + 'Refusing to check out the requested commit for this fork pull request. ' +
@@ -101,7 +110,8 @@ async function commitIsInWorkflowHistory(
authToken: string, authToken: string,
owner: string, owner: string,
repo: string, repo: string,
commit: string commit: string,
githubServerUrl?: string
): Promise<boolean> { ): Promise<boolean> {
const workflowSha = github.context.sha?.toLowerCase() || '' const workflowSha = github.context.sha?.toLowerCase() || ''
if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) { if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) {
@@ -116,7 +126,7 @@ async function commitIsInWorkflowHistory(
try { try {
// Compare immutable commits so the result does not depend on current PR metadata. // Compare immutable commits so the result does not depend on current PR metadata.
const {data} = await github const {data} = await github
.getOctokit(authToken) .getOctokit(authToken, {baseUrl: getServerApiUrl(githubServerUrl)})
.rest.repos.compareCommitsWithBasehead({ .rest.repos.compareCommitsWithBasehead({
owner, owner,
repo, repo,