mirror of
https://github.com/actions/checkout.git
synced 2026-12-04 03:22:56 +08:00
Compare commits
1 Commits
fix-workfl
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b897c0b0f3 |
4
.github/workflows/update-test-ubuntu-git.yml
vendored
4
.github/workflows/update-test-ubuntu-git.yml
vendored
@@ -31,7 +31,7 @@ jobs:
|
|||||||
# Use `docker/login-action` to log in to GHCR.io.
|
# Use `docker/login-action` to log in to GHCR.io.
|
||||||
# Once published, the packages are scoped to the account defined here.
|
# Once published, the packages are scoped to the account defined here.
|
||||||
- name: Log in to the ghcr.io container registry
|
- name: Log in to the ghcr.io container registry
|
||||||
uses: docker/login-action@v4.4.0
|
uses: docker/login-action@v4.6.0
|
||||||
with:
|
with:
|
||||||
registry: ${{ env.REGISTRY }}
|
registry: ${{ env.REGISTRY }}
|
||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
@@ -48,7 +48,7 @@ jobs:
|
|||||||
|
|
||||||
# Use `docker/build-push-action` to build (and optionally publish) the image.
|
# Use `docker/build-push-action` to build (and optionally publish) the image.
|
||||||
- name: Build Docker Image (with optional Push)
|
- name: Build Docker Image (with optional Push)
|
||||||
uses: docker/build-push-action@v7.3.0
|
uses: docker/build-push-action@v7.4.0
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: images/test-ubuntu-git.Dockerfile
|
file: images/test-ubuntu-git.Dockerfile
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
## What's new
|
## What's new
|
||||||
|
|
||||||
- Safer fork pull request handling: checkout now refuses to check out fork pull request code by default when the workflow is triggered by `pull_request_target` or `workflow_run`. These triggers run with the base repository's `GITHUB_TOKEN`, secrets, and runner access, where executing a fork's code commonly leads to "pwn request" vulnerabilities.
|
- Safer fork pull request handling: checkout now refuses to check out fork pull request code by default when the workflow is triggered by `pull_request_target` or `workflow_run`. These triggers run with the base repository's `GITHUB_TOKEN`, secrets, and runner access, where executing a fork's code commonly leads to "pwn request" vulnerabilities.
|
||||||
- When a fork pull request triggers a `workflow_run`, checkout only accepts explicit commit SHAs in the workflow commit's history for the same repository by default. Pins other than the workflow commit need a GitHub API comparison with `contents: read` permission. If the comparison cannot verify the pin, use a trusted branch ref instead.
|
|
||||||
- To opt in after [reviewing the risks](https://gh.io/securely-using-pull_request_target), set the new `allow-unsafe-pr-checkout: true` input.
|
- To opt in after [reviewing the risks](https://gh.io/securely-using-pull_request_target), set the new `allow-unsafe-pr-checkout: true` input.
|
||||||
- Migrated `actions/checkout` to ESM to support new versions of the `@actions/*` packages.
|
- Migrated `actions/checkout` to ESM to support new versions of the `@actions/*` packages.
|
||||||
- Updated direct and transitive dependencies, including security fixes for known vulnerabilities.
|
- Updated direct and transitive dependencies, including security fixes for known vulnerabilities.
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import {
|
|||||||
expect,
|
expect,
|
||||||
beforeAll,
|
beforeAll,
|
||||||
beforeEach,
|
beforeEach,
|
||||||
afterEach,
|
|
||||||
afterAll
|
afterAll
|
||||||
} from '@jest/globals'
|
} from '@jest/globals'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
@@ -53,12 +52,9 @@ jest.unstable_mockModule('@actions/core', () => ({
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
// Mock @actions/github before loading input-helper
|
// Mock @actions/github before loading input-helper
|
||||||
const mockCompareCommits = jest.fn<() => Promise<unknown>>()
|
|
||||||
jest.unstable_mockModule('@actions/github', () => ({
|
jest.unstable_mockModule('@actions/github', () => ({
|
||||||
context: mockGithubContext,
|
context: mockGithubContext,
|
||||||
getOctokit: jest.fn(() => ({
|
getOctokit: jest.fn()
|
||||||
rest: {repos: {compareCommitsWithBasehead: mockCompareCommits}}
|
|
||||||
}))
|
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Mock fs-helper
|
// Mock fs-helper
|
||||||
@@ -76,7 +72,6 @@ jest.unstable_mockModule('../src/workflow-context-helper.js', () => ({
|
|||||||
|
|
||||||
// Dynamic imports after mocking
|
// Dynamic imports after mocking
|
||||||
const core = await import('@actions/core')
|
const core = await import('@actions/core')
|
||||||
const github = await import('@actions/github')
|
|
||||||
const inputHelper = await import('../src/input-helper.js')
|
const inputHelper = await import('../src/input-helper.js')
|
||||||
type IGitSourceSettings =
|
type IGitSourceSettings =
|
||||||
import('../src/git-source-settings.js').IGitSourceSettings
|
import('../src/git-source-settings.js').IGitSourceSettings
|
||||||
@@ -270,176 +265,4 @@ describe('input-helper tests', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('explicit checkout pins on a fork PR workflow_run', () => {
|
|
||||||
const originalServerUrl = process.env['GITHUB_SERVER_URL']
|
|
||||||
const originalApiUrl = process.env['GITHUB_API_URL']
|
|
||||||
const workflowSha = '1234567890123456789012345678901234567890'
|
|
||||||
const ancestorSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
|
|
||||||
const mergeSha = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env['GITHUB_SERVER_URL'] = 'https://github.com'
|
|
||||||
process.env['GITHUB_API_URL'] = 'https://api.github.com'
|
|
||||||
inputs.token = 'checkout-token'
|
|
||||||
mockGithubContext.eventName = 'workflow_run'
|
|
||||||
mockGithubContext.payload = {
|
|
||||||
repository: {id: 100},
|
|
||||||
workflow_run: {
|
|
||||||
event: 'pull_request',
|
|
||||||
head_repository: {id: 200, full_name: 'fork-owner/some-repo'},
|
|
||||||
head_sha: 'cccccccccccccccccccccccccccccccccccccccc'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mockCompareCommits.mockReset()
|
|
||||||
mockCompareCommits.mockRejectedValue(new Error('API unavailable'))
|
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(() => {
|
|
||||||
mockGithubContext.eventName = ''
|
|
||||||
mockGithubContext.payload = {}
|
|
||||||
if (originalServerUrl === undefined) {
|
|
||||||
delete process.env['GITHUB_SERVER_URL']
|
|
||||||
} 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([
|
|
||||||
'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 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
|
|
||||||
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('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([
|
|
||||||
['a PR merge commit outside workflow history', workflowSha, ancestorSha],
|
|
||||||
['a comparison against an unexpected base', ancestorSha, mergeSha]
|
|
||||||
])('refuses %s without PR metadata', async (_, base, mergeBase) => {
|
|
||||||
inputs.ref = mergeSha
|
|
||||||
mockCompareCommits.mockResolvedValue({
|
|
||||||
data: {
|
|
||||||
base_commit: {sha: base},
|
|
||||||
merge_base_commit: {sha: mergeBase}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
await expect(inputHelper.getInputs()).rejects.toThrow(
|
|
||||||
/Refusing to check out the requested commit/
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('allows a pin verified to be in workflow history', async () => {
|
|
||||||
inputs.ref = ancestorSha.toUpperCase()
|
|
||||||
mockCompareCommits.mockResolvedValue({
|
|
||||||
data: {
|
|
||||||
base_commit: {sha: workflowSha},
|
|
||||||
merge_base_commit: {sha: ancestorSha}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
await expect(inputHelper.getInputs()).resolves.toMatchObject({
|
|
||||||
ref: '',
|
|
||||||
commit: inputs.ref
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('refuses a pin if the comparison is unavailable', async () => {
|
|
||||||
inputs.ref = mergeSha
|
|
||||||
|
|
||||||
await expect(inputHelper.getInputs()).rejects.toThrow(
|
|
||||||
/Unable to verify the requested commit/
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('allows a pin when the workflow explicitly opts out', async () => {
|
|
||||||
inputs.ref = mergeSha
|
|
||||||
inputs['allow-unsafe-pr-checkout'] = 'true'
|
|
||||||
|
|
||||||
await expect(inputHelper.getInputs()).resolves.toMatchObject({
|
|
||||||
ref: '',
|
|
||||||
commit: mergeSha
|
|
||||||
})
|
|
||||||
expect(mockCompareCommits).not.toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
|
|
||||||
it.each([
|
|
||||||
['default checkout', '', 'refs/heads/some-ref', workflowSha],
|
|
||||||
['named branch', 'refs/heads/main', 'refs/heads/main', undefined],
|
|
||||||
['workflow commit', workflowSha, '', workflowSha]
|
|
||||||
])('allows %s without a comparison', async (_, inputRef, ref, commit) => {
|
|
||||||
inputs.ref = inputRef
|
|
||||||
|
|
||||||
const settings = await inputHelper.getInputs()
|
|
||||||
expect(settings.ref).toBe(ref)
|
|
||||||
expect(settings.commit).toBe(commit)
|
|
||||||
expect(mockCompareCommits).not.toHaveBeenCalled()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -34,12 +34,6 @@ jest.unstable_mockModule('@actions/github', () => ({
|
|||||||
const {assertSafePrCheckout} =
|
const {assertSafePrCheckout} =
|
||||||
await import('../src/unsafe-pr-checkout-helper.js')
|
await import('../src/unsafe-pr-checkout-helper.js')
|
||||||
|
|
||||||
function check(
|
|
||||||
input: Parameters<typeof assertSafePrCheckout>[0]
|
|
||||||
): Promise<void> {
|
|
||||||
return assertSafePrCheckout(input, 'checkout-token')
|
|
||||||
}
|
|
||||||
|
|
||||||
const originalEventName = mockContext.eventName
|
const originalEventName = mockContext.eventName
|
||||||
const originalPayload = mockContext.payload
|
const originalPayload = mockContext.payload
|
||||||
|
|
||||||
@@ -100,191 +94,191 @@ describe('unsafe-pr-checkout-helper', () => {
|
|||||||
mockContext.payload = originalPayload
|
mockContext.payload = originalPayload
|
||||||
})
|
})
|
||||||
|
|
||||||
it('allows pull_request events untouched', async () => {
|
it('allows pull_request events untouched', () => {
|
||||||
setContext('pull_request', forkPullRequestTargetPayload())
|
setContext('pull_request', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: 'attacker/fork',
|
qualifiedRepository: 'attacker/fork',
|
||||||
ref: 'refs/pull/1/merge',
|
ref: 'refs/pull/1/merge',
|
||||||
commit: '',
|
commit: '',
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).resolves.toBeUndefined()
|
).not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('allows pull_request_target default checkout (base branch)', async () => {
|
it('allows pull_request_target default checkout (base branch)', () => {
|
||||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: 'refs/heads/main',
|
ref: 'refs/heads/main',
|
||||||
commit: SAFE_BASE_SHA,
|
commit: SAFE_BASE_SHA,
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).resolves.toBeUndefined()
|
).not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('allows same-repo pull_request_target checkout of PR head', async () => {
|
it('allows same-repo pull_request_target checkout of PR head', () => {
|
||||||
setContext('pull_request_target', sameRepoPullRequestTargetPayload())
|
setContext('pull_request_target', sameRepoPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: '',
|
ref: '',
|
||||||
commit: PR_HEAD_SHA,
|
commit: PR_HEAD_SHA,
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).resolves.toBeUndefined()
|
).not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('refuses pull_request_target fork PR head SHA checkout', async () => {
|
it('refuses pull_request_target fork PR head SHA checkout', () => {
|
||||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: '',
|
ref: '',
|
||||||
commit: PR_HEAD_SHA,
|
commit: PR_HEAD_SHA,
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).rejects.toThrow(/Refusing to check out fork pull request code/)
|
).toThrow(/Refusing to check out fork pull request code/)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('refuses pull_request_target fork PR merge_commit_sha checkout', async () => {
|
it('refuses pull_request_target fork PR merge_commit_sha checkout', () => {
|
||||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: '',
|
ref: '',
|
||||||
commit: PR_MERGE_SHA,
|
commit: PR_MERGE_SHA,
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).rejects.toThrow(/allow-unsafe-pr-checkout/)
|
).toThrow(/allow-unsafe-pr-checkout/)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('refuses pull_request_target fork PR ref pattern (head)', async () => {
|
it('refuses pull_request_target fork PR ref pattern (head)', () => {
|
||||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: 'refs/pull/42/head',
|
ref: 'refs/pull/42/head',
|
||||||
commit: '',
|
commit: '',
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).rejects.toThrow()
|
).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('refuses pull_request_target fork PR ref pattern (merge)', async () => {
|
it('refuses pull_request_target fork PR ref pattern (merge)', () => {
|
||||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: 'refs/pull/42/merge',
|
ref: 'refs/pull/42/merge',
|
||||||
commit: '',
|
commit: '',
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).rejects.toThrow()
|
).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('refuses pull_request_target when repository points at the fork', async () => {
|
it('refuses pull_request_target when repository points at the fork', () => {
|
||||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: FORK_QUALIFIED_REPO,
|
qualifiedRepository: FORK_QUALIFIED_REPO,
|
||||||
ref: 'refs/heads/main',
|
ref: 'refs/heads/main',
|
||||||
commit: '',
|
commit: '',
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).rejects.toThrow()
|
).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('allows pull_request_target checkout of an unrelated third-party repo', async () => {
|
it('allows pull_request_target checkout of an unrelated third-party repo', () => {
|
||||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: 'some-other/unrelated',
|
qualifiedRepository: 'some-other/unrelated',
|
||||||
ref: 'refs/heads/main',
|
ref: 'refs/heads/main',
|
||||||
commit: '',
|
commit: '',
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).resolves.toBeUndefined()
|
).not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('refuses pull_request_target ignoring repository case differences', async () => {
|
it('refuses pull_request_target ignoring repository case differences', () => {
|
||||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: FORK_QUALIFIED_REPO.toUpperCase(),
|
qualifiedRepository: FORK_QUALIFIED_REPO.toUpperCase(),
|
||||||
ref: '',
|
ref: '',
|
||||||
commit: '',
|
commit: '',
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).rejects.toThrow()
|
).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('refuses pull_request_target ignoring commit SHA case differences', async () => {
|
it('refuses pull_request_target ignoring commit SHA case differences', () => {
|
||||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: '',
|
ref: '',
|
||||||
commit: PR_HEAD_SHA.toUpperCase(),
|
commit: PR_HEAD_SHA.toUpperCase(),
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).rejects.toThrow()
|
).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('allows pull_request_target fork PR checkout when opted in', async () => {
|
it('allows pull_request_target fork PR checkout when opted in', () => {
|
||||||
setContext('pull_request_target', forkPullRequestTargetPayload())
|
setContext('pull_request_target', forkPullRequestTargetPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: 'refs/pull/42/merge',
|
ref: 'refs/pull/42/merge',
|
||||||
commit: '',
|
commit: '',
|
||||||
allowUnsafePrCheckout: true
|
allowUnsafePrCheckout: true
|
||||||
})
|
})
|
||||||
).resolves.toBeUndefined()
|
).not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('refuses workflow_run fork PR head_commit.id checkout', async () => {
|
it('refuses workflow_run fork PR head_commit.id checkout', () => {
|
||||||
setContext('workflow_run', forkWorkflowRunPayload())
|
setContext('workflow_run', forkWorkflowRunPayload())
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: '',
|
ref: '',
|
||||||
commit: WORKFLOW_RUN_HEAD_COMMIT_SHA,
|
commit: WORKFLOW_RUN_HEAD_COMMIT_SHA,
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).rejects.toThrow(/Refusing to check out fork pull request code/)
|
).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('refuses workflow_run with pull_request_target underlying event', async () => {
|
it('refuses workflow_run with pull_request_target underlying event', () => {
|
||||||
const payload = forkWorkflowRunPayload() as {
|
const payload = forkWorkflowRunPayload() as {
|
||||||
workflow_run: {event: string}
|
workflow_run: {event: string}
|
||||||
}
|
}
|
||||||
payload.workflow_run.event = 'pull_request_target'
|
payload.workflow_run.event = 'pull_request_target'
|
||||||
setContext('workflow_run', payload)
|
setContext('workflow_run', payload)
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: '',
|
ref: '',
|
||||||
commit: WORKFLOW_RUN_HEAD_COMMIT_SHA,
|
commit: WORKFLOW_RUN_HEAD_COMMIT_SHA,
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).rejects.toThrow(/Refusing to check out fork pull request code/)
|
).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('allows workflow_run same-repo PR (head_repository.id matches base)', async () => {
|
it('allows workflow_run same-repo PR (head_repository.id matches base)', () => {
|
||||||
const payload = forkWorkflowRunPayload() as {
|
const payload = forkWorkflowRunPayload() as {
|
||||||
workflow_run: {head_repository: {id: number}}
|
workflow_run: {head_repository: {id: number}}
|
||||||
}
|
}
|
||||||
payload.workflow_run.head_repository.id = BASE_REPO_ID
|
payload.workflow_run.head_repository.id = BASE_REPO_ID
|
||||||
setContext('workflow_run', payload)
|
setContext('workflow_run', payload)
|
||||||
await expect(
|
expect(() =>
|
||||||
check({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository: BASE_QUALIFIED_REPO,
|
qualifiedRepository: BASE_QUALIFIED_REPO,
|
||||||
ref: '',
|
ref: '',
|
||||||
commit: WORKFLOW_RUN_HEAD_COMMIT_SHA,
|
commit: WORKFLOW_RUN_HEAD_COMMIT_SHA,
|
||||||
allowUnsafePrCheckout: false
|
allowUnsafePrCheckout: false
|
||||||
})
|
})
|
||||||
).resolves.toBeUndefined()
|
).not.toThrow()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
58
dist/index.js
vendored
58
dist/index.js
vendored
@@ -41969,9 +41969,8 @@ async function getGitCommandManager(settings) {
|
|||||||
;// CONCATENATED MODULE: ./src/unsafe-pr-checkout-helper.ts
|
;// CONCATENATED MODULE: ./src/unsafe-pr-checkout-helper.ts
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/;
|
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/;
|
||||||
async function assertSafePrCheckout(input, authToken) {
|
function assertSafePrCheckout(input) {
|
||||||
if (input.allowUnsafePrCheckout) {
|
if (input.allowUnsafePrCheckout) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -42015,21 +42014,10 @@ async function assertSafePrCheckout(input, authToken) {
|
|||||||
const repositoryMatchesPrHead = typeof prHeadRepoFullName === 'string' &&
|
const repositoryMatchesPrHead = typeof prHeadRepoFullName === 'string' &&
|
||||||
input.qualifiedRepository.toLowerCase() === prHeadRepoFullName.toLowerCase();
|
input.qualifiedRepository.toLowerCase() === prHeadRepoFullName.toLowerCase();
|
||||||
const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref);
|
const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref);
|
||||||
const commit = input.commit?.toLowerCase();
|
const commitMatchesPrHeadSha = !!input.commit && prShas.includes(input.commit.toLowerCase());
|
||||||
const commitMatchesPrHeadSha = !!commit && prShas.includes(commit);
|
|
||||||
if (!repositoryMatchesPrHead &&
|
if (!repositoryMatchesPrHead &&
|
||||||
!refMatchesPullPattern &&
|
!refMatchesPullPattern &&
|
||||||
!commitMatchesPrHeadSha) {
|
!commitMatchesPrHeadSha) {
|
||||||
if (eventName === 'workflow_run' && !input.ref && commit) {
|
|
||||||
const { owner, repo } = github_context.repo;
|
|
||||||
if (input.qualifiedRepository.toLowerCase() ===
|
|
||||||
`${owner}/${repo}`.toLowerCase() &&
|
|
||||||
fetchesFromWorkflowServer(input) &&
|
|
||||||
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit))) {
|
|
||||||
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.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new Error(`Refusing to check out fork pull request code from a '${eventName}' workflow. ` +
|
throw new Error(`Refusing to check out fork pull request code from a '${eventName}' workflow. ` +
|
||||||
@@ -42039,40 +42027,6 @@ 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.`);
|
||||||
}
|
}
|
||||||
// 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() || '';
|
|
||||||
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.');
|
|
||||||
}
|
|
||||||
if (commit === workflowSha) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
// Compare immutable commits so the result does not depend on current PR metadata.
|
|
||||||
const { data } = await getOctokit(authToken, { baseUrl: getServerApiUrl() })
|
|
||||||
.rest.repos.compareCommitsWithBasehead({
|
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
basehead: `${workflowSha}...${commit}`,
|
|
||||||
per_page: 1
|
|
||||||
});
|
|
||||||
return (data?.base_commit?.sha === workflowSha &&
|
|
||||||
data?.merge_base_commit?.sha === commit);
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
throw new Error('Unable to verify the requested commit against the workflow commit history. ' +
|
|
||||||
'Check that the checkout token has contents: read, or use a trusted branch ref.', { cause: error });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function pushIfSha(target, value) {
|
function pushIfSha(target, value) {
|
||||||
if (typeof value === 'string' && value.length > 0) {
|
if (typeof value === 'string' && value.length > 0) {
|
||||||
target.push(value.toLowerCase());
|
target.push(value.toLowerCase());
|
||||||
@@ -42259,14 +42213,12 @@ async function getInputs() {
|
|||||||
// repository or ref.
|
// repository or ref.
|
||||||
const isDefaultCheckout = isWorkflowRepository && !getInput('ref');
|
const isDefaultCheckout = isWorkflowRepository && !getInput('ref');
|
||||||
if (!isDefaultCheckout) {
|
if (!isDefaultCheckout) {
|
||||||
await assertSafePrCheckout({
|
assertSafePrCheckout({
|
||||||
qualifiedRepository,
|
qualifiedRepository,
|
||||||
ref: result.ref,
|
ref: result.ref,
|
||||||
commit: result.commit,
|
commit: result.commit,
|
||||||
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
|
allowUnsafePrCheckout: result.allowUnsafePrCheckout
|
||||||
githubServerUrl: result.githubServerUrl,
|
});
|
||||||
usesSsh: !!result.sshKey
|
|
||||||
}, result.authToken);
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,17 +191,12 @@ export async function getInputs(): Promise<IGitSourceSettings> {
|
|||||||
// repository or ref.
|
// repository or ref.
|
||||||
const isDefaultCheckout = isWorkflowRepository && !core.getInput('ref')
|
const isDefaultCheckout = isWorkflowRepository && !core.getInput('ref')
|
||||||
if (!isDefaultCheckout) {
|
if (!isDefaultCheckout) {
|
||||||
await unsafePrCheckoutHelper.assertSafePrCheckout(
|
unsafePrCheckoutHelper.assertSafePrCheckout({
|
||||||
{
|
qualifiedRepository,
|
||||||
qualifiedRepository,
|
ref: result.ref,
|
||||||
ref: result.ref,
|
commit: result.commit,
|
||||||
commit: result.commit,
|
allowUnsafePrCheckout: result.allowUnsafePrCheckout
|
||||||
allowUnsafePrCheckout: result.allowUnsafePrCheckout,
|
})
|
||||||
githubServerUrl: result.githubServerUrl,
|
|
||||||
usesSsh: !!result.sshKey
|
|
||||||
},
|
|
||||||
result.authToken
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import * as github from '@actions/github'
|
import * as github from '@actions/github'
|
||||||
import {fromPayload} from './ref-helper.js'
|
import {fromPayload} from './ref-helper.js'
|
||||||
import {getServerApiUrl, getServerUrl} from './url-helper.js'
|
|
||||||
|
|
||||||
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/
|
const PR_REF_PATTERN = /^refs\/pull\/[0-9]+\/(?:head|merge)$/
|
||||||
|
|
||||||
@@ -9,14 +8,9 @@ export interface IUnsafePrCheckoutInput {
|
|||||||
ref: string
|
ref: string
|
||||||
commit: string | undefined
|
commit: string | undefined
|
||||||
allowUnsafePrCheckout: boolean
|
allowUnsafePrCheckout: boolean
|
||||||
githubServerUrl?: string
|
|
||||||
usesSsh?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function assertSafePrCheckout(
|
export function assertSafePrCheckout(input: IUnsafePrCheckoutInput): void {
|
||||||
input: IUnsafePrCheckoutInput,
|
|
||||||
authToken: string
|
|
||||||
): Promise<void> {
|
|
||||||
if (input.allowUnsafePrCheckout) {
|
if (input.allowUnsafePrCheckout) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -66,28 +60,14 @@ export async function assertSafePrCheckout(
|
|||||||
typeof prHeadRepoFullName === 'string' &&
|
typeof prHeadRepoFullName === 'string' &&
|
||||||
input.qualifiedRepository.toLowerCase() === prHeadRepoFullName.toLowerCase()
|
input.qualifiedRepository.toLowerCase() === prHeadRepoFullName.toLowerCase()
|
||||||
const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref)
|
const refMatchesPullPattern = PR_REF_PATTERN.test(input.ref)
|
||||||
const commit = input.commit?.toLowerCase()
|
const commitMatchesPrHeadSha =
|
||||||
const commitMatchesPrHeadSha = !!commit && prShas.includes(commit)
|
!!input.commit && prShas.includes(input.commit.toLowerCase())
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!repositoryMatchesPrHead &&
|
!repositoryMatchesPrHead &&
|
||||||
!refMatchesPullPattern &&
|
!refMatchesPullPattern &&
|
||||||
!commitMatchesPrHeadSha
|
!commitMatchesPrHeadSha
|
||||||
) {
|
) {
|
||||||
if (eventName === 'workflow_run' && !input.ref && commit) {
|
|
||||||
const {owner, repo} = github.context.repo
|
|
||||||
if (
|
|
||||||
input.qualifiedRepository.toLowerCase() ===
|
|
||||||
`${owner}/${repo}`.toLowerCase() &&
|
|
||||||
fetchesFromWorkflowServer(input) &&
|
|
||||||
!(await commitIsInWorkflowHistory(authToken, owner, repo, commit))
|
|
||||||
) {
|
|
||||||
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.'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,55 +81,6 @@ 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(
|
|
||||||
authToken: string,
|
|
||||||
owner: string,
|
|
||||||
repo: string,
|
|
||||||
commit: string
|
|
||||||
): Promise<boolean> {
|
|
||||||
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.'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (commit === workflowSha) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Compare immutable commits so the result does not depend on current PR metadata.
|
|
||||||
const {data} = await github
|
|
||||||
.getOctokit(authToken, {baseUrl: getServerApiUrl()})
|
|
||||||
.rest.repos.compareCommitsWithBasehead({
|
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
basehead: `${workflowSha}...${commit}`,
|
|
||||||
per_page: 1
|
|
||||||
})
|
|
||||||
return (
|
|
||||||
data?.base_commit?.sha === workflowSha &&
|
|
||||||
data?.merge_base_commit?.sha === commit
|
|
||||||
)
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(
|
|
||||||
'Unable to verify the requested commit against the workflow commit history. ' +
|
|
||||||
'Check that the checkout token has contents: read, or use a trusted branch ref.',
|
|
||||||
{cause: error}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function pushIfSha(target: string[], value: unknown): void {
|
function pushIfSha(target: string[], value: unknown): void {
|
||||||
if (typeof value === 'string' && value.length > 0) {
|
if (typeof value === 'string' && value.length > 0) {
|
||||||
target.push(value.toLowerCase())
|
target.push(value.toLowerCase())
|
||||||
|
|||||||
Reference in New Issue
Block a user