You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
10 KiB

Sign merges, CRUD, Wiki and Repository initialisation with gpg key (#7631) This PR fixes #7598 by providing a configurable way of signing commits across the Gitea instance. Per repository configurability and import/generation of trusted secure keys is not provided by this PR - from a security PoV that's probably impossible to do properly. Similarly web-signing, that is asking the user to sign something, is not implemented - this could be done at a later stage however. ## Features - [x] If commit.gpgsign is set in .gitconfig sign commits and files created through repofiles. (merges should already have been signed.) - [x] Verify commits signed with the default gpg as valid - [x] Signer, Committer and Author can all be different - [x] Allow signer to be arbitrarily different - We still require the key to have an activated email on Gitea. A more complete implementation would be to use a keyserver and mark external-or-unactivated with an "unknown" trust level icon. - [x] Add a signing-key.gpg endpoint to get the default gpg pub key if available - Rather than add a fake web-flow user I've added this as an endpoint on /api/v1/signing-key.gpg - [x] Try to match the default key with a user on gitea - this is done at verification time - [x] Make things configurable? - app.ini configuration done - [x] when checking commits are signed need to check if they're actually verifiable too - [x] Add documentation I have decided that adjusting the docker to create a default gpg key is not the correct thing to do and therefore have not implemented this.
5 years ago
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "path/filepath"
  11. "testing"
  12. "time"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/setting"
  17. api "code.gitea.io/gitea/modules/structs"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. func getCreateFileOptions() api.CreateFileOptions {
  21. content := "This is new text"
  22. contentEncoded := base64.StdEncoding.EncodeToString([]byte(content))
  23. return api.CreateFileOptions{
  24. FileOptions: api.FileOptions{
  25. BranchName: "master",
  26. NewBranchName: "master",
  27. Message: "Making this new file new/file.txt",
  28. Author: api.Identity{
  29. Name: "Anne Doe",
  30. Email: "annedoe@example.com",
  31. },
  32. Committer: api.Identity{
  33. Name: "John Doe",
  34. Email: "johndoe@example.com",
  35. },
  36. Dates: api.CommitDateOptions{
  37. Author: time.Unix(946684810, 0),
  38. Committer: time.Unix(978307190, 0),
  39. },
  40. },
  41. Content: contentEncoded,
  42. }
  43. }
  44. func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileResponse {
  45. sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
  46. encoding := "base64"
  47. content := "VGhpcyBpcyBuZXcgdGV4dA=="
  48. selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
  49. htmlURL := setting.AppURL + "user2/repo1/src/branch/master/" + treePath
  50. gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha
  51. downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
  52. return &api.FileResponse{
  53. Content: &api.ContentsResponse{
  54. Name: filepath.Base(treePath),
  55. Path: treePath,
  56. SHA: sha,
  57. Size: 16,
  58. Type: "file",
  59. Encoding: &encoding,
  60. Content: &content,
  61. URL: &selfURL,
  62. HTMLURL: &htmlURL,
  63. GitURL: &gitURL,
  64. DownloadURL: &downloadURL,
  65. Links: &api.FileLinksResponse{
  66. Self: &selfURL,
  67. GitURL: &gitURL,
  68. HTMLURL: &htmlURL,
  69. },
  70. },
  71. Commit: &api.FileCommitResponse{
  72. CommitMeta: api.CommitMeta{
  73. URL: setting.AppURL + "api/v1/repos/user2/repo1/git/commits/" + commitID,
  74. SHA: commitID,
  75. },
  76. HTMLURL: setting.AppURL + "user2/repo1/commit/" + commitID,
  77. Author: &api.CommitUser{
  78. Identity: api.Identity{
  79. Name: "Anne Doe",
  80. Email: "annedoe@example.com",
  81. },
  82. Date: "2000-01-01T00:00:10Z",
  83. },
  84. Committer: &api.CommitUser{
  85. Identity: api.Identity{
  86. Name: "John Doe",
  87. Email: "johndoe@example.com",
  88. },
  89. Date: "2000-12-31T23:59:50Z",
  90. },
  91. Message: "Updates README.md\n",
  92. },
  93. Verification: &api.PayloadCommitVerification{
  94. Verified: false,
  95. Reason: "gpg.error.not_signed_commit",
  96. Signature: "",
  97. Payload: "",
  98. },
  99. }
  100. }
  101. func TestAPICreateFile(t *testing.T) {
  102. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  103. user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
  104. user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
  105. user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
  106. repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
  107. repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
  108. repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
  109. fileID := 0
  110. // Get user2's token
  111. session := loginUser(t, user2.Name)
  112. token2 := getTokenForLoggedInUser(t, session)
  113. session = emptyTestSession(t)
  114. // Get user4's token
  115. session = loginUser(t, user4.Name)
  116. token4 := getTokenForLoggedInUser(t, session)
  117. session = emptyTestSession(t)
  118. // Test creating a file in repo1 which user2 owns, try both with branch and empty branch
  119. for _, branch := range [...]string{
  120. "master", // Branch
  121. "", // Empty branch
  122. } {
  123. createFileOptions := getCreateFileOptions()
  124. createFileOptions.BranchName = branch
  125. fileID++
  126. treePath := fmt.Sprintf("new/file%d.txt", fileID)
  127. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  128. req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
  129. resp := session.MakeRequest(t, req, http.StatusCreated)
  130. gitRepo, _ := git.OpenRepository(repo1.RepoPath())
  131. commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
  132. expectedFileResponse := getExpectedFileResponseForCreate(commitID, treePath)
  133. var fileResponse api.FileResponse
  134. DecodeJSON(t, resp, &fileResponse)
  135. assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
  136. assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
  137. assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
  138. assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
  139. assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
  140. assert.EqualValues(t, expectedFileResponse.Commit.Author.Date, fileResponse.Commit.Author.Date)
  141. assert.EqualValues(t, expectedFileResponse.Commit.Committer.Email, fileResponse.Commit.Committer.Email)
  142. assert.EqualValues(t, expectedFileResponse.Commit.Committer.Name, fileResponse.Commit.Committer.Name)
  143. assert.EqualValues(t, expectedFileResponse.Commit.Committer.Date, fileResponse.Commit.Committer.Date)
  144. gitRepo.Close()
  145. }
  146. // Test creating a file in a new branch
  147. createFileOptions := getCreateFileOptions()
  148. createFileOptions.BranchName = repo1.DefaultBranch
  149. createFileOptions.NewBranchName = "new_branch"
  150. fileID++
  151. treePath := fmt.Sprintf("new/file%d.txt", fileID)
  152. url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  153. req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
  154. resp := session.MakeRequest(t, req, http.StatusCreated)
  155. var fileResponse api.FileResponse
  156. DecodeJSON(t, resp, &fileResponse)
  157. expectedSHA := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
  158. expectedHTMLURL := fmt.Sprintf(setting.AppURL+"user2/repo1/src/branch/new_branch/new/file%d.txt", fileID)
  159. expectedDownloadURL := fmt.Sprintf(setting.AppURL+"user2/repo1/raw/branch/new_branch/new/file%d.txt", fileID)
  160. assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA)
  161. assert.EqualValues(t, expectedHTMLURL, *fileResponse.Content.HTMLURL)
  162. assert.EqualValues(t, expectedDownloadURL, *fileResponse.Content.DownloadURL)
  163. assert.EqualValues(t, createFileOptions.Message+"\n", fileResponse.Commit.Message)
  164. // Test creating a file without a message
  165. createFileOptions = getCreateFileOptions()
  166. createFileOptions.Message = ""
  167. fileID++
  168. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  169. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  170. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  171. resp = session.MakeRequest(t, req, http.StatusCreated)
  172. DecodeJSON(t, resp, &fileResponse)
  173. expectedMessage := "Add '" + treePath + "'\n"
  174. assert.EqualValues(t, expectedMessage, fileResponse.Commit.Message)
  175. // Test trying to create a file that already exists, should fail
  176. createFileOptions = getCreateFileOptions()
  177. treePath = "README.md"
  178. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
  179. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  180. resp = session.MakeRequest(t, req, http.StatusInternalServerError)
  181. expectedAPIError := context.APIError{
  182. Message: "repository file already exists [path: " + treePath + "]",
  183. URL: setting.API.SwaggerURL,
  184. }
  185. var apiError context.APIError
  186. DecodeJSON(t, resp, &apiError)
  187. assert.Equal(t, expectedAPIError, apiError)
  188. // Test creating a file in repo1 by user4 who does not have write access
  189. createFileOptions = getCreateFileOptions()
  190. fileID++
  191. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  192. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
  193. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  194. session.MakeRequest(t, req, http.StatusNotFound)
  195. // Tests a repo with no token given so will fail
  196. createFileOptions = getCreateFileOptions()
  197. fileID++
  198. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  199. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
  200. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  201. session.MakeRequest(t, req, http.StatusNotFound)
  202. // Test using access token for a private repo that the user of the token owns
  203. createFileOptions = getCreateFileOptions()
  204. fileID++
  205. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  206. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
  207. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  208. session.MakeRequest(t, req, http.StatusCreated)
  209. // Test using org repo "user3/repo3" where user2 is a collaborator
  210. createFileOptions = getCreateFileOptions()
  211. fileID++
  212. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  213. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
  214. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  215. session.MakeRequest(t, req, http.StatusCreated)
  216. // Test using org repo "user3/repo3" with no user token
  217. createFileOptions = getCreateFileOptions()
  218. fileID++
  219. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  220. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
  221. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  222. session.MakeRequest(t, req, http.StatusNotFound)
  223. // Test using repo "user2/repo1" where user4 is a NOT collaborator
  224. createFileOptions = getCreateFileOptions()
  225. fileID++
  226. treePath = fmt.Sprintf("new/file%d.txt", fileID)
  227. url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
  228. req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
  229. session.MakeRequest(t, req, http.StatusForbidden)
  230. })
  231. }