mirror of
https://github.com/actions/checkout
synced 2026-12-03 20:43:07 +00:00
Scope workflow history verification to the checkout server
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
1 parent
57c9bdfe25
commit
b1df8e93a6
4 files changed
+107
-10
No files matched your search
@@ -175,6 +175,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},
|
||||
|
||||
Vendored
+8
-5
@@ -2013,7 +2013,8 @@ function getInputs() {
|
||||
qualifiedRepository,
|
||||
ref: result.ref,
|
||||
commit: result.commit,
|
||||
allowUnsafePrCheckout: result.allowUnsafePrCheckout
|
||||
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
|
||||
githubServerUrl: result.githubServerUrl
|
||||
}, result.authToken);
|
||||
}
|
||||
return result;
|
||||
@@ -2632,6 +2633,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) {
|
||||
@@ -2686,8 +2688,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.');
|
||||
@@ -2703,7 +2706,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();
|
||||
@@ -2716,7 +2719,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,
|
||||
|
||||
+2
-1
@@ -196,7 +196,8 @@ export async function getInputs(): Promise<IGitSourceSettings> {
|
||||
qualifiedRepository,
|
||||
ref: result.ref,
|
||||
commit: result.commit,
|
||||
allowUnsafePrCheckout: result.allowUnsafePrCheckout
|
||||
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
|
||||
githubServerUrl: result.githubServerUrl
|
||||
},
|
||||
result.authToken
|
||||
)
|
||||
|
||||
@@ -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<boolean> {
|
||||
const workflowSha = github.context.sha.toLowerCase()
|
||||
if (!/^[0-9a-f]{40}$/.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,
|
||||
|
||||
Reference in new issue
Block a user