mirror of
https://github.com/actions/checkout.git
synced 2026-12-04 03:22:56 +08:00
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>
This commit is contained in:
@@ -175,6 +175,7 @@ describe('input-helper tests', () => {
|
|||||||
|
|
||||||
describe('workflow checkout server', () => {
|
describe('workflow checkout server', () => {
|
||||||
const originalServerUrl = process.env['GITHUB_SERVER_URL']
|
const originalServerUrl = process.env['GITHUB_SERVER_URL']
|
||||||
|
const originalApiUrl = process.env['GITHUB_API_URL']
|
||||||
const originalEventName = github.context.eventName
|
const originalEventName = github.context.eventName
|
||||||
const originalPayload = github.context.payload
|
const originalPayload = github.context.payload
|
||||||
const workflowSha = '1234567890123456789012345678901234567890'
|
const workflowSha = '1234567890123456789012345678901234567890'
|
||||||
@@ -184,6 +185,7 @@ describe('input-helper tests', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env['GITHUB_SERVER_URL'] = 'https://github.com'
|
process.env['GITHUB_SERVER_URL'] = 'https://github.com'
|
||||||
|
process.env['GITHUB_API_URL'] = 'https://api.github.com'
|
||||||
github.context.eventName = 'workflow_run'
|
github.context.eventName = 'workflow_run'
|
||||||
github.context.payload = {
|
github.context.payload = {
|
||||||
repository: {id: 100, name: 'some-repo', owner: {login: 'some-owner'}},
|
repository: {id: 100, name: 'some-repo', owner: {login: 'some-owner'}},
|
||||||
@@ -216,6 +218,11 @@ describe('input-helper tests', () => {
|
|||||||
} else {
|
} else {
|
||||||
process.env['GITHUB_SERVER_URL'] = originalServerUrl
|
process.env['GITHUB_SERVER_URL'] = originalServerUrl
|
||||||
}
|
}
|
||||||
|
if (originalApiUrl === undefined) {
|
||||||
|
delete process.env['GITHUB_API_URL']
|
||||||
|
} else {
|
||||||
|
process.env['GITHUB_API_URL'] = originalApiUrl
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
@@ -247,9 +254,10 @@ describe('input-helper tests', () => {
|
|||||||
'https://api.github.com'
|
'https://api.github.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) => {
|
async (workflowServer, checkoutServer, apiUrl) => {
|
||||||
process.env['GITHUB_SERVER_URL'] = workflowServer
|
process.env['GITHUB_SERVER_URL'] = workflowServer
|
||||||
|
process.env['GITHUB_API_URL'] = apiUrl
|
||||||
inputs['github-server-url'] = checkoutServer
|
inputs['github-server-url'] = checkoutServer
|
||||||
|
|
||||||
await expect(inputHelper.getInputs()).resolves.toMatchObject({
|
await expect(inputHelper.getInputs()).resolves.toMatchObject({
|
||||||
@@ -261,6 +269,24 @@ describe('input-helper tests', () => {
|
|||||||
expect(mockCompareCommits).toHaveBeenCalledTimes(1)
|
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', () => {
|
describe('unsafe PR checkout guard', () => {
|
||||||
|
|||||||
20
dist/index.js
vendored
20
dist/index.js
vendored
@@ -1807,7 +1807,8 @@ function getInputs() {
|
|||||||
ref: result.ref,
|
ref: result.ref,
|
||||||
commit: result.commit,
|
commit: result.commit,
|
||||||
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
|
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
|
||||||
githubServerUrl: result.githubServerUrl
|
githubServerUrl: result.githubServerUrl,
|
||||||
|
usesSsh: !!result.sshKey
|
||||||
}, result.authToken);
|
}, result.authToken);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -2460,8 +2461,8 @@ function assertSafePrCheckout(input, authToken) {
|
|||||||
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() &&
|
||||||
(0, url_helper_1.getServerUrl)(input.githubServerUrl).origin === (0, url_helper_1.getServerUrl)().origin) {
|
fetchesFromWorkflowServer(input)) {
|
||||||
return commitIsInWorkflowHistory(authToken, commit, input.githubServerUrl).then(isInHistory => {
|
return commitIsInWorkflowHistory(authToken, commit).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.');
|
||||||
@@ -2478,7 +2479,16 @@ function assertSafePrCheckout(input, authToken) {
|
|||||||
`on the actions/checkout step.`);
|
`on the actions/checkout step.`);
|
||||||
}
|
}
|
||||||
exports.assertSafePrCheckout = assertSafePrCheckout;
|
exports.assertSafePrCheckout = assertSafePrCheckout;
|
||||||
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) {
|
||||||
var _a, _b;
|
var _a, _b;
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
const workflowSha = github.context.sha.toLowerCase();
|
const workflowSha = github.context.sha.toLowerCase();
|
||||||
@@ -2491,7 +2501,7 @@ function commitIsInWorkflowHistory(authToken, commit, githubServerUrl) {
|
|||||||
const { owner, repo } = github.context.repo;
|
const { owner, repo } = github.context.repo;
|
||||||
try {
|
try {
|
||||||
const { data } = yield github
|
const { data } = yield github
|
||||||
.getOctokit(authToken, { baseUrl: (0, url_helper_1.getServerApiUrl)(githubServerUrl) })
|
.getOctokit(authToken, { baseUrl: (0, url_helper_1.getServerApiUrl)() })
|
||||||
.rest.repos.compareCommitsWithBasehead({
|
.rest.repos.compareCommitsWithBasehead({
|
||||||
owner,
|
owner,
|
||||||
repo,
|
repo,
|
||||||
|
|||||||
@@ -182,7 +182,8 @@ export async function getInputs(): Promise<IGitSourceSettings> {
|
|||||||
ref: result.ref,
|
ref: result.ref,
|
||||||
commit: result.commit,
|
commit: result.commit,
|
||||||
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
|
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
|
||||||
githubServerUrl: result.githubServerUrl
|
githubServerUrl: result.githubServerUrl,
|
||||||
|
usesSsh: !!result.sshKey
|
||||||
},
|
},
|
||||||
result.authToken
|
result.authToken
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ export interface IUnsafePrCheckoutInput {
|
|||||||
commit: string | undefined
|
commit: string | undefined
|
||||||
allowUnsafePrCheckout: boolean
|
allowUnsafePrCheckout: boolean
|
||||||
githubServerUrl?: string
|
githubServerUrl?: string
|
||||||
|
usesSsh?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assertSafePrCheckout(
|
export function assertSafePrCheckout(
|
||||||
@@ -79,13 +80,9 @@ export function assertSafePrCheckout(
|
|||||||
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
|
fetchesFromWorkflowServer(input)
|
||||||
) {
|
) {
|
||||||
return commitIsInWorkflowHistory(
|
return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => {
|
||||||
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. ' +
|
||||||
@@ -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(
|
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)) {
|
||||||
@@ -125,7 +131,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, {baseUrl: getServerApiUrl(githubServerUrl)})
|
.getOctokit(authToken, {baseUrl: getServerApiUrl()})
|
||||||
.rest.repos.compareCommitsWithBasehead({
|
.rest.repos.compareCommitsWithBasehead({
|
||||||
owner,
|
owner,
|
||||||
repo,
|
repo,
|
||||||
|
|||||||
Reference in New Issue
Block a user