http.go 11 KB

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