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:
Francesco Renzi
2026-12-03 18:28:21 +01:00
co-authored by Copilot App
parent 8b33a4c30e
commit 152ac2d544
4 changed files with 60 additions and 18 deletions
+28 -1
View File
@@ -273,12 +273,14 @@ describe('input-helper tests', () => {
describe('explicit checkout pins on a fork PR workflow_run', () => { describe('explicit checkout pins on a fork PR workflow_run', () => {
const originalServerUrl = process.env['GITHUB_SERVER_URL'] const originalServerUrl = process.env['GITHUB_SERVER_URL']
const originalApiUrl = process.env['GITHUB_API_URL']
const workflowSha = '1234567890123456789012345678901234567890' const workflowSha = '1234567890123456789012345678901234567890'
const ancestorSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' const ancestorSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
const mergeSha = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' const mergeSha = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
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'
inputs.token = 'checkout-token' inputs.token = 'checkout-token'
mockGithubContext.eventName = 'workflow_run' mockGithubContext.eventName = 'workflow_run'
mockGithubContext.payload = { mockGithubContext.payload = {
@@ -301,6 +303,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([
@@ -329,9 +336,10 @@ describe('input-helper tests', () => {
], ],
['https://org.ghe.com', 'https://org.ghe.com/', 'https://api.org.ghe.com'] ['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) => { 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
inputs.ref = ancestorSha inputs.ref = ancestorSha
mockCompareCommits.mockResolvedValue({ mockCompareCommits.mockResolvedValue({
@@ -351,6 +359,25 @@ describe('input-helper tests', () => {
} }
) )
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'
inputs.ref = mergeSha
mockCompareCommits.mockResolvedValue({
data: {
base_commit: {sha: workflowSha},
merge_base_commit: {sha: ancestorSha}
}
})
await expect(inputHelper.getInputs()).rejects.toThrow(
/Refusing to check out the requested commit/
)
expect(github.getOctokit).toHaveBeenCalledWith('checkout-token', {
baseUrl: 'https://api.github.com'
})
})
it.each([ it.each([
['a PR merge commit outside workflow history', workflowSha, ancestorSha], ['a PR merge commit outside workflow history', workflowSha, ancestorSha],
['a comparison against an unexpected base', ancestorSha, mergeSha] ['a comparison against an unexpected base', ancestorSha, mergeSha]
+15 -5
View File
@@ -42024,8 +42024,8 @@ async function assertSafePrCheckout(input, authToken) {
const { owner, repo } = github_context.repo; const { owner, repo } = github_context.repo;
if (input.qualifiedRepository.toLowerCase() === if (input.qualifiedRepository.toLowerCase() ===
`${owner}/${repo}`.toLowerCase() && `${owner}/${repo}`.toLowerCase() &&
getServerUrl(input.githubServerUrl).origin === getServerUrl().origin && fetchesFromWorkflowServer(input) &&
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit, input.githubServerUrl))) { !(await commitIsInWorkflowHistory(authToken, owner, repo, commit))) {
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.');
} }
@@ -42039,7 +42039,16 @@ async 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.`);
} }
async function commitIsInWorkflowHistory(authToken, owner, repo, 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 = getServerUrl(input.githubServerUrl);
const workflowServer = getServerUrl();
return input.usesSsh
? checkoutServer.hostname === workflowServer.hostname
: checkoutServer.origin === workflowServer.origin;
}
async function commitIsInWorkflowHistory(authToken, owner, repo, commit) {
const workflowSha = github_context.sha?.toLowerCase() || ''; const workflowSha = github_context.sha?.toLowerCase() || '';
if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) { if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) {
throw new Error('Unable to verify the requested commit: the workflow commit SHA is missing or invalid.'); throw new Error('Unable to verify the requested commit: the workflow commit SHA is missing or invalid.');
@@ -42049,7 +42058,7 @@ async function commitIsInWorkflowHistory(authToken, owner, repo, commit, githubS
} }
try { try {
// Compare immutable commits so the result does not depend on current PR metadata. // Compare immutable commits so the result does not depend on current PR metadata.
const { data } = await getOctokit(authToken, { baseUrl: getServerApiUrl(githubServerUrl) }) const { data } = await getOctokit(authToken, { baseUrl: getServerApiUrl() })
.rest.repos.compareCommitsWithBasehead({ .rest.repos.compareCommitsWithBasehead({
owner, owner,
repo, repo,
@@ -42255,7 +42264,8 @@ async 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;
+2 -1
View File
@@ -197,7 +197,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
) )
+15 -11
View File
@@ -10,6 +10,7 @@ export interface IUnsafePrCheckoutInput {
commit: string | undefined commit: string | undefined
allowUnsafePrCheckout: boolean allowUnsafePrCheckout: boolean
githubServerUrl?: string githubServerUrl?: string
usesSsh?: boolean
} }
export async function assertSafePrCheckout( export async function assertSafePrCheckout(
@@ -78,14 +79,8 @@ export async function assertSafePrCheckout(
if ( if (
input.qualifiedRepository.toLowerCase() === input.qualifiedRepository.toLowerCase() ===
`${owner}/${repo}`.toLowerCase() && `${owner}/${repo}`.toLowerCase() &&
getServerUrl(input.githubServerUrl).origin === getServerUrl().origin && fetchesFromWorkflowServer(input) &&
!(await commitIsInWorkflowHistory( !(await commitIsInWorkflowHistory(authToken, owner, repo, commit))
authToken,
owner,
repo,
commit,
input.githubServerUrl
))
) { ) {
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. ' +
@@ -106,12 +101,21 @@ export async 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,
owner: string, owner: string,
repo: string, repo: 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}|[0-9a-f]{64})$/.test(workflowSha)) { if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) {
@@ -126,7 +130,7 @@ async function commitIsInWorkflowHistory(
try { try {
// Compare immutable commits so the result does not depend on current PR metadata. // Compare immutable commits so the result does not depend on current PR metadata.
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,