Skip to content

Commit 62661c4

Browse files
authored
skip running unsafe pr check if input is default (#2518)
1 parent e8d4307 commit 62661c4

3 files changed

Lines changed: 72 additions & 12 deletions

File tree

‎__test__/input-helper.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,50 @@ describe('input-helper tests', () => {
180180
const settings: IGitSourceSettings = await inputHelper.getInputs()
181181
expect(settings.workflowOrganizationId).toBe(123456)
182182
})
183+
184+
describe('unsafe PR checkout guard', () => {
185+
const forkPayload = {
186+
repository: {id: 100},
187+
pull_request: {
188+
head: {
189+
sha: '1234567890123456789012345678901234567890',
190+
repo: {id: 200, full_name: 'attacker/fork'}
191+
},
192+
merge_commit_sha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
193+
}
194+
}
195+
196+
it('allows the default self-checkout on a fork pull_request_target', async () => {
197+
const originalEvent = mockGithubContext.eventName
198+
const originalPayload = mockGithubContext.payload
199+
try {
200+
mockGithubContext.eventName = 'pull_request_target'
201+
mockGithubContext.payload = forkPayload
202+
// Simulate a rebase/fast-forward merge where the base tip (event SHA)
203+
// equals the PR head SHA. The default self-checkout must still succeed.
204+
mockGithubContext.sha = '1234567890123456789012345678901234567890'
205+
const settings: IGitSourceSettings = await inputHelper.getInputs()
206+
expect(settings.commit).toBe('1234567890123456789012345678901234567890')
207+
} finally {
208+
mockGithubContext.eventName = originalEvent
209+
mockGithubContext.payload = originalPayload
210+
}
211+
})
212+
213+
it('refuses an explicit fork repository on pull_request_target', async () => {
214+
const originalEvent = mockGithubContext.eventName
215+
const originalPayload = mockGithubContext.payload
216+
try {
217+
mockGithubContext.eventName = 'pull_request_target'
218+
mockGithubContext.payload = forkPayload
219+
inputs.repository = 'attacker/fork'
220+
await expect(inputHelper.getInputs()).rejects.toThrow(
221+
/Refusing to check out fork pull request code/
222+
)
223+
} finally {
224+
mockGithubContext.eventName = originalEvent
225+
mockGithubContext.payload = originalPayload
226+
}
227+
})
228+
})
183229
})

‎dist/index.js‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42191,12 +42191,19 @@ async function getInputs() {
4219142191
(getInput('allow-unsafe-pr-checkout') || 'false').toUpperCase() ===
4219242192
'TRUE';
4219342193
core_debug(`allow unsafe PR checkout = ${result.allowUnsafePrCheckout}`);
42194-
assertSafePrCheckout({
42195-
qualifiedRepository,
42196-
ref: result.ref,
42197-
commit: result.commit,
42198-
allowUnsafePrCheckout: result.allowUnsafePrCheckout
42199-
});
42194+
// The default self-checkout (this repository with no explicit ref) always
42195+
// resolves to the trusted ref/commit GitHub set for the triggering event, so
42196+
// the fork-checkout guard only needs to run when the caller customized the
42197+
// repository or ref.
42198+
const isDefaultCheckout = isWorkflowRepository && !getInput('ref');
42199+
if (!isDefaultCheckout) {
42200+
assertSafePrCheckout({
42201+
qualifiedRepository,
42202+
ref: result.ref,
42203+
commit: result.commit,
42204+
allowUnsafePrCheckout: result.allowUnsafePrCheckout
42205+
});
42206+
}
4220042207
return result;
4220142208
}
4220242209

‎src/input-helper.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,19 @@ export async function getInputs(): Promise<IGitSourceSettings> {
168168
'TRUE'
169169
core.debug(`allow unsafe PR checkout = ${result.allowUnsafePrCheckout}`)
170170

171-
unsafePrCheckoutHelper.assertSafePrCheckout({
172-
qualifiedRepository,
173-
ref: result.ref,
174-
commit: result.commit,
175-
allowUnsafePrCheckout: result.allowUnsafePrCheckout
176-
})
171+
// The default self-checkout (this repository with no explicit ref) always
172+
// resolves to the trusted ref/commit GitHub set for the triggering event, so
173+
// the fork-checkout guard only needs to run when the caller customized the
174+
// repository or ref.
175+
const isDefaultCheckout = isWorkflowRepository && !core.getInput('ref')
176+
if (!isDefaultCheckout) {
177+
unsafePrCheckoutHelper.assertSafePrCheckout({
178+
qualifiedRepository,
179+
ref: result.ref,
180+
commit: result.commit,
181+
allowUnsafePrCheckout: result.allowUnsafePrCheckout
182+
})
183+
}
177184

178185
return result
179186
}

0 commit comments

Comments
 (0)