Expire join codes on the setup's side, bind the join port to its address, trust the peer address

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-04 10:16:53 +02:00
1 parent 27c49d0af9
commit 1f406247e4
4 files changed
+67 -15

No files matched your search

+6
View File
@@ -102,6 +102,12 @@ APIs change between minors without a deprecation period.
account is not active — suspended, pending, or gone — is now refused as a
revoked key is, within `auth.cache_ttl`, and works again when the account
is reinstated.
- **A join code is a credential only for its hour, and only on its
address.** The setup checked a code's expiry on the joining side alone, so
a code stayed good for as long as the setup ran; it now refuses an expired
one itself. Its listener binds the address the codes name rather than
every interface, and a joined machine is known by the address it connected
from, not the one it claimed.
### Everything that can be changed on a running cluster, from the console
+6 -2
View File
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net"
"net/mail"
"os"
"strings"
@@ -448,8 +449,11 @@ func (m *setupModel) startJoins() (tea.Cmd, bool) {
continue
}
if m.listener == nil {
l, err := joinpkg.Listen(fmt.Sprintf(":%d", joinpkg.DefaultPort),
fmt.Sprintf("%s:%d", m.thisAddr(), joinpkg.DefaultPort), time.Hour)
// On the address the codes name, not every interface: a code is
// a credential, and the port it is spent on need not face more
// than the network the machines join from.
addr := net.JoinHostPort(m.thisAddr(), fmt.Sprint(joinpkg.DefaultPort))
l, err := joinpkg.Listen(addr, addr, time.Hour)
if err != nil {
m.formErr = err.Error()
return nil, false
+38 -13
View File
@@ -133,11 +133,20 @@ type response struct {
type Listener struct {
ln net.Listener
addr string
// now is time.Now, replaced by tests.
now func() time.Time
fingerprint [16]byte
ttl time.Duration
mu sync.Mutex
pending map[[16]byte]chan *Remote // by secret
pending map[[16]byte]waiting // by secret
}
// waiting is a code not yet used: where its machine is handed over, and
// until when it may come.
type waiting struct {
ch chan *Remote
expires time.Time
}
// Listen starts listening on bind (":7444") and advertises addr ("10.0.0.5:7444")
@@ -151,7 +160,7 @@ func Listen(bind, addr string, ttl time.Duration) (*Listener, error) {
if err != nil {
return nil, fmt.Errorf("listening for machines to join on %s: %w", bind, err)
}
l := &Listener{ln: ln, addr: addr, fingerprint: fp, ttl: ttl, pending: map[[16]byte]chan *Remote{}}
l := &Listener{ln: ln, addr: addr, fingerprint: fp, ttl: ttl, pending: map[[16]byte]waiting{}, now: time.Now}
go l.accept()
return l, nil
}
@@ -188,11 +197,12 @@ func (l *Listener) Code(role string) (Code, <-chan *Remote) {
var secret [16]byte
_, _ = rand.Read(secret[:])
ch := make(chan *Remote, 1)
expires := time.Now().Add(l.ttl)
l.mu.Lock()
l.pending[secret] = ch
l.pending[secret] = waiting{ch: ch, expires: expires}
l.mu.Unlock()
return Code{Addr: l.addr, Fingerprint: l.fingerprint, Secret: secret,
Expires: time.Now().Add(l.ttl).Unix(), Role: role}, ch
Expires: expires.Unix(), Role: role}, ch
}
// Close stops listening; machines already joined stay connected until their
@@ -220,16 +230,29 @@ func (l *Listener) admit(c net.Conn) {
}
l.mu.Lock()
var ch chan *Remote
expired := false
now := l.now()
for secret, p := range l.pending {
if subtle.ConstantTimeCompare(secret[:], h.Secret) == 1 {
ch = p
// One machine a code: whoever comes second is turned away.
// One machine a code: whoever comes second is turned away. And
// not after it expires, which the joining side checking was no
// guarantee of: a code is a credential for as long as this
// listener runs, unless this side looks too.
delete(l.pending, secret)
if now.After(p.expires) {
expired = true
} else {
ch = p.ch
}
}
}
l.mu.Unlock()
if ch == nil {
_, _ = c.Write([]byte(`{"error":"this code is not one the setup is waiting for, or it was used already"}` + "\n"))
msg := "this code is not one the setup is waiting for, or it was used already"
if expired {
msg = "this code expired; ask for a new one"
}
_, _ = c.Write([]byte(`{"error":"` + msg + `"}` + "\n"))
_ = c.Close()
return
}
@@ -238,17 +261,19 @@ func (l *Listener) admit(c net.Conn) {
_ = c.Close()
return
}
addr := h.Addr
if addr == "" {
addr, _, _ = net.SplitHostPort(c.RemoteAddr().String())
}
ch <- &Remote{conn: c, r: r, Hostname: h.Hostname, Addr: addr}
// The address it connected from, not the one it says it has: the setup
// lets that address into the store, and a claim is anyone's to make.
addr, _, _ := net.SplitHostPort(c.RemoteAddr().String())
ch <- &Remote{conn: c, r: r, Hostname: h.Hostname, Addr: addr, Claimed: h.Addr}
}
// Remote is a joined machine, as the setup runs things on it.
type Remote struct {
Hostname string
Addr string
// Addr is where it connected from; Claimed what it said its address is,
// for showing beside it when the two differ.
Addr string
Claimed string
mu sync.Mutex
conn net.Conn
+17
View File
@@ -92,3 +92,20 @@ func TestAnExpiredOrDamagedCodeIsRefusedBeforeDialling(t *testing.T) {
t.Errorf("%+v, %v", back, err)
}
}
func TestTheSetupRefusesAnExpiredCodeEvenFromAJoinerThatDidNotLook(t *testing.T) {
l := listen(t)
code, arrived := l.Code("node")
// Time moves on past the code's life on the setup's side; the joiner is
// handed the code as it was, without checking it again.
l.now = func() time.Time { return time.Now().Add(2 * time.Hour) }
err := Serve(context.Background(), code, func(string) {})
if err == nil || !strings.Contains(err.Error(), "expired") {
t.Fatalf("an expired code was taken: %v", err)
}
select {
case m := <-arrived:
t.Fatalf("a machine arrived on an expired code: %+v", m)
default:
}
}