Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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.

Mint a one-use session for each front door connection

e075258f0e4456f654680eeb7ba246e80d6c9a97 · Keenan Nemetz · 16d ago · parent 5ff8dfc9

Mint a one-use session for each front door connection

sshd can authenticate a Vole key and then hand the request to something else.
This adds the two ends of that handoff, plus the capability that crosses it.

`vole ssh-authorized-keys` is sshd's AuthorizedKeysCommand. It opens the roster
and the run credentials without running a schema or a migration — a write on the
path sshd takes before it knows who is connecting is a write nobody watches — and
for an active key prints the client's key back with `restrict,command=` and a
token. An unknown key, a retired account, and an expired credential all produce
no line and no row, so sshd refuses before any Vole process runs.

The token is 256 random bits, stored only as a SHA-256 hash, good for one use and
sixty seconds, and bound to the client endpoint sshd reports for the connection.
That binding is what closes the one weakness of a capability in argv: `/proc`
lets another local user read the forced command, but not spend it from their own
connection. A token that turns up at the wrong endpoint is deleted, not left for
the rest of its minute.

`vole ssh-exec` is the forced command, and `vole ssh-shell` is the account's
login shell. sshd runs a forced command *through* the login shell with `-c`, so a
shell that refuses everything would break every command it fronts: this one
recognises the single line ssh-authorized-keys printed, execs it directly, and
refuses anything else, including an interactive session. Generating that line
therefore refuses a config path containing whitespace rather than quoting it into
shell syntax.

The wire between them is `internal/sshwire`: one hello, one accept or refusal,
then length-prefixed stdin, stdout, stderr and exit frames over the Unix socket,
so the streams stay separate and backpressure is a blocked writer rather than a
buffer. `ssh_socket` is where that socket lives; it is absolute-only because
sshd's working directory is not the instance's.

The socket listener, the token sweeper, and the configuration that enables the
mode are #305. Until then nothing here runs unless sshd is wired to it.

internal/sshwire/sshwire_test.go +175

@@ -0,0 +1,175 @@
1 package sshwire
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "encoding/json"
7 "errors"
8 "io"
9 "net"
10 "path/filepath"
11 "strings"
12 "testing"
13 "time"
14 )
15
16 func TestFramesRoundTrip(t *testing.T) {
17 for _, payload := range [][]byte{nil, []byte("x"), bytes.Repeat([]byte("a"), MaxFrame)} {
18 var buf bytes.Buffer
19 if err := WriteFrame(&buf, KindStdout, payload); err != nil {
20 t.Fatalf("%d-byte frame: %v", len(payload), err)
21 }
22 kind, got, err := ReadFrame(&buf)
23 if err != nil {
24 t.Fatalf("%d-byte frame: %v", len(payload), err)
25 }
26 if kind != KindStdout || !bytes.Equal(got, payload) {
27 t.Fatalf("%d-byte frame came back as %q, %d bytes", len(payload), kind, len(got))
28 }
29 }
30 }
31
32 func TestOversizedFramesAreRefused(t *testing.T) {
33 var buf bytes.Buffer
34 if err := WriteFrame(&buf, KindStdout, bytes.Repeat([]byte("a"), MaxFrame+1)); err == nil {
35 t.Error("a frame larger than MaxFrame was written")
36 }
37
38 // A length this side never sent, which is what a broken peer would do: refuse
39 // it before allocating the payload.
40 var head [5]byte
41 head[0] = byte(KindStdout)
42 binary.BigEndian.PutUint32(head[1:], MaxFrame+1)
43 if _, _, err := ReadFrame(bytes.NewReader(head[:])); err == nil {
44 t.Error("an oversized length was accepted")
45 }
46 }
47
48 // TestExecRunsOneSession drives the client against a listener that answers by
49 // hand, which is what `vole serve` will do: the client's input arrives, then
50 // stdout, stderr, and a status.
51 func TestExecRunsOneSession(t *testing.T) {
52 srv := serveOnce(t, func(t *testing.T, conn net.Conn) {
53 if err := WriteJSON(conn, KindAccept, Accept{OK: true}); err != nil {
54 t.Error(err)
55 return
56 }
57 var in bytes.Buffer
58 for {
59 kind, payload, err := ReadFrame(conn)
60 if err != nil {
61 t.Errorf("read: %v", err)
62 return
63 }
64 if kind == KindStdinEOF {
65 break
66 }
67 if kind != KindStdin {
68 t.Errorf("unexpected %q frame before end of input", string(rune(kind)))
69 return
70 }
71 in.Write(payload)
72 }
73 _ = WriteFrame(conn, KindStdout, append([]byte("saw "), in.Bytes()...))
74 _ = WriteFrame(conn, KindStderr, []byte("a warning"))
75 _ = WriteJSON(conn, KindExit, Exit{Code: 129})
76 })
77
78 var out, errOut bytes.Buffer
79 hello := Hello{Session: "tok", Command: "git-upload-pack 'demo.git'", Remote: "203.0.113.5 4242 192.0.2.10 22"}
80 code, err := Exec(srv.socket, hello, strings.NewReader("hello"), &out, &errOut)
81 if err != nil {
82 t.Fatalf("exec: %v", err)
83 }
84 if code != 129 {
85 t.Errorf("exit %d, want 129", code)
86 }
87 if out.String() != "saw hello" {
88 t.Errorf("stdout %q", out.String())
89 }
90 if errOut.String() != "a warning" {
91 t.Errorf("stderr %q", errOut.String())
92 }
93 if got := srv.hello(t); got != hello {
94 t.Errorf("the hello lost fields: %+v", got)
95 }
96 }
97
98 func TestExecReportsARefusal(t *testing.T) {
99 srv := serveOnce(t, func(t *testing.T, conn net.Conn) {
100 _ = WriteJSON(conn, KindAccept, Accept{Error: "this session token was already spent"})
101 })
102 _, err := Exec(srv.socket, Hello{Session: "tok"}, nil, io.Discard, io.Discard)
103 if err == nil || !strings.Contains(err.Error(), "already spent") {
104 t.Fatalf("exec = %v, want the refusal's reason", err)
105 }
106 }
107
108 func TestExecWithoutASocket(t *testing.T) {
109 if _, err := Exec(filepath.Join(t.TempDir(), "missing.sock"), Hello{Session: "tok"}, nil, io.Discard, io.Discard); err == nil {
110 t.Fatal("a session with no socket reached something")
111 }
112 if _, err := Exec("", Hello{Session: "tok"}, nil, io.Discard, io.Discard); err == nil {
113 t.Fatal("an empty socket path was accepted")
114 }
115 }
116
117 func TestExecWithoutAnExitStatusFails(t *testing.T) {
118 srv := serveOnce(t, func(t *testing.T, conn net.Conn) {
119 _ = WriteJSON(conn, KindAccept, Accept{OK: true})
120 _ = WriteFrame(conn, KindStdout, []byte("partial"))
121 // Then the connection ends, which a command that crashed would do.
122 })
123 _, err := Exec(srv.socket, Hello{Session: "tok"}, nil, io.Discard, io.Discard)
124 if err == nil {
125 t.Fatal("a session that ended without a status was reported as complete")
126 }
127 if !errors.Is(err, io.EOF) && !strings.Contains(err.Error(), "exit status") {
128 t.Errorf("error %q does not say what was missing", err)
129 }
130 }
131
132 // fakeServer is one accepted connection, answered by a test.
133 type fakeServer struct {
134 socket string
135 ln net.Listener
136 got chan Hello
137 }
138
139 // serveOnce starts a Unix listener and answers one connection with fn.
140 func serveOnce(t *testing.T, fn func(*testing.T, net.Conn)) *fakeServer {
141 t.Helper()
142 s := &fakeServer{socket: filepath.Join(t.TempDir(), "ssh.sock"), got: make(chan Hello, 1)}
143 ln, err := net.Listen("unix", s.socket)
144 if err != nil {
145 t.Fatalf("listen: %v", err)
146 }
147 s.ln = ln
148 t.Cleanup(func() { ln.Close() })
149 go func() {
150 conn, err := ln.Accept()
151 if err != nil {
152 return
153 }
154 defer conn.Close()
155 var hello Hello
156 if kind, payload, err := ReadFrame(conn); err == nil && kind == KindHello {
157 _ = json.Unmarshal(payload, &hello)
158 }
159 s.got <- hello
160 fn(t, conn)
161 }()
162 return s
163 }
164
165 // hello waits for the request the client sent.
166 func (s *fakeServer) hello(t *testing.T) Hello {
167 t.Helper()
168 select {
169 case h := <-s.got:
170 return h
171 case <-time.After(10 * time.Second):
172 t.Fatal("the client never sent a hello")
173 return Hello{}
174 }
175 }