Files
magpie/internal/plugin/signout_hook_test.go
T
yetone cd83c3147f plugin: a hook's auth.set doesn't sign in again an account signed out while it ran (#5126, #5119)
Cause: a plugin hook that saves a sign-in through client.auth.set (a
models list spending a rotating refresh token) wrote it into
plugin-auth.json unconditionally. A listing that began before a
sign-out, or before a failed move's putBack took the accounts back out
of the plugin (plugin.Take), landed after it and wrote the account in
again. TestMoveToPlugin's reset signs fakeco out, but a background
listing set off by the step before spent a@fake's "rot-a" token again
(rot-a+++++ seen in the file right after the sign-out), so the next
Move saw a@fake as the plugin's own (movedAccount.Was), restored it on
failure, and "the plugin kept an account of a failed move".
TestMoveCutShort (#5119) fails the same way: the account Take removed
came back through the same write. Not a leak between runs: the race is
inside one run, and more runs only gave it more chances. It is a real
bug for users too: signing out of a rotating-token account while its
models are listed brought it back.

Fix: inScope in host.js notes whether the account was signed in when
the call began (had); auth.set skips the write under the file's lock
when it was and is gone now, as renewLocked already does for renewals.
Sign-in steps (signingIn) are not held to it, their account is new.

Test: TestSignOutOutlastsRunningHook holds the fake plugin's models
hook (new FAKE_MODELS_HOLD: marker file in, release file out) across a
SignOut in a real host and checks the account stays gone, and that a
hook not raced still keeps the token it saved.

Run (all at this tree):
- mote-run go test -count=30 -run 'TestMoveToPlugin$|TestMoveCutShort$'
  ./internal/provider: ok. Before the fix the same run on mote failed
  3 TestMoveToPlugin + 9 TestMoveCutShort of 30 each; -count=15 of
  TestMoveToPlugin alone failed 2 to 4 of 15.
- wsl-run go test -count=20 of the two plus the new test: ok. Before
  the fix: 4 + 17 of 20 failed on Linux.
- mote-run go test -count=5 -run TestMoveToPlugin$: ok.
- mote-run go test -count=5 TestSignOutOutlastsRunningHook: ok.
- mote-run go test -timeout 45m ./internal/provider ./internal/plugin:
  ok (provider 171.6s, plugin 160.4s).
- gofmt -l clean; go vet, GOOS=linux and GOOS=windows go vet of both
  packages: ok.

Broken by hand: host.js reverted with the test kept:
TestSignOutOutlastsRunningHook red 3 of 3 ("the hook signed the account
in again"), and the provider tests red as above.

Not tried: a real vendor's rotating-token plugin signed out mid-listing
(the fake plugin's rot- sign-ins stand in for it); the full ./... suite
runs after the push.
2026-11-04 13:23:28 +08:00

85 lines
2.1 KiB
Go

package plugin
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// An account signed out while one of its hooks runs stays signed out: the
// hook's auth.set (a models list spending a rotating refresh token) lands
// after the sign-out and must not sign it in again (#5126: a failed move's
// account came back in the plugin this way). One not signed out keeps the
// token the hook saved.
func TestSignOutOutlastsRunningHook(t *testing.T) {
sandbox(t)
hold := filepath.Join(t.TempDir(), "hold")
t.Setenv("FAKE_MODELS_HOLD", hold)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/fake/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
t.Cleanup(Settle)
signIn := func() string {
t.Helper()
k, err := Import(ctx, "fakeco", map[string]any{"type": "oauth", "refresh": "rot-x", "access": "a", "expires": 9e15, "accountId": "x@fake"})
if err != nil {
t.Fatal(err)
}
return k
}
// the hook comes, and waits
arrived := func() {
t.Helper()
for {
if _, err := os.Stat(hold + ".in"); err == nil {
return
}
select {
case <-ctx.Done():
t.Fatal("the models hook never came")
case <-time.After(10 * time.Millisecond):
}
}
}
checked := func(key string) chan error {
done := make(chan error, 1)
go func() {
_, err := Check(ctx, "fakeco", key)
done <- err
}()
return done
}
// signed out while the hook holds the token it is about to save
key := signIn()
done := checked(key)
arrived()
if err := SignOut(ctx, "fakeco", ""); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(hold, nil, 0o600); err != nil {
t.Fatal(err)
}
<-done
if a := Auths("fakeco"); len(a) != 0 {
t.Fatalf("the hook signed the account in again: %v", a)
}
// not signed out: the token it spent is replaced by one it saved
key = signIn()
if err := <-checked(key); err != nil {
t.Fatal(err)
}
// (a listing the sign-in set off may spend it again too)
if a := Auths("fakeco"); !strings.HasPrefix(fmt.Sprint(a[key]["refresh"]), "rot-x+") {
t.Fatalf("the hook's renewed token wasn't kept: %v", a)
}
}