internal/httpd/control.go
299 lines · 9194 bytes
1package httpd
2
3import (
4 "bytes"
5 "encoding/json"
6 "io"
7 "net/http"
8 "strings"
9
10 "gitbay.org/gitbay/internal/control"
11 "gitbay.org/gitbay/internal/gitutil"
12 "gitbay.org/gitbay/internal/protocol"
13 "gitbay.org/gitbay/internal/store"
14)
15
16// runControl executes a control command as the browser session's user,
17// through the same registry the CLI and the JSON API reach. Web writes
18// never reimplement command logic — merge gates, review rules, and audit
19// entries stay in one place — so the surfaces cannot drift apart.
20//
21// ViaAPI is set, which marks the request as one that arrived over HTTP.
22// Nothing is held back from that door any more (#234): what a caller may
23// do is the account's rights and its credential's scope, decided in one
24// place for every surface.
25func (s *Server) runControl(u store.User, argv []string) (out string, msg string, ok bool) {
26 out, msg, code := s.runControlCode(u, argv)
27 return out, msg, code == protocol.ExitOK
28}
29
30// runControlCode is runControl with the exit code, for handlers that
31// answer a form: not-found and denied deserve their own statuses rather
32// than a redirect carrying the message (#106).
33func (s *Server) runControlCode(u store.User, argv []string) (out string, msg string, code int) {
34 var stdout, stderr bytes.Buffer
35 ctx := &control.Ctx{
36 User: u,
37 Source: "web",
38 Scope: "full",
39 Store: s.st,
40 Cfg: s.cfg,
41 Stdin: strings.NewReader(""),
42 Stdout: &stdout,
43 Stderr: &stderr,
44 ViaAPI: true,
45 }
46 code = control.Dispatch(ctx, argv)
47 m := strings.TrimSpace(stderr.String())
48 if m == "" {
49 m = strings.TrimSpace(stdout.String())
50 }
51 return stdout.String(), m, code
52}
53
54// runControlStream runs a command whose output is written as it is
55// produced: stdout goes to out, and done ends the command when the
56// request does. msg is stderr.
57func (s *Server) runControlStream(u store.User, argv []string, out io.Writer, done <-chan struct{}) (msg string, code int) {
58 var stderr bytes.Buffer
59 ctx := &control.Ctx{
60 User: u,
61 Source: "web",
62 Scope: "full",
63 Store: s.st,
64 Cfg: s.cfg,
65 Stdin: strings.NewReader(""),
66 Stdout: out,
67 Stderr: &stderr,
68 ViaAPI: true,
69 Done: done,
70 }
71 code = control.Dispatch(ctx, argv)
72 return strings.TrimSpace(stderr.String()), code
73}
74
75// done finishes a form action by exit code: back to the page on success,
76// the 404 page when the thing does not exist, and back to the page with
77// the message for anything else. A refusal is feedback on the page a
78// person was looking at, whether it is a merge gate, a permission they
79// lack, or a field they got wrong; only a thing that does not exist has
80// no page to go back to.
81func (s *Server) done(w http.ResponseWriter, r *http.Request, code int, msg string,
82 redirect func(http.ResponseWriter, *http.Request, string)) {
83 switch code {
84 case protocol.ExitOK:
85 redirect(w, r, "")
86 case protocol.ExitNotFound:
87 s.notFound(w, r)
88 default:
89 redirect(w, r, msg)
90 }
91}
92
93// runControlStdin is runControl for the handful of commands whose input
94// arrives on stdin: public keys, and review comment bodies. Stdin is
95// also where a secret goes when one is set through this path, since
96// argv is world-readable in /proc and the audit log keeps flag values.
97func (s *Server) runControlStdin(u store.User, argv []string, stdin string) (msg string, ok bool) {
98 msg, code := s.runControlStdinCode(u, argv, stdin)
99 return msg, code == protocol.ExitOK
100}
101
102func (s *Server) runControlStdinCode(u store.User, argv []string, stdin string) (msg string, code int) {
103 var stdout, stderr bytes.Buffer
104 ctx := &control.Ctx{
105 User: u,
106 Source: "web",
107 Scope: "full",
108 Store: s.st,
109 Cfg: s.cfg,
110 Stdin: strings.NewReader(stdin),
111 Stdout: &stdout,
112 Stderr: &stderr,
113 ViaAPI: true,
114 }
115 code = control.Dispatch(ctx, argv)
116 m := strings.TrimSpace(stderr.String())
117 if m == "" {
118 m = strings.TrimSpace(stdout.String())
119 }
120 return m, code
121}
122
123// runControlInto runs a command in JSON mode and decodes its data into
124// target. Read handlers use it so the web renders exactly what the CLI
125// and the API return, rather than reaching past the registry into git.
126func (s *Server) runControlInto(u store.User, argv []string, target any) (msg string, ok bool) {
127 code, msg := s.dispatchInto(u, argv, target)
128 return msg, code == protocol.ExitOK
129}
130
131// runControlIntoCode is runControlInto for handlers that have to tell
132// "no such thing" from "that failed": a profile page 404s on the first
133// and errors on the second.
134func (s *Server) runControlIntoCode(u store.User, argv []string, target any) (code int, msg string) {
135 return s.dispatchInto(u, argv, target)
136}
137
138func (s *Server) dispatchInto(u store.User, argv []string, target any) (int, string) {
139 return s.dispatchIntoStdin(u, argv, "", target)
140}
141
142// dispatchIntoStdin is dispatchInto with a body on stdin, decoding the
143// command's named payload rather than a map (#126).
144func (s *Server) dispatchIntoStdin(u store.User, argv []string, stdin string, target any) (int, string) {
145 var stdout, stderr bytes.Buffer
146 ctx := &control.Ctx{
147 User: u,
148 Source: "web",
149 Scope: "full",
150 Store: s.st,
151 Cfg: s.cfg,
152 Stdin: strings.NewReader(stdin),
153 Stdout: &stdout,
154 Stderr: &stderr,
155 JSON: true,
156 ViaAPI: true,
157 }
158 code := control.Dispatch(ctx, argv)
159 var env struct {
160 Data json.RawMessage `json:"data"`
161 Error string `json:"error"`
162 }
163 json.Unmarshal(stdout.Bytes(), &env)
164 if code != protocol.ExitOK {
165 m := env.Error
166 if m == "" {
167 m = strings.TrimSpace(stderr.String())
168 }
169 return code, m
170 }
171 if len(env.Data) > 0 {
172 if err := json.Unmarshal(env.Data, target); err != nil {
173 return protocol.ExitFailure, "unreadable response"
174 }
175 }
176 return protocol.ExitOK, ""
177}
178
179// dispatchJSON runs a command in JSON mode with stdin and returns its exit
180// code and, on failure, the message. In JSON mode a failure is an envelope
181// carrying the message rather than stderr text, so both paths are read
182// from the same envelope. A handler that wants the payload uses
183// runControlInto, which decodes into the command's own type instead of a
184// map nothing type-checks.
185func (s *Server) dispatchJSON(u store.User, argv []string, stdin string) (code int, msg string) {
186 var stdout, stderr bytes.Buffer
187 ctx := &control.Ctx{
188 User: u,
189 Source: "web",
190 Scope: "full",
191 Store: s.st,
192 Cfg: s.cfg,
193 Stdin: strings.NewReader(stdin),
194 Stdout: &stdout,
195 Stderr: &stderr,
196 JSON: true,
197 ViaAPI: true,
198 }
199 code = control.Dispatch(ctx, argv)
200 var env struct {
201 Error string `json:"error"`
202 }
203 json.Unmarshal(stdout.Bytes(), &env)
204 if code != protocol.ExitOK {
205 m := env.Error
206 if m == "" {
207 m = strings.TrimSpace(stderr.String())
208 }
209 if m == "" {
210 m = "the command failed"
211 }
212 return code, m
213 }
214 return code, ""
215}
216
217// authorNames maps commit author addresses to account names for one
218// request. A commit carries whatever name git was configured with; when
219// the address is a verified address here, the account's own name is the
220// truthful one to show, and it links somewhere.
221type authorNames struct {
222 st *store.Store
223 cache map[string]string
224}
225
226func (s *Server) authorNames() *authorNames {
227 return &authorNames{st: s.st, cache: map[string]string{}}
228}
229
230// name returns the account name for an address, or the commit's own
231// author name when no account has verified it.
232func (a *authorNames) name(email, fallback string) string {
233 if email == "" {
234 return fallback
235 }
236 if got, ok := a.cache[email]; ok {
237 if got == "" {
238 return fallback
239 }
240 return got
241 }
242 name, _ := a.st.UsernameByVerifiedEmail(email)
243 a.cache[email] = name
244 if name == "" {
245 return fallback
246 }
247 return name
248}
249
250// account returns the account name behind an address, if any, so callers
251// can link the displayed name to a profile.
252func (a *authorNames) account(email string) (string, bool) {
253 if email == "" {
254 return "", false
255 }
256 if got, ok := a.cache[email]; ok {
257 return got, got != ""
258 }
259 name, _ := a.st.UsernameByVerifiedEmail(email)
260 a.cache[email] = name
261 return name, name != ""
262}
263
264// namedCommit is a listing commit plus the account behind its author
265// address, when there is one, so the name can link to a profile.
266type namedCommit struct {
267 gitutil.EntryCommit
268 User string
269}
270
271// namedCommits rewrites listing authors to account names where the
272// address is verified here.
273func (s *Server) namedCommits(m map[string]gitutil.EntryCommit) map[string]namedCommit {
274 names := s.authorNames()
275 out := make(map[string]namedCommit, len(m))
276 for k, c := range m {
277 user, _ := names.account(c.Email)
278 c.Author = names.name(c.Email, c.Author)
279 out[k] = namedCommit{EntryCommit: c, User: user}
280 }
281 return out
282}
283
284// namedTip does the same for the single commit above a tree listing.
285func (s *Server) namedTip(c gitutil.EntryCommit) namedCommit {
286 names := s.authorNames()
287 user, _ := names.account(c.Email)
288 c.Author = names.name(c.Email, c.Author)
289 return namedCommit{EntryCommit: c, User: user}
290}
291
292// webViewer is the account behind a page request, or the zero user when
293// the instance serves the web without accounts.
294func (s *Server) webViewer(r *http.Request) store.User {
295 if s.cfg.Web.Mode != "accounts" {
296 return store.User{}
297 }
298 return s.viewer(r)
299}