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:30:20 +01:00
parent f38dcf9239
commit 15c98b676d
4 changed files with 59 additions and 16 deletions

View File

@@ -197,6 +197,7 @@ describe('input-helper tests', () => {
describe('workflow checkout server', () => {
const originalServerUrl = process.env['GITHUB_SERVER_URL']
const originalApiUrl = process.env['GITHUB_API_URL']
const originalEventName = github.context.eventName
const originalPayload = github.context.payload
const workflowSha = '1234567890123456789012345678901234567890'
@@ -205,6 +206,7 @@ describe('input-helper tests', () => {
beforeEach(() => {
process.env['GITHUB_SERVER_URL'] = 'https://github.com'
process.env['GITHUB_API_URL'] = 'https://api.github.com'
github.context.eventName = 'workflow_run'
github.context.payload = {
repository: {id: 100, name: 'some-repo', owner: {login: 'some-owner'}},
@@ -237,6 +239,11 @@ describe('input-helper tests', () => {
} else {
process.env['GITHUB_SERVER_URL'] = originalServerUrl
}
if (originalApiUrl === undefined) {
delete process.env['GITHUB_API_URL']
} else {
process.env['GITHUB_API_URL'] = originalApiUrl
}
})
it.each([
@@ -264,9 +271,10 @@ describe('input-helper tests', () => {
],
['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) => {
process.env['GITHUB_SERVER_URL'] = workflowServer
process.env['GITHUB_API_URL'] = apiUrl
inputs['github-server-url'] = checkoutServer
await expect(inputHelper.getInputs()).resolves.toMatchObject({
@@ -278,6 +286,24 @@ describe('input-helper tests', () => {
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', () => {

20
dist/index.js vendored
View File

@@ -2178,7 +2178,8 @@ function getInputs() {
ref: result.ref,
commit: result.commit,
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
githubServerUrl: result.githubServerUrl
githubServerUrl: result.githubServerUrl,
usesSsh: !!result.sshKey
}, result.authToken);
}
return result;
@@ -2869,8 +2870,8 @@ function assertSafePrCheckout(input, authToken) {
commit &&
input.qualifiedRepository.toLowerCase() ===
`${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 => {
fetchesFromWorkflowServer(input)) {
return commitIsInWorkflowHistory(authToken, commit).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.');
@@ -2886,7 +2887,16 @@ 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, 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) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const workflowSha = github.context.sha.toLowerCase();
@@ -2899,7 +2909,7 @@ function commitIsInWorkflowHistory(authToken, commit, githubServerUrl) {
const { owner, repo } = github.context.repo;
try {
const { data } = yield github
.getOctokit(authToken, { baseUrl: (0, url_helper_1.getServerApiUrl)(githubServerUrl) })
.getOctokit(authToken, { baseUrl: (0, url_helper_1.getServerApiUrl)() })
.rest.repos.compareCommitsWithBasehead({
owner,
repo,

View File

@@ -197,7 +197,8 @@ export async function getInputs(): Promise<IGitSourceSettings> {
ref: result.ref,
commit: result.commit,
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
githubServerUrl: result.githubServerUrl
githubServerUrl: result.githubServerUrl,
usesSsh: !!result.sshKey
},
result.authToken
)

View File

@@ -10,6 +10,7 @@ export interface IUnsafePrCheckoutInput {
commit: string | undefined
allowUnsafePrCheckout: boolean
githubServerUrl?: string
usesSsh?: boolean
}
export function assertSafePrCheckout(
@@ -79,13 +80,9 @@ export function assertSafePrCheckout(
commit &&
input.qualifiedRepository.toLowerCase() ===
`${github.context.repo.owner}/${github.context.repo.repo}`.toLowerCase() &&
getServerUrl(input.githubServerUrl).origin === getServerUrl().origin
fetchesFromWorkflowServer(input)
) {
return commitIsInWorkflowHistory(
authToken,
commit,
input.githubServerUrl
).then(isInHistory => {
return commitIsInWorkflowHistory(authToken, commit).then(isInHistory => {
if (!isInHistory) {
throw new Error(
'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(
authToken: string,
commit: string,
githubServerUrl?: string
commit: string
): Promise<boolean> {
const workflowSha = github.context.sha.toLowerCase()
if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) {
@@ -125,7 +131,7 @@ async function commitIsInWorkflowHistory(
const {owner, repo} = github.context.repo
try {
const {data} = await github
.getOctokit(authToken, {baseUrl: getServerApiUrl(githubServerUrl)})
.getOctokit(authToken, {baseUrl: getServerApiUrl()})
.rest.repos.compareCommitsWithBasehead({
owner,
repo,