Files
magpie/internal/mcpauth/mcpauth_cancel_test.go
T
ggbdpqandfactory-droid[bot] 1a7aa76b4b mcpauth: an MCP sign-in canceled while its token is being fetched no longer saves the late token or replaces the server's sign-in (#1750, ggbdpq)
Cancel set the flow canceled, but a callback already trading its code
went on to write mcp-signins.json and call Changed when the token came
back, so a sign-in the user had ended was saved, and an existing
sign-in to that server was replaced. The callback's write now happens
under the flow's lock only while the flow is live (flow.keep), so
Cancel or the timeout and the commit can't both win; a commit that wins
still tells the agents before the flow says done.

ggbdpq's patch (46440adc), cherry-picked unchanged.

Run (reviewer): TestCanceledMCPCallbackDoesNotSave (real discovery,
registration, PKCE and loopback callback against mcpauthtest, the token
reply held until Cancel) fails on the old mcpauth.go in both cases
("saved credentials", "redistributed credentials 1 times") and passes
with it, -race -count=5; go test -race ./internal/mcpauth/... ok.
Not tried with a real MCP server's OAuth.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
2026-10-14 04:59:08 +08:00

102 lines
2.5 KiB
Go

package mcpauth_test
import (
"bytes"
"io"
"net/http"
"os"
"path/filepath"
"sync"
"sync/atomic"
"testing"
"github.com/yetone/magpie/internal/mcpauth"
"github.com/yetone/magpie/internal/mcpauth/mcpauthtest"
"github.com/yetone/magpie/internal/settings"
)
type heldTokenResponse struct {
base http.RoundTripper
entered chan struct{}
release chan struct{}
once sync.Once
}
func (h *heldTokenResponse) RoundTrip(r *http.Request) (*http.Response, error) {
resp, err := h.base.RoundTrip(r)
if err == nil && r.URL.Path == "/oauth/token" {
h.once.Do(func() { close(h.entered) })
<-h.release
}
return resp, err
}
// Cancel must still win after the browser claims its callback, while the
// token endpoint's successful reply is in flight.
func TestCanceledMCPCallbackDoesNotSave(t *testing.T) {
for _, previous := range []bool{false, true} {
name := "new sign-in"
if previous {
name = "existing sign-in"
}
t.Run(name, func(t *testing.T) {
home(t)
f := mcpauthtest.New(t)
if previous {
f.SignIn(t, "demo")
}
path := filepath.Join(settings.Dir(), "mcp-signins.json")
before, err := os.ReadFile(path)
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
h := &heldTokenResponse{
base: http.DefaultTransport,
entered: make(chan struct{}),
release: make(chan struct{}),
}
oldClient, oldChanged := mcpauth.Client, mcpauth.Changed
mcpauth.Client = &http.Client{Transport: h}
var changed atomic.Int32
mcpauth.Changed = func(string) { changed.Add(1) }
t.Cleanup(func() {
mcpauth.Client, mcpauth.Changed = oldClient, oldChanged
})
st, err := mcpauth.Start(t.Context(), "demo", f.URL)
if err != nil {
t.Fatal(err)
}
defer mcpauth.Cancel(st.ID)
done := make(chan error, 1)
go func() {
resp, err := http.Get(st.URL)
if err == nil {
_, err = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
done <- err
}()
<-h.entered
mcpauth.Cancel(st.ID)
got, _ := mcpauth.Progress(st.ID)
if got.State != "canceled" {
t.Errorf("state after cancellation = %q", got.State)
}
close(h.release)
if err := <-done; err != nil {
t.Fatal(err)
}
after, err := os.ReadFile(path)
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
if !bytes.Equal(before, after) {
t.Errorf("canceled callback saved credentials: previous sign-in = %v", previous)
}
if got := changed.Load(); got != 0 {
t.Errorf("canceled callback redistributed credentials %d times", got)
}
})
}
}