internal/httpd/web.go
650 lines · 17024 bytes
1package httpd
2
3import (
4 "bytes"
5 "fmt"
6
7 "gitbay.org/gitbay/internal/policy"
8 "html/template"
9 "net/http"
10 "path"
11 "strconv"
12 "strings"
13 "time"
14
15 "github.com/alecthomas/chroma/v2/formatters/html"
16 "github.com/alecthomas/chroma/v2/lexers"
17 "github.com/alecthomas/chroma/v2/styles"
18 "github.com/microcosm-cc/bluemonday"
19 "github.com/niklasfasching/go-org/org"
20 "github.com/yuin/goldmark"
21
22 "gitbay.org/gitbay/internal/control"
23 "gitbay.org/gitbay/internal/gitutil"
24 "gitbay.org/gitbay/internal/sig"
25 "gitbay.org/gitbay/internal/store"
26 "gitbay.org/gitbay/internal/web"
27)
28
29const maxRenderBytes = 1 << 20 // largest blob rendered inline
30
31func (s *Server) render(w http.ResponseWriter, page string, data any) {
32 var buf bytes.Buffer
33 if err := web.Render(&buf, page, data); err != nil {
34 http.Error(w, "template error: "+err.Error(), http.StatusInternalServerError)
35 return
36 }
37 w.Header().Set("Content-Type", "text/html; charset=utf-8")
38 buf.WriteTo(w)
39}
40
41func (s *Server) siteName() string {
42 h := strings.TrimPrefix(strings.TrimPrefix(s.cfg.Server.SiteURL, "https://"), "http://")
43 return strings.TrimSuffix(h, "/")
44}
45
46func (s *Server) stylesheet(w http.ResponseWriter, r *http.Request) {
47 w.Header().Set("Content-Type", "text/css; charset=utf-8")
48 w.Write(web.StyleCSS)
49}
50
51func (s *Server) index(w http.ResponseWriter, r *http.Request) {
52 repos, err := s.st.ListPublicRepos()
53 if err != nil {
54 http.Error(w, "internal error", http.StatusInternalServerError)
55 return
56 }
57 var viewer store.User
58 var mine []store.Repo
59 if s.cfg.Web.Mode == "accounts" {
60 if viewer = s.viewer(r); viewer.ID != 0 {
61 all, err := s.st.ListReposForUser(viewer.ID)
62 if err == nil {
63 for _, rp := range all {
64 if rp.Visibility == "private" {
65 mine = append(mine, rp)
66 }
67 }
68 }
69 }
70 }
71 s.render(w, "index.html", struct {
72 Site string
73 Viewer string
74 Repos []store.Repo
75 Mine []store.Repo
76 }{s.siteName(), viewer.Username, repos, mine})
77}
78
79// repoPage is the shared context for repo-scoped pages.
80type repoPage struct {
81 Site string
82 Viewer string
83 Repo store.Repo
84 Ref string
85 CloneURL string
86 Dir string
87}
88
89// repoFor resolves the repo for a web request; false means 404 was sent.
90// Anonymous visitors see public repos only; in accounts mode a logged-in
91// viewer additionally sees repos their grants allow. Private and missing
92// repos are indistinguishable either way.
93func (s *Server) repoFor(w http.ResponseWriter, r *http.Request, ref string) (repoPage, bool) {
94 var repo store.Repo
95 var viewer store.User
96 if s.cfg.Web.Mode == "accounts" {
97 viewer = s.viewer(r)
98 }
99 repo, err := s.st.RepoByPath(r.PathValue("owner") + "/" + r.PathValue("repo"))
100 ok := err == nil
101 if ok {
102 grant := ""
103 if viewer.ID != 0 {
104 grant, _ = s.st.AccessRole(repo.ID, viewer.ID)
105 }
106 ok = policyCanRead(viewer, repo, grant)
107 }
108 if !ok {
109 http.NotFound(w, r)
110 return repoPage{}, false
111 }
112 if ref == "" {
113 ref = repo.DefaultBranch
114 }
115 return repoPage{
116 Site: s.siteName(),
117 Viewer: viewer.Username,
118 Repo: repo,
119 Ref: ref,
120 CloneURL: s.cfg.Server.SiteURL + "/" + repo.Path() + ".git",
121 Dir: control.RepoDir(s.cfg.Server.Root, repo.OwnerName, repo.Name),
122 }, true
123}
124
125type crumb struct {
126 Name string
127 URL string
128}
129
130func crumbs(p repoPage, kind, filePath string) []crumb {
131 var cs []crumb
132 base := "/" + p.Repo.Path() + "/" + kind + "/" + p.Ref + "/"
133 acc := ""
134 for _, part := range strings.Split(filePath, "/") {
135 if part == "" {
136 continue
137 }
138 acc = path.Join(acc, part)
139 cs = append(cs, crumb{Name: part, URL: base + acc})
140 }
141 return cs
142}
143
144// ownerPage renders /{owner} for users and orgs: the repositories the
145// viewer may see, org membership either direction. Owner names are not
146// secret (they are on every commit); repository visibility rules hold.
147func (s *Server) ownerPage(w http.ResponseWriter, r *http.Request) {
148 name := r.PathValue("owner")
149 var viewer store.User
150 if s.cfg.Web.Mode == "accounts" {
151 viewer = s.viewer(r)
152 }
153
154 kind := "user"
155 var ownerID int64
156 var members []store.OrgMember
157 var orgs []store.OrgMember
158 if u, err := s.st.UserByUsername(name); err == nil {
159 ownerID = u.ID
160 orgs, _ = s.st.ListOrgsForUser(u.ID)
161 } else if o, err := s.st.OrgByName(name); err == nil {
162 kind, ownerID = "org", o.ID
163 members, _ = s.st.OrgMembers(o.ID)
164 } else {
165 http.NotFound(w, r)
166 return
167 }
168
169 all, err := s.st.ListReposForOwner(kind, ownerID)
170 if err != nil {
171 http.Error(w, "internal error", http.StatusInternalServerError)
172 return
173 }
174 var visible []store.Repo
175 for _, repo := range all {
176 grant := ""
177 if viewer.ID != 0 {
178 grant, _ = s.st.AccessRole(repo.ID, viewer.ID)
179 }
180 if policy.CanRead(viewer, repo, grant) {
181 visible = append(visible, repo)
182 }
183 }
184 s.render(w, "owner.html", struct {
185 Site string
186 Viewer string
187 Owner string
188 Kind string
189 Repos []store.Repo
190 Members []store.OrgMember
191 Orgs []store.OrgMember
192 }{s.siteName(), viewer.Username, name, kind, visible, members, orgs})
193}
194
195func (s *Server) repoHome(w http.ResponseWriter, r *http.Request) {
196 p, ok := s.repoFor(w, r, "")
197 if !ok {
198 return
199 }
200 s.renderTree(w, r, p, "")
201}
202
203func (s *Server) tree(w http.ResponseWriter, r *http.Request) {
204 p, ok := s.repoFor(w, r, r.PathValue("ref"))
205 if !ok {
206 return
207 }
208 s.renderTree(w, r, p, strings.Trim(r.PathValue("path"), "/"))
209}
210
211func (s *Server) renderTree(w http.ResponseWriter, r *http.Request, p repoPage, dirPath string) {
212 if _, err := gitutil.ResolveRef(p.Dir, p.Ref); err != nil {
213 // Empty repo: render the page with no entries rather than 404.
214 s.render(w, "tree.html", struct {
215 repoPage
216 Crumbs []crumb
217 Prefix string
218 Entries []gitutil.TreeEntry
219 ReadmeHTML template.HTML
220 }{repoPage: p})
221 return
222 }
223 entries, err := gitutil.ListTree(p.Dir, p.Ref, dirPath)
224 if err != nil {
225 http.NotFound(w, r)
226 return
227 }
228 prefix := ""
229 if dirPath != "" {
230 prefix = dirPath + "/"
231 }
232
233 var readmeHTML template.HTML
234 if name := pickReadme(entries); name != "" {
235 if raw, err := gitutil.ReadBlob(p.Dir, p.Ref, prefix+name, maxRenderBytes); err == nil {
236 readmeHTML = renderReadme(name, raw)
237 }
238 }
239
240 s.render(w, "tree.html", struct {
241 repoPage
242 Crumbs []crumb
243 Prefix string
244 Entries []gitutil.TreeEntry
245 ReadmeHTML template.HTML
246 }{p, crumbs(p, "tree", dirPath), prefix, entries, readmeHTML})
247}
248
249func (s *Server) blob(w http.ResponseWriter, r *http.Request) {
250 p, ok := s.repoFor(w, r, r.PathValue("ref"))
251 if !ok {
252 return
253 }
254 filePath := strings.Trim(r.PathValue("path"), "/")
255 data, err := gitutil.ReadBlob(p.Dir, p.Ref, filePath, maxRenderBytes+1)
256 if err != nil {
257 http.NotFound(w, r)
258 return
259 }
260 binary := gitutil.IsBinary(data) || len(data) > maxRenderBytes
261
262 var codeHTML template.HTML
263 if !binary {
264 codeHTML = highlight(filePath, data)
265 }
266 cs := crumbs(p, "blob", filePath)
267 base := ""
268 if len(cs) > 0 {
269 base = cs[len(cs)-1].Name
270 cs = cs[:len(cs)-1]
271 }
272 s.render(w, "blob.html", struct {
273 repoPage
274 Crumbs []crumb
275 Base string
276 Path string
277 Binary bool
278 Size int
279 CodeHTML template.HTML
280 }{p, cs, base, filePath, binary, len(data), codeHTML})
281}
282
283func highlight(filePath string, data []byte) template.HTML {
284 lexer := lexers.Match(filePath)
285 if lexer == nil {
286 lexer = lexers.Fallback
287 }
288 style := styles.Get("friendly")
289 formatter := html.New(html.WithLineNumbers(true), html.LineNumbersInTable(false))
290 iterator, err := lexer.Tokenise(nil, string(data))
291 if err != nil {
292 return template.HTML("<pre>" + template.HTMLEscapeString(string(data)) + "</pre>")
293 }
294 var buf bytes.Buffer
295 if err := formatter.Format(&buf, style, iterator); err != nil {
296 return template.HTML("<pre>" + template.HTMLEscapeString(string(data)) + "</pre>")
297 }
298 return template.HTML(buf.String())
299}
300
301func (s *Server) raw(w http.ResponseWriter, r *http.Request) {
302 p, ok := s.repoFor(w, r, r.PathValue("ref"))
303 if !ok {
304 return
305 }
306 filePath := strings.Trim(r.PathValue("path"), "/")
307 data, err := gitutil.ReadBlob(p.Dir, p.Ref, filePath, s.cfg.Limits.MaxBlobBytes)
308 if err != nil {
309 http.NotFound(w, r)
310 return
311 }
312 // Serve inert: never let repo content execute in the forge's origin.
313 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
314 w.Header().Set("X-Content-Type-Options", "nosniff")
315 w.Write(data)
316}
317
318// readmeRank orders competing README files: richer renderers win.
319var readmeRank = map[string]int{".md": 1, ".markdown": 1, ".org": 2, ".html": 3, ".htm": 3}
320
321// pickReadme returns the best README-ish blob in a tree listing: any file
322// named "readme" or "readme.<ext>" (case-insensitive), preferring formats
323// we can render richly.
324func pickReadme(entries []gitutil.TreeEntry) string {
325 best, bestRank := "", 1<<30
326 for _, e := range entries {
327 if e.Type != "blob" {
328 continue
329 }
330 lower := strings.ToLower(e.Name)
331 if lower != "readme" && !strings.HasPrefix(lower, "readme.") {
332 continue
333 }
334 rank, ok := readmeRank[path.Ext(lower)]
335 if !ok {
336 rank = 10 // plaintext fallback
337 }
338 if rank < bestRank {
339 best, bestRank = e.Name, rank
340 }
341 }
342 return best
343}
344
345// ugcPolicy sanitizes rendered repo content before it enters the forge's
346// origin: markdown is already safe (goldmark drops raw HTML), but org-mode
347// output and repo-authored HTML are not.
348var ugcPolicy = bluemonday.UGCPolicy()
349
350// renderReadme renders a README by extension: markdown, org-mode, and
351// (sanitized) HTML richly; everything else as escaped plaintext.
352func renderReadme(name string, raw []byte) template.HTML {
353 plain := func() template.HTML {
354 return template.HTML("<pre>" + template.HTMLEscapeString(string(raw)) + "</pre>")
355 }
356 if gitutil.IsBinary(raw) {
357 return ""
358 }
359 switch path.Ext(strings.ToLower(name)) {
360 case ".md", ".markdown":
361 var buf bytes.Buffer
362 if goldmark.Convert(raw, &buf) != nil {
363 return plain()
364 }
365 return template.HTML(buf.String())
366 case ".org":
367 doc := org.New().Parse(bytes.NewReader(raw), name)
368 html, err := doc.Write(org.NewHTMLWriter())
369 if err != nil {
370 return plain()
371 }
372 return template.HTML(ugcPolicy.Sanitize(html))
373 case ".html", ".htm":
374 return template.HTML(ugcPolicy.Sanitize(string(raw)))
375 default:
376 return plain()
377 }
378}
379
380type diffLine struct {
381 Class string
382 Text string
383}
384
385func classifyDiff(patch string) []diffLine {
386 var lines []diffLine
387 for _, l := range strings.Split(patch, "\n") {
388 class := ""
389 switch {
390 case strings.HasPrefix(l, "+++"), strings.HasPrefix(l, "---"), strings.HasPrefix(l, "diff "), strings.HasPrefix(l, "index "):
391 class = "meta"
392 case strings.HasPrefix(l, "@@"):
393 class = "hunk"
394 case strings.HasPrefix(l, "+"):
395 class = "add"
396 case strings.HasPrefix(l, "-"):
397 class = "del"
398 }
399 lines = append(lines, diffLine{class, l})
400 }
401 return lines
402}
403
404type sigView struct {
405 State string
406 Signer string
407 Fingerprint string
408}
409
410func (s *Server) sigFor(repo store.Repo, dir, sha string) (sigView, *sig.Commit) {
411 raw, err := gitutil.ReadCommit(dir, sha)
412 if err != nil {
413 return sigView{State: "unsigned"}, nil
414 }
415 parsed, err := sig.ParseCommit(raw)
416 if err != nil {
417 return sigView{State: "unsigned"}, nil
418 }
419 res, err := control.VerifyCommitCached(s.st, repo, parsed, sha)
420 if err != nil {
421 return sigView{State: "unsigned"}, parsed
422 }
423 v := sigView{State: string(res.State), Fingerprint: res.KeyFingerprint}
424 if res.SignerUserID != 0 {
425 if u, err := s.st.UserByID(res.SignerUserID); err == nil {
426 v.Signer = u.Username
427 }
428 }
429 return v, parsed
430}
431
432func (s *Server) log(w http.ResponseWriter, r *http.Request) {
433 ref := r.PathValue("ref")
434 p, ok := s.repoFor(w, r, ref)
435 if !ok {
436 return
437 }
438 const pageSize = 50
439 shas, err := gitutil.RevList(p.Dir, p.Ref, pageSize+1)
440 if err != nil {
441 http.NotFound(w, r)
442 return
443 }
444 next := ""
445 if len(shas) > pageSize {
446 next = shas[pageSize]
447 shas = shas[:pageSize]
448 }
449 type row struct {
450 SHA, ShortSHA, Subject, AuthorName, AuthorEmail, Date string
451 Sig sigView
452 }
453 var rows []row
454 for _, sha := range shas {
455 v, parsed := s.sigFor(p.Repo, p.Dir, sha)
456 rw := row{SHA: sha, ShortSHA: sha[:10], Sig: v}
457 if parsed != nil {
458 rw.Subject = parsed.Subject
459 rw.AuthorName = parsed.AuthorName
460 rw.AuthorEmail = parsed.AuthorEmail
461 rw.Date = time.Unix(parsed.AuthorUnix, 0).UTC().Format("2006-01-02")
462 }
463 rows = append(rows, rw)
464 }
465 s.render(w, "log.html", struct {
466 repoPage
467 Commits []row
468 NextSHA string
469 }{p, rows, next})
470}
471
472func (s *Server) commit(w http.ResponseWriter, r *http.Request) {
473 p, ok := s.repoFor(w, r, "")
474 if !ok {
475 return
476 }
477 sha := r.PathValue("sha")
478 full, err := gitutil.ResolveRef(p.Dir, sha)
479 if err != nil {
480 http.NotFound(w, r)
481 return
482 }
483 v, parsed := s.sigFor(p.Repo, p.Dir, full)
484 if parsed == nil {
485 http.NotFound(w, r)
486 return
487 }
488 patch, _ := gitutil.ShowPatch(p.Dir, full, 4<<20)
489 lines := classifyDiff(patch)
490 committerEmail := ""
491 if parsed.CommitterEmail != parsed.AuthorEmail {
492 committerEmail = parsed.CommitterEmail
493 }
494 msg := ""
495 if i := bytes.Index(parsed.Payload, []byte("\n\n")); i >= 0 {
496 msg = string(parsed.Payload[i+2:])
497 }
498 s.render(w, "commit.html", struct {
499 repoPage
500 SHA, ShortSHA, AuthorName, AuthorEmail, CommitterEmail, Date, Message string
501 Sig sigView
502 DiffLines []diffLine
503 }{p, full, full[:10], parsed.AuthorName, parsed.AuthorEmail, committerEmail,
504 time.Unix(parsed.AuthorUnix, 0).UTC().Format(time.RFC3339), msg, v, lines})
505}
506
507func (s *Server) issues(w http.ResponseWriter, r *http.Request) {
508 p, ok := s.repoFor(w, r, "")
509 if !ok {
510 return
511 }
512 state := r.URL.Query().Get("state")
513 if state != "closed" && state != "all" {
514 state = "open"
515 }
516 issues, err := s.st.ListIssues(p.Repo.ID, state)
517 if err != nil {
518 http.Error(w, "internal error", http.StatusInternalServerError)
519 return
520 }
521 s.render(w, "issues.html", struct {
522 repoPage
523 State string
524 Issues []store.Issue
525 }{p, state, issues})
526}
527
528func (s *Server) issue(w http.ResponseWriter, r *http.Request) {
529 p, ok := s.repoFor(w, r, "")
530 if !ok {
531 return
532 }
533 n, err := strconv.ParseInt(r.PathValue("n"), 10, 64)
534 if err != nil {
535 http.NotFound(w, r)
536 return
537 }
538 iss, err := s.st.IssueByNumber(p.Repo.ID, n)
539 if err != nil {
540 http.NotFound(w, r)
541 return
542 }
543 comments, err := s.st.ListIssueComments(iss.ID)
544 if err != nil {
545 http.Error(w, "internal error", http.StatusInternalServerError)
546 return
547 }
548 s.render(w, "issue.html", struct {
549 repoPage
550 Issue store.Issue
551 Comments []store.IssueComment
552 }{p, iss, comments})
553}
554
555func (s *Server) mrs(w http.ResponseWriter, r *http.Request) {
556 p, ok := s.repoFor(w, r, "")
557 if !ok {
558 return
559 }
560 state := r.URL.Query().Get("state")
561 if state == "" {
562 state = "open"
563 }
564 valid := map[string]bool{"open": true, "merged": true, "closed": true, "source_gone": true, "all": true}
565 if !valid[state] {
566 state = "open"
567 }
568 mrs, err := s.st.ListMRs(p.Repo.ID, state)
569 if err != nil {
570 http.Error(w, "internal error", http.StatusInternalServerError)
571 return
572 }
573 s.render(w, "mrs.html", struct {
574 repoPage
575 State string
576 MRs []store.MR
577 }{p, state, mrs})
578}
579
580func (s *Server) mr(w http.ResponseWriter, r *http.Request) {
581 p, ok := s.repoFor(w, r, "")
582 if !ok {
583 return
584 }
585 n, err := strconv.ParseInt(r.PathValue("n"), 10, 64)
586 if err != nil {
587 http.NotFound(w, r)
588 return
589 }
590 m, err := s.st.MRByNumber(p.Repo.ID, n)
591 if err != nil {
592 http.NotFound(w, r)
593 return
594 }
595 comments, _ := s.st.ListMRComments(m.ID)
596 reviews, _ := s.st.ListMRReviews(m.ID)
597
598 headRef := fmt.Sprintf("refs/merge-requests/%d/head", m.Number)
599 var lines []diffLine
600 if base, err := gitutil.MergeBase(p.Dir, "refs/heads/"+m.TargetRef, headRef); err == nil {
601 if patch, err := gitutil.Diff(p.Dir, base, headRef, 4<<20); err == nil {
602 lines = classifyDiff(patch)
603 }
604 }
605 s.render(w, "mr.html", struct {
606 repoPage
607 MR store.MR
608 Comments []store.IssueComment
609 Reviews []store.MRReview
610 DiffLines []diffLine
611 }{p, m, comments, reviews, lines})
612}
613
614func (s *Server) refs(w http.ResponseWriter, r *http.Request) {
615 p, ok := s.repoFor(w, r, "")
616 if !ok {
617 return
618 }
619 branches, _ := gitutil.Refs(p.Dir, "heads")
620 tags, _ := gitutil.Refs(p.Dir, "tags")
621 s.render(w, "refs.html", struct {
622 repoPage
623 Branches, Tags []gitutil.Ref
624 }{p, branches, tags})
625}
626
627func (s *Server) archive(w http.ResponseWriter, r *http.Request) {
628 p, ok := s.repoFor(w, r, "")
629 if !ok {
630 return
631 }
632 file := r.PathValue("file")
633 ref, ok := strings.CutSuffix(file, ".tar.gz")
634 if !ok {
635 http.NotFound(w, r)
636 return
637 }
638 if _, err := gitutil.ResolveRef(p.Dir, ref); err != nil {
639 http.NotFound(w, r)
640 return
641 }
642 prefix := fmt.Sprintf("%s-%s", p.Repo.Name, ref)
643 w.Header().Set("Content-Type", "application/gzip")
644 w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", prefix+".tar.gz"))
645 gitutil.Archive(p.Dir, ref, prefix, w)
646}
647
648func policyCanRead(u store.User, repo store.Repo, grant string) bool {
649 return policy.CanRead(u, repo, grant)
650}