internal/httpd/smart.go
126 lines · 4241 bytes
1// Package httpd serves the HTTP listener: anonymous smart-HTTP git reads for
2// public repositories, and (from M5) the web UI. There is no authentication
3// on this listener by design — private repositories answer 404 everywhere,
4// and pushes are refused with a pkt-line ERR so no git version ever falls
5// back to asking for credentials.
6package httpd
7
8import (
9 "compress/gzip"
10 "fmt"
11 "io"
12 "net/http"
13 "os"
14 "os/exec"
15 "strings"
16
17 "gitbay.org/gitbay/internal/config"
18 "gitbay.org/gitbay/internal/control"
19 "gitbay.org/gitbay/internal/store"
20)
21
22type Server struct {
23 cfg config.Config
24 st *store.Store
25 apiLimit *apiLimiter
26}
27
28func New(cfg config.Config, st *store.Store) *Server {
29 return &Server{cfg: cfg, st: st, apiLimit: newAPILimiter(cfg.Limits.APIRate)}
30}
31
32// receivePackRefusal exists only to fail legibly if a client POSTs without
33// reading the advertisement first.
34func (s *Server) receivePackRefusal(w http.ResponseWriter, r *http.Request) {
35 http.Error(w, s.pushRefusalMessage(r.PathValue("owner"), r.PathValue("repo")), http.StatusForbidden)
36}
37
38// publicRepo resolves owner/name and returns it only if it exists and is
39// public. Every failure mode is the same 404.
40func (s *Server) publicRepo(owner, name string) (store.Repo, bool) {
41 repo, err := s.st.RepoByPath(owner + "/" + name)
42 if err != nil || repo.Visibility != "public" {
43 return store.Repo{}, false
44 }
45 return repo, true
46}
47
48func pktLine(w io.Writer, s string) {
49 fmt.Fprintf(w, "%04x%s", len(s)+4, s)
50}
51
52func pktFlush(w io.Writer) { io.WriteString(w, "0000") }
53
54func (s *Server) pushRefusalMessage(owner, repo string) string {
55 host := strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(s.cfg.Server.SiteURL, "https://"), "http://"), "/")
56 name := strings.TrimSuffix(repo, ".git")
57 return fmt.Sprintf("pushes to this forge go over SSH: git remote set-url --push origin git@%s:%s/%s.git", host, owner, name)
58}
59
60func (s *Server) infoRefs(w http.ResponseWriter, r *http.Request) {
61 owner, name := r.PathValue("owner"), r.PathValue("repo")
62 repo, ok := s.publicRepo(owner, name)
63 if !ok {
64 http.NotFound(w, r)
65 return
66 }
67 switch service := r.URL.Query().Get("service"); service {
68 case "git-upload-pack":
69 w.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement")
70 w.Header().Set("Cache-Control", "no-cache")
71 pktLine(w, "# service=git-upload-pack\n")
72 pktFlush(w)
73 dir := control.RepoDir(s.cfg.Server.Root, repo.OwnerName, repo.Name)
74 cmd := exec.CommandContext(r.Context(), "git", "upload-pack", "--stateless-rpc", "--advertise-refs", dir)
75 cmd.Env = append(os.Environ(), gitProtocolEnv(r)...)
76 cmd.Stdout = w
77 cmd.Run()
78 case "git-receive-pack":
79 // HTTP 200 with a pkt-line ERR: every git version renders this as
80 // "fatal: remote error: ..." and never falls back to credential
81 // prompting the way a 401/403 would.
82 w.Header().Set("Content-Type", "application/x-git-receive-pack-advertisement")
83 w.Header().Set("Cache-Control", "no-cache")
84 pktLine(w, "# service=git-receive-pack\n")
85 pktFlush(w)
86 pktLine(w, "ERR "+s.pushRefusalMessage(owner, name)+"\n")
87 default:
88 // Dumb-protocol clients are not supported.
89 http.NotFound(w, r)
90 }
91}
92
93func (s *Server) uploadPack(w http.ResponseWriter, r *http.Request) {
94 repo, ok := s.publicRepo(r.PathValue("owner"), r.PathValue("repo"))
95 if !ok {
96 http.NotFound(w, r)
97 return
98 }
99 body := io.Reader(r.Body)
100 if r.Header.Get("Content-Encoding") == "gzip" {
101 gz, err := gzip.NewReader(body)
102 if err != nil {
103 http.Error(w, "bad gzip body", http.StatusBadRequest)
104 return
105 }
106 defer gz.Close()
107 body = gz
108 }
109 w.Header().Set("Content-Type", "application/x-git-upload-pack-result")
110 w.Header().Set("Cache-Control", "no-cache")
111 dir := control.RepoDir(s.cfg.Server.Root, repo.OwnerName, repo.Name)
112 cmd := exec.CommandContext(r.Context(), "git", "upload-pack", "--stateless-rpc", dir)
113 cmd.Env = append(os.Environ(), gitProtocolEnv(r)...)
114 cmd.Stdin = body
115 cmd.Stdout = w
116 cmd.Run()
117}
118
119// gitProtocolEnv forwards the client's protocol negotiation header so
120// protocol v2 works over stateless HTTP.
121func gitProtocolEnv(r *http.Request) []string {
122 if p := r.Header.Get("Git-Protocol"); p != "" {
123 return []string{"GIT_PROTOCOL=" + p}
124 }
125 return nil
126}