vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Run the exec verbs on a session, not an SSH channel
5ff8dfc964d8e05c5f5b7a8ec5e5f229178e5e4c · Keenan Nemetz · 16d ago · parent 22e290b0
Run the exec verbs on a session, not an SSH channel Every handler behind the exec verb took an *xssh.Channel, and the two that answer the exec request took the request too. That tied twenty functions to the SSH transport, but the front door needs the same verbs, with the same access rules, from a forced command that reaches the service over a local socket. A Session is now what a verb runs on: stdin, stdout, a separate error stream, Accept, CloseWrite, Exit, and Close. sshSession is the only implementation that knows what an SSH channel is, so the handlers no longer name a transport and a second one cannot become a second copy of the access rules. No behaviour change. The handlers keep their names and bodies; the refusal and exit-status paths move behind the interface, which is also what removes the standalone exit helper. A new test drives vole-whoami and git-upload-pack over SSH and over a session with no channel behind it, comparing stdout, stderr, and the exit status of both.
server/ssh/session_test.go +137
| @@ -0,0 +1,137 @@ | |||
| 1 | package ssh | ||
| 2 | |||
| 3 | // The exec verbs run on a Session, not on an SSH channel. These tests are what | ||
| 4 | // hold that seam to its promise: the same command, the same bytes on both | ||
| 5 | // streams, and the same exit status with no SSH connection in the picture. | ||
| 6 | |||
| 7 | import ( | ||
| 8 | "bytes" | ||
| 9 | "errors" | ||
| 10 | "io" | ||
| 11 | "strings" | ||
| 12 | "testing" | ||
| 13 | |||
| 14 | xssh "golang.org/x/crypto/ssh" | ||
| 15 | ) | ||
| 16 | |||
| 17 | // localSession is a Session with nothing behind it. It is the shape a second | ||
| 18 | // transport will take, kept here so a handler cannot quietly grow a dependency | ||
| 19 | // on a channel it will not have. | ||
| 20 | type localSession struct { | ||
| 21 | stdin bytes.Buffer | ||
| 22 | stdout bytes.Buffer | ||
| 23 | stderr bytes.Buffer | ||
| 24 | |||
| 25 | accepted bool | ||
| 26 | exited bool | ||
| 27 | code int | ||
| 28 | closed bool | ||
| 29 | } | ||
| 30 | |||
| 31 | func (s *localSession) Read(p []byte) (int, error) { return s.stdin.Read(p) } | ||
| 32 | func (s *localSession) Write(p []byte) (int, error) { return s.stdout.Write(p) } | ||
| 33 | func (s *localSession) Stderr() io.Writer { return &s.stderr } | ||
| 34 | func (s *localSession) CloseWrite() error { return nil } | ||
| 35 | func (s *localSession) Close() error { s.closed = true; return nil } | ||
| 36 | |||
| 37 | func (s *localSession) Accept() error { s.accepted = true; return nil } | ||
| 38 | |||
| 39 | func (s *localSession) Exit(code int) { | ||
| 40 | s.exited, s.code = true, code | ||
| 41 | } | ||
| 42 | |||
| 43 | // TestVerbsRunOnALocalSession drives the same two commands over SSH and over a | ||
| 44 | // Session that is not an SSH channel, then compares everything the client would | ||
| 45 | // see. It is the equivalence test the front door needs, at the size the seam has | ||
| 46 | // today: one read-only identity query and one git read, so both the in-process | ||
| 47 | // and the subprocess path are exercised. | ||
| 48 | func TestVerbsRunOnALocalSession(t *testing.T) { | ||
| 49 | t.Parallel() | ||
| 50 | h := newHarness(t) | ||
| 51 | work := workRepo(t) | ||
| 52 | if _, errs, err := h.git(work, h.keyPath, "push", h.url(), "main:refs/heads/main"); err != nil { | ||
| 53 | t.Fatalf("seed push failed: %v\n%s", err, errs) | ||
| 54 | } | ||
| 55 | |||
| 56 | for _, command := range []string{ | ||
| 57 | "vole-whoami", | ||
| 58 | "git-upload-pack 'demo.git'", | ||
| 59 | } { | ||
| 60 | t.Run(command, func(t *testing.T) { | ||
| 61 | // The SSH answer, which is the behaviour that must not change. | ||
| 62 | client := h.dial(t, h.keyPath) | ||
| 63 | sess, err := client.NewSession() | ||
| 64 | if err != nil { | ||
| 65 | t.Fatalf("new session: %v", err) | ||
| 66 | } | ||
| 67 | var remoteErr bytes.Buffer | ||
| 68 | sess.Stderr = &remoteErr | ||
| 69 | out, err := sess.Output(command) | ||
| 70 | sess.Close() | ||
| 71 | code := 0 | ||
| 72 | if err != nil { | ||
| 73 | var exit *xssh.ExitError | ||
| 74 | if !errors.As(err, &exit) { | ||
| 75 | t.Fatalf("%q over SSH: %v", command, err) | ||
| 76 | } | ||
| 77 | code = exit.ExitStatus() | ||
| 78 | } | ||
| 79 | |||
| 80 | // The same command with no SSH channel underneath it. | ||
| 81 | local := &localSession{} | ||
| 82 | h.srv.exec(local, command, "writer", "", "127.0.0.1:1") | ||
| 83 | |||
| 84 | if !local.accepted { | ||
| 85 | t.Errorf("%q: the exec request was never accepted", command) | ||
| 86 | } | ||
| 87 | if !local.exited { | ||
| 88 | t.Errorf("%q: no exit status was reported", command) | ||
| 89 | } | ||
| 90 | if got, want := local.stdout.String(), string(out); got != want { | ||
| 91 | t.Errorf("%q: local stdout differs from SSH\n local: %q\n ssh: %q", command, got, want) | ||
| 92 | } | ||
| 93 | // git-upload-pack answers a client that closes stdin without a want | ||
| 94 | // list with an error and status 128. That is the same on both paths, | ||
| 95 | // which is the point: the local transport must not turn a hung-up | ||
| 96 | // client into a different answer. | ||
| 97 | if got, want := local.stderr.String(), remoteErr.String(); got != want { | ||
| 98 | t.Errorf("%q: local stderr differs from SSH\n local: %q\n ssh: %q", command, got, want) | ||
| 99 | } | ||
| 100 | if local.code != code { | ||
| 101 | t.Errorf("%q: local exit %d, ssh exit %d", command, local.code, code) | ||
| 102 | } | ||
| 103 | }) | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | // TestRefusalRunsOnALocalSession covers the path every verb shares: a command | ||
| 108 | // that is refused is answered on the session with a status, and nothing runs. | ||
| 109 | func TestRefusalRunsOnALocalSession(t *testing.T) { | ||
| 110 | t.Parallel() | ||
| 111 | h := newHarness(t) | ||
| 112 | local := &localSession{} | ||
| 113 | h.srv.exec(local, "cat /etc/passwd", "writer", "", "127.0.0.1:1") | ||
| 114 | |||
| 115 | if !local.accepted || !local.exited { | ||
| 116 | t.Fatalf("accepted=%v exited=%v; a refusal is still answered on the session", local.accepted, local.exited) | ||
| 117 | } | ||
| 118 | if local.code != 1 { | ||
| 119 | t.Errorf("exit %d, want 1", local.code) | ||
| 120 | } | ||
| 121 | if strings.Contains(local.stdout.String(), "root:") { | ||
| 122 | t.Errorf("the refused command ran: %q", local.stdout.String()) | ||
| 123 | } | ||
| 124 | if !strings.Contains(local.stdout.String(), "not permitted") { | ||
| 125 | t.Errorf("the refusal does not say why: %q", local.stdout.String()) | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | // TestSessionWithoutRequestAccepts covers the session the connection layer | ||
| 130 | // builds only to report that it refused a shell: there is no exec request to | ||
| 131 | // answer, and that is not an error. | ||
| 132 | func TestSessionWithoutRequestAccepts(t *testing.T) { | ||
| 133 | t.Parallel() | ||
| 134 | if err := (&sshSession{}).Accept(); err != nil { | ||
| 135 | t.Fatalf("Accept with no request = %v, want nil", err) | ||
| 136 | } | ||
| 137 | } | ||