From a6d00f2c31f14072678309fe8ff89743c67be051 Mon Sep 17 00:00:00 2001 From: Francesco Renzi Date: Thu, 3 Dec 2026 18:31:37 +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 | 28 +++++++++++++++++++++++++++- dist/index.js | 20 +++++++++++++++----- src/input-helper.ts | 3 ++- src/unsafe-pr-checkout-helper.ts | 24 +++++++++++++++--------- 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index 6910906..5e30ac6 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -177,6 +177,7 @@ describe('input-helper tests', () => { describe('workflow checkout server', () => { const originalServerUrl = process.env['GITHUB_SERVER_URL'] + const originalApiUrl = process.env['GITHUB_API_URL'] const originalEventName = github.context.eventName const originalPayload = github.context.payload const workflowSha = '1234567890123456789012345678901234567890' @@ -185,6 +186,7 @@ describe('input-helper tests', () => { beforeEach(() => { process.env['GITHUB_SERVER_URL'] = 'https://github.com' + process.env['GITHUB_API_URL'] = 'https://api.github.com' github.context.eventName = 'workflow_run' github.context.payload = { repository: {id: 100, name: 'some-repo', owner: {login: 'some-owner'}}, @@ -217,6 +219,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([ @@ -244,9 +251,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 await expect(inputHelper.getInputs()).resolves.toMatchObject({ @@ -258,6 +266,24 @@ describe('input-helper tests', () => { expect(mockCompareCommits).toHaveBeenCalledTimes(1) } ) + + 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' + mockCompareCommits.mockResolvedValue({ + data: { + base_commit: {sha: workflowSha}, + merge_base_commit: {sha: workflowSha} + } + }) + + await expect(inputHelper.getInputs()).rejects.toThrow( + /Refusing to check out the requested commit/ + ) + expect(github.getOctokit).toHaveBeenCalledWith('checkout-token', { + baseUrl: 'https://api.github.com' + }) + }) }) describe('unsafe PR checkout guard', () => { diff --git a/dist/index.js b/dist/index.js index 5652be6..df80364 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2014,7 +2014,8 @@ function getInputs() { ref: result.ref, commit: result.commit, allowUnsafePrCheckout: result.allowUnsafePrCheckout, - githubServerUrl: result.githubServerUrl + githubServerUrl: result.githubServerUrl, + usesSsh: !!result.sshKey }, result.authToken); } return result; @@ -2689,8 +2690,8 @@ function assertSafePrCheckout(input, authToken) { commit && input.qualifiedRepository.toLowerCase() === `${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() && - (0, url_helper_1.getServerUrl)(input.githubServerUrl).origin === (0, url_helper_1.getServerUrl)().origin) { - return commitIsInWorkflowHistory(authToken, commit, input.githubServerUrl).then(isInHistory => { + fetchesFromWorkflowServer(input)) { + return commitIsInWorkflowHistory(authToken, commit).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.'); @@ -2706,7 +2707,16 @@ 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.`); } -function commitIsInWorkflowHistory(authToken, 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 = (0, url_helper_1.getServerUrl)(input.githubServerUrl); + const workflowServer = (0, url_helper_1.getServerUrl)(); + return input.usesSsh + ? checkoutServer.hostname === workflowServer.hostname + : checkoutServer.origin === workflowServer.origin; +} +function commitIsInWorkflowHistory(authToken, commit) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const workflowSha = github.context.sha.toLowerCase(); @@ -2719,7 +2729,7 @@ function commitIsInWorkflowHistory(authToken, commit, githubServerUrl) { const { owner, repo } = github.context.repo; try { const { data } = yield github - .getOctokit(authToken, { baseUrl: (0, url_helper_1.getServerApiUrl)(githubServerUrl) }) + .getOctokit(authToken, { baseUrl: (0, url_helper_1.getServerApiUrl)() }) .rest.repos.compareCommitsWithBasehead({ owner, repo, diff --git a/src/input-helper.ts b/src/input-helper.ts index 72ca397..d30b637 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 cf83855..fcca9b1 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 function assertSafePrCheckout( @@ -79,13 +80,9 @@ export function assertSafePrCheckout( commit && input.qualifiedRepository.toLowerCase() === `${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() && - getServerUrl(input.githubServerUrl).origin === getServerUrl().origin + fetchesFromWorkflowServer(input) ) { - return commitIsInWorkflowHistory( - authToken, - commit, - input.githubServerUrl - ).then(isInHistory => { + return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => { if (!isInHistory) { throw new Error( 'Refusing to check out the requested commit for this fork pull request. ' + @@ -107,10 +104,19 @@ export 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, - commit: string, - githubServerUrl?: string + commit: string ): Promise { const workflowSha = github.context.sha.toLowerCase() if (!/^[0-9a-f]{40}$/.test(workflowSha)) { @@ -125,7 +131,7 @@ async function commitIsInWorkflowHistory( const {owner, repo} = github.context.repo try { const {data} = await github - .getOctokit(authToken, {baseUrl: getServerApiUrl(githubServerUrl)}) + .getOctokit(authToken, {baseUrl: getServerApiUrl()}) .rest.repos.compareCommitsWithBasehead({ owner, repo,