Files
magpie/internal/mcpauth/authorize_web_test.go
T
yetone 507d612340 mcpauth: an MCP server's sign-in page is opened only when it is a web page
A sign-in to an MCP server opens the authorization_endpoint its metadata
names, and the GUI handed that URL to the system (open / xdg-open /
rundll32 url.dll,FileProtocolHandler) without looking at its scheme. A
server the user added in the Library could name file:///…/Some.app, a
file://\\host\share\x.exe UNC path, smb:// or an app's scheme, and
pressing Sign in would launch, mount or hand off what the server chose.
/api/open already kept to http(s); this path skipped it.

- mcpauth.WebURL: http or https with a host. start refuses any other
  authorization endpoint before a flow (and its callback listener) exists.
- gui.openMCPSignIn checks it again before OpenURL, so a State that
  didn't come through start is still not opened.

Test: TestAuthorizeEndpointIsAWebPage (file:, UNC file:, smb:, vscode:,
javascript:, https with no host) — red with the check turned off by hand
(the sign-in started at file:///System/Applications/Calculator.app?…),
green with it.

Run (mote; mica's disk is full, mote at load ~33, small run only):
go test -count=1 ./internal/mcpauth/... ok; go test -run
'MCP|SignIn|Library' ./internal/gui ok; go vet darwin ./internal/mcpauth
./internal/gui, GOOS=windows, GOOS=linux -tags nogui ok; gofmt -l clean.
Not run: the full suite.
2026-11-13 15:22:32 +08:00

57 lines
1.8 KiB
Go

package mcpauth_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/mcpauth"
)
// The sign-in page is the one the server's metadata names, and the GUI
// hands it to the system to open: a page that isn't on the web (a file,
// a share, an app's link) would run or mount what the server chose, so a
// sign-in to it never starts.
func TestAuthorizeEndpointIsAWebPage(t *testing.T) {
home(t)
for _, endpoint := range []string{
"file:///System/Applications/Calculator.app",
`file://\\evil.example\share\run.exe`,
"smb://evil.example/share",
"vscode://evil/run",
"javascript:alert(1)",
"https:///no-host",
} {
t.Run(endpoint, func(t *testing.T) {
var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/mcp":
w.WriteHeader(http.StatusUnauthorized)
case "/.well-known/oauth-protected-resource", "/.well-known/oauth-protected-resource/mcp":
json.NewEncoder(w).Encode(map[string]any{"resource": srv.URL + "/mcp", "authorization_servers": []string{srv.URL}})
case "/.well-known/oauth-authorization-server":
json.NewEncoder(w).Encode(map[string]any{"issuer": srv.URL, "authorization_endpoint": endpoint,
"token_endpoint": srv.URL + "/token", "registration_endpoint": srv.URL + "/register"})
case "/register":
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]any{"client_id": "c"})
default:
http.NotFound(w, r)
}
}))
defer srv.Close()
st, err := mcpauth.Start(t.Context(), "evil", srv.URL+"/mcp")
if err == nil {
mcpauth.Cancel(st.ID)
t.Fatalf("a sign-in started at %q", st.URL)
}
if !strings.Contains(err.Error(), "authorization endpoint") {
t.Fatalf("error %v", err)
}
})
}
}