From 152ac2d54428b05d4eb0b005cb437880a60a20c3 Mon Sep 17 00:00:00 2001 From: Francesco Renzi Date: Thu, 3 Dec 2026 18:28:21 +0100 Subject: [PATCH] Verify SSH pins against the workflow server SSH fetch URLs keep only the hostname of github-server-url, so a port or scheme change no longer skips workflow history verification. Verification always uses the workflow's API endpoint. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- __test__/input-helper.test.ts | 29 ++++++++++++++++++++++++++++- dist/index.js | 20 +++++++++++++++----- src/input-helper.ts | 3 ++- src/unsafe-pr-checkout-helper.ts | 26 +++++++++++++++----------- 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index 72d8b0d..44c4afd 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -273,12 +273,14 @@ describe('input-helper tests', () => { describe('explicit checkout pins on a fork PR workflow_run', () => { const originalServerUrl = process.env['GITHUB_SERVER_URL'] + const originalApiUrl = process.env['GITHUB_API_URL'] const workflowSha = '1234567890123456789012345678901234567890' const ancestorSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' const mergeSha = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' beforeEach(() => { process.env['GITHUB_SERVER_URL'] = 'https://github.com' + process.env['GITHUB_API_URL'] = 'https://api.github.com' inputs.token = 'checkout-token' mockGithubContext.eventName = 'workflow_run' mockGithubContext.payload = { @@ -301,6 +303,11 @@ describe('input-helper tests', () => { } else { process.env['GITHUB_SERVER_URL'] = originalServerUrl } + if (originalApiUrl === undefined) { + delete process.env['GITHUB_API_URL'] + } else { + process.env['GITHUB_API_URL'] = originalApiUrl + } }) it.each([ @@ -329,9 +336,10 @@ describe('input-helper tests', () => { ], ['https://org.ghe.com', 'https://org.ghe.com/', 'https://api.org.ghe.com'] ])( - 'uses the checkout API URL for workflow history on %s', + 'uses the workflow API URL for workflow history on %s', async (workflowServer, checkoutServer, apiUrl) => { process.env['GITHUB_SERVER_URL'] = workflowServer + process.env['GITHUB_API_URL'] = apiUrl inputs['github-server-url'] = checkoutServer inputs.ref = ancestorSha mockCompareCommits.mockResolvedValue({ @@ -351,6 +359,25 @@ describe('input-helper tests', () => { } ) + it('verifies an SSH pin whose host matches the workflow server', async () => { + inputs['ssh-key'] = 'some ssh private key' + inputs['github-server-url'] = 'https://github.com:8443' + inputs.ref = mergeSha + mockCompareCommits.mockResolvedValue({ + data: { + base_commit: {sha: workflowSha}, + merge_base_commit: {sha: ancestorSha} + } + }) + + await expect(inputHelper.getInputs()).rejects.toThrow( + /Refusing to check out the requested commit/ + ) + expect(github.getOctokit).toHaveBeenCalledWith('checkout-token', { + baseUrl: 'https://api.github.com' + }) + }) + 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 663a498..f837b71 100644 --- a/dist/index.js +++ b/dist/index.js @@ -42024,8 +42024,8 @@ async function assertSafePrCheckout(input, authToken) { const { owner, repo } = github_context.repo; if (input.qualifiedRepository.toLowerCase() === `${owner}/${repo}`.toLowerCase() && - getServerUrl(input.githubServerUrl).origin === getServerUrl().origin && - !(await commitIsInWorkflowHistory(authToken, owner, repo, commit, input.githubServerUrl))) { + fetchesFromWorkflowServer(input) && + !(await commitIsInWorkflowHistory(authToken, owner, repo, commit))) { 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.'); } @@ -42039,7 +42039,16 @@ 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, githubServerUrl) { +// SSH fetch URLs keep only the hostname, so the scheme and port cannot +// distinguish the checkout server from the workflow server. +function fetchesFromWorkflowServer(input) { + const checkoutServer = getServerUrl(input.githubServerUrl); + const workflowServer = getServerUrl(); + return input.usesSsh + ? checkoutServer.hostname === workflowServer.hostname + : checkoutServer.origin === workflowServer.origin; +} +async function commitIsInWorkflowHistory(authToken, owner, repo, commit) { 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.'); @@ -42049,7 +42058,7 @@ async function commitIsInWorkflowHistory(authToken, owner, repo, commit, githubS } try { // Compare immutable commits so the result does not depend on current PR metadata. - const { data } = await getOctokit(authToken, { baseUrl: getServerApiUrl(githubServerUrl) }) + const { data } = await getOctokit(authToken, { baseUrl: getServerApiUrl() }) .rest.repos.compareCommitsWithBasehead({ owner, repo, @@ -42255,7 +42264,8 @@ async function getInputs() { ref: result.ref, commit: result.commit, allowUnsafePrCheckout: result.allowUnsafePrCheckout, - githubServerUrl: result.githubServerUrl + githubServerUrl: result.githubServerUrl, + usesSsh: !!result.sshKey }, result.authToken); } return result; diff --git a/src/input-helper.ts b/src/input-helper.ts index 01a44f7..3ccf35c 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -197,7 +197,8 @@ export async function getInputs(): Promise { ref: result.ref, commit: result.commit, allowUnsafePrCheckout: result.allowUnsafePrCheckout, - githubServerUrl: result.githubServerUrl + githubServerUrl: result.githubServerUrl, + usesSsh: !!result.sshKey }, result.authToken ) diff --git a/src/unsafe-pr-checkout-helper.ts b/src/unsafe-pr-checkout-helper.ts index 1ff80de..3edb8d5 100644 --- a/src/unsafe-pr-checkout-helper.ts +++ b/src/unsafe-pr-checkout-helper.ts @@ -10,6 +10,7 @@ export interface IUnsafePrCheckoutInput { commit: string | undefined allowUnsafePrCheckout: boolean githubServerUrl?: string + usesSsh?: boolean } export async function assertSafePrCheckout( @@ -78,14 +79,8 @@ export async function assertSafePrCheckout( if ( input.qualifiedRepository.toLowerCase() === `${owner}/${repo}`.toLowerCase() && - getServerUrl(input.githubServerUrl).origin === getServerUrl().origin && - !(await commitIsInWorkflowHistory( - authToken, - owner, - repo, - commit, - input.githubServerUrl - )) + fetchesFromWorkflowServer(input) && + !(await commitIsInWorkflowHistory(authToken, owner, repo, commit)) ) { throw new Error( 'Refusing to check out the requested commit for this fork pull request. ' + @@ -106,12 +101,21 @@ export async function assertSafePrCheckout( ) } +// SSH fetch URLs keep only the hostname, so the scheme and port cannot +// distinguish the checkout server from the workflow server. +function fetchesFromWorkflowServer(input: IUnsafePrCheckoutInput): boolean { + const checkoutServer = getServerUrl(input.githubServerUrl) + const workflowServer = getServerUrl() + return input.usesSsh + ? checkoutServer.hostname === workflowServer.hostname + : checkoutServer.origin === workflowServer.origin +} + async function commitIsInWorkflowHistory( authToken: string, owner: string, repo: string, - commit: string, - githubServerUrl?: string + commit: string ): Promise { const workflowSha = github.context.sha?.toLowerCase() || '' if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) { @@ -126,7 +130,7 @@ async function commitIsInWorkflowHistory( try { // Compare immutable commits so the result does not depend on current PR metadata. const {data} = await github - .getOctokit(authToken, {baseUrl: getServerApiUrl(githubServerUrl)}) + .getOctokit(authToken, {baseUrl: getServerApiUrl()}) .rest.repos.compareCommitsWithBasehead({ owner, repo,