Scope workflow history verification to the checkout server

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Francesco Renzi
2026-12-03 18:09:48 +01:00
co-authored by Copilot App
parent a4ddf0647e
commit 8b33a4c30e
4 changed files with 78 additions and 8 deletions
+56
View File
@@ -76,6 +76,7 @@ jest.unstable_mockModule('../src/workflow-context-helper.js', () => ({
// Dynamic imports after mocking
const core = await import('@actions/core')
const github = await import('@actions/github')
const inputHelper = await import('../src/input-helper.js')
type IGitSourceSettings =
import('../src/git-source-settings.js').IGitSourceSettings
@@ -271,11 +272,13 @@ describe('input-helper tests', () => {
})
describe('explicit checkout pins on a fork PR workflow_run', () => {
const originalServerUrl = process.env['GITHUB_SERVER_URL']
const workflowSha = '1234567890123456789012345678901234567890'
const ancestorSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
const mergeSha = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
beforeEach(() => {
process.env['GITHUB_SERVER_URL'] = 'https://github.com'
inputs.token = 'checkout-token'
mockGithubContext.eventName = 'workflow_run'
mockGithubContext.payload = {
@@ -293,8 +296,61 @@ describe('input-helper tests', () => {
afterEach(() => {
mockGithubContext.eventName = ''
mockGithubContext.payload = {}
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.ref = mergeSha
inputs['github-server-url'] = serverUrl
await expect(inputHelper.getInputs()).resolves.toMatchObject({
commit: mergeSha
})
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
inputs.ref = ancestorSha
mockCompareCommits.mockResolvedValue({
data: {
base_commit: {sha: workflowSha},
merge_base_commit: {sha: ancestorSha}
}
})
await expect(inputHelper.getInputs()).resolves.toMatchObject({
commit: ancestorSha
})
expect(github.getOctokit).toHaveBeenCalledWith('checkout-token', {
baseUrl: apiUrl
})
expect(mockCompareCommits).toHaveBeenCalledTimes(1)
}
)
it.each([
['a PR merge commit outside workflow history', workflowSha, ancestorSha],
['a comparison against an unexpected base', ancestorSha, mergeSha]
+7 -4
View File
@@ -41969,6 +41969,7 @@ async function getGitCommandManager(settings) {
;// CONCATENATED MODULE: ./src/unsafe-pr-checkout-helper.ts
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/;
async function assertSafePrCheckout(input, authToken) {
if (input.allowUnsafePrCheckout) {
@@ -42023,7 +42024,8 @@ async function assertSafePrCheckout(input, authToken) {
const { owner, repo } = github_context.repo;
if (input.qualifiedRepository.toLowerCase() ===
`${owner}/${repo}`.toLowerCase() &&
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit))) {
getServerUrl(input.githubServerUrl).origin === getServerUrl().origin &&
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit, input.githubServerUrl))) {
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.');
}
@@ -42037,7 +42039,7 @@ async 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.`);
}
async function commitIsInWorkflowHistory(authToken, owner, repo, commit) {
async function commitIsInWorkflowHistory(authToken, owner, repo, commit, githubServerUrl) {
const workflowSha = github_context.sha?.toLowerCase() || '';
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.');
@@ -42047,7 +42049,7 @@ async function commitIsInWorkflowHistory(authToken, owner, repo, commit) {
}
try {
// Compare immutable commits so the result does not depend on current PR metadata.
const { data } = await getOctokit(authToken)
const { data } = await getOctokit(authToken, { baseUrl: getServerApiUrl(githubServerUrl) })
.rest.repos.compareCommitsWithBasehead({
owner,
repo,
@@ -42252,7 +42254,8 @@ async function getInputs() {
qualifiedRepository,
ref: result.ref,
commit: result.commit,
allowUnsafePrCheckout: result.allowUnsafePrCheckout
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
githubServerUrl: result.githubServerUrl
}, result.authToken);
}
return result;
+2 -1
View File
@@ -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
)
+13 -3
View File
@@ -1,5 +1,6 @@
import * as github from '@actions/github'
import {fromPayload} from './ref-helper.js'
import {getServerApiUrl, getServerUrl} from './url-helper.js'
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 async function assertSafePrCheckout(
@@ -76,7 +78,14 @@ export async function assertSafePrCheckout(
if (
input.qualifiedRepository.toLowerCase() ===
`${owner}/${repo}`.toLowerCase() &&
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit))
getServerUrl(input.githubServerUrl).origin === getServerUrl().origin &&
!(await commitIsInWorkflowHistory(
authToken,
owner,
repo,
commit,
input.githubServerUrl
))
) {
throw new Error(
'Refusing to check out the requested commit for this fork pull request. ' +
@@ -101,7 +110,8 @@ async function commitIsInWorkflowHistory(
authToken: string,
owner: string,
repo: string,
commit: string
commit: string,
githubServerUrl?: string
): Promise<boolean> {
const workflowSha = github.context.sha?.toLowerCase() || ''
if (!/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(workflowSha)) {
@@ -116,7 +126,7 @@ async function commitIsInWorkflowHistory(
try {
// Compare immutable commits so the result does not depend on current PR metadata.
const {data} = await github
.getOctokit(authToken)
.getOctokit(authToken, {baseUrl: getServerApiUrl(githubServerUrl)})
.rest.repos.compareCommitsWithBasehead({
owner,
repo,