mirror of
https://github.com/actions/checkout.git
synced 2026-12-04 03:22:56 +08: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:
@@ -175,6 +175,91 @@ describe('input-helper tests', () => {
|
|||||||
expect(settings.workflowOrganizationId).toBe(123456)
|
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', () => {
|
describe('unsafe PR checkout guard', () => {
|
||||||
const forkPayload = {
|
const forkPayload = {
|
||||||
repository: {id: 100},
|
repository: {id: 100},
|
||||||
|
|||||||
13
dist/index.js
vendored
13
dist/index.js
vendored
@@ -2013,7 +2013,8 @@ function getInputs() {
|
|||||||
qualifiedRepository,
|
qualifiedRepository,
|
||||||
ref: result.ref,
|
ref: result.ref,
|
||||||
commit: result.commit,
|
commit: result.commit,
|
||||||
allowUnsafePrCheckout: result.allowUnsafePrCheckout
|
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
|
||||||
|
githubServerUrl: result.githubServerUrl
|
||||||
}, result.authToken);
|
}, result.authToken);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -2632,6 +2633,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
|
|||||||
exports.assertSafePrCheckout = assertSafePrCheckout;
|
exports.assertSafePrCheckout = assertSafePrCheckout;
|
||||||
const github = __importStar(__nccwpck_require__(5438));
|
const github = __importStar(__nccwpck_require__(5438));
|
||||||
const ref_helper_1 = __nccwpck_require__(8601);
|
const ref_helper_1 = __nccwpck_require__(8601);
|
||||||
|
const url_helper_1 = __nccwpck_require__(9437);
|
||||||
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/;
|
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/;
|
||||||
function assertSafePrCheckout(input, authToken) {
|
function assertSafePrCheckout(input, authToken) {
|
||||||
if (input.allowUnsafePrCheckout) {
|
if (input.allowUnsafePrCheckout) {
|
||||||
@@ -2686,8 +2688,9 @@ function assertSafePrCheckout(input, authToken) {
|
|||||||
!input.ref &&
|
!input.ref &&
|
||||||
commit &&
|
commit &&
|
||||||
input.qualifiedRepository.toLowerCase() ===
|
input.qualifiedRepository.toLowerCase() ===
|
||||||
`${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase()) {
|
`${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() &&
|
||||||
return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => {
|
(0, url_helper_1.getServerUrl)(input.githubServerUrl).origin === (0, url_helper_1.getServerUrl)().origin) {
|
||||||
|
return commitIsInWorkflowHistory(authToken, commit, input.githubServerUrl).then(isInHistory => {
|
||||||
if (!isInHistory) {
|
if (!isInHistory) {
|
||||||
throw new Error('Refusing to check out the requested commit for this fork pull request. ' +
|
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.');
|
'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' ` +
|
`at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' ` +
|
||||||
`on the actions/checkout step.`);
|
`on the actions/checkout step.`);
|
||||||
}
|
}
|
||||||
function commitIsInWorkflowHistory(authToken, commit) {
|
function commitIsInWorkflowHistory(authToken, commit, githubServerUrl) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
var _a, _b;
|
var _a, _b;
|
||||||
const workflowSha = github.context.sha.toLowerCase();
|
const workflowSha = github.context.sha.toLowerCase();
|
||||||
@@ -2716,7 +2719,7 @@ function commitIsInWorkflowHistory(authToken, commit) {
|
|||||||
const { owner, repo } = github.context.repo;
|
const { owner, repo } = github.context.repo;
|
||||||
try {
|
try {
|
||||||
const { data } = yield github
|
const { data } = yield github
|
||||||
.getOctokit(authToken)
|
.getOctokit(authToken, { baseUrl: (0, url_helper_1.getServerApiUrl)(githubServerUrl) })
|
||||||
.rest.repos.compareCommitsWithBasehead({
|
.rest.repos.compareCommitsWithBasehead({
|
||||||
owner,
|
owner,
|
||||||
repo,
|
repo,
|
||||||
|
|||||||
@@ -196,7 +196,8 @@ export async function getInputs(): Promise<IGitSourceSettings> {
|
|||||||
qualifiedRepository,
|
qualifiedRepository,
|
||||||
ref: result.ref,
|
ref: result.ref,
|
||||||
commit: result.commit,
|
commit: result.commit,
|
||||||
allowUnsafePrCheckout: result.allowUnsafePrCheckout
|
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
|
||||||
|
githubServerUrl: result.githubServerUrl
|
||||||
},
|
},
|
||||||
result.authToken
|
result.authToken
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import * as github from '@actions/github'
|
import * as github from '@actions/github'
|
||||||
import {fromPayload} from './ref-helper'
|
import {fromPayload} from './ref-helper'
|
||||||
|
import {getServerApiUrl, getServerUrl} from './url-helper'
|
||||||
|
|
||||||
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/
|
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/
|
||||||
|
|
||||||
@@ -8,6 +9,7 @@ export interface IUnsafePrCheckoutInput {
|
|||||||
ref: string
|
ref: string
|
||||||
commit: string | undefined
|
commit: string | undefined
|
||||||
allowUnsafePrCheckout: boolean
|
allowUnsafePrCheckout: boolean
|
||||||
|
githubServerUrl?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assertSafePrCheckout(
|
export function assertSafePrCheckout(
|
||||||
@@ -76,9 +78,14 @@ export function assertSafePrCheckout(
|
|||||||
!input.ref &&
|
!input.ref &&
|
||||||
commit &&
|
commit &&
|
||||||
input.qualifiedRepository.toLowerCase() ===
|
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) {
|
if (!isInHistory) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Refusing to check out the requested commit for this fork pull request. ' +
|
'Refusing to check out the requested commit for this fork pull request. ' +
|
||||||
@@ -102,7 +109,8 @@ export function assertSafePrCheckout(
|
|||||||
|
|
||||||
async function commitIsInWorkflowHistory(
|
async function commitIsInWorkflowHistory(
|
||||||
authToken: string,
|
authToken: string,
|
||||||
commit: string
|
commit: string,
|
||||||
|
githubServerUrl?: string
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const workflowSha = github.context.sha.toLowerCase()
|
const workflowSha = github.context.sha.toLowerCase()
|
||||||
if (!/^[0-9a-f]{40}$/.test(workflowSha)) {
|
if (!/^[0-9a-f]{40}$/.test(workflowSha)) {
|
||||||
@@ -117,7 +125,7 @@ async function commitIsInWorkflowHistory(
|
|||||||
const {owner, repo} = github.context.repo
|
const {owner, repo} = github.context.repo
|
||||||
try {
|
try {
|
||||||
const {data} = await github
|
const {data} = await github
|
||||||
.getOctokit(authToken)
|
.getOctokit(authToken, {baseUrl: getServerApiUrl(githubServerUrl)})
|
||||||
.rest.repos.compareCommitsWithBasehead({
|
.rest.repos.compareCommitsWithBasehead({
|
||||||
owner,
|
owner,
|
||||||
repo,
|
repo,
|
||||||
|
|||||||
Reference in New Issue
Block a user