Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ AGENTFLOW_PLATFORM_MCP_URL=
# Optional managed Git and TAPD web hosts, with a port if required.
AGENTFLOW_GONGFENG_HOST=
AGENTFLOW_TAPD_HOST=
# Optional self-hosted GitLab instances allowed to receive Git HTTPS tokens
# (comma-separated host or host:port). Empty disables GitLab repositories.
AGENTFLOW_GITLAB_ALLOWED_HOSTS=
# Optional private networks the control plane may dial for Git
# (comma-separated CIDRs, for example an internal GitLab on 10.0.0.0/8).
AGENTFLOW_GIT_EGRESS_ALLOW_CIDRS=
# Optional deployment-level credential for a configured tracker MCP.
AGENTFLOW_MCP_TOKEN=
# Optional model IDs available to your CodeBuddy account (comma-separated):
Expand Down
6 changes: 6 additions & 0 deletions chatbot/src/QuickAccessWizard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,14 @@ function parseGitHubRepo(raw) {
}

// Explicit SSH keeps its transport. Managed HTTPS links use the configured host.
/**
* GitLab 自托管主机(gitlab:<host>/<path> 或内网裸 URL)优先于工蜂与通用 HTTPS,
* 让向导也能绑定与 web 控制台一致的主机级仓库标识。
* --- @ty.aicoding@1795420649000 ---
*/
export function parseGongfengRepo(raw, host = "") {
return globalThis.LoopForgeGitSSH(raw) ||
globalThis.LoopForgeGitLab?.(raw) ||
globalThis.LoopForgeGongfengRepository(raw, host) ||
globalThis.LoopForgeGitHTTPS(raw) ||
parseGitHubRepo(raw);
Expand Down
5 changes: 5 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ services:
AGENTFLOW_GITHUB_CLIENT_ID: ${AGENTFLOW_GITHUB_CLIENT_ID:-}
AGENTFLOW_GONGFENG_HOST: ${AGENTFLOW_GONGFENG_HOST:-}
AGENTFLOW_TAPD_HOST: ${AGENTFLOW_TAPD_HOST:-}
# Comma-separated self-hosted GitLab instances (host or host:port) allowed
# to receive per-host Git HTTPS tokens. Empty disables GitLab repositories.
AGENTFLOW_GITLAB_ALLOWED_HOSTS: ${AGENTFLOW_GITLAB_ALLOWED_HOSTS:-}
# Private networks the control plane may dial for Git (internal GitLab, etc).
AGENTFLOW_GIT_EGRESS_ALLOW_CIDRS: ${AGENTFLOW_GIT_EGRESS_ALLOW_CIDRS:-}
AGENTFLOW_MCP_TOKEN: ${AGENTFLOW_MCP_TOKEN:-}
AGENTFLOW_GIT_SSH_KNOWN_HOSTS: ${AGENTFLOW_GIT_SSH_KNOWN_HOSTS:-}
AGENTFLOW_ASSISTANT_SESSION_DIR: /data/assistant
Expand Down
4 changes: 4 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ Use Git SSH for GitLab.com, self-managed GitLab, Gitea, Gogs, GitHub, and other

Repositories automatically use the current user's key for their SSH host and port; repositories on the same host and port share that key. The repository form only shows a red warning when that host has no personal key. This reminder does not block saving the repository or completing setup; credentials and host trust can be configured before the first Git operation. There is no repository-specific or default fallback key. Checkout and SOP execution use the responsible user's keys. Conversation submission and the workspace terminal use the requesting user's keys. The temporary signing agent closes when the command or terminal ends. A terminal's agent never lives in the shared workspace: with the `bwrap` driver it sits in a private directory bound only into that terminal, and with the `local` driver (no isolation) it stays outside the workspace in a 0700 directory. Docker and seatbelt workspaces share one filesystem view among all their processes, so their terminals get no personal SSH key; push from there with the platform's submit action. `git@github.com:` and `ssh://git@github.com/` addresses are saved as GitHub repositories (HTTPS + token, automatic pull requests) unless `provider` is explicitly `ssh`. To rotate a key, replace it in settings and revoke the old public key at the host.

### Self-hosted GitLab over HTTPS

Self-hosted GitLab MR delivery is opt-in per instance. Set `AGENTFLOW_GITLAB_ALLOWED_HOSTS` to a comma-separated list of instances, each as `host` or `host:port`; an `https://` prefix, a trailing slash and letter case are normalized away. For example: `gitlab.example.com,gitlab.internal:8443`. Only listed hosts can be bound as `gitlab:<host>/<group>/<repo>` repositories, and the owning or requesting user's per-host Git HTTPS credential is sent to them as `PRIVATE-TOKEN` for MR create/merge and status sync. Bind a repository by pasting its full `http(s)://host/group/repo` URL (bare URLs are accepted for loopback and RFC1918 instances plus `.internal`/`.local` names; public self-hosted instances must use the explicit `gitlab:<host>/<group>/<repo>` identity) or by entering the prefixed identity directly. Users register the token under **Personal settings → Git HTTPS credentials** with platform `gitlab` for that host. Unlisted hosts are rejected when a repository is saved, and the platform refuses to send any credential to them; loopback and RFC1918 addresses are not exempt, because a service on an internal address can capture the token too. When the instance is only reachable on private addresses, also list its networks in `AGENTFLOW_GIT_EGRESS_ALLOW_CIDRS` so the control plane (token verification and MR API calls) may dial it. An empty or unset allowlist disables GitLab repository bindings. The list is read per request, so an edit takes effect without a restart; declare the instance before users bind repositories. When a turn ends, merge requests the agent opened on a declared instance (authored by the speaking user, head commit still in the workspace) are registered as the requirement's delivery, the same way GitHub pull requests are. GitLab issue import is not supported; MR delivery state is refreshed by the MR sync task instead.

SSH handles clone, fetch and push. A conversation delivery returns the branch and commit SHA. You can optionally add a repository web URL to open the host and create a PR/MR manually. Issue import and PR/MR automation need a hosting API credential. To automate those actions, add a hosting-specific MCP in **Settings → Assets → MCP**, give it its own scoped token, and attach it to the relevant agent or workflow. [CNB does not support Git SSH](https://docs.cnb.cool/en/guide/git-access.html); it uses HTTPS with a CNB access token, which is outside this integration.

## Accounts
Expand Down
87 changes: 87 additions & 0 deletions internal/domain/gitlab_allowlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package domain

import (
"net"
"os"
"strings"
)

// GitLabAllowedHostsEnv 声明本部署允许接入的自托管 GitLab 实例:逗号分隔,
// 每项为 host 或 host:port,可带 http(s):// 前缀(归一化时去掉)。
//
// 它是凭据的信任边界而不是可选项:PRIVATE-TOKEN 只会发往这里声明的实例。
// 未声明时一律拒绝,包括回环与内网地址——项目成员可以在任意内网地址上起
// 一个服务,若按"内网即可信"放行,owner 的 Git 令牌仍会被送过去。运维在
// 部署配置里显式声明实例,才把该实例纳入凭据可达范围。
const GitLabAllowedHostsEnv = "AGENTFLOW_GITLAB_ALLOWED_HOSTS"

// GitLabAllowedHosts 返回归一化后的实例 allowlist(小写、去 scheme/斜杠)。
func GitLabAllowedHosts() []string {
raw := os.Getenv(GitLabAllowedHostsEnv)
if strings.TrimSpace(raw) == "" {
return nil
}
entries := []string{}
for _, item := range strings.Split(raw, ",") {
if entry := normalizeGitLabHost(item); entry != "" {
entries = append(entries, entry)
}
}
return entries
}

// GitLabHostAllowed 报告 host[:port] 是否是管理员显式声明的 GitLab 实例。
// 带端口的条目光精确匹配 host:port;只写 host 的条目匹配该 host 的任意端口。
func GitLabHostAllowed(host string) bool {
target := normalizeGitLabHost(host)
if target == "" {
return false
}
targetHost, _ := splitGitLabHostPort(target)
for _, entry := range GitLabAllowedHosts() {
if entry == target {
return true
}
entryHost, entryHasPort := splitGitLabHostPort(entry)
// 显式写了端口的条目只匹配同一端口(上面的精确比较已覆盖),
// 不再按 host 放宽。
if !entryHasPort && entryHost == targetHost {
return true
}
}
return false
}

// GitLabInstanceHost 从 "gitlab:<host>/<group>/<repo>" 标识取 host。
func GitLabInstanceHost(id string) (string, bool) {
rest, found := strings.CutPrefix(strings.TrimSpace(id), "gitlab:")
if !found {
return "", false
}
slash := strings.Index(rest, "/")
if slash <= 0 {
return "", false
}
return rest[:slash], true
}

// normalizeGitLabHost 归一化 allowlist 条目与待匹配 host:去空白、去
// http(s):// 前缀与结尾斜杠、转小写。
func normalizeGitLabHost(raw string) string {
host := strings.ToLower(strings.TrimSpace(raw))
host = strings.TrimPrefix(host, "https://")
host = strings.TrimPrefix(host, "http://")
return strings.TrimSuffix(host, "/")
}

// splitGitLabHostPort 拆出 host 与"是否显式带端口"。解析失败(如 IPv6 字面量
// 未加方括号)时按无端口处理,由精确比较兜底。
func splitGitLabHostPort(hostport string) (string, bool) {
if host, _, err := net.SplitHostPort(hostport); err == nil {
return host, true
}
if strings.HasPrefix(hostport, "[") && strings.HasSuffix(hostport, "]") {
return strings.Trim(hostport, "[]"), false
}
return hostport, false
}
53 changes: 53 additions & 0 deletions internal/domain/gitlab_allowlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package domain

import "testing"

// 回归(评审阻塞项):token 只能发往管理员显式声明的 GitLab 实例。
// 未配置白名单时任何主机(含回环/内网)都不得放行,避免成员绑定
// gitlab:<任意 host> 后把 owner 的 Git 令牌当 PRIVATE-TOKEN 发出去。
func TestGitLabHostAllowedRequiresExplicitAllowlist(t *testing.T) {
t.Setenv(GitLabAllowedHostsEnv, "")
for _, host := range []string{
"gitlab.example.test",
"gitlab.example.test:8443",
"127.0.0.1:8080",
"10.1.2.3",
"gitlab.corp.internal",
} {
if GitLabHostAllowed(host) {
t.Fatalf("host %q must be rejected without an admin allowlist", host)
}
}
}

func TestGitLabHostAllowedMatchesConfiguredInstances(t *testing.T) {
t.Setenv(GitLabAllowedHostsEnv, " https://GitLab.Example.Test , gitlab.port.test:8443 ,,")
cases := map[string]bool{
"gitlab.example.test": true, // scheme 前缀与大小写都归一化
"GitLab.Example.Test": true, // host 匹配大小写不敏感
"gitlab.example.test:8443": true, // 只写 host 的条目匹配任意端口
"gitlab.port.test:8443": true, // 带端口的条目精确匹配
"gitlab.port.test": false, // 条目已钉死端口
"gitlab.port.test:9443": false,
"sub.gitlab.example.test": false, // 不做通配/子串匹配
"gitlab.attacker.example": false,
"": false,
}
for host, want := range cases {
if got := GitLabHostAllowed(host); got != want {
t.Fatalf("GitLabHostAllowed(%q) = %v, want %v", host, got, want)
}
}
}

func TestGitLabInstanceHostParsesPrefixedID(t *testing.T) {
host, ok := GitLabInstanceHost("gitlab:gitlab.example.test:8443/group/repo")
if !ok || host != "gitlab.example.test:8443" {
t.Fatalf("host = %q ok = %v, want gitlab.example.test:8443", host, ok)
}
for _, bad := range []string{"", "gitlab:", "gitlab:/group/repo", "gitlab:host", "group/repo"} {
if _, ok := GitLabInstanceHost(bad); ok {
t.Fatalf("GitLabInstanceHost(%q) must fail", bad)
}
}
}
128 changes: 128 additions & 0 deletions internal/domain/gitlab_repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package domain

import "testing"

// 测试主机名全部使用占位符(RFC2606 保留域 example.test),
// 不出现任何真实内网地址或项目路由。ParseGitLabRepositoryURL 的裸 URL 路径
// 要求可信内网主机,因此示例统一使用显式 gitlab: 前缀形态(管理员声明,任意主机)。

func TestParseGitLabRepositoryURL(t *testing.T) {
want := "gitlab:gitlab.example.test/group/repository"
cases := []string{
"gitlab:gitlab.example.test/group/repository",
"gitlab:gitlab.example.test/group/repository.git",
}
for _, raw := range cases {
got, ok := ParseGitLabRepositoryURL(raw)
if !ok || got.Project != want {
t.Fatalf("%s: got %+v ok=%v want %s", raw, got, ok, want)
}
// 前缀不带 scheme 信息:按安全默认 https,绝不静默降级成明文 http。
if got.Name != "repository" || got.CloneURL != "https://gitlab.example.test/group/repository.git" {
t.Fatalf("%s: name=%s clone=%s", raw, got.Name, got.CloneURL)
}
}
}

func TestParseGitLabRepositoryURLPreservesScheme(t *testing.T) {
// 显式 https 保留;显式 http(内网实例常见)也保留,来源 URL 是权威。
if got, ok := ParseGitLabRepositoryURL("https://gitlab.example.internal/g/r"); !ok ||
got.WebURL != "https://gitlab.example.internal/g/r" || got.CloneURL != "https://gitlab.example.internal/g/r.git" {
t.Fatalf("https must be preserved: %+v %v", got, ok)
}
if got, ok := ParseGitLabRepositoryURL("http://gitlab.example.internal/g/r"); !ok ||
got.WebURL != "http://gitlab.example.internal/g/r" || got.CloneURL != "http://gitlab.example.internal/g/r.git" {
t.Fatalf("explicit http must be kept for internal instances: %+v %v", got, ok)
}
}

func TestGitLabInstanceScheme(t *testing.T) {
// host[:port] 与标识一致时取 URL 的 scheme;不匹配或缺失时默认 https。
if s := GitLabInstanceScheme("gitlab.example.test:8443", "https://gitlab.example.test:8443/g/r.git", "http://other/g/r"); s != "https" {
t.Fatalf("matched url scheme = %s, want https", s)
}
if s := GitLabInstanceScheme("gitlab.example.test:8443", "http://gitlab.example.test:8443/g/r.git"); s != "http" {
t.Fatalf("explicit http record = %s, want http", s)
}
if s := GitLabInstanceScheme("gitlab.example.test", "https://other.example.test/g/r", ""); s != "https" {
t.Fatalf("unmatched urls must fall back to https, got %s", s)
}
}

func TestParseGitLabRepositoryURLWithPort(t *testing.T) {
got, ok := ParseGitLabRepositoryURL("gitlab:gitlab.example.test:8443/team/service")
if !ok {
t.Fatalf("port URL rejected")
}
if got.Project != "gitlab:gitlab.example.test:8443/team/service" {
t.Fatalf("got %s", got.Project)
}
}

func TestParseGitLabRepositoryURLRejectsImpersonation(t *testing.T) {
for _, raw := range []string{
"https://github.com.attacker.example/owner/repo",
"https://evil.example/owner/repo",
"http://user:secret@gitlab.example.test/g/r",
"gitlab.example.test", // 无项目路径
"gitlab:gitlab.example.test", // 无项目路径
"gitlab:/group/repo", // 缺主机
} {
if got, ok := ParseGitLabRepositoryURL(raw); ok {
t.Fatalf("invalid reference accepted: %s %+v", raw, got)
}
}
// 显式 gitlab: 前缀是信任边界内的管理员声明,可指向任意主机(含公网自建)。
if got, ok := ParseGitLabRepositoryURL("gitlab:gitlab.self-hosted.example/team/repo"); !ok ||
got.Project != "gitlab:gitlab.self-hosted.example/team/repo" {
t.Fatalf("explicit prefix rejected: %+v %v", got, ok)
}
}

func TestIsTrustedGitLabHost(t *testing.T) {
// 私有/回环网段应受信任(只断言网段类别,不引用真实主机)。
for _, host := range []string{"127.0.0.1", "10.0.0.1", "192.168.0.1", "172.16.0.1", "172.31.255.254"} {
if !isTrustedGitLabHost(host) {
t.Errorf("internal range host %s should be trusted", host)
}
}
// 公网地址、仿冒主机名、空值不受信任(192.0.2.x/198.51.100.x/203.0.113.x 为文档保留段)。
// 172.0.0.0/8 内 RFC1918 之外的公网段(172.217/16 Google、172.64/13 Cloudflare)
// 必须拒绝:整段放行等于让公网主机冒充内网实例。link-local(云元数据端点)同样拒绝。
// 主机名只认 .internal/.local 后缀:包含 ".internal" 子串的公网攻击域名
// (gitlab.internal.attacker.example)不是内网主机。
for _, host := range []string{"203.0.113.1", "198.51.100.2", "192.0.2.3", "172.217.0.1", "172.64.1.1", "172.32.0.1", "172.149.0.1", "172.153.0.1", "169.254.169.254", "169.254.0.1", "gitlab.internal.attacker.example", "internal.example.com", "github.com.attacker.example", "gitlab.public.example", ""} {
if isTrustedGitLabHost(host) {
t.Errorf("external host %s must not be trusted", host)
}
}
// 非常规网段走显式 gitlab: 前缀声明(管理员行为,任意主机含公网自建)。
if got, ok := ParseGitLabRepositoryURL("gitlab:gitlab.self-hosted.example/team/repo"); !ok {
t.Fatalf("explicit prefix must remain the escape hatch for non-RFC1918 instances: %+v", got)
}
if got, ok := ParseGitLabRepositoryURL("http://172.217.0.1/group/repo"); ok {
t.Fatalf("bare public 172.x URL must be rejected without the prefix: %+v", got)
}
}

func TestParseRepositoryURLKeepsGitHubAndGitLabApart(t *testing.T) {
if got, ok := ParseRepositoryURL("https://github.com/Tencent/LoopForge"); !ok ||
got.Project != "github:Tencent/LoopForge" {
t.Fatalf("github routing broken: %+v %v", got, ok)
}
if got, ok := ParseRepositoryURL("gitlab:gitlab.example.test/group/repo"); !ok ||
got.Project != "gitlab:gitlab.example.test/group/repo" {
t.Fatalf("gitlab prefix routing broken: %+v %v", got, ok)
}
if got, ok := ParseRepositoryURL("https://github.com.attacker.example/owner/repo"); ok {
t.Fatalf("impersonation accepted: %+v", got)
}
}

// MR 兜底网页链接与解析层同一安全默认:https,不再硬编码 http://。
// 真实 web_url 由 API 响应覆盖,这里只兜底。
func TestGongfengMRURLDefaultsToHTTPSForGitLab(t *testing.T) {
if got := GongfengMRURL("gitlab:gitlab.example.test/g/r", 7); got != "https://gitlab.example.test/g/r/-/merge_requests/7" {
t.Fatalf("gitlab MR url = %s, want https fallback", got)
}
}
Loading