From 248f467054d7ce8a6211a4cc5c48e44bc1f2e50d Mon Sep 17 00:00:00 2001 From: Lucas Winther Date: Wed, 11 Nov 2026 18:52:39 +0100 Subject: [PATCH] protondrive: don't save refreshed tokens after disconnect A token refresh still in flight when the remote was disconnected could write its new tokens back to the config after Disconnect removed them. Disconnect now waits for it and removes them afterwards. Co-Authored-By: Claude Opus 5.5 --- backend/protondrive/session.go | 4 +++ backend/protondrive/session_internal_test.go | 36 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/backend/protondrive/session.go b/backend/protondrive/session.go index 4c94ffb1f..7febdd78b 100644 --- a/backend/protondrive/session.go +++ b/backend/protondrive/session.go @@ -622,7 +622,11 @@ func (s *session) logout(ctx context.Context) error { err = nil } } + // A refresh in flight would otherwise save its new tokens after + // these are removed. + s.refreshMu.Lock() s.saveCredentials(credentials{}) + s.refreshMu.Unlock() if err != nil { return fmt.Errorf("sign out: %w", err) } diff --git a/backend/protondrive/session_internal_test.go b/backend/protondrive/session_internal_test.go index 3bdd0938d..bee05c61f 100644 --- a/backend/protondrive/session_internal_test.go +++ b/backend/protondrive/session_internal_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/base64" "errors" + "io" "net/http" "net/url" "regexp" @@ -544,3 +545,38 @@ func TestCallDuringLogout(t *testing.T) { assert.Equal(t, 1, srv.Requests()["GET /tests/auth"], "retried without credentials") assert.Zero(t, srv.Requests()[refreshRoute]) } + +func TestRefreshDuringLogout(t *testing.T) { + t.Parallel() + ctx := context.Background() + srv := protontest.New(t) + tokens := srv.NewSession(srv.AddUser("user", "pass")) + refreshing, release := make(chan struct{}), make(chan struct{}) + // The refresh succeeds, but its answer arrives only after the + // session has signed out. + client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) { + if r.URL.Path != "/auth/v4/refresh" { + return srv.Client().Transport.RoundTrip(r) + } + close(refreshing) + <-release + body := `{"Code":1000,"UID":"` + tokens.UID + `","AccessToken":"new-access","RefreshToken":"new-refresh"}` + return &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"application/json"}}, Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil + })} + store := &memoryStore{creds: credentials{UID: tokens.UID, AccessToken: tokens.AccessToken, RefreshToken: tokens.RefreshToken, KeyPassword: "key-pass"}} + s := newSession(client, srv.URL(), testAppVersion, store) + srv.Inject(protontest.Fault{Method: "GET", Pattern: "/tests/auth", Times: 1, Status: http.StatusUnauthorized}) + + errc := make(chan error, 1) + go func() { errc <- authCall(ctx, s) }() + <-refreshing + logoutErr := make(chan error, 1) + go func() { logoutErr <- s.logout(ctx) }() + require.Eventually(t, func() bool { return srv.Requests()["DELETE /core/v4/auth"] == 1 }, 5*time.Second, 5*time.Millisecond) + close(release) + require.NoError(t, <-logoutErr) + assert.Error(t, <-errc, "the call succeeded with tokens refreshed after signing out") + + assert.Equal(t, credentials{}, store.Load(), "tokens saved after signing out") + assert.Equal(t, credentials{}, s.current()) +}