oficina / base Public

Drop the old host token before a reconnect can fail

Connect revokes a ready member's old device and then asks Equipo to
start a new sign-in. If that start failed, the old host token stayed
valid. Now, right after revoking, Base marks the old link lost and
deletes its host token in one transaction (store.LoseEquipoLink),
before anything else can fail. The fake Equipo can fail the next
sign-in start (FailNextBegin) to test it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev a1410ed6401869c7201c0726bf87b61bb149f349 parent 9c3a940
3 files changed, +45 −0
  • internal/equipo/equipotest/fake.go +18 −0
  • internal/web/ask_connect.go +9 −0
  • internal/webtest/ask_test.go +18 −0
diff --git a/internal/equipo/equipotest/fake.go b/internal/equipo/equipotest/fake.go
index b5a398f..e17b4cd 100644
--- a/internal/equipo/equipotest/fake.go
+++ b/internal/equipo/equipotest/fake.go
@@ -59,6 +59,8 @@ type Fake struct {
hold chan struct{}
entered chan struct{}
failSend bool
+ // failBegin makes the next device sign-in start answer 503.
+ failBegin bool
// snapHold and snapEntered do for the next snapshot what hold and
// entered do for the next exchange.
snapHold, snapEntered chan struct{}
@@ -87,6 +89,14 @@ func (f *Fake) HoldNextSnapshot() (entered <-chan struct{}, release func()) {
return f.snapEntered, func() { close(hold) }
}
+// FailNextBegin makes the next device sign-in start (POST
+// /api/v1/auth/devices) answer 503, as when Equipo is down.
+func (f *Fake) FailNextBegin() {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ f.failBegin = true
+}
+
// FailNextSend makes the next new message's answer a 503 after Equipo has
// recorded it, as when the answer is lost on the way back.
func (f *Fake) FailNextSend() {
@@ -201,6 +211,14 @@ func (f *Fake) serve(w http.ResponseWriter, r *http.Request) {
}
func (f *Fake) begin(w http.ResponseWriter, r *http.Request) {
+ f.mu.Lock()
+ fail := f.failBegin
+ f.failBegin = false
+ f.mu.Unlock()
+ if fail {
+ writeError(w, r, 503, "unavailable")
+ return
+ }
var in struct {
PublicKey string `json:"public_key"`
Label string `json:"label"`
diff --git a/internal/web/ask_connect.go b/internal/web/ask_connect.go
index b0e6fae..60bf668 100644
--- a/internal/web/ask_connect.go
+++ b/internal/web/ask_connect.go
@@ -47,6 +47,15 @@ func (a *app) askConnectStart(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if old, sec, state, err := a.askLink(ctx, me); err == nil && state == askReady {
a.revoke(ctx, old, sec)
+ // The old connection is over: its link is lost and its host token
+ // goes with it, in one transaction, before anything below can fail.
+ unlock := a.lockMember(me.ID)
+ err = store.LoseEquipoLink(ctx, a.db, me.ID, old.DeviceID)
+ unlock()
+ if err != nil {
+ a.fail(w, err)
+ return
+ }
}
pub, key, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
diff --git a/internal/webtest/ask_test.go b/internal/webtest/ask_test.go
index e679168..03774ea 100644
--- a/internal/webtest/ask_test.go
+++ b/internal/webtest/ask_test.go
@@ -1100,3 +1100,21 @@ func TestDisconnectFailsWhileTokenStays(t *testing.T) {
t.Fatalf("disconnect: %d, token names %q", r.Code, a.hostMember(token))
}
}
+
+// Reconnecting revokes the old device first; if Equipo then fails to
+// start the new sign-in, the old host token must already name nobody.
+func TestReconnectFailureDropsHostToken(t *testing.T) {
+ a, f := newAskApp(t)
+ a.connectEquipo(f)
+ token, _, _ := hostOf(t, f, -1)
+ f.FailNextBegin()
+ if r := a.post("/ask/connect", url.Values{"next": {"settings"}}); r.Code != http.StatusBadGateway {
+ t.Fatalf("connect with Equipo down: %d", r.Code)
+ }
+ if a.hostMember(token) != "" {
+ t.Fatal("the revoked connection's host token still names the member")
+ }
+ if s := a.get("/settings").Body; strings.Contains(s, `action="/ask/disconnect"`) {
+ t.Fatalf("Settings shows a revoked connection as connected:\n%s", s)
+ }
+}