From 334bb182e92fac5048d4ff5c3396a821249e512e Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 13 Oct 2026 14:45:04 +0800 Subject: [PATCH] plugin: Bun's release check asks GitHub's API with the GitHub token from Settings too (#1942, bingqilinweimaotai) What changed: an audit of every request magpie makes to GitHub's API after the git-plugin fix. The library's skill checks, private-repo probes and RTK's release check already carried the token; the Plugins page's git-plugin check and topic search got it in the previous commit. The one left was Bun's newest-release check (bunLatest), asked of api.github.com/repos/oven-sh/bun/releases/latest anonymously through getURL. getURLFrom now has a request to GitHub's API's host carry the token (ghtoken.Authorize), and bunLatest asks githubAPI so tests can move it. Bun's zips (github.com, npmmirror) are other hosts and never get it. With a token set, that one request isn't sent to the MAGPIE_MIRRORS fallback (source.Do sends nothing with Authorization to a mirror), the same as the library's API requests. Left as they are: Copilot's api.github.com requests carry the user's Copilot sign-in, not this token; raw.githubusercontent.com and codeload aren't the rate-limited REST API; site/worker.js is the website's. What was run: - TestBunReleaseCheckCarriesGitHubToken (new): bunLatest is asked with "Bearer github-env", a download from another host with none. With the Authorize call removed it fails: "Bun's release was asked of GitHub's API with Authorization """. - go test -tags nogui -run 'Bun' ./internal/plugin: ok. - gofmt -l clean; go vet -tags nogui darwin, GOOS=linux, GOOS=windows on internal/plugin: clean. --- internal/plugin/bun.go | 4 ++++ internal/plugin/bunupdate.go | 2 +- internal/plugin/github_token_test.go | 36 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/internal/plugin/bun.go b/internal/plugin/bun.go index f3e8757a9..269ec1fcc 100644 --- a/internal/plugin/bun.go +++ b/internal/plugin/bun.go @@ -23,6 +23,7 @@ import ( "time" "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/ghtoken" "github.com/yetone/magpie/internal/libc" "github.com/yetone/magpie/internal/netproxy" "github.com/yetone/magpie/internal/source" @@ -283,6 +284,9 @@ func getURLFrom(ctx context.Context, url string, limit int64, mirror bool) ([]by if err != nil { return nil, err } + // Bun's newest release is asked of GitHub's API: with the user's + // GitHub token, as every request magpie makes to it is (#1942) + ghtoken.Authorize(req, githubAPI) var res *http.Response if mirror { res, err = source.Do(http.DefaultClient, req) diff --git a/internal/plugin/bunupdate.go b/internal/plugin/bunupdate.go index 2740c94cb..649afe73d 100644 --- a/internal/plugin/bunupdate.go +++ b/internal/plugin/bunupdate.go @@ -145,7 +145,7 @@ func bunReported(exe string) string { // bunLatest is Bun's newest release and when it was published (tests stub it). var bunLatest = func(ctx context.Context) (string, time.Time, error) { - b, err := getURL(ctx, "https://api.github.com/repos/oven-sh/bun/releases/latest", 4<<20) + b, err := getURL(ctx, githubAPI+"/repos/oven-sh/bun/releases/latest", 4<<20) if err != nil { return "", time.Time{}, err } diff --git a/internal/plugin/github_token_test.go b/internal/plugin/github_token_test.go index 298f0b65d..02a39b0d5 100644 --- a/internal/plugin/github_token_test.go +++ b/internal/plugin/github_token_test.go @@ -169,3 +169,39 @@ func TestGitPluginCheckCarriesGitHubToken(t *testing.T) { t.Errorf("a host other than GitHub's API was sent Authorization %q", rawAuth) } } + +// Bun's newest release, asked of GitHub's API, carries the token too; a +// Bun download from another host never does (#1942). +func TestBunReleaseCheckCarriesGitHubToken(t *testing.T) { + dir := t.TempDir() + t.Setenv("HOME", dir) + t.Setenv("USERPROFILE", dir) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config")) + t.Setenv("GITHUB_TOKEN", "github-env") + var apiAuth, dlAuth string + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + apiAuth = r.Header.Get("Authorization") + fmt.Fprint(w, `{"tag_name":"bun-v1.4.2","published_at":"2026-10-01T00:00:00Z"}`) + })) + t.Cleanup(api.Close) + dl := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + dlAuth = r.Header.Get("Authorization") + fmt.Fprint(w, "zip") + })) + t.Cleanup(dl.Close) + was := githubAPI + githubAPI = api.URL + t.Cleanup(func() { githubAPI = was }) + if v, _, err := bunLatest(context.Background()); err != nil || v != "1.4.2" { + t.Fatalf("bunLatest = %q, %v", v, err) + } + if apiAuth != "Bearer github-env" { + t.Errorf("Bun's release was asked of GitHub's API with Authorization %q", apiAuth) + } + if _, err := getURL(context.Background(), dl.URL+"/bun-darwin-aarch64.zip", 1<<10); err != nil { + t.Fatal(err) + } + if dlAuth != "" { + t.Errorf("a download from another host was sent Authorization %q", dlAuth) + } +}