From d27f659b4cdffa621902210abecdfd23b74a2761 Mon Sep 17 00:00:00 2001 From: him0 Date: Sat, 21 Nov 2026 03:58:42 +0900 Subject: [PATCH] fix: only call gh for GitHub hosts The host passed to `gh api graphql --hostname` comes from the remote URL, which can be any git server. gh sends GH_ENTERPRISE_TOKEN (and GITHUB_ENTERPRISE_TOKEN) to every host other than github.com, so a sync in a repository whose remote is not on GitHub sent that token to the remote's host. gh is now only called for github.com, *.ghe.com, the host in GH_HOST and hosts with credentials stored by `gh auth login`. Any other host is a failed lookup: the branches that depend on it are kept. Co-Authored-By: Claude Mythos 5.2 --- README.ja.md | 4 ++- README.md | 4 ++- main.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++-- main_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 4 deletions(-) diff --git a/README.ja.md b/README.ja.md index 84b0cb4..1ebeb4f 100644 --- a/README.ja.md +++ b/README.ja.md @@ -59,6 +59,8 @@ upstream が削除されたブランチは、次のどちらかに当てはま pull request は `gh api graphql` で確認します。確認するのは各 pull request の最後の 100 コミットだけで、追加の fetch はしません。確認できなかった場合、その結果が必要なブランチは残します。[トラブルシューティング](#トラブルシューティング)を参照してください。 +`gh` を呼ぶのは、github.com、`*.ghe.com`、`GH_HOST` のホスト、`gh auth login` でログイン済みのホストだけです。それ以外のホストの remote には、`GH_ENTERPRISE_TOKEN` のものも含めてトークンを送りません。 + ### 安全性 - merge、rebase、強制更新、push は一切しません。ローカルにしか無いコミットを持つブランチは必ず残ります。 @@ -154,7 +156,7 @@ main remote に使える `HEAD`、`main`、`master` がありません。`git re `branch..merge` が無いか、不正です。`git branch --set-upstream-to=/ ` で直してください。 **`warning: couldn't look up merged pull requests: `** -`gh` が未認証か、remote が GitHub 上に無いか、API が失敗したか 30 秒以内に応答しませんでした。確認できるようになるまで、コミットがデフォルトブランチに含まれないブランチは `... and pull requests couldn't be looked up; not deleted` と警告して残します。`gh auth status` を確認してください。`~/.ssh/config` のホストエイリアスや `ssh.github.com` は `ssh -G` で解決します。 +`gh` が未認証か、remote が GitHub 上に無いか、API が失敗したか 30 秒以内に応答しませんでした。確認できるようになるまで、コミットがデフォルトブランチに含まれないブランチは `... and pull requests couldn't be looked up; not deleted` と警告して残します。`gh auth status` を確認してください。GitHub Enterprise Server の場合は、`gh auth login --hostname ` でログインするか、`GH_HOST` でそのホストを指定する必要があります。`GH_ENTERPRISE_TOKEN` のトークンだけでは確認しません。`~/.ssh/config` のホストエイリアスや `ssh.github.com` は `ssh -G` で解決します。 バグを報告するときは、`gh sync --version` と `gh sync --verbose` の出力を添えてください。 diff --git a/README.md b/README.md index 5ceadfb..454f6b3 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,8 @@ A branch with commits added on top of a merged pull request's head is kept. Pull requests are looked up with `gh api graphql`. Only the last 100 commits of each are checked, and nothing extra is fetched. If the lookup fails, the branches that depend on it are kept; see [Troubleshooting](#troubleshooting). +`gh` is only called for github.com, `*.ghe.com`, the host in `GH_HOST`, and hosts you logged in to with `gh auth login`. A remote on any other host is never sent a token, including one from `GH_ENTERPRISE_TOKEN`. + ### Safety - gh-sync never merges, rebases, force-updates or pushes. A branch with local-only commits is always kept. @@ -154,7 +156,7 @@ Local changes in the checked-out branch would be overwritten. Commit or stash th `branch..merge` is missing or invalid. Fix it with `git branch --set-upstream-to=/ `. **`warning: couldn't look up merged pull requests: `** -`gh` is not authenticated, the remote is not on GitHub, or the API failed or took more than 30 seconds. Branches whose commits are not in the default branch are kept, with `... and pull requests couldn't be looked up; not deleted`, until the lookup works. Check `gh auth status`. SSH host aliases from `~/.ssh/config` and `ssh.github.com` are resolved with `ssh -G`. +`gh` is not authenticated, the remote is not on GitHub, or the API failed or took more than 30 seconds. Branches whose commits are not in the default branch are kept, with `... and pull requests couldn't be looked up; not deleted`, until the lookup works. Check `gh auth status`. For GitHub Enterprise Server, the host must be logged in to with `gh auth login --hostname ` or named by `GH_HOST`; a token in `GH_ENTERPRISE_TOKEN` alone is not enough. SSH host aliases from `~/.ssh/config` and `ssh.github.com` are resolved with `ssh -G`. When reporting a bug, please include the output of `gh sync --version` and `gh sync --verbose`. diff --git a/main.go b/main.go index c5a2321..d40ecd2 100644 --- a/main.go +++ b/main.go @@ -79,7 +79,7 @@ func newColorConfig(enabled bool) colorConfig { // are not in the default branch (squash or rebase merges). Tests stub it. // // sshHosts caches the host names resolved with `ssh -G`, so ssh runs at most -// once per host per sync. +// once per host per sync. ghHosts does the same for checkGhHost. // // removeWorktrees makes deleteBranch remove the worktree of a branch it is // about to delete, when git agrees to without --force, instead of keeping the @@ -96,6 +96,7 @@ type runConfig struct { removeWorktrees bool findMergedPRs mergedPRFinder sshHosts map[string]string + ghHosts map[string]bool report *syncReport } @@ -238,6 +239,7 @@ func run(args []string, findMergedPRs mergedPRFinder) error { dryRun: dryRun, findMergedPRs: findMergedPRs, sshHosts: make(map[string]string), + ghHosts: make(map[string]bool), report: &syncReport{}, } @@ -1422,6 +1424,9 @@ func findMergedPRsWithGh(cfg runConfig, remote Remote, base string, heads []stri return nil, err } repo.host = resolveGitHubHost(cfg, remote.URL, repo.host) + if err := checkGhHost(cfg, repo.host); err != nil { + return nil, err + } query, vars := buildMergedPRQuery(repo, base, heads) args := []string{"api", "graphql", "--hostname", repo.host, "-F", "query=@-"} for _, v := range vars { @@ -1539,7 +1544,7 @@ var scpLikeURLRe = regexp.MustCompile(`^(?:[^@/]+@)?([^/:]+):(.+)$`) // parseGitHubURL extracts host, owner and repository name from a remote URL // in https, ssh, git or scp-like form. Local paths and URLs whose path is not // exactly "/" are rejected. Whether the host is actually a GitHub -// host is left to gh, which fails for hosts it isn't authenticated to. +// host is decided by checkGhHost. func parseGitHubURL(raw string) (gitHubRepo, error) { var host, path string if strings.Contains(raw, "://") { @@ -1566,6 +1571,67 @@ func parseGitHubURL(raw string) (gitHubRepo, error) { return gitHubRepo{host: host, owner: parts[0], name: parts[1]}, nil } +// ghTokenEnvVars are the environment variables gh reads a token from. +// GH_ENTERPRISE_TOKEN and GITHUB_ENTERPRISE_TOKEN apply to every host other +// than github.com, whatever it is; in a codespace GITHUB_TOKEN does too. +var ghTokenEnvVars = []string{"GH_TOKEN", "GITHUB_TOKEN", "GH_ENTERPRISE_TOKEN", "GITHUB_ENTERPRISE_TOKEN"} + +// checkGhHost returns an error unless host is a GitHub host that gh may send +// a token to. The host comes from the remote URL, which can be any git +// server, and gh sends a token from the environment (see ghTokenEnvVars) to +// any host it is asked to call. So gh is only called for: +// +// - github.com and GitHub Enterprise Cloud with data residency (*.ghe.com), +// - the host named by GH_HOST, +// - a host `gh auth login` stored credentials for. +func checkGhHost(cfg runConfig, host string) error { + allowed, cached := cfg.ghHosts[host] + if !cached { + allowed = isGitHubCloudHost(host) || strings.EqualFold(host, os.Getenv("GH_HOST")) || ghHasStoredToken(cfg, host) + if cfg.ghHosts != nil { + cfg.ghHosts[host] = allowed + } + } + if !allowed { + return fmt.Errorf("%q is not a GitHub host gh is logged in to (run `gh auth login --hostname `, or set GH_HOST for GitHub Enterprise Server)", host) + } + return nil +} + +// isGitHubCloudHost reports whether host is operated by GitHub: github.com, +// one of its subdomains, or a *.ghe.com tenant. +func isGitHubCloudHost(host string) bool { + host = strings.ToLower(host) + return host == "github.com" || strings.HasSuffix(host, ".github.com") || strings.HasSuffix(host, ".ghe.com") +} + +// ghHasStoredToken reports whether gh has credentials of its own for host, +// from `gh auth login`. `gh auth token` is run without the token environment +// variables, which would otherwise answer for any host. The token it prints +// is discarded. +func ghHasStoredToken(cfg runConfig, host string) bool { + args := []string{"auth", "token", "--hostname", host} + verboseLog(cfg, "gh", args) + ctx, cancel := context.WithTimeout(context.Background(), ghTimeout) + defer cancel() + cmd := exec.CommandContext(ctx, "gh", args...) + cmd.Env = withoutEnvVars(os.Environ(), ghTokenEnvVars) + return cmd.Run() == nil +} + +// withoutEnvVars returns env ("NAME=value" entries) without the variables in +// names. Names are compared without regard to case, as Windows does. +func withoutEnvVars(env, names []string) []string { + kept := make([]string, 0, len(env)) + for _, entry := range env { + name, _, _ := strings.Cut(entry, "=") + if !slices.ContainsFunc(names, func(n string) bool { return strings.EqualFold(n, name) }) { + kept = append(kept, entry) + } + } + return kept +} + // sshTimeout bounds `ssh -G`, which only reads the SSH config. const sshTimeout = 5 * time.Second diff --git a/main_test.go b/main_test.go index 9ccb070..f5fcfe9 100644 --- a/main_test.go +++ b/main_test.go @@ -2329,3 +2329,77 @@ func TestRunSkipsUpstreamOutsideFetchRefspec(t *testing.T) { } } } + +func TestIsGitHubCloudHost(t *testing.T) { + tests := []struct { + host string + want bool + }{ + {"github.com", true}, + {"GitHub.com", true}, + {"gist.github.com", true}, + {"acme.ghe.com", true}, + {"github.example.com", false}, + {"gitlab.com", false}, + {"evilgithub.com", false}, + {"github.com.evil.example", false}, + {"ghe.com.evil.example", false}, + {"", false}, + } + for _, tt := range tests { + if got := isGitHubCloudHost(tt.host); got != tt.want { + t.Errorf("isGitHubCloudHost(%q) = %v, want %v", tt.host, got, tt.want) + } + } +} + +func TestWithoutEnvVars(t *testing.T) { + env := []string{"PATH=/bin", "GH_TOKEN=a", "gh_enterprise_token=b", "GH_TOKEN_OTHER=c", "GITHUB_ENTERPRISE_TOKEN=d=e"} + got := withoutEnvVars(env, ghTokenEnvVars) + want := []string{"PATH=/bin", "GH_TOKEN_OTHER=c"} + if !slices.Equal(got, want) { + t.Errorf("withoutEnvVars = %q, want %q", got, want) + } +} + +func TestCheckGhHost(t *testing.T) { + // An empty PATH makes `gh auth token` fail, as it does for a host gh has + // no stored credentials for: a token in the environment must not make an + // arbitrary host acceptable. + t.Setenv("PATH", "") + t.Setenv("GH_ENTERPRISE_TOKEN", "token") + t.Setenv("GH_HOST", "ghes.example.com") + tests := []struct { + host string + wantErr bool + }{ + {"github.com", false}, + {"acme.ghe.com", false}, + {"ghes.example.com", false}, + {"GHES.example.com", false}, + {"gitlab.com", true}, + } + for _, tt := range tests { + cfg := runConfig{ghHosts: make(map[string]bool)} + if err := checkGhHost(cfg, tt.host); (err != nil) != tt.wantErr { + t.Errorf("checkGhHost(%q) error = %v, wantErr %v", tt.host, err, tt.wantErr) + } + } + + // A cached answer is used without asking gh again. + cfg := runConfig{ghHosts: map[string]bool{"ghes2.example.com": true}} + if err := checkGhHost(cfg, "ghes2.example.com"); err != nil { + t.Errorf("checkGhHost with a cached host: %v", err) + } +} + +func TestFindMergedPRsWithGhRejectsOtherHosts(t *testing.T) { + t.Setenv("PATH", "") + t.Setenv("GH_ENTERPRISE_TOKEN", "token") + t.Setenv("GH_HOST", "") + cfg := runConfig{sshHosts: make(map[string]string), ghHosts: make(map[string]bool)} + _, err := findMergedPRsWithGh(cfg, Remote{Name: "origin", URL: "https://gitlab.com/owner/repo.git"}, "main", []string{"feature"}) + if err == nil || !strings.Contains(err.Error(), "not a GitHub host") { + t.Errorf("findMergedPRsWithGh error = %v, want a host rejection", err) + } +}