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.

Blame · forge/sshsession/sshsession_test.go

Line attribution on main · 166 line(s)

e075258f
Keenan Nemetz
16d ago
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
16a92c6f
Keenan Nemetz
16d ago
12 // connection is one client's four endpoint values in SSH_CONNECTION's order:
13 // client address, client port, server address, server port.
14 const connection = "203.0.113.5 4242 192.0.2.10 22"
15
e075258f
Keenan Nemetz
16d ago
16 func newStore(t *testing.T) *Store {
17 t.Helper()
18 s, err := Open(filepath.Join(t.TempDir(), "instance.db"))
19 if err != nil {
20 t.Fatalf("open: %v", err)
21 }
22 t.Cleanup(func() { s.Close() })
23 return s
24 }
25
26 func TestTokenIsSpentOnceAndOnlyByItsConnection(t *testing.T) {
27 now := time.Now().UTC()
28 s := newStore(t)
16a92c6f
Keenan Nemetz
16d ago
29 token, err := s.Mint(Token{Account: "alice", Client: connection}, now)
e075258f
Keenan Nemetz
16d ago
30 if err != nil {
31 t.Fatalf("mint: %v", err)
32 }
33
16a92c6f
Keenan Nemetz
16d ago
34 // The same connection, in the order sshd's %C gives it on OpenSSH 9.6p1:
35 // server first. It has to open the session the mint was for.
36 got, err := s.Consume(token, "192.0.2.10 22 203.0.113.5 4242", now.Add(time.Second))
e075258f
Keenan Nemetz
16d ago
37 if err != nil {
38 t.Fatalf("consume: %v", err)
39 }
40 if got == nil || got.Account != "alice" {
41 t.Fatalf("consume = %+v, want alice", got)
42 }
16a92c6f
Keenan Nemetz
16d ago
43 if again, err := s.Consume(token, connection, now.Add(2*time.Second)); err != nil || again != nil {
e075258f
Keenan Nemetz
16d ago
44 t.Fatalf("second consume = %+v, %v; want nil, nil", again, err)
45 }
46 }
47
48 func TestTokenFromAnotherEndpointIsRefusedAndSpent(t *testing.T) {
49 now := time.Now().UTC()
50 s := newStore(t)
16a92c6f
Keenan Nemetz
16d ago
51 token, err := s.Mint(Token{Account: "alice", Client: connection}, now)
e075258f
Keenan Nemetz
16d ago
52 if err != nil {
53 t.Fatal(err)
54 }
16a92c6f
Keenan Nemetz
16d ago
55 got, err := s.Consume(token, "198.51.100.9 4242 192.0.2.10 22", now)
e075258f
Keenan Nemetz
16d ago
56 if err != nil {
57 t.Fatalf("consume: %v", err)
58 }
59 if got != nil {
60 t.Fatalf("a token from another endpoint opened a session: %+v", got)
61 }
62 // A capability that turned up at the wrong endpoint has leaked. It must not
63 // be available for the rest of its minute.
16a92c6f
Keenan Nemetz
16d ago
64 if again, err := s.Consume(token, connection, now); err != nil || again != nil {
e075258f
Keenan Nemetz
16d ago
65 t.Fatalf("the refused token was still usable: %+v, %v", again, err)
66 }
67 }
68
69 func TestExpiredTokenIsRefused(t *testing.T) {
70 now := time.Now().UTC()
71 s := newStore(t)
16a92c6f
Keenan Nemetz
16d ago
72 token, err := s.Mint(Token{Account: "alice", Client: connection}, now)
e075258f
Keenan Nemetz
16d ago
73 if err != nil {
74 t.Fatal(err)
75 }
16a92c6f
Keenan Nemetz
16d ago
76 got, err := s.Consume(token, connection, now.Add(Lifetime))
e075258f
Keenan Nemetz
16d ago
77 if err != nil {
78 t.Fatalf("consume: %v", err)
79 }
80 if got != nil {
81 t.Fatalf("a token at the end of its life opened a session: %+v", got)
82 }
83 }
84
85 func TestMintPrunesAndPruneEmpties(t *testing.T) {
86 now := time.Now().UTC()
87 s := newStore(t)
16a92c6f
Keenan Nemetz
16d ago
88 if _, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 1 192.0.2.10 22"}, now); err != nil {
e075258f
Keenan Nemetz
16d ago
89 t.Fatal(err)
90 }
91 // A minute later both the mint and the sweep are allowed to drop the first.
16a92c6f
Keenan Nemetz
16d ago
92 if _, err := s.Mint(Token{Account: "bob", Client: "203.0.113.5 2 192.0.2.10 22"}, now.Add(Lifetime)); err != nil {
e075258f
Keenan Nemetz
16d ago
93 t.Fatal(err)
94 }
95 if n, err := s.Prune(now.Add(2 * Lifetime)); err != nil || n != 1 {
96 t.Fatalf("prune = %d, %v; want 1 row", n, err)
97 }
98 if n, err := s.Prune(now.Add(2 * Lifetime)); err != nil || n != 0 {
99 t.Fatalf("second prune = %d, %v; want 0 rows", n, err)
100 }
101 }
102
103 func TestMintNeedsAnAccountAndAConnection(t *testing.T) {
104 now := time.Now().UTC()
105 s := newStore(t)
16a92c6f
Keenan Nemetz
16d ago
106 if _, err := s.Mint(Token{Client: connection}, now); err == nil {
e075258f
Keenan Nemetz
16d ago
107 t.Error("a token without an account was minted")
108 }
109 if _, err := s.Mint(Token{Account: "alice"}, now); err == nil {
16a92c6f
Keenan Nemetz
16d ago
110 t.Error("a token without a connection was minted")
111 }
112 if _, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 4242"}, now); err == nil {
113 t.Error("a token for a half a connection was minted")
114 }
115 }
116
117 // TestNormalizeClientIgnoresTheOrderOfTheFourValues is the regression test for
118 // the bug that refused every session on a real host: sshd's %C and
119 // SSH_CONNECTION carry the same four values in opposite order.
120 func TestNormalizeClientIgnoresTheOrderOfTheFourValues(t *testing.T) {
121 // The first string is what OpenSSH 9.6p1 expanded %C to on a real host; the
122 // second is that connection's SSH_CONNECTION.
123 cpp := "159.203.28.66 22 97.120.120.103 58036"
124 sshConnection := "97.120.120.103 58036 159.203.28.66 22"
125 a, err := NormalizeClient(cpp)
126 if err != nil {
127 t.Fatalf("NormalizeClient(%%C) = %v", err)
128 }
129 b, err := NormalizeClient(sshConnection)
130 if err != nil {
131 t.Fatalf("NormalizeClient(SSH_CONNECTION) = %v", err)
e075258f
Keenan Nemetz
16d ago
132 }
16a92c6f
Keenan Nemetz
16d ago
133 if a != b {
134 t.Fatalf("the same connection canonicalizes differently: %q vs %q", a, b)
135 }
136
137 // A different connection must not canonicalize to the same value. The client
138 // port is what distinguishes two connections from one address.
139 other, err := NormalizeClient("97.120.120.103 58037 159.203.28.66 22")
140 if err != nil {
141 t.Fatal(err)
142 }
143 if other == a {
144 t.Errorf("a different client port canonicalized to %q", other)
e075258f
Keenan Nemetz
16d ago
145 }
146 }
147
16a92c6f
Keenan Nemetz
16d ago
148 func TestNormalizeClientNeedsAllFourEndpoints(t *testing.T) {
149 for _, in := range []string{
150 "",
151 "203.0.113.5",
152 "203.0.113.5 4242",
153 "203.0.113.5 4242 192.0.2.10",
154 "203.0.113.5 4242 192.0.2.10 22 extra",
e075258f
Keenan Nemetz
16d ago
155 } {
16a92c6f
Keenan Nemetz
16d ago
156 if got, err := NormalizeClient(in); err == nil {
157 t.Errorf("NormalizeClient(%q) = %q, want an error", in, got)
e075258f
Keenan Nemetz
16d ago
158 }
159 }
16a92c6f
Keenan Nemetz
16d ago
160
161 // Whitespace between the values is not significant.
162 got, err := NormalizeClient(" 203.0.113.5 4242\t192.0.2.10 22 ")
163 if err != nil || got != "192.0.2.10 203.0.113.5 22 4242" {
164 t.Errorf("NormalizeClient with padding = %q, %v", got, err)
165 }
e075258f
Keenan Nemetz
16d ago
166 }