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.

forge/sshsession/sshsession_test.go +132

@@ -0,0 +1,132 @@
1 package sshsession
2
3 // The token is the only thing that proves a forced command was authorised by an
4 // authenticated connection. These tests are about the ways it must stop working.
5
6 import (
7 "path/filepath"
8 "testing"
9 "time"
10 )
11
12 func newStore(t *testing.T) *Store {
13 t.Helper()
14 s, err := Open(filepath.Join(t.TempDir(), "instance.db"))
15 if err != nil {
16 t.Fatalf("open: %v", err)
17 }
18 t.Cleanup(func() { s.Close() })
19 return s
20 }
21
22 func TestTokenIsSpentOnceAndOnlyByItsConnection(t *testing.T) {
23 now := time.Now().UTC()
24 s := newStore(t)
25 token, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 4242"}, now)
26 if err != nil {
27 t.Fatalf("mint: %v", err)
28 }
29
30 // The server half of sshd's %C is the same accept either way, so it is not
31 // part of the binding: only the address and port the client came from are.
32 got, err := s.Consume(token, "203.0.113.5 4242 192.0.2.10 22", now.Add(time.Second))
33 if err != nil {
34 t.Fatalf("consume: %v", err)
35 }
36 if got == nil || got.Account != "alice" {
37 t.Fatalf("consume = %+v, want alice", got)
38 }
39 if again, err := s.Consume(token, "203.0.113.5 4242", now.Add(2*time.Second)); err != nil || again != nil {
40 t.Fatalf("second consume = %+v, %v; want nil, nil", again, err)
41 }
42 }
43
44 func TestTokenFromAnotherEndpointIsRefusedAndSpent(t *testing.T) {
45 now := time.Now().UTC()
46 s := newStore(t)
47 token, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 4242"}, now)
48 if err != nil {
49 t.Fatal(err)
50 }
51 got, err := s.Consume(token, "198.51.100.9 4242", now)
52 if err != nil {
53 t.Fatalf("consume: %v", err)
54 }
55 if got != nil {
56 t.Fatalf("a token from another endpoint opened a session: %+v", got)
57 }
58 // A capability that turned up at the wrong endpoint has leaked. It must not
59 // be available for the rest of its minute.
60 if again, err := s.Consume(token, "203.0.113.5 4242", now); err != nil || again != nil {
61 t.Fatalf("the refused token was still usable: %+v, %v", again, err)
62 }
63 }
64
65 func TestExpiredTokenIsRefused(t *testing.T) {
66 now := time.Now().UTC()
67 s := newStore(t)
68 token, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 4242"}, now)
69 if err != nil {
70 t.Fatal(err)
71 }
72 got, err := s.Consume(token, "203.0.113.5 4242", now.Add(Lifetime))
73 if err != nil {
74 t.Fatalf("consume: %v", err)
75 }
76 if got != nil {
77 t.Fatalf("a token at the end of its life opened a session: %+v", got)
78 }
79 }
80
81 func TestMintPrunesAndPruneEmpties(t *testing.T) {
82 now := time.Now().UTC()
83 s := newStore(t)
84 if _, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 1"}, now); err != nil {
85 t.Fatal(err)
86 }
87 // A minute later both the mint and the sweep are allowed to drop the first.
88 if _, err := s.Mint(Token{Account: "bob", Client: "203.0.113.5 2"}, now.Add(Lifetime)); err != nil {
89 t.Fatal(err)
90 }
91 if n, err := s.Prune(now.Add(2 * Lifetime)); err != nil || n != 1 {
92 t.Fatalf("prune = %d, %v; want 1 row", n, err)
93 }
94 if n, err := s.Prune(now.Add(2 * Lifetime)); err != nil || n != 0 {
95 t.Fatalf("second prune = %d, %v; want 0 rows", n, err)
96 }
97 }
98
99 func TestMintNeedsAnAccountAndAConnection(t *testing.T) {
100 now := time.Now().UTC()
101 s := newStore(t)
102 if _, err := s.Mint(Token{Client: "203.0.113.5 1"}, now); err == nil {
103 t.Error("a token without an account was minted")
104 }
105 if _, err := s.Mint(Token{Account: "alice"}, now); err == nil {
106 t.Error("a token without a client endpoint was minted")
107 }
108 if _, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5"}, now); err == nil {
109 t.Error("a token for a client with no port was minted")
110 }
111 }
112
113 func TestNormalizeClient(t *testing.T) {
114 for _, tc := range []struct {
115 in, want string
116 ok bool
117 }{
118 {"203.0.113.5 4242 192.0.2.10 22", "203.0.113.5 4242", true},
119 {" 203.0.113.5 4242 ", "203.0.113.5 4242", true},
120 {"2001:db8::1 4242 2001:db8::2 22", "2001:db8::1 4242", true},
121 {"", "", false},
122 {"203.0.113.5", "", false},
123 } {
124 got, err := NormalizeClient(tc.in)
125 if tc.ok && (err != nil || got != tc.want) {
126 t.Errorf("NormalizeClient(%q) = %q, %v; want %q", tc.in, got, err, tc.want)
127 }
128 if !tc.ok && err == nil {
129 t.Errorf("NormalizeClient(%q) = %q, want an error", tc.in, got)
130 }
131 }
132 }