From 8b33a4c30e28065e1d65ae233a2b7e83175fdbc7 Mon Sep 17 00:00:00 2001 From: Francesco Renzi Date: Thu, 3 Dec 2026 18:09:48 +0100 Subject: [PATCH] Scope workflow history verification to the checkout server Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- __test__/input-helper.test.ts | 56 ++++++++++++++++++++++++++++++++ dist/index.js | 11 ++++--- src/input-helper.ts | 3 +- src/unsafe-pr-checkout-helper.ts | 16 +++++++-- 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index 2d1da1c..72d8b0d 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -76,6 +76,7 @@ jest.unstable_mockModule('../src/workflow-context-helper.js', () => ({ // Dynamic imports after mocking const core = await import('@actions/core') +const github = await import('@actions/github') const inputHelper = await import('../src/input-helper.js') type 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', () => { + const originalServerUrl = process.env['GITHUB_SERVER_URL'] const workflowSha = '1234567890123456789012345678901234567890' const ancestorSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' const mergeSha = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' beforeEach(() => { + process.env['GITHUB_SERVER_URL'] = 'https://github.com' inputs.token = 'checkout-token' mockGithubContext.eventName = 'workflow_run' mockGithubContext.payload = { @@ -293,8 +296,61 @@ describe('input-helper tests', () => { afterEach(() => { mockGithubContext.eventName = '' 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([ ['a PR merge commit outside workflow history', workflowSha, ancestorSha], ['a comparison against an unexpected base', ancestorSha, mergeSha] diff --git a/dist/index.js b/dist/index.js index ffa52c4..663a498 100644 --- a/dist/index.js +++ b/dist/index.js @@ -41969,6 +41969,7 @@ async function getGitCommandManager(settings) { ;// CONCATENATED MODULE: ./src/unsafe-pr-checkout-helper.ts + const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/; async function assertSafePrCheckout(input, authToken) { if (input.allowUnsafePrCheckout) { @@ -42023,7 +42024,8 @@ async function assertSafePrCheckout(input, authToken) { const { owner, repo } = github_context.repo; if (input.qualifiedRepository.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. ' + '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' ` + `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() || ''; 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.'); @@ -42047,7 +42049,7 @@ async function commitIsInWorkflowHistory(authToken, owner, repo, commit) { } try { // 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({ owner, repo, @@ -42252,7 +42254,8 @@ async function getInputs() { qualifiedRepository, ref: result.ref, commit: result.commit, - allowUnsafePrCheckout: result.allowUnsafePrCheckout + allowUnsafePrCheckout: result.allowUnsafePrCheckout, + githubServerUrl: result.githubServerUrl }, result.authToken); } return result; diff --git a/src/input-helper.ts b/src/input-helper.ts index 490c67e..01a44f7 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -196,7 +196,8 @@ export async function getInputs(): Promise { qualifiedRepository, ref: result.ref, commit: result.commit, - allowUnsafePrCheckout: result.allowUnsafePrCheckout + allowUnsafePrCheckout: result.allowUnsafePrCheckout, + githubServerUrl: result.githubServerUrl }, result.authToken ) diff --git a/src/unsafe-pr-checkout-helper.ts b/src/unsafe-pr-checkout-helper.ts index f310e04..1ff80de 100644 --- a/src/unsafe-pr-checkout-helper.ts +++ b/src/unsafe-pr-checkout-helper.ts @@ -1,5 +1,6 @@ import * as github from '@actions/github' import {fromPayload} from './ref-helper.js' +import {getServerApiUrl, getServerUrl} from './url-helper.js' const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/ @@ -8,6 +9,7 @@ export interface IUnsafePrCheckoutInput { ref: string commit: string | undefined allowUnsafePrCheckout: boolean + githubServerUrl?: string } export async function assertSafePrCheckout( @@ -76,7 +78,14 @@ export async function assertSafePrCheckout( if ( input.qualifiedRepository.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. ' + @@ -101,7 +110,8 @@ async function commitIsInWorkflowHistory( authToken: string, owner: string, repo: string, - commit: string + commit: string, + githubServerUrl?: string ): Promise { const workflowSha = github.context.sha?.toLowerCase() || '' if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) { @@ -116,7 +126,7 @@ async function commitIsInWorkflowHistory( try { // Compare immutable commits so the result does not depend on current PR metadata. const {data} = await github - .getOctokit(authToken) + .getOctokit(authToken, {baseUrl: getServerApiUrl(githubServerUrl)}) .rest.repos.compareCommitsWithBasehead({ owner, repo,