http.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright 2017 The Gogs 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 repo
  5. import (
  6. "bytes"
  7. "compress/gzip"
  8. "fmt"
  9. "net/http"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "github.com/Unknwon/com"
  18. log "gopkg.in/clog.v1"
  19. "gopkg.in/macaron.v1"
  20. "github.com/gogits/gogs/models"
  21. "github.com/gogits/gogs/models/errors"
  22. "github.com/gogits/gogs/pkg/context"
  23. "github.com/gogits/gogs/pkg/setting"
  24. "github.com/gogits/gogs/pkg/tool"
  25. )
  26. const (
  27. ENV_AUTH_USER_ID = "GOGS_AUTH_USER_ID"
  28. ENV_AUTH_USER_NAME = "GOGS_AUTH_USER_NAME"
  29. ENV_AUTH_USER_EMAIL = "GOGS_AUTH_USER_EMAIL"
  30. ENV_REPO_OWNER_NAME = "GOGS_REPO_OWNER_NAME"
  31. ENV_REPO_OWNER_SALT_MD5 = "GOGS_REPO_OWNER_SALT_MD5"
  32. ENV_REPO_ID = "GOGS_REPO_ID"
  33. ENV_REPO_NAME = "GOGS_REPO_NAME"
  34. ENV_REPO_CUSTOM_HOOKS_PATH = "GOGS_REPO_CUSTOM_HOOKS_PATH"
  35. )
  36. type HTTPContext struct {
  37. *context.Context
  38. OwnerName string
  39. OwnerSalt string
  40. RepoID int64
  41. RepoName string
  42. AuthUser *models.User
  43. }
  44. // askCredentials responses HTTP header and status which informs client to provide credentials.
  45. func askCredentials(c *context.Context, status int, text string) {
  46. c.Resp.Header().Set("WWW-Authenticate", "Basic realm=\".\"")
  47. c.HandleText(status, text)
  48. }
  49. func HTTPContexter() macaron.Handler {
  50. return func(c *context.Context) {
  51. if len(setting.HTTP.AccessControlAllowOrigin) > 0 {
  52. // Set CORS headers for browser-based git clients
  53. c.Resp.Header().Set("Access-Control-Allow-Origin", setting.HTTP.AccessControlAllowOrigin)
  54. c.Resp.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
  55. // Handle preflight OPTIONS request
  56. if c.Req.Method == "OPTIONS" {
  57. c.Status(http.StatusOK)
  58. return
  59. }
  60. }
  61. ownerName := c.Params(":username")
  62. repoName := strings.TrimSuffix(c.Params(":reponame"), ".git")
  63. repoName = strings.TrimSuffix(repoName, ".wiki")
  64. isPull := c.Query("service") == "git-upload-pack" ||
  65. strings.HasSuffix(c.Req.URL.Path, "git-upload-pack") ||
  66. c.Req.Method == "GET"
  67. owner, err := models.GetUserByName(ownerName)
  68. if err != nil {
  69. c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
  70. return
  71. }
  72. repo, err := models.GetRepositoryByName(owner.ID, repoName)
  73. if err != nil {
  74. c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
  75. return
  76. }
  77. // Authentication is not required for pulling from public repositories.
  78. if isPull && !repo.IsPrivate && !setting.Service.RequireSignInView {
  79. c.Map(&HTTPContext{
  80. Context: c,
  81. })
  82. return
  83. }
  84. // In case user requested a wrong URL and not intended to access Git objects.
  85. action := c.Params("*")
  86. if !strings.Contains(action, "git-") &&
  87. !strings.Contains(action, "info/") &&
  88. !strings.Contains(action, "HEAD") &&
  89. !strings.Contains(action, "objects/") {
  90. c.NotFound()
  91. return
  92. }
  93. // Handle HTTP Basic Authentication
  94. authHead := c.Req.Header.Get("Authorization")
  95. if len(authHead) == 0 {
  96. askCredentials(c, http.StatusUnauthorized, "")
  97. return
  98. }
  99. auths := strings.Fields(authHead)
  100. if len(auths) != 2 || auths[0] != "Basic" {
  101. askCredentials(c, http.StatusUnauthorized, "")
  102. return
  103. }
  104. authUsername, authPassword, err := tool.BasicAuthDecode(auths[1])
  105. if err != nil {
  106. askCredentials(c, http.StatusUnauthorized, "")
  107. return
  108. }
  109. authUser, err := models.UserLogin(authUsername, authPassword, -1)
  110. if err != nil && !errors.IsUserNotExist(err) {
  111. c.Handle(http.StatusInternalServerError, "UserLogin", err)
  112. return
  113. }
  114. // If username and password combination failed, try again using username as a token.
  115. if authUser == nil {
  116. token, err := models.GetAccessTokenBySHA(authUsername)
  117. if err != nil {
  118. if models.IsErrAccessTokenEmpty(err) || models.IsErrAccessTokenNotExist(err) {
  119. askCredentials(c, http.StatusUnauthorized, "")
  120. } else {
  121. c.Handle(http.StatusInternalServerError, "GetAccessTokenBySHA", err)
  122. }
  123. return
  124. }
  125. token.Updated = time.Now()
  126. authUser, err = models.GetUserByID(token.UID)
  127. if err != nil {
  128. // Once we found token, we're supposed to find its related user,
  129. // thus any error is unexpected.
  130. c.Handle(http.StatusInternalServerError, "GetUserByID", err)
  131. return
  132. }
  133. } else if authUser.IsEnabledTwoFactor() {
  134. askCredentials(c, http.StatusUnauthorized, `User with two-factor authentication enabled cannot perform HTTP/HTTPS operations via plain username and password
  135. Please create and use personal access token on user settings page`)
  136. return
  137. }
  138. log.Trace("HTTPGit - Authenticated user: %s", authUser.Name)
  139. mode := models.ACCESS_MODE_WRITE
  140. if isPull {
  141. mode = models.ACCESS_MODE_READ
  142. }
  143. has, err := models.HasAccess(authUser.ID, repo, mode)
  144. if err != nil {
  145. c.Handle(http.StatusInternalServerError, "HasAccess", err)
  146. return
  147. } else if !has {
  148. askCredentials(c, http.StatusForbidden, "User permission denied")
  149. return
  150. }
  151. if !isPull && repo.IsMirror {
  152. c.HandleText(http.StatusForbidden, "Mirror repository is read-only")
  153. return
  154. }
  155. c.Map(&HTTPContext{
  156. Context: c,
  157. OwnerName: ownerName,
  158. OwnerSalt: owner.Salt,
  159. RepoID: repo.ID,
  160. RepoName: repoName,
  161. AuthUser: authUser,
  162. })
  163. }
  164. }
  165. type serviceHandler struct {
  166. w http.ResponseWriter
  167. r *http.Request
  168. dir string
  169. file string
  170. authUser *models.User
  171. ownerName string
  172. ownerSalt string
  173. repoID int64
  174. repoName string
  175. }
  176. func (h *serviceHandler) setHeaderNoCache() {
  177. h.w.Header().Set("Expires", "Fri, 01 Jan 1980 00:00:00 GMT")
  178. h.w.Header().Set("Pragma", "no-cache")
  179. h.w.Header().Set("Cache-Control", "no-cache, max-age=0, must-revalidate")
  180. }
  181. func (h *serviceHandler) setHeaderCacheForever() {
  182. now := time.Now().Unix()
  183. expires := now + 31536000
  184. h.w.Header().Set("Date", fmt.Sprintf("%d", now))
  185. h.w.Header().Set("Expires", fmt.Sprintf("%d", expires))
  186. h.w.Header().Set("Cache-Control", "public, max-age=31536000")
  187. }
  188. func (h *serviceHandler) sendFile(contentType string) {
  189. reqFile := path.Join(h.dir, h.file)
  190. fi, err := os.Stat(reqFile)
  191. if os.IsNotExist(err) {
  192. h.w.WriteHeader(http.StatusNotFound)
  193. return
  194. }
  195. h.w.Header().Set("Content-Type", contentType)
  196. h.w.Header().Set("Content-Length", fmt.Sprintf("%d", fi.Size()))
  197. h.w.Header().Set("Last-Modified", fi.ModTime().Format(http.TimeFormat))
  198. http.ServeFile(h.w, h.r, reqFile)
  199. }
  200. type ComposeHookEnvsOptions struct {
  201. AuthUser *models.User
  202. OwnerName string
  203. OwnerSalt string
  204. RepoID int64
  205. RepoName string
  206. RepoPath string
  207. }
  208. func ComposeHookEnvs(opts ComposeHookEnvsOptions) []string {
  209. envs := []string{
  210. "SSH_ORIGINAL_COMMAND=1",
  211. ENV_AUTH_USER_ID + "=" + com.ToStr(opts.AuthUser.ID),
  212. ENV_AUTH_USER_NAME + "=" + opts.AuthUser.Name,
  213. ENV_AUTH_USER_EMAIL + "=" + opts.AuthUser.Email,
  214. ENV_REPO_OWNER_NAME + "=" + opts.OwnerName,
  215. ENV_REPO_OWNER_SALT_MD5 + "=" + tool.MD5(opts.OwnerSalt),
  216. ENV_REPO_ID + "=" + com.ToStr(opts.RepoID),
  217. ENV_REPO_NAME + "=" + opts.RepoName,
  218. ENV_REPO_CUSTOM_HOOKS_PATH + "=" + path.Join(opts.RepoPath, "custom_hooks"),
  219. }
  220. return envs
  221. }
  222. func serviceRPC(h serviceHandler, service string) {
  223. defer h.r.Body.Close()
  224. if h.r.Header.Get("Content-Type") != fmt.Sprintf("application/x-git-%s-request", service) {
  225. h.w.WriteHeader(http.StatusUnauthorized)
  226. return
  227. }
  228. h.w.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-result", service))
  229. var (
  230. reqBody = h.r.Body
  231. err error
  232. )
  233. // Handle GZIP
  234. if h.r.Header.Get("Content-Encoding") == "gzip" {
  235. reqBody, err = gzip.NewReader(reqBody)
  236. if err != nil {
  237. log.Error(2, "HTTP.Get: fail to create gzip reader: %v", err)
  238. h.w.WriteHeader(http.StatusInternalServerError)
  239. return
  240. }
  241. }
  242. var stderr bytes.Buffer
  243. cmd := exec.Command("git", service, "--stateless-rpc", h.dir)
  244. if service == "receive-pack" {
  245. cmd.Env = append(os.Environ(), ComposeHookEnvs(ComposeHookEnvsOptions{
  246. AuthUser: h.authUser,
  247. OwnerName: h.ownerName,
  248. OwnerSalt: h.ownerSalt,
  249. RepoID: h.repoID,
  250. RepoName: h.repoName,
  251. RepoPath: h.dir,
  252. })...)
  253. }
  254. cmd.Dir = h.dir
  255. cmd.Stdout = h.w
  256. cmd.Stderr = &stderr
  257. cmd.Stdin = reqBody
  258. if err = cmd.Run(); err != nil {
  259. log.Error(2, "HTTP.serviceRPC: fail to serve RPC '%s': %v - %s", service, err, stderr)
  260. h.w.WriteHeader(http.StatusInternalServerError)
  261. return
  262. }
  263. }
  264. func serviceUploadPack(h serviceHandler) {
  265. serviceRPC(h, "upload-pack")
  266. }
  267. func serviceReceivePack(h serviceHandler) {
  268. serviceRPC(h, "receive-pack")
  269. }
  270. func getServiceType(r *http.Request) string {
  271. serviceType := r.FormValue("service")
  272. if !strings.HasPrefix(serviceType, "git-") {
  273. return ""
  274. }
  275. return strings.TrimPrefix(serviceType, "git-")
  276. }
  277. // FIXME: use process module
  278. func gitCommand(dir string, args ...string) []byte {
  279. cmd := exec.Command("git", args...)
  280. cmd.Dir = dir
  281. out, err := cmd.Output()
  282. if err != nil {
  283. log.Error(2, fmt.Sprintf("Git: %v - %s", err, out))
  284. }
  285. return out
  286. }
  287. func updateServerInfo(dir string) []byte {
  288. return gitCommand(dir, "update-server-info")
  289. }
  290. func packetWrite(str string) []byte {
  291. s := strconv.FormatInt(int64(len(str)+4), 16)
  292. if len(s)%4 != 0 {
  293. s = strings.Repeat("0", 4-len(s)%4) + s
  294. }
  295. return []byte(s + str)
  296. }
  297. func getInfoRefs(h serviceHandler) {
  298. h.setHeaderNoCache()
  299. service := getServiceType(h.r)
  300. if service != "upload-pack" && service != "receive-pack" {
  301. updateServerInfo(h.dir)
  302. h.sendFile("text/plain; charset=utf-8")
  303. return
  304. }
  305. refs := gitCommand(h.dir, service, "--stateless-rpc", "--advertise-refs", ".")
  306. h.w.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-advertisement", service))
  307. h.w.WriteHeader(http.StatusOK)
  308. h.w.Write(packetWrite("# service=git-" + service + "\n"))
  309. h.w.Write([]byte("0000"))
  310. h.w.Write(refs)
  311. }
  312. func getTextFile(h serviceHandler) {
  313. h.setHeaderNoCache()
  314. h.sendFile("text/plain")
  315. }
  316. func getInfoPacks(h serviceHandler) {
  317. h.setHeaderCacheForever()
  318. h.sendFile("text/plain; charset=utf-8")
  319. }
  320. func getLooseObject(h serviceHandler) {
  321. h.setHeaderCacheForever()
  322. h.sendFile("application/x-git-loose-object")
  323. }
  324. func getPackFile(h serviceHandler) {
  325. h.setHeaderCacheForever()
  326. h.sendFile("application/x-git-packed-objects")
  327. }
  328. func getIdxFile(h serviceHandler) {
  329. h.setHeaderCacheForever()
  330. h.sendFile("application/x-git-packed-objects-toc")
  331. }
  332. var routes = []struct {
  333. reg *regexp.Regexp
  334. method string
  335. handler func(serviceHandler)
  336. }{
  337. {regexp.MustCompile("(.*?)/git-upload-pack$"), "POST", serviceUploadPack},
  338. {regexp.MustCompile("(.*?)/git-receive-pack$"), "POST", serviceReceivePack},
  339. {regexp.MustCompile("(.*?)/info/refs$"), "GET", getInfoRefs},
  340. {regexp.MustCompile("(.*?)/HEAD$"), "GET", getTextFile},
  341. {regexp.MustCompile("(.*?)/objects/info/alternates$"), "GET", getTextFile},
  342. {regexp.MustCompile("(.*?)/objects/info/http-alternates$"), "GET", getTextFile},
  343. {regexp.MustCompile("(.*?)/objects/info/packs$"), "GET", getInfoPacks},
  344. {regexp.MustCompile("(.*?)/objects/info/[^/]*$"), "GET", getTextFile},
  345. {regexp.MustCompile("(.*?)/objects/[0-9a-f]{2}/[0-9a-f]{38}$"), "GET", getLooseObject},
  346. {regexp.MustCompile("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.pack$"), "GET", getPackFile},
  347. {regexp.MustCompile("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.idx$"), "GET", getIdxFile},
  348. }
  349. func getGitRepoPath(dir string) (string, error) {
  350. if !strings.HasSuffix(dir, ".git") {
  351. dir += ".git"
  352. }
  353. filename := path.Join(setting.RepoRootPath, dir)
  354. if _, err := os.Stat(filename); os.IsNotExist(err) {
  355. return "", err
  356. }
  357. return filename, nil
  358. }
  359. func HTTP(c *HTTPContext) {
  360. for _, route := range routes {
  361. reqPath := strings.ToLower(c.Req.URL.Path)
  362. m := route.reg.FindStringSubmatch(reqPath)
  363. if m == nil {
  364. continue
  365. }
  366. // We perform check here because routes matched in cmd/web.go is wider than needed,
  367. // but we only want to output this message only if user is really trying to access
  368. // Git HTTP endpoints.
  369. if setting.Repository.DisableHTTPGit {
  370. c.HandleText(http.StatusForbidden, "Interacting with repositories by HTTP protocol is not disabled")
  371. return
  372. }
  373. if route.method != c.Req.Method {
  374. c.NotFound()
  375. return
  376. }
  377. file := strings.TrimPrefix(reqPath, m[1]+"/")
  378. dir, err := getGitRepoPath(m[1])
  379. if err != nil {
  380. log.Warn("HTTP.getGitRepoPath: %v", err)
  381. c.NotFound()
  382. return
  383. }
  384. route.handler(serviceHandler{
  385. w: c.Resp,
  386. r: c.Req.Request,
  387. dir: dir,
  388. file: file,
  389. authUser: c.AuthUser,
  390. ownerName: c.OwnerName,
  391. ownerSalt: c.OwnerSalt,
  392. repoID: c.RepoID,
  393. repoName: c.RepoName,
  394. })
  395. return
  396. }
  397. c.NotFound()
  398. }