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.
Bind a session token to the connection, not to a field order
16a92c6f424836fdaf267be21e6acff022655300 · Keenan Nemetz · 16d ago · parent bca53bda
Bind a session token to the connection, not to a field order The front door refused every session on a real host. sshd's %C and SSH_CONNECTION carry the same four endpoint values in opposite order — on OpenSSH 9.6p1 %C is server-first, SSH_CONNECTION is client-first, and the man page describes %C the client-first way — so comparing the first two fields compared a server address with a client address. NormalizeClient now sorts the four values. The binding depends on what identifies the connection, which both sides agree on, rather than on an order that is neither documented consistently nor stable. Fewer than four values is refused, so a format change fails closed instead of silently weakening the check to two fields. The regression test uses the pair of strings captured from the host, and pins that a different client port does not canonicalize to the same value. No test caught this because every one of them fed the same string to both sides. A consistent assumption is not a test of the assumption. Closes #313.
forge/sshsession/sshsession_test.go +66−32
| @@ -9,6 +9,10 @@ import ( | |||
| 9 | 9 | "time" | |
| 10 | 10 | ) | |
| 11 | 11 | ||
| 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 | |||
| 12 | 16 | func newStore(t *testing.T) *Store { | |
| 13 | 17 | t.Helper() | |
| 14 | 18 | s, err := Open(filepath.Join(t.TempDir(), "instance.db")) | |
| @@ -22,21 +26,21 @@ func newStore(t *testing.T) *Store { | |||
| 22 | 26 | func TestTokenIsSpentOnceAndOnlyByItsConnection(t *testing.T) { | |
| 23 | 27 | now := time.Now().UTC() | |
| 24 | 28 | s := newStore(t) | |
| 25 | token, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 4242"}, now) | ||
| 29 | token, err := s.Mint(Token{Account: "alice", Client: connection}, now) | ||
| 26 | 30 | if err != nil { | |
| 27 | 31 | t.Fatalf("mint: %v", err) | |
| 28 | 32 | } | |
| 29 | 33 | ||
| 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)) | ||
| 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)) | ||
| 33 | 37 | if err != nil { | |
| 34 | 38 | t.Fatalf("consume: %v", err) | |
| 35 | 39 | } | |
| 36 | 40 | if got == nil || got.Account != "alice" { | |
| 37 | 41 | t.Fatalf("consume = %+v, want alice", got) | |
| 38 | 42 | } | |
| 39 | if again, err := s.Consume(token, "203.0.113.5 4242", now.Add(2*time.Second)); err != nil || again != nil { | ||
| 43 | if again, err := s.Consume(token, connection, now.Add(2*time.Second)); err != nil || again != nil { | ||
| 40 | 44 | t.Fatalf("second consume = %+v, %v; want nil, nil", again, err) | |
| 41 | 45 | } | |
| 42 | 46 | } | |
| @@ -44,11 +48,11 @@ func TestTokenIsSpentOnceAndOnlyByItsConnection(t *testing.T) { | |||
| 44 | 48 | func TestTokenFromAnotherEndpointIsRefusedAndSpent(t *testing.T) { | |
| 45 | 49 | now := time.Now().UTC() | |
| 46 | 50 | s := newStore(t) | |
| 47 | token, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 4242"}, now) | ||
| 51 | token, err := s.Mint(Token{Account: "alice", Client: connection}, now) | ||
| 48 | 52 | if err != nil { | |
| 49 | 53 | t.Fatal(err) | |
| 50 | 54 | } | |
| 51 | got, err := s.Consume(token, "198.51.100.9 4242", now) | ||
| 55 | got, err := s.Consume(token, "198.51.100.9 4242 192.0.2.10 22", now) | ||
| 52 | 56 | if err != nil { | |
| 53 | 57 | t.Fatalf("consume: %v", err) | |
| 54 | 58 | } | |
| @@ -57,7 +61,7 @@ func TestTokenFromAnotherEndpointIsRefusedAndSpent(t *testing.T) { | |||
| 57 | 61 | } | |
| 58 | 62 | // A capability that turned up at the wrong endpoint has leaked. It must not | |
| 59 | 63 | // 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 { | ||
| 64 | if again, err := s.Consume(token, connection, now); err != nil || again != nil { | ||
| 61 | 65 | t.Fatalf("the refused token was still usable: %+v, %v", again, err) | |
| 62 | 66 | } | |
| 63 | 67 | } | |
| @@ -65,11 +69,11 @@ func TestTokenFromAnotherEndpointIsRefusedAndSpent(t *testing.T) { | |||
| 65 | 69 | func TestExpiredTokenIsRefused(t *testing.T) { | |
| 66 | 70 | now := time.Now().UTC() | |
| 67 | 71 | s := newStore(t) | |
| 68 | token, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 4242"}, now) | ||
| 72 | token, err := s.Mint(Token{Account: "alice", Client: connection}, now) | ||
| 69 | 73 | if err != nil { | |
| 70 | 74 | t.Fatal(err) | |
| 71 | 75 | } | |
| 72 | got, err := s.Consume(token, "203.0.113.5 4242", now.Add(Lifetime)) | ||
| 76 | got, err := s.Consume(token, connection, now.Add(Lifetime)) | ||
| 73 | 77 | if err != nil { | |
| 74 | 78 | t.Fatalf("consume: %v", err) | |
| 75 | 79 | } | |
| @@ -81,11 +85,11 @@ func TestExpiredTokenIsRefused(t *testing.T) { | |||
| 81 | 85 | func TestMintPrunesAndPruneEmpties(t *testing.T) { | |
| 82 | 86 | now := time.Now().UTC() | |
| 83 | 87 | s := newStore(t) | |
| 84 | if _, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 1"}, now); err != nil { | ||
| 88 | if _, err := s.Mint(Token{Account: "alice", Client: "203.0.113.5 1 192.0.2.10 22"}, now); err != nil { | ||
| 85 | 89 | t.Fatal(err) | |
| 86 | 90 | } | |
| 87 | 91 | // 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 { | ||
| 92 | if _, err := s.Mint(Token{Account: "bob", Client: "203.0.113.5 2 192.0.2.10 22"}, now.Add(Lifetime)); err != nil { | ||
| 89 | 93 | t.Fatal(err) | |
| 90 | 94 | } | |
| 91 | 95 | if n, err := s.Prune(now.Add(2 * Lifetime)); err != nil || n != 1 { | |
| @@ -99,34 +103,64 @@ func TestMintPrunesAndPruneEmpties(t *testing.T) { | |||
| 99 | 103 | func TestMintNeedsAnAccountAndAConnection(t *testing.T) { | |
| 100 | 104 | now := time.Now().UTC() | |
| 101 | 105 | s := newStore(t) | |
| 102 | if _, err := s.Mint(Token{Client: "203.0.113.5 1"}, now); err == nil { | ||
| 106 | if _, err := s.Mint(Token{Client: connection}, now); err == nil { | ||
| 103 | 107 | t.Error("a token without an account was minted") | |
| 104 | 108 | } | |
| 105 | 109 | if _, err := s.Mint(Token{Account: "alice"}, now); err == nil { | |
| 106 | t.Error("a token without a client endpoint was minted") | ||
| 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) | ||
| 107 | 132 | } | |
| 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") | ||
| 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) | ||
| 110 | 145 | } | |
| 111 | 146 | } | |
| 112 | 147 | ||
| 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}, | ||
| 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", | ||
| 123 | 155 | } { | |
| 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) | ||
| 156 | if got, err := NormalizeClient(in); err == nil { | ||
| 157 | t.Errorf("NormalizeClient(%q) = %q, want an error", in, got) | ||
| 130 | 158 | } | |
| 131 | 159 | } | |
| 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 | } | ||
| 132 | 166 | } | |