2
0

input-helper.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 sha to empty when explicit ref', async () => {
  127. inputs.ref = 'refs/heads/some-other-ref'
  128. const settings: IGitSourceSettings = await inputHelper.getInputs()
  129. expect(settings.ref).toBe('refs/heads/some-other-ref')
  130. expect(settings.commit).toBeFalsy()
  131. })
  132. it('does not reclassify a ref as sha when a BOM is prefixed', async () => {
  133. // A fork branch named "<U+FEFF>" + 40 hex chars. core.getInput trims the
  134. // BOM by default, which previously collapsed this into a bare SHA and
  135. // bypassed the unsafe fork PR checkout guard.
  136. inputs.ref = '\uFEFF522d932fae5296da51fdf431934425ecf891c6a2'
  137. const settings: IGitSourceSettings = await inputHelper.getInputs()
  138. expect(settings.commit).toBeFalsy()
  139. expect(settings.ref).toBe('522d932fae5296da51fdf431934425ecf891c6a2')
  140. })
  141. it('treats a sha surrounded by ascii whitespace as a commit', async () => {
  142. // ASCII whitespace can only come from the workflow author's YAML (git ref
  143. // names cannot contain it), so trimming it and treating the value as a
  144. // commit is safe.
  145. inputs.ref = ' 1111111111222222222233333333334444444444 '
  146. const settings: IGitSourceSettings = await inputHelper.getInputs()
  147. expect(settings.ref).toBeFalsy()
  148. expect(settings.commit).toBe('1111111111222222222233333333334444444444')
  149. })
  150. it('sets workflow organization ID', async () => {
  151. const settings: IGitSourceSettings = await inputHelper.getInputs()
  152. expect(settings.workflowOrganizationId).toBe(123456)
  153. })
  154. describe('unsafe PR checkout guard', () => {
  155. const forkPayload = {
  156. repository: {id: 100},
  157. pull_request: {
  158. head: {
  159. sha: '1234567890123456789012345678901234567890',
  160. repo: {id: 200, full_name: 'attacker/fork'}
  161. },
  162. merge_commit_sha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  163. }
  164. }
  165. it('allows the default self-checkout on a fork pull_request_target', async () => {
  166. const originalEvent = github.context.eventName
  167. const originalPayload = github.context.payload
  168. const originalSha = github.context.sha
  169. try {
  170. github.context.eventName = 'pull_request_target'
  171. github.context.payload = forkPayload as any
  172. // Simulate a rebase/fast-forward merge where the base tip (event SHA)
  173. // equals the PR head SHA. The default self-checkout must still succeed.
  174. github.context.sha = '1234567890123456789012345678901234567890'
  175. const settings: IGitSourceSettings = await inputHelper.getInputs()
  176. expect(settings.commit).toBe('1234567890123456789012345678901234567890')
  177. } finally {
  178. github.context.eventName = originalEvent
  179. github.context.payload = originalPayload
  180. github.context.sha = originalSha
  181. }
  182. })
  183. it('refuses an explicit fork repository on pull_request_target', async () => {
  184. const originalEvent = github.context.eventName
  185. const originalPayload = github.context.payload
  186. try {
  187. github.context.eventName = 'pull_request_target'
  188. github.context.payload = forkPayload as any
  189. inputs.repository = 'attacker/fork'
  190. await expect(inputHelper.getInputs()).rejects.toThrow(
  191. /Refusing to check out fork pull request code/
  192. )
  193. } finally {
  194. github.context.eventName = originalEvent
  195. github.context.payload = originalPayload
  196. }
  197. })
  198. })
  199. })