internal/httpd/control.go

a715db2f2843215b5324bf4fec93c67bb4b1ccac
gitbay/internal/httpd/control.go

215 lines · 6160 bytes

  1package httpd
  2
  3import (
  4	"bytes"
  5	"encoding/json"
  6	"strings"
  7
  8	"gitbay.org/gitbay/internal/control"
  9	"gitbay.org/gitbay/internal/gitutil"
 10	"gitbay.org/gitbay/internal/protocol"
 11	"gitbay.org/gitbay/internal/store"
 12)
 13
 14// runControl executes a control command as the browser session's user,
 15// through the same registry the CLI and the JSON API reach. Web writes
 16// never reimplement command logic — merge gates, review rules, and audit
 17// entries stay in one place — so the surfaces cannot drift apart.
 18//
 19// ViaAPI is set, which refuses SSHOnly commands: anything whose input is a
 20// credential (secrets, mirror tokens, session minting) stays on SSH.
 21func (s *Server) runControl(u store.User, argv []string) (out string, msg string, ok bool) {
 22	var stdout, stderr bytes.Buffer
 23	ctx := &control.Ctx{
 24		User:   u,
 25		Source: "web",
 26		Scope:  "full",
 27		Store:  s.st,
 28		Cfg:    s.cfg,
 29		Stdin:  strings.NewReader(""),
 30		Stdout: &stdout,
 31		Stderr: &stderr,
 32		ViaAPI: true,
 33	}
 34	code := control.Dispatch(ctx, argv)
 35	m := strings.TrimSpace(stderr.String())
 36	if m == "" {
 37		m = strings.TrimSpace(stdout.String())
 38	}
 39	return stdout.String(), m, code == protocol.ExitOK
 40}
 41
 42// runControlStdin is runControl for the handful of commands whose input
 43// arrives on stdin. Public keys are the only such input the web accepts:
 44// they are not secret, and pasting one into a browser is how people who
 45// have not set up the CLI get their first key registered. Secrets, tokens
 46// and mirror credentials remain SSHOnly and are refused by the dispatcher.
 47func (s *Server) runControlStdin(u store.User, argv []string, stdin string) (msg string, ok bool) {
 48	var stdout, stderr bytes.Buffer
 49	ctx := &control.Ctx{
 50		User:   u,
 51		Source: "web",
 52		Scope:  "full",
 53		Store:  s.st,
 54		Cfg:    s.cfg,
 55		Stdin:  strings.NewReader(stdin),
 56		Stdout: &stdout,
 57		Stderr: &stderr,
 58		ViaAPI: true,
 59	}
 60	code := control.Dispatch(ctx, argv)
 61	m := strings.TrimSpace(stderr.String())
 62	if m == "" {
 63		m = strings.TrimSpace(stdout.String())
 64	}
 65	return m, code == protocol.ExitOK
 66}
 67
 68// runControlInto runs a command in JSON mode and decodes its data into
 69// target. Read handlers use it so the web renders exactly what the CLI
 70// and the API return, rather than reaching past the registry into git.
 71func (s *Server) runControlInto(u store.User, argv []string, target any) (msg string, ok bool) {
 72	var stdout, stderr bytes.Buffer
 73	ctx := &control.Ctx{
 74		User:   u,
 75		Source: "web",
 76		Scope:  "full",
 77		Store:  s.st,
 78		Cfg:    s.cfg,
 79		Stdin:  strings.NewReader(""),
 80		Stdout: &stdout,
 81		Stderr: &stderr,
 82		JSON:   true,
 83		ViaAPI: true,
 84	}
 85	code := control.Dispatch(ctx, argv)
 86	var env struct {
 87		Data  json.RawMessage `json:"data"`
 88		Error string          `json:"error"`
 89	}
 90	json.Unmarshal(stdout.Bytes(), &env)
 91	if code != protocol.ExitOK {
 92		m := env.Error
 93		if m == "" {
 94			m = strings.TrimSpace(stderr.String())
 95		}
 96		return m, false
 97	}
 98	if len(env.Data) > 0 {
 99		if err := json.Unmarshal(env.Data, target); err != nil {
100			return "unreadable response", false
101		}
102	}
103	return "", true
104}
105
106// runControlJSON runs a command in JSON mode and returns its data object.
107// In JSON mode a failure is an envelope carrying the message rather than
108// stderr text, so both paths are read from the same envelope.
109func (s *Server) runControlJSON(u store.User, argv []string) (data map[string]any, msg string, ok bool) {
110	var stdout, stderr bytes.Buffer
111	ctx := &control.Ctx{
112		User:   u,
113		Source: "web",
114		Scope:  "full",
115		Store:  s.st,
116		Cfg:    s.cfg,
117		Stdin:  strings.NewReader(""),
118		Stdout: &stdout,
119		Stderr: &stderr,
120		JSON:   true,
121		ViaAPI: true,
122	}
123	code := control.Dispatch(ctx, argv)
124	var env struct {
125		Data  map[string]any `json:"data"`
126		Error string         `json:"error"`
127	}
128	json.Unmarshal(stdout.Bytes(), &env)
129	if code != protocol.ExitOK {
130		m := env.Error
131		if m == "" {
132			m = strings.TrimSpace(stderr.String())
133		}
134		if m == "" {
135			m = "the command failed"
136		}
137		return nil, m, false
138	}
139	return env.Data, "", true
140}
141
142// authorNames maps commit author addresses to account names for one
143// request. A commit carries whatever name git was configured with; when
144// the address is a verified address here, the account's own name is the
145// truthful one to show, and it links somewhere.
146type authorNames struct {
147	st    *store.Store
148	cache map[string]string
149}
150
151func (s *Server) authorNames() *authorNames {
152	return &authorNames{st: s.st, cache: map[string]string{}}
153}
154
155// name returns the account name for an address, or the commit's own
156// author name when no account has verified it.
157func (a *authorNames) name(email, fallback string) string {
158	if email == "" {
159		return fallback
160	}
161	if got, ok := a.cache[email]; ok {
162		if got == "" {
163			return fallback
164		}
165		return got
166	}
167	name, _ := a.st.UsernameByVerifiedEmail(email)
168	a.cache[email] = name
169	if name == "" {
170		return fallback
171	}
172	return name
173}
174
175// account returns the account name behind an address, if any, so callers
176// can link the displayed name to a profile.
177func (a *authorNames) account(email string) (string, bool) {
178	if email == "" {
179		return "", false
180	}
181	if got, ok := a.cache[email]; ok {
182		return got, got != ""
183	}
184	name, _ := a.st.UsernameByVerifiedEmail(email)
185	a.cache[email] = name
186	return name, name != ""
187}
188
189// namedCommit is a listing commit plus the account behind its author
190// address, when there is one, so the name can link to a profile.
191type namedCommit struct {
192	gitutil.EntryCommit
193	User string
194}
195
196// namedCommits rewrites listing authors to account names where the
197// address is verified here.
198func (s *Server) namedCommits(m map[string]gitutil.EntryCommit) map[string]namedCommit {
199	names := s.authorNames()
200	out := make(map[string]namedCommit, len(m))
201	for k, c := range m {
202		user, _ := names.account(c.Email)
203		c.Author = names.name(c.Email, c.Author)
204		out[k] = namedCommit{EntryCommit: c, User: user}
205	}
206	return out
207}
208
209// namedTip does the same for the single commit above a tree listing.
210func (s *Server) namedTip(c gitutil.EntryCommit) namedCommit {
211	names := s.authorNames()
212	user, _ := names.account(c.Email)
213	c.Author = names.name(c.Email, c.Author)
214	return namedCommit{EntryCommit: c, User: user}
215}