Commit a582e3d398

a582e3d39894297342158889a5765630b4704e9d

parent: eed104c810

Verified · cmc

cmc <hello@cleberg.net> · 2026-09-23 05:48 UTC

sshd, api: end a command when its reader goes away

Ref #250

Layout: unified · split

cmd/gitbayd/system.go +1 −1
@@ -93,7 +93,7 @@ func shellCmd() *cobra.Command {
93 fmt.Fprintf(os.Stderr, "gitbay control plane: interactive shells are not available.\nTry: ssh <host> help\n") 93 fmt.Fprintf(os.Stderr, "gitbay control plane: interactive shells are not available.\nTry: ssh <host> help\n")
94 os.Exit(protocol.ExitUsage) 94 os.Exit(protocol.ExitUsage)
95 } 95 }
96 code := sshd.Exec(cfg, st, user, key.Scope, key.Fingerprint, cmdline, os.Stdin, os.Stdout, os.Stderr) 96 code := sshd.Exec(cfg, st, user, key.Scope, key.Fingerprint, cmdline, os.Stdin, os.Stdout, os.Stderr, nil)
97 st.Close() 97 st.Close()
98 os.Exit(code) 98 os.Exit(code)
99 return nil 99 return nil
internal/httpd/api.go +1
@@ -73,6 +73,7 @@ func (s *Server) apiCmd(w http.ResponseWriter, r *http.Request) {
73 JSON: true, 73 JSON: true,
74 ViaAPI: true, 74 ViaAPI: true,
75 ReadOnly: scope == "read", 75 ReadOnly: scope == "read",
76 Done: r.Context().Done(),
76 } 77 }
77 code := control.Dispatch(ctx, req.Argv) 78 code := control.Dispatch(ctx, req.Argv)
78 79
internal/httpd/apiread.go +1
@@ -62,6 +62,7 @@ func (s *Server) apiRead(w http.ResponseWriter, r *http.Request) {
62 JSON: true, 62 JSON: true,
63 ViaAPI: true, 63 ViaAPI: true,
64 ReadOnly: true, 64 ReadOnly: true,
65 Done: r.Context().Done(),
65 } 66 }
66 code := control.Dispatch(ctx, argv) 67 code := control.Dispatch(ctx, argv)
67 68
internal/sshd/sshd.go +15 −4
@@ -238,7 +238,17 @@ func (s *Server) handleSession(sconn *ssh.ServerConn, ch ssh.Channel, reqs <-cha
238 continue 238 continue
239 } 239 }
240 req.Reply(true, nil) 240 req.Reply(true, nil)
241 code := s.runExec(sconn, ch, payload.Command) 241 // x/crypto closes reqs when the client closes the channel. That
242 // is how a follow learns nobody is reading: the CLI's shared
243 // connection outlives a Ctrl-C, the channel does not.
244 done := make(chan struct{})
245 go func() {
246 for r := range reqs {
247 r.Reply(false, nil)
248 }
249 close(done)
250 }()
251 code := s.runExec(sconn, ch, payload.Command, done)
242 sendExit(ch, code) 252 sendExit(ch, code)
243 return 253 return
244 case "shell": 254 case "shell":
@@ -260,7 +270,7 @@ func sendExit(ch ssh.Channel, code int) {
260 ch.SendRequest("exit-status", false, ssh.Marshal(&msg)) 270 ch.SendRequest("exit-status", false, ssh.Marshal(&msg))
261} 271}
262 272
263func (s *Server) runExec(sconn *ssh.ServerConn, ch ssh.Channel, cmdline string) int { 273func (s *Server) runExec(sconn *ssh.ServerConn, ch ssh.Channel, cmdline string, done <-chan struct{}) int {
264 ext := sconn.Permissions.Extensions 274 ext := sconn.Permissions.Extensions
265 if blob := ext["anon-key"]; blob != "" { 275 if blob := ext["anon-key"]; blob != "" {
266 return s.runAnonymous(ch, blob, cmdline) 276 return s.runAnonymous(ch, blob, cmdline)
@@ -273,7 +283,7 @@ func (s *Server) runExec(sconn *ssh.ServerConn, ch ssh.Channel, cmdline string)
273 return protocol.ExitDenied 283 return protocol.ExitDenied
274 } 284 }
275 _ = s.st.TouchSSHKey(keyID) 285 _ = s.st.TouchSSHKey(keyID)
276 return Exec(s.cfg, s.st, user, ext["scope"], ext["key-fp"], cmdline, ch, ch, ch.Stderr()) 286 return Exec(s.cfg, s.st, user, ext["scope"], ext["key-fp"], cmdline, ch, ch, ch.Stderr(), done)
277} 287}
278 288
279// runAnonymous handles a session from an unregistered key: the register 289// runAnonymous handles a session from an unregistered key: the register
@@ -304,7 +314,7 @@ func (s *Server) runAnonymous(ch ssh.Channel, keyB64, cmdline string) int {
304// single dispatch path shared by the embedded listener and the system-sshd 314// single dispatch path shared by the embedded listener and the system-sshd
305// forced command (gitbayd shell). 315// forced command (gitbayd shell).
306func Exec(cfg config.Config, st *store.Store, user store.User, scope, source, cmdline string, 316func Exec(cfg config.Config, st *store.Store, user store.User, scope, source, cmdline string,
307 stdin io.Reader, stdout, stderr io.Writer) int { 317 stdin io.Reader, stdout, stderr io.Writer, done <-chan struct{}) int {
308 if user.Disabled { 318 if user.Disabled {
309 fmt.Fprintln(stderr, "this account is disabled; contact the instance admin") 319 fmt.Fprintln(stderr, "this account is disabled; contact the instance admin")
310 return protocol.ExitDenied 320 return protocol.ExitDenied
@@ -341,6 +351,7 @@ func Exec(cfg config.Config, st *store.Store, user store.User, scope, source, cm
341 Stdin: stdin, 351 Stdin: stdin,
342 Stdout: stdout, 352 Stdout: stdout,
343 Stderr: stderr, 353 Stderr: stderr,
354 Done: done,
344 } 355 }
345 return control.Dispatch(ctx, argv) 356 return control.Dispatch(ctx, argv)
346} 357}