diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index c35a735..a9390a1 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -195,6 +195,91 @@ describe('input-helper tests', () => { expect(settings.workflowOrganizationId).toBe(123456) }) + describe('workflow checkout server', () => { + const originalServerUrl = process.env['GITHUB_SERVER_URL'] + const originalEventName = github.context.eventName + const originalPayload = github.context.payload + const workflowSha = '1234567890123456789012345678901234567890' + const ancestorSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + const mockCompareCommits = jest.fn() + + beforeEach(() => { + process.env['GITHUB_SERVER_URL'] = 'https://github.com' + github.context.eventName = 'workflow_run' + github.context.payload = { + repository: {id: 100, name: 'some-repo', owner: {login: 'some-owner'}}, + workflow_run: { + event: 'pull_request', + head_repository: {id: 200, full_name: 'fork-owner/some-repo'}, + head_sha: 'cccccccccccccccccccccccccccccccccccccccc' + } + } + inputs.token = 'checkout-token' + inputs.ref = ancestorSha + mockCompareCommits.mockReset() + mockCompareCommits.mockResolvedValue({ + data: { + base_commit: {sha: workflowSha}, + merge_base_commit: {sha: ancestorSha} + } + }) + jest.spyOn(github, 'getOctokit').mockReturnValue({ + rest: {repos: {compareCommitsWithBasehead: mockCompareCommits}} + } as any) + }) + + afterEach(() => { + github.context.eventName = originalEventName + github.context.payload = originalPayload + jest.mocked(github.getOctokit).mockRestore() + 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['github-server-url'] = serverUrl + + await expect(inputHelper.getInputs()).resolves.toMatchObject({ + commit: ancestorSha + }) + 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 + + await expect(inputHelper.getInputs()).resolves.toMatchObject({ + commit: ancestorSha + }) + expect(github.getOctokit).toHaveBeenCalledWith('checkout-token', { + baseUrl: apiUrl + }) + expect(mockCompareCommits).toHaveBeenCalledTimes(1) + } + ) + }) + describe('unsafe PR checkout guard', () => { const forkPayload = { repository: {id: 100}, diff --git a/dist/index.js b/dist/index.js index a03c077..3c71f4b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2177,7 +2177,8 @@ function getInputs() { qualifiedRepository, ref: result.ref, commit: result.commit, - allowUnsafePrCheckout: result.allowUnsafePrCheckout + allowUnsafePrCheckout: result.allowUnsafePrCheckout, + githubServerUrl: result.githubServerUrl }, result.authToken); } return result; @@ -2812,6 +2813,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.assertSafePrCheckout = assertSafePrCheckout; const github = __importStar(__nccwpck_require__(5438)); const ref_helper_1 = __nccwpck_require__(8601); +const url_helper_1 = __nccwpck_require__(9437); const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/; function assertSafePrCheckout(input, authToken) { if (input.allowUnsafePrCheckout) { @@ -2866,8 +2868,9 @@ function assertSafePrCheckout(input, authToken) { !input.ref && commit && input.qualifiedRepository.toLowerCase() === - `${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase()) { - return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => { + `${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 => { 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.'); @@ -2883,7 +2886,7 @@ 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) { +function commitIsInWorkflowHistory(authToken, commit, githubServerUrl) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const workflowSha = github.context.sha.toLowerCase(); @@ -2896,7 +2899,7 @@ function commitIsInWorkflowHistory(authToken, commit) { const { owner, repo } = github.context.repo; try { const { data } = yield github - .getOctokit(authToken) + .getOctokit(authToken, { baseUrl: (0, url_helper_1.getServerApiUrl)(githubServerUrl) }) .rest.repos.compareCommitsWithBasehead({ owner, repo, diff --git a/src/input-helper.ts b/src/input-helper.ts index 2a023e9..f892cd7 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 6fbe50f..271c84c 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' +import {getServerApiUrl, getServerUrl} from './url-helper' 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 function assertSafePrCheckout( @@ -76,9 +78,14 @@ export function assertSafePrCheckout( !input.ref && commit && input.qualifiedRepository.toLowerCase() === - `${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() + `${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() && + getServerUrl(input.githubServerUrl).origin === getServerUrl().origin ) { - return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => { + 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. ' + @@ -102,7 +109,8 @@ export function assertSafePrCheckout( async function commitIsInWorkflowHistory( authToken: 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)) { @@ -117,7 +125,7 @@ async function commitIsInWorkflowHistory( const {owner, repo} = github.context.repo try { const {data} = await github - .getOctokit(authToken) + .getOctokit(authToken, {baseUrl: getServerApiUrl(githubServerUrl)}) .rest.repos.compareCommitsWithBasehead({ owner, repo,