input-helper.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import * as core from '@actions/core'
  2. import * as fsHelper from '../lib/fs-helper'
  3. import * as github from '@actions/github'
  4. import * as inputHelper from '../lib/input-helper'
  5. import * as path from 'path'
  6. import * as workflowContextHelper from '../lib/workflow-context-helper'
  7. import {IGitSourceSettings} from '../lib/git-source-settings'
  8. const originalGitHubWorkspace = process.env['GITHUB_WORKSPACE']
  9. const gitHubWorkspace = path.resolve('/checkout-tests/workspace')
  10. // Inputs for mock @actions/core
  11. let inputs = {} as any
  12. // Replicate @actions/core getInput behavior: it trims whitespace by default
  13. // (String.prototype.trim(), which strips characters such as a leading U+FEFF BOM)
  14. // unless trimWhitespace is explicitly set to false.
  15. const getInputImpl = (name: string, options?: {trimWhitespace?: boolean}) => {
  16. const val = inputs[name] ?? ''
  17. if (options && options.trimWhitespace === false) {
  18. return val
  19. }
  20. return typeof val === 'string' ? val.trim() : val
  21. }
  22. // Shallow clone original @actions/github context
  23. let originalContext = {...github.context}
  24. describe('input-helper tests', () => {
  25. beforeAll(() => {
  26. // Mock getInput
  27. jest.spyOn(core, 'getInput').mockImplementation(getInputImpl as any)
  28. // Mock error/warning/info/debug
  29. jest.spyOn(core, 'error').mockImplementation(jest.fn())
  30. jest.spyOn(core, 'warning').mockImplementation(jest.fn())
  31. jest.spyOn(core, 'info').mockImplementation(jest.fn())
  32. jest.spyOn(core, 'debug').mockImplementation(jest.fn())
  33. // Mock github context
  34. jest.spyOn(github.context, 'repo', 'get').mockImplementation(() => {
  35. return {
  36. owner: 'some-owner',
  37. repo: 'some-repo'
  38. }
  39. })
  40. github.context.ref = 'refs/heads/some-ref'
  41. github.context.sha = '1234567890123456789012345678901234567890'
  42. // Mock ./fs-helper directoryExistsSync()
  43. jest
  44. .spyOn(fsHelper, 'directoryExistsSync')
  45. .mockImplementation((path: string) => path == gitHubWorkspace)
  46. // Mock ./workflowContextHelper getOrganizationId()
  47. jest
  48. .spyOn(workflowContextHelper, 'getOrganizationId')
  49. .mockImplementation(() => Promise.resolve(123456))
  50. // GitHub workspace
  51. process.env['GITHUB_WORKSPACE'] = gitHubWorkspace
  52. })
  53. beforeEach(() => {
  54. // Reset inputs
  55. inputs = {}
  56. })
  57. afterAll(() => {
  58. // Restore GitHub workspace
  59. delete process.env['GITHUB_WORKSPACE']
  60. if (originalGitHubWorkspace) {
  61. process.env['GITHUB_WORKSPACE'] = originalGitHubWorkspace
  62. }
  63. // Restore @actions/github context
  64. github.context.ref = originalContext.ref
  65. github.context.sha = originalContext.sha
  66. // Restore
  67. jest.restoreAllMocks()
  68. })
  69. it('sets defaults', async () => {
  70. const settings: IGitSourceSettings = await inputHelper.getInputs()
  71. expect(settings).toBeTruthy()
  72. expect(settings.authToken).toBeFalsy()
  73. expect(settings.clean).toBe(true)
  74. expect(settings.commit).toBeTruthy()
  75. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  76. expect(settings.filter).toBe(undefined)
  77. expect(settings.sparseCheckout).toBe(undefined)
  78. expect(settings.sparseCheckoutConeMode).toBe(true)
  79. expect(settings.fetchDepth).toBe(1)
  80. expect(settings.fetchTags).toBe(false)
  81. expect(settings.showProgress).toBe(true)
  82. expect(settings.lfs).toBe(false)
  83. expect(settings.ref).toBe('refs/heads/some-ref')
  84. expect(settings.repositoryName).toBe('some-repo')
  85. expect(settings.repositoryOwner).toBe('some-owner')
  86. expect(settings.repositoryPath).toBe(gitHubWorkspace)
  87. expect(settings.setSafeDirectory).toBe(true)
  88. expect(settings.allowUnsafePrCheckout).toBe(false)
  89. })
  90. it('qualifies ref', async () => {
  91. let originalRef = github.context.ref
  92. try {
  93. github.context.ref = 'some-unqualified-ref'
  94. const settings: IGitSourceSettings = await inputHelper.getInputs()
  95. expect(settings).toBeTruthy()
  96. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  97. expect(settings.ref).toBe('refs/heads/some-unqualified-ref')
  98. } finally {
  99. github.context.ref = originalRef
  100. }
  101. })
  102. it('requires qualified repo', async () => {
  103. inputs.repository = 'some-unqualified-repo'
  104. try {
  105. await inputHelper.getInputs()
  106. throw 'should not reach here'
  107. } catch (err) {
  108. expect(`(${(err as any).message}`).toMatch(
  109. "Invalid repository 'some-unqualified-repo'"
  110. )
  111. }
  112. })
  113. it('roots path', async () => {
  114. inputs.path = 'some-directory/some-subdirectory'
  115. const settings: IGitSourceSettings = await inputHelper.getInputs()
  116. expect(settings.repositoryPath).toBe(
  117. path.join(gitHubWorkspace, 'some-directory', 'some-subdirectory')
  118. )
  119. })
  120. it('sets ref to empty when explicit sha', async () => {
  121. inputs.ref = '1111111111222222222233333333334444444444'
  122. const settings: IGitSourceSettings = await inputHelper.getInputs()
  123. expect(settings.ref).toBeFalsy()
  124. expect(settings.commit).toBe('1111111111222222222233333333334444444444')
  125. })
  126. it('sets ref to empty when explicit sha-256', async () => {
  127. inputs.ref =
  128. '1111111111222222222233333333334444444444555555555566666666667777'
  129. const settings: IGitSourceSettings = await inputHelper.getInputs()
  130. expect(settings.ref).toBeFalsy()
  131. expect(settings.commit).toBe(
  132. '1111111111222222222233333333334444444444555555555566666666667777'
  133. )
  134. })
  135. it('sets sha to empty when explicit ref', async () => {
  136. inputs.ref = 'refs/heads/some-other-ref'
  137. const settings: IGitSourceSettings = await inputHelper.getInputs()
  138. expect(settings.ref).toBe('refs/heads/some-other-ref')
  139. expect(settings.commit).toBeFalsy()
  140. })
  141. it('does not reclassify a ref as sha when a BOM is prefixed', async () => {
  142. // A fork branch named "<U+FEFF>" + 40 hex chars. core.getInput trims the
  143. // BOM by default, which previously collapsed this into a bare SHA and
  144. // bypassed the unsafe fork PR checkout guard.
  145. inputs.ref = '\uFEFF522d932fae5296da51fdf431934425ecf891c6a2'
  146. const settings: IGitSourceSettings = await inputHelper.getInputs()
  147. expect(settings.commit).toBeFalsy()
  148. expect(settings.ref).toBe('522d932fae5296da51fdf431934425ecf891c6a2')
  149. })
  150. it('does not reclassify a sha-256 ref as sha when a BOM is prefixed', async () => {
  151. inputs.ref =
  152. '\uFEFF1111111111222222222233333333334444444444555555555566666666667777'
  153. const settings: IGitSourceSettings = await inputHelper.getInputs()
  154. expect(settings.commit).toBeFalsy()
  155. expect(settings.ref).toBe(
  156. '1111111111222222222233333333334444444444555555555566666666667777'
  157. )
  158. })
  159. it('treats a sha surrounded by ascii whitespace as a commit', async () => {
  160. // ASCII whitespace can only come from the workflow author's YAML (git ref
  161. // names cannot contain it), so trimming it and treating the value as a
  162. // commit is safe.
  163. inputs.ref = ' 1111111111222222222233333333334444444444 '
  164. const settings: IGitSourceSettings = await inputHelper.getInputs()
  165. expect(settings.ref).toBeFalsy()
  166. expect(settings.commit).toBe('1111111111222222222233333333334444444444')
  167. })
  168. it('sets workflow organization ID', async () => {
  169. const settings: IGitSourceSettings = await inputHelper.getInputs()
  170. expect(settings.workflowOrganizationId).toBe(123456)
  171. })
  172. describe('unsafe PR checkout guard', () => {
  173. const forkPayload = {
  174. repository: {id: 100},
  175. pull_request: {
  176. head: {
  177. sha: '1234567890123456789012345678901234567890',
  178. repo: {id: 200, full_name: 'attacker/fork'}
  179. },
  180. merge_commit_sha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  181. }
  182. }
  183. it('allows the default self-checkout on a fork pull_request_target', async () => {
  184. const originalEvent = github.context.eventName
  185. const originalPayload = github.context.payload
  186. const originalSha = github.context.sha
  187. try {
  188. github.context.eventName = 'pull_request_target'
  189. github.context.payload = forkPayload as any
  190. // Simulate a rebase/fast-forward merge where the base tip (event SHA)
  191. // equals the PR head SHA. The default self-checkout must still succeed.
  192. github.context.sha = '1234567890123456789012345678901234567890'
  193. const settings: IGitSourceSettings = await inputHelper.getInputs()
  194. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  195. } finally {
  196. github.context.eventName = originalEvent
  197. github.context.payload = originalPayload
  198. github.context.sha = originalSha
  199. }
  200. })
  201. it('refuses an explicit fork repository on pull_request_target', async () => {
  202. const originalEvent = github.context.eventName
  203. const originalPayload = github.context.payload
  204. try {
  205. github.context.eventName = 'pull_request_target'
  206. github.context.payload = forkPayload as any
  207. inputs.repository = 'attacker/fork'
  208. await expect(inputHelper.getInputs()).rejects.toThrow(
  209. /Refusing to check out fork pull request code/
  210. )
  211. } finally {
  212. github.context.eventName = originalEvent
  213. github.context.payload = originalPayload
  214. }
  215. })
  216. })
  217. })