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 <noreply@anthropic.com>
This commit is contained in:
Lucas WintherandClaude Opus 5.5 committed 2026-11-11 18:59:21 +01:00
1 parent 9598e22d90
commit 248f467054
2 files changed
+40

No files matched your search

+4
View File
@@ -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)
}
@@ -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())
}