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.
|
5ff8dfc9
Keenan Nemetz
16d ago
|
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 | ||
|
c5f2c5d4
Keenan Nemetz
16d ago
|
17 | // memorySession is a Session with nothing behind it. It is the shape a second |
|
5ff8dfc9
Keenan Nemetz
16d ago
|
18 | // transport will take, kept here so a handler cannot quietly grow a dependency |
| 19 | // on a channel it will not have. | |
|
c5f2c5d4
Keenan Nemetz
16d ago
|
20 | type memorySession struct { |
|
5ff8dfc9
Keenan Nemetz
16d ago
|
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 | ||
|
c5f2c5d4
Keenan Nemetz
16d ago
|
31 | func (s *memorySession) Read(p []byte) (int, error) { return s.stdin.Read(p) } |
| 32 | func (s *memorySession) Write(p []byte) (int, error) { return s.stdout.Write(p) } | |
| 33 | func (s *memorySession) Stderr() io.Writer { return &s.stderr } | |
| 34 | func (s *memorySession) CloseWrite() error { return nil } | |
| 35 | func (s *memorySession) Close() error { s.closed = true; return nil } | |
|
5ff8dfc9
Keenan Nemetz
16d ago
|
36 | |
|
c5f2c5d4
Keenan Nemetz
16d ago
|
37 | func (s *memorySession) Accept() error { s.accepted = true; return nil } |
|
5ff8dfc9
Keenan Nemetz
16d ago
|
38 | |
|
c5f2c5d4
Keenan Nemetz
16d ago
|
39 | func (s *memorySession) Exit(code int) { |
|
5ff8dfc9
Keenan Nemetz
16d ago
|
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. | |
|
c5f2c5d4
Keenan Nemetz
16d ago
|
81 | local := &memorySession{} |
|
5ff8dfc9
Keenan Nemetz
16d ago
|
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) | |
|
c5f2c5d4
Keenan Nemetz
16d ago
|
112 | local := &memorySession{} |
|
5ff8dfc9
Keenan Nemetz
16d ago
|
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 | ||
|
5bc57be7
Keenan Nemetz
1d ago
|
129 | // TestGitRefusalGoesToStderr covers the stream a refusal belongs on. git reads |
| 130 | // the channel body as a pkt-line stream, so a sentence written there is the | |
| 131 | // first four bytes of a frame and git drops it as "protocol error: bad line | |
| 132 | // length character". Both git verbs answer on stderr instead, which the client | |
| 133 | // shows on its terminal rather than parsing it as protocol. | |
| 134 | func TestGitRefusalGoesToStderr(t *testing.T) { | |
| 135 | t.Parallel() | |
| 136 | h := newHarness(t) | |
| 137 | for _, tc := range []struct { | |
| 138 | name string | |
| 139 | command string | |
| 140 | want string | |
| 141 | }{ | |
| 142 | {"upload missing", "git-upload-pack 'nope.git'", `repository "nope" not found`}, | |
| 143 | {"receive missing", "git-receive-pack 'nope.git'", `repository "nope" not found`}, | |
| 144 | {"upload invalid path", "git-upload-pack '/../../etc.git'", "invalid repository path"}, | |
| 145 | {"receive malformed", "git-receive-pack '/unterminated", "malformed command"}, | |
| 146 | {"upload oversized", "git-upload-pack '" + strings.Repeat("x", maxCommandBytes) + "'", "command line is longer"}, | |
| 147 | } { | |
| 148 | t.Run(tc.name, func(t *testing.T) { | |
| 149 | local := &memorySession{} | |
| 150 | h.srv.exec(local, tc.command, "writer", "", "127.0.0.1:1") | |
| 151 | ||
| 152 | if !local.accepted || !local.exited { | |
| 153 | t.Fatalf("accepted=%v exited=%v; a refusal is still answered on the session", local.accepted, local.exited) | |
| 154 | } | |
| 155 | if local.code == 0 { | |
| 156 | t.Errorf("exit %d, want nonzero", local.code) | |
| 157 | } | |
| 158 | if local.stdout.Len() != 0 { | |
| 159 | t.Errorf("the refusal reached stdout, where git reads a pkt-line stream: %q", local.stdout.String()) | |
| 160 | } | |
| 161 | if !strings.Contains(local.stderr.String(), tc.want) { | |
| 162 | t.Errorf("stderr = %q, want it to contain %q", local.stderr.String(), tc.want) | |
| 163 | } | |
| 164 | }) | |
| 165 | } | |
| 166 | } | |
| 167 | ||
| 168 | // TestVoleVerbRefusalStaysOnTheChannelBody is the other half: the CLI reads its | |
| 169 | // verbs' answers from the channel body, so routing every refusal to stderr would | |
| 170 | // break them instead. | |
| 171 | func TestVoleVerbRefusalStaysOnTheChannelBody(t *testing.T) { | |
| 172 | t.Parallel() | |
| 173 | h := newHarness(t) | |
| 174 | local := &memorySession{} | |
| 175 | h.srv.exec(local, "vole-whoami '/../../etc.git'", "writer", "", "127.0.0.1:1") | |
| 176 | ||
| 177 | if !local.accepted || !local.exited { | |
| 178 | t.Fatalf("accepted=%v exited=%v; a refusal is still answered on the session", local.accepted, local.exited) | |
| 179 | } | |
| 180 | if local.code != 1 { | |
| 181 | t.Errorf("exit %d, want 1", local.code) | |
| 182 | } | |
| 183 | if local.stderr.Len() != 0 { | |
| 184 | t.Errorf("a Vole verb's refusal reached stderr, where its CLI does not read: %q", local.stderr.String()) | |
| 185 | } | |
| 186 | if !strings.Contains(local.stdout.String(), "invalid repository path") { | |
| 187 | t.Errorf("the refusal does not say why: %q", local.stdout.String()) | |
| 188 | } | |
| 189 | } | |
| 190 | ||
|
5ff8dfc9
Keenan Nemetz
16d ago
|
191 | // TestSessionWithoutRequestAccepts covers the session the connection layer |
| 192 | // builds only to report that it refused a shell: there is no exec request to | |
| 193 | // answer, and that is not an error. | |
| 194 | func TestSessionWithoutRequestAccepts(t *testing.T) { | |
| 195 | t.Parallel() | |
| 196 | if err := (&sshSession{}).Accept(); err != nil { | |
| 197 | t.Fatalf("Accept with no request = %v, want nil", err) | |
| 198 | } | |
| 199 | } |