Commit Graph
4 Commits
Author SHA1 Message Date
yetone dc1af8231b ghtoken: a Settings token GitHub refuses is said, and requests go without one (#3926)
A GitHub token in Settings → Network and sharing that GitHub refused
(401, expired or revoked) failed every plugin and skill check, so a
stale token was worse than none. Now it is treated as an environment
token already was: the request is asked again without it, the token
isn't sent again while magpie runs or until the field changes, and the
anonymous answer (60 an hour) is the one taken. This overturns the
package comment and TestSettingsTokenRefusedStaysAnError: the token
being the user's choice is a reason to tell them, not to fail the
check.

The page stays honest about it:
- Settings › Network and sharing still shows the token, masked, with
  Remove, and says in red that GitHub refused it and requests go
  without one until it is replaced (/api/settings githubTokenRefused).
  Before, a refused token simply read as none being set.
- After a Settings token's refusal requests go anonymous, not with
  GITHUB_TOKEN/GH_TOKEN: the user put the Settings one ahead of those,
  and the page says "without one".

Patch by MYCF711 (fix/settings-token-fallback, 7d93f1b5): the ghtoken
change and its tests. Changed from it: the notice is in Settings' row,
where the token lives, instead of the patch's Plugins page note,
which cost ~630 bytes of core script the budget (28 bytes of room at
c54478904) doesn't have; the Library's skill checks reach Settings'
row too. Token() doesn't fall through to the environment; the
library's skill check test (the sibling the patch left red) expects
the anonymous answer; the unreachable new Refused() message is
dropped (Refused's "where" now holds for a refused Settings token).
app.js stays in its budget by shortening renderGitHubToken's comment.

Run at this tree (base c54478904), on mote:
- go test -tags nogui ./internal/ghtoken ./internal/plugin
  ./internal/library ./internal/gui: ok (TestCoreScriptBudget
  included; no budget raised). GOOS=linux and windows go vet, go build
  ./...: clean.
- github-token-refused.test.cjs (new; chromium and webkit, en and zh,
  1000 and 440px): red 8 of 8 against origin/main's assets, green
  with the change. With github-token and i18n-keys: 21 of 22; the red
  is i18n-keys "a plugin's words are its own" ("Runs as"), red at
  origin/main 74dcf40aa alone too (#3897).
- Every GUI file that opens Settings › Network and sharing
  (allow-ips, api-key-theme, bare-ids, browser-access, cors-origins,
  gateway-port, lan-docker, proxy-test, settings-proxy-auth,
  settings-proxy-remember): 126 of 126.
- Broken by hand: Token() falling through to GITHUB_TOKEN after the
  Settings token's refusal turns TestSettingsTokenRefusedGoesWithoutEnvToken
  red; MYCF711 showed TestSettingsTokenRefusedAskedAgainWithout red with
  from == "settings" back in Do.

Not tried: a real revoked token against api.github.com. The Plugins and
Library pages have no notice of their own (Settings' row says it).
2026-10-28 03:50:46 +08:00
yetone 0f044e9447 plugins: a GITHUB_TOKEN GitHub refuses is dropped for anonymous requests, not shown as an error (till on Discord)
till's Plugins page said "无法连接 GitHub: GitHub refused GITHUB_TOKEN
(no GitHub token is set in Settings → Network and sharing)". ghtoken.Token
falls back to the environment's GITHUB_TOKEN / GH_TOKEN, and a stale one
there made every GitHub read fail, though GitHub serves the same reads
without a token.

ghtoken.Do now sends a GitHub API request with the token. When the token
came from the environment and GitHub answers 401, it remembers that token
as refused for the rest of the process (Token() skips it, so GH_TOKEN is
still tried after a refused GITHUB_TOKEN, and Settings no longer shows it)
and asks once more without Authorization, returning that answer. A token
set in Settings is never dropped: its 401 is still Refused()'s error. An
anonymous rate limit after the drop is said by LimitedBy as before.

Every caller goes through it: plugin fetchJSONFrom (tagged-repo search,
git plugin checks, githubHead) and getURLFrom (Bun), and library apiGet,
skills fetch, skilltree fetchBlob and latestRTK via doGitHub.
internal/davsync/github.go stays strict: its token is the one the user gave
sync to write a private repo, outside ghtoken, and an anonymous retry can't
write there, so it would only hide why sync failed.

Tests (httptest only): ghtoken_test covers an env token refused then asked
again without, not sent again (and GH_TOKEN tried next), a Settings token
refused stays an error, an env token refused then an anonymous rate limit
is reported, and another host untouched. plugin TestTaggedReposStaleEnvTokenDropped
is till's case: TaggedRepos with GITHUB_TOKEN=stale-till lists 7 repos, no
error. github_token_test and library TestGitHubRateLimitSaid now expect the
env token dropped and the Settings token still refused.

Broken by hand, each red: (a) no anonymous retry: every new env test, the
plugin and library tests red, and TestTaggedReposStaleEnvTokenDropped shows
till's exact error; (b) no memory of the refusal: NotSentAgain, the plugin
and library tests red; (c) a Settings token also retried:
SettingsTokenRefusedStaysAnError, the plugin and library tests red.

Run on the commit before the rebase (base 9b04c4e20):
- gofmt -l: clean. go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows: clean.
- set -o pipefail; go test -timeout 45m ./... at load ~30: 62 packages ok.
  agent, gateway and library ran past 45m while still progressing; gui
  failed TestSetAnswersWhileDevinAndProvidersHang (set took 10.26s, #1375)
  and TestMainWindowCorners (measuring killed, #1936/#2645); provider failed
  TestPluginPicksComeBackWithTheirList (#3051, fixed on main in c922225f1).
- Each rerun alone at load 21-56: library ok 809.7s, provider ok 308.8s,
  gui ok 159.5s, agent ok 1673.3s, gateway ok 780.4s.

After rebasing onto a3ebed202 (main moved 8 commits): gofmt -l clean; go vet
darwin, linux -tags nogui, windows clean; go test -count=1 at load ~57:
ghtoken ok 2.9s, plugin ok 277.6s, library ok 144.2s.
2026-10-21 06:16:31 +08:00
yetone 334bb182e9 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.
2026-10-13 15:19:54 +08:00
yetone 431707944e plugin, library: Check for updates asks GitHub with the GitHub token from Settings, and says when GitHub's rate limit is used up (#1942, bingqilinweimaotai)
What changed: a plugin installed from GitHub (github:owner/repo) is
checked by asking GitHub's API for its branch's commit (githubHead,
through fetchJSON). That request went without a token, so a token saved
in Settings → Network and sharing did nothing for it: the check stayed
at GitHub's 60 requests an hour for the whole address, which a shared
proxy exit uses up, and the row said only "<url>: 403 Forbidden".

- The token lookup, where a token may go and how a rate limit is told
  move from internal/library into a new internal/ghtoken, shared by the
  library and the plugin package (plugin can't import library).
  The order is unchanged: Settings, then GITHUB_TOKEN, then GH_TOKEN.
- plugin's fetchJSON has a request to GitHub's API's host (githubAPI)
  carry it, which covers the git plugins' check and the Plugins page's
  topic search. Raw files, npm and every other host never get it, and
  source.Do already sends nothing with Authorization to a mirror.
- An answer from GitHub's API that is a rate limit (429, or 403 with
  X-RateLimit-Remaining 0, Retry-After or a rate-limit message) is a
  ghtoken.Limited: the check's row has Why "limited" and Limited {until,
  token} for the page, as the library's skill check already had. A 401
  to a request with a token names which token was refused, never the
  token.

What was run:
- TestGitPluginCheckCarriesGitHubToken (new, internal/plugin): asks with
  no token / GH_TOKEN / GITHUB_TOKEN / Settings in that order of
  precedence, a 403 rate limit with and without a token, a 429 with
  Retry-After, a refused GH_TOKEN, and a raw-file host that must get no
  Authorization. Broken by hand twice: with fetchJSON's Authorize removed
  it fails "GH_TOKEN: GitHub's API was asked with Authorization "", want
  "Bearer gh-env"" (and the other two sources); with the limit check
  removed it fails with Why "registry", Limited nil, "403 Forbidden".
- go test -tags nogui -count=1 ./internal/library ./internal/plugin
  (temp HOME, pinned caches): ok, the library's TestGitHubToken*,
  TestGitHubRateLimitSaid among them.
- gofmt -l clean; go vet -tags nogui on darwin, GOOS=linux and
  GOOS=windows for internal/plugin, library, ghtoken, gui: clean.
- Not tried against the real api.github.com (tests stay off the network).
2026-10-13 15:19:53 +08:00