diff --git a/.env.example b/.env.example index a03f66b3d..36def48a5 100644 --- a/.env.example +++ b/.env.example @@ -16,7 +16,7 @@ AGENTFLOW_PLATFORM_MCP_URL= # Optional model IDs available to your CodeBuddy account (comma-separated): AGENTFLOW_ASSISTANT_MODELS= AGENTFLOW_ASSISTANT_MODEL= -# Optional path to administrator-verified SSH host keys. See docs/deployment.md. +# Optional platform path to administrator-verified SSH host keys. See docs/deployment.md. AGENTFLOW_GIT_SSH_KNOWN_HOSTS= # Worker separation, installed runners and preview settings retain AGENTFLOW_ names. # See docs/deployment.md. Production HTTPS: AGENTFLOW_SECURE_COOKIES=true. diff --git a/docs/conversation-workspaces.md b/docs/conversation-workspaces.md index fcc77e864..cdefa7b78 100644 --- a/docs/conversation-workspaces.md +++ b/docs/conversation-workspaces.md @@ -18,7 +18,7 @@ - HTTPS:个人配置的 Git Token。`GIT_ASKPASS` 指向工作区 `.agentflow/runtime/git-askpass-hosts`,脚本本身不含令牌,从提示里解析出 URL 主机,只在它恰好是 `https://github.com` 时以 `x-access-token` 应答;`github.com.example`、`github.com@example`、`http://` 等其他地址都拿不到凭据。带 `oauth2:`/`private:` 前缀的令牌不是 GitHub 令牌,不会注入。`GIT_TERMINAL_PROMPT=0`,不会卡在交互输入上。 - `GH_TOKEN`:同一个令牌,供 `gh` 或 `curl` 调 GitHub API。平台进程自身的 `GH_TOKEN`/`GITHUB_TOKEN` 不会透传进沙箱。 -- SSH:个人 SSH 密钥按 `host:port` 绑定(不再按项目登记的仓库地址),任何克隆地址只要主机端口匹配就能用。私钥只在平台进程内存中的临时 ssh-agent 里,沙箱只看到 agent socket、公钥与 ssh config;主机公钥必须固定:平台内置 GitHub 公布的 `github.com:22` 与 `ssh.github.com:443` 公钥(来自 api.github.com/meta),其他主机由管理员通过 `AGENTFLOW_GIT_SSH_KNOWN_HOSTS` 固定,两者合并生效(文件里的 `@revoked` 同样作用于内置公钥);未固定的主机报错说明原因。 +- SSH 克隆使用按 `host:port` 绑定的个人密钥。同一主机和端口的仓库共用该密钥。执行进程在内存中持有私钥,并通过临时 ssh-agent 签名。沙箱只接收 agent socket、公钥与 SSH 配置。平台内置 `github.com:22` 与 `ssh.github.com:443` 的主机公钥,其他主机由管理员通过 `AGENTFLOW_GIT_SSH_KNOWN_HOSTS` 配置。Runner 0.11.1 及以上会在启动工作区会话时收到匹配的条目。平台条目优先,本地 Runner 配置可补充其他主机和端口,两处的 `@revoked` 条目都生效。 - 提交身份:`GIT_CONFIG_SYSTEM` 设为发言人的登录名,邮箱 `@users.noreply.loopforge.invalid`;Agent 可以用 `git config --global/--local` 覆盖。 工具结果和回复都会对发言人的令牌(含去掉前缀后的裸令牌)做脱敏;提示词要求 Agent 不打印、不回显、不写入令牌。 diff --git a/docs/deployment.md b/docs/deployment.md index ed1819455..6e3bd6764 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -101,7 +101,8 @@ A clone that is stopped this way is not retried, since a retry would wait on the Use Git SSH for GitLab.com, self-managed GitLab, Gitea, Gogs, GitHub, and other Git hosts that offer this transport. In **Project settings → Code repositories** or **Quick access**, enter the clone address, such as `git@gitlab.com:group/repo.git` or `ssh://git@git.example.org:2222/group/repo.git`, and select the existing default branch. GitHub HTTPS continues to use GitHub authorization or a PAT. 1. GitHub (`github.com:22` and `ssh.github.com:443`) works without configuration: LoopForge pins the host keys GitHub publishes at `api.github.com/meta`. For any other host, the deployment administrator creates a dedicated OpenSSH `known_hosts` file; its entries, including `@revoked`, are combined with the built-in GitHub keys. Verify the host keys against the provider's published fingerprints or with the server administrator before adding them. Use exact hostnames or IP addresses, with `[host]:port` for a nondefault port. Wildcards and hashed entries do not grant access. For a trusted team deployment, add internal hosts explicitly to this same file. -2. For hosts other than GitHub, set `AGENTFLOW_GIT_SSH_KNOWN_HOSTS` to the file's absolute path on the platform, workers and execution machines. For Compose, put it under the existing `LOOPFORGE_DATA_DIR` bind mount and set the absolute path in `.env`. Install Git and the OpenSSH client on execution machines and in custom sandbox images. Upgrade execution machines to Runner **0.9.3** or later. Docker sandbox commands need the sandbox and execution process to share the Linux kernel for the signing agent socket. With a Docker VM separate from a native runner, use managed checkout and delivery, or use a native sandbox for interactive Git commands. +2. Set `AGENTFLOW_GIT_SSH_KNOWN_HOSTS` to the file's absolute path on the platform. For Compose, put the file under the existing `LOOPFORGE_DATA_DIR` bind mount and set the absolute path in `.env`. Runner **0.11.1** and later receive the matching host entries when a task or workspace request needs Git SSH. The encoded entries have a 1 MiB allowance per dispatch. Platform entries take precedence for the same host and port. Runner-local entries supply pins for other endpoints, and `@revoked` entries from both sources apply. Runners **0.9.3** through **0.11.0** still need their own file and variable. Configure the variable on any separate worker that connects to the SSH host directly. + Install Git and the OpenSSH client on execution machines and in custom sandbox images. Docker sandbox commands need the sandbox and execution process to share the Linux kernel for the signing agent socket. With a Docker VM separate from a native runner, use managed checkout and delivery, or use a native sandbox for interactive Git commands. 3. Generate dedicated SSH keys with no passphrase for unattended execution. Add each public key to your hosting account and grant access to the repositories it needs. Grant write access if the run must push. Under **Personal settings → Git SSH keys**, add one private key per SSH host, such as `gitlab.com` or `git.example.org:2222`. An omitted port means 22. Edit that host's entry to replace its key. 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. diff --git a/internal/domain/runner.go b/internal/domain/runner.go index acee4c600..963a4b77b 100644 --- a/internal/domain/runner.go +++ b/internal/domain/runner.go @@ -138,6 +138,9 @@ func (r Runner) SupportsGitHTTP() bool { return versionAtLeast(r.RunnerVersion, RunnerVersionGitHTTP) } +// RunnerVersionGitSSHKnownHosts is the first runner to accept platform SSH pins. +const RunnerVersionGitSSHKnownHosts = "0.11.1" + func (r Runner) SupportsSharedPush() bool { return versionAtLeast(r.RunnerVersion, RunnerVersionSharedPush) } @@ -376,6 +379,8 @@ type RunnerTask struct { RunID string `json:"runId,omitempty"` NodeID string `json:"nodeId,omitempty"` Invocation *RunnerInvocation `json:"invocation,omitempty"` + // GitSSHKnownHosts is attached on authenticated claim and is never queued. + GitSSHKnownHosts *GitSSHKnownHosts `json:"gitSshKnownHosts,omitempty"` // Sandbox is the workflow sandbox definition resolved by the control // plane. The runner creates it locally so Docker/seatbelt/bwrap isolation // lives on the same machine that executes the Agent CLI. @@ -435,6 +440,7 @@ type RunnerSandboxSpec struct { // PublicRunnerTask strips task-scoped credentials from management responses. // The authenticated claiming runner receives the original task payload. func PublicRunnerTask(task RunnerTask) RunnerTask { + task.GitSSHKnownHosts = nil if task.PreviewArchive != nil { copySpec := *task.PreviewArchive copySpec.UploadToken = "" diff --git a/internal/domain/runner_workspace.go b/internal/domain/runner_workspace.go index 6e86a970a..5ffdb4fc2 100644 --- a/internal/domain/runner_workspace.go +++ b/internal/domain/runner_workspace.go @@ -33,8 +33,22 @@ const ( RunnerWorkspaceOperationCheckouts = "checkouts" RunnerWorkspaceOperationMaintenance = "maintenance" RunnerWorkspaceOperationLoginModels = "login_models" + RunnerWorkspaceOperationConversationCLI = "conversation_process" ) +// RunnerSSHKnownHostsWireLimit is the encoded allowance for the optional +// known_hosts field in a runner task claim. +const RunnerSSHKnownHostsWireLimit = 1 << 20 + +// GitSSHKnownHosts carries the platform's scoped SSH host keys over an +// authenticated runner transport. Configuration errors are also sent so the +// runner can fail closed when the platform cannot supply the configured pins. +type GitSSHKnownHosts struct { + Entries string `json:"entries,omitempty"` + ReadFailed bool `json:"readFailed,omitempty"` + TooLarge bool `json:"tooLarge,omitempty"` +} + // RunnerWorkspaceRequest is a bounded workspace request sent over the // runner's outbound control socket. Responses are transient and are never // persisted in runner_tasks or the run record. @@ -63,6 +77,7 @@ type RunnerWorkspaceRequest struct { Terminal *WorkspaceTerminalRequest `json:"terminal,omitempty"` TerminalOwner int64 `json:"terminalOwner,omitempty"` TerminalGitSSHKeys map[string]string `json:"terminalGitSshKeys,omitempty"` + GitSSHKnownHosts *GitSSHKnownHosts `json:"gitSshKnownHosts,omitempty"` // TerminalGitHTTPCredentials belongs to TerminalOwner and is sent only on open. TerminalGitHTTPCredentials map[string]GitHTTPExecutionCredential `json:"terminalGitHttpCredentials,omitempty"` // WorkspaceRunID selects the shared checkout owner; RunID continues to diff --git a/internal/gitwork/ssh.go b/internal/gitwork/ssh.go index 5f62b63ab..599ea10b9 100644 --- a/internal/gitwork/ssh.go +++ b/internal/gitwork/ssh.go @@ -1,7 +1,6 @@ package gitwork import ( - "bytes" "context" "crypto/sha256" "errors" @@ -64,48 +63,6 @@ github.com,[ssh.github.com]:443 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAy github.com,[ssh.github.com]:443 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk= ` -func sshHostKeys(remotes []SSHRemote) ([]byte, error) { - content := []byte(builtinKnownHosts) - if path := strings.TrimSpace(os.Getenv(SSHKnownHostsEnv)); path != "" { - configured, err := os.ReadFile(path) - if err != nil { - return nil, fmt.Errorf("无法读取 %s 指定的主机密钥文件", SSHKnownHostsEnv) - } - // Configured entries come first so an @revoked line still applies. - content = append(append(configured, '\n'), content...) - } - allowed := make(map[string]bool, len(remotes)) - for _, remote := range remotes { - allowed[knownhosts.Normalize(net.JoinHostPort(remote.Host, remote.Port))] = false - } - var pinned bytes.Buffer - for _, line := range bytes.Split(content, []byte("\n")) { - marker, hosts, key, _, _, err := ssh.ParseKnownHosts(line) - if err != nil || (marker != "" && marker != "revoked" && marker != "cert-authority") { - continue - } - for _, host := range hosts { - host = strings.ToLower(host) - if _, ok := allowed[host]; !ok { - continue - } - if marker != "" { - fmt.Fprintf(&pinned, "@%s ", marker) - } - fmt.Fprintf(&pinned, "%s %s", host, ssh.MarshalAuthorizedKey(key)) - if marker != "revoked" { - allowed[host] = true - } - } - } - for _, remote := range remotes { - if endpoint := knownhosts.Normalize(net.JoinHostPort(remote.Host, remote.Port)); !allowed[endpoint] { - return nil, fmt.Errorf("SSH 主机 %s 未获信任:平台只内置了 github.com 的公钥,其他主机请管理员在 %s 中添加并核对该主机和端口的公钥", endpoint, SSHKnownHostsEnv) - } - } - return pinned.Bytes(), nil -} - // sshBinding ties one repository remote to its host alias and private key. type sshBinding struct { remote SSHRemote @@ -155,7 +112,7 @@ func bindSSHRemotes(environment map[string]string, keys map[string]string) ([]ss // Aliases bind each repository to its selected host key. // The filesystem contains a socket and public data only. clientRoot is the // mount path corresponding to root for a client in a container. -func StartSSH(environment map[string]string, keys map[string]string, root, clientRoot string) (func(), error) { +func StartSSH(ctx context.Context, environment map[string]string, keys map[string]string, root, clientRoot string) (func(), error) { if len(keys) == 0 { return func() {}, nil } @@ -163,14 +120,14 @@ func StartSSH(environment map[string]string, keys map[string]string, root, clien if err != nil { return nil, err } - return startSSHSession(environment, bindings, remotes, root, clientRoot) + return startSSHSession(ctx, environment, bindings, remotes, root, clientRoot) } // StartSSHHosts gives every SSH remote on the given hosts the matching key, // without knowing the repositories in advance: an agent that clones // git@github.com:org/repo.git itself gets the requester's github.com key. // keys maps "host:port" endpoints (as stored per user) to private keys. -func StartSSHHosts(environment map[string]string, keys map[string]string, root, clientRoot string) (func(), error) { +func StartSSHHosts(ctx context.Context, environment map[string]string, keys map[string]string, root, clientRoot string) (func(), error) { endpoints := make([]string, 0, len(keys)) for endpoint, key := range keys { if strings.TrimSpace(key) != "" { @@ -193,17 +150,17 @@ func StartSSHHosts(environment map[string]string, keys map[string]string, root, bindings = append(bindings, sshBinding{remote: remote, alias: fmt.Sprintf("lf-%x", sum[:16]), key: keys[endpoint], match: remote.Host}) remotes = append(remotes, remote) } - return startSSHSession(environment, bindings, remotes, root, clientRoot) + return startSSHSession(ctx, environment, bindings, remotes, root, clientRoot) } -func startSSHSession(environment map[string]string, bindings []sshBinding, remotes []SSHRemote, root, clientRoot string) (func(), error) { +func startSSHSession(ctx context.Context, environment map[string]string, bindings []sshBinding, remotes []SSHRemote, root, clientRoot string) (func(), error) { environment["GIT_SSH_VARIANT"] = "ssh" if len(bindings) == 0 { // Keep terminals usable for editing without using a host identity. environment["GIT_SSH_COMMAND"] = `sh -c 'echo "未配置 Git SSH 密钥,请在个人配置中添加" >&2; exit 1' --` return func() {}, nil } - pinned, err := sshHostKeys(remotes) + pinned, err := sshHostKeys(ctx, remotes) if err != nil { return nil, err } @@ -374,7 +331,7 @@ func sshCommandEnvironment(ctx context.Context, args []string) (map[string]strin return nil, nil, fmt.Errorf("%s: %w", remote, ErrSSHKeyRequired) } env := make(map[string]string) - cleanup, err := StartSSH(env, map[string]string{remote: keys[remote]}, "", "") + cleanup, err := StartSSH(ctx, env, map[string]string{remote: keys[remote]}, "", "") return env, cleanup, err } } diff --git a/internal/gitwork/ssh_bind_test.go b/internal/gitwork/ssh_bind_test.go index 5a7747ddf..e4b7a09cc 100644 --- a/internal/gitwork/ssh_bind_test.go +++ b/internal/gitwork/ssh_bind_test.go @@ -7,6 +7,10 @@ import ( "path/filepath" "strings" "testing" + + "agentflow/internal/domain" + + "golang.org/x/crypto/ssh" ) func TestBindSSHRemotesRewritesSortedURLsAfterExistingConfig(t *testing.T) { @@ -56,7 +60,7 @@ func TestBindSSHRemotesRejectsInvalidRemote(t *testing.T) { func TestStartSSHWithoutAssignedKeyOnlyRewritesURLs(t *testing.T) { raw := "git@gitlab.com:group/a.git" env := map[string]string{} - stop, err := StartSSH(env, map[string]string{raw: ""}, t.TempDir(), "") + stop, err := StartSSH(t.Context(), env, map[string]string{raw: ""}, t.TempDir(), "") if err != nil { t.Fatal(err) } @@ -73,12 +77,12 @@ func TestStartSSHWithoutAssignedKeyOnlyRewritesURLs(t *testing.T) { // by host:port instead of by registered remote. func TestStartSSHHostsBindsByEndpoint(t *testing.T) { env := map[string]string{} - stop, err := StartSSHHosts(env, map[string]string{"git.example.org:22": "", "other.example.org:2222": " "}, t.TempDir(), "") + stop, err := StartSSHHosts(t.Context(), env, map[string]string{"git.example.org:22": "", "other.example.org:2222": " "}, t.TempDir(), "") if err != nil || len(env) != 0 { t.Fatalf("keys without content must bind nothing: env=%v err=%v", env, err) } stop() - if _, err := StartSSHHosts(map[string]string{}, map[string]string{"git.example.org": "KEY"}, t.TempDir(), ""); err == nil { + if _, err := StartSSHHosts(t.Context(), map[string]string{}, map[string]string{"git.example.org": "KEY"}, t.TempDir(), ""); err == nil { t.Fatal("an endpoint without a port was accepted") } if got := (sshBinding{alias: "lf-x", match: "git.example.org"}).matchHost(); got != "git.example.org" { @@ -91,14 +95,14 @@ func TestStartSSHHostsBindsByEndpoint(t *testing.T) { func TestBuiltinKnownHostsTrustGitHubOnly(t *testing.T) { t.Setenv(SSHKnownHostsEnv, "") - pinned, err := sshHostKeys([]SSHRemote{{Host: "github.com", Port: "22"}, {Host: "ssh.github.com", Port: "443"}}) + pinned, err := sshHostKeys(t.Context(), []SSHRemote{{Host: "github.com", Port: "22"}, {Host: "ssh.github.com", Port: "443"}}) if err != nil { t.Fatalf("github.com should be trusted without configuration: %v", err) } if got := string(pinned); !strings.Contains(got, "github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl") || !strings.Contains(got, "[ssh.github.com]:443 ssh-rsa") { t.Fatalf("pinned keys = %s", got) } - if _, err := sshHostKeys([]SSHRemote{{Host: "git.example.org", Port: "22"}}); err == nil || !strings.Contains(err.Error(), SSHKnownHostsEnv) { + if _, err := sshHostKeys(t.Context(), []SSHRemote{{Host: "git.example.org", Port: "22"}}); err == nil || !strings.Contains(err.Error(), SSHKnownHostsEnv) { t.Fatalf("other hosts still need configured keys: %v", err) } file := filepath.Join(t.TempDir(), "known_hosts") @@ -106,7 +110,137 @@ func TestBuiltinKnownHostsTrustGitHubOnly(t *testing.T) { t.Fatal(err) } t.Setenv(SSHKnownHostsEnv, file) - if _, err := sshHostKeys([]SSHRemote{{Host: "git.example.org", Port: "22"}, {Host: "github.com", Port: "22"}}); err != nil { + if _, err := sshHostKeys(t.Context(), []SSHRemote{{Host: "git.example.org", Port: "22"}, {Host: "github.com", Port: "22"}}); err != nil { t.Fatalf("configured and built-in keys combine: %v", err) } } + +func TestPlatformSSHKnownHostsAreScopedAndTakePrecedence(t *testing.T) { + _, _, publicKey, _, _, err := ssh.ParseKnownHosts([]byte(builtinKnownHosts)) + if err != nil { + t.Fatal(err) + } + key := string(ssh.MarshalAuthorizedKey(publicKey)) + const host = "[gitlab.example.org]:2222" + const remote = "ssh://git@gitlab.example.org:2222/team/app.git" + const legacyHost = "legacy.example.org" + platformFile := filepath.Join(t.TempDir(), "platform_known_hosts") + content := host + ",gitlab.example.org,unrelated.example.org " + key + + "*.example.org " + key + "unassigned.example.org " + key + if err := os.WriteFile(platformFile, []byte(content), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv(SSHKnownHostsEnv, platformFile) + platform := SSHKnownHostsForKeys(map[string]string{remote: "identity", "git@unassigned.example.org:group/repo": ""}) + if platform == nil || platform.Entries != host+" "+key || platform.ReadFailed { + t.Fatalf("scoped platform pins = %+v", platform) + } + + localFile := filepath.Join(t.TempDir(), "runner_known_hosts") + local := "@cert-authority " + host + " " + key + legacyHost + " " + key + if err := os.WriteFile(localFile, []byte(local), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv(SSHKnownHostsEnv, localFile) + ctx := WithSSHKnownHosts(t.Context(), platform) + pinned, err := sshHostKeys(ctx, []SSHRemote{{Host: "gitlab.example.org", Port: "2222"}, {Host: legacyHost, Port: "22"}}) + if err != nil || string(pinned) != platform.Entries+legacyHost+" "+key { + t.Fatalf("platform and runner pins = %q, err = %v", pinned, err) + } + if _, err := sshHostKeys(ctx, []SSHRemote{{Host: "gitlab.example.org", Port: "22"}}); err == nil { + t.Fatal("known hosts pin was accepted on a different port") + } + + repo, err := ParseSSHRemote(remote) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(platformFile, []byte("@revoked "+platform.Entries), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv(SSHKnownHostsEnv, platformFile) + revoked := SSHKnownHostsForKeys(map[string]string{repo.Endpoint(): "identity"}) + if revoked == nil || revoked.Entries != "@revoked "+platform.Entries { + t.Fatalf("revocation was lost on dispatch: %+v", revoked) + } + t.Setenv(SSHKnownHostsEnv, localFile) + if _, err := sshHostKeys(WithSSHKnownHosts(t.Context(), revoked), []SSHRemote{repo}); err == nil || !strings.Contains(err.Error(), "未获信任") { + t.Fatalf("local pins bypassed platform revocation: %v", err) + } +} + +func TestRunnerSSHRevocationSurvivesPlatformKnownHosts(t *testing.T) { + _, _, builtinKey, _, remaining, err := ssh.ParseKnownHosts([]byte(builtinKnownHosts)) + if err != nil { + t.Fatal(err) + } + _, _, platformKey, _, _, err := ssh.ParseKnownHosts(remaining) + if err != nil { + t.Fatal(err) + } + path := filepath.Join(t.TempDir(), "known_hosts") + if err := os.WriteFile(path, []byte("github.com "+string(ssh.MarshalAuthorizedKey(platformKey))), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv(SSHKnownHostsEnv, path) + platform := SSHKnownHostsForKeys(map[string]string{"github.com:22": "identity"}) + if platform == nil { + t.Fatal("platform host pin is missing") + } + revoked := "@revoked github.com " + string(ssh.MarshalAuthorizedKey(builtinKey)) + if err := os.WriteFile(path, []byte(revoked), 0o600); err != nil { + t.Fatal(err) + } + ctx := WithSSHKnownHosts(t.Context(), platform) + pinned, err := sshHostKeys(ctx, []SSHRemote{{Host: "github.com", Port: "22"}}) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(pinned), revoked) { + t.Fatal("the platform host pin displaced the runner-local revocation") + } +} + +func TestUnreadablePlatformSSHKnownHostsFailsClosed(t *testing.T) { + t.Setenv(SSHKnownHostsEnv, filepath.Join(t.TempDir(), "missing")) + platform := SSHKnownHostsForKeys(map[string]string{"github.com:22": "identity"}) + if platform == nil || !platform.ReadFailed { + t.Fatalf("platform read failure = %+v", platform) + } + t.Setenv(SSHKnownHostsEnv, "") + ctx := WithSSHKnownHosts(t.Context(), platform) + if _, err := sshHostKeys(ctx, []SSHRemote{{Host: "github.com", Port: "22"}}); err == nil || !strings.Contains(err.Error(), "无法读取平台") { + t.Fatalf("platform read failure was hidden: %v", err) + } +} + +func TestOversizedPlatformSSHKnownHostsFailsClosed(t *testing.T) { + _, _, publicKey, _, _, err := ssh.ParseKnownHosts([]byte(builtinKnownHosts)) + if err != nil { + t.Fatal(err) + } + const host = "gitlab.example.org" + line := host + " " + string(ssh.MarshalAuthorizedKey(publicKey)) + // A line's newline takes two bytes in JSON. The unencoded file fits in the + // allowance, but the matching entries do not fit after escaping. + lines := domain.RunnerSSHKnownHostsWireLimit/(len(line)+1) + 1 + path := filepath.Join(t.TempDir(), "known_hosts") + if err := os.WriteFile(path, []byte(strings.Repeat(line, lines)), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv(SSHKnownHostsEnv, path) + platform := SSHKnownHostsForKeys(map[string]string{host + ":22": "identity"}) + if platform == nil { + t.Fatal("oversized platform config was omitted") + } + if !platform.TooLarge || platform.ReadFailed || platform.Entries != "" { + t.Fatalf("oversized platform config has %d entry bytes, tooLarge = %v, readFailed = %v", len(platform.Entries), platform.TooLarge, platform.ReadFailed) + } + if err := os.WriteFile(path, []byte(line), 0o600); err != nil { + t.Fatal(err) + } + ctx := WithSSHKnownHosts(t.Context(), platform) + if _, err := sshHostKeys(ctx, []SSHRemote{{Host: host, Port: "22"}}); err == nil || !strings.Contains(err.Error(), "超过大小上限") { + t.Fatalf("oversized platform pins were silently bypassed: %v", err) + } +} diff --git a/internal/gitwork/ssh_known_hosts.go b/internal/gitwork/ssh_known_hosts.go new file mode 100644 index 000000000..a9c5421cc --- /dev/null +++ b/internal/gitwork/ssh_known_hosts.go @@ -0,0 +1,151 @@ +package gitwork + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "maps" + "os" + "slices" + "strings" + + "agentflow/internal/domain" + + "golang.org/x/crypto/ssh" + "golang.org/x/crypto/ssh/knownhosts" +) + +type sshKnownHostsContext struct{} + +// WithSSHKnownHosts makes the platform's pins available to Git commands and +// sandbox credential bindings for this runner request. +func WithSSHKnownHosts(ctx context.Context, knownHosts *domain.GitSSHKnownHosts) context.Context { + if knownHosts == nil { + return ctx + } + return context.WithValue(ctx, sshKnownHostsContext{}, knownHosts) +} + +// SSHKnownHostsForKeys selects configured host keys for an authenticated +// runner dispatch. Each set maps clone URLs or host:port scopes to identities. +func SSHKnownHostsForKeys(keySets ...map[string]string) *domain.GitSSHKnownHosts { + endpoints := make(map[string]struct{}) + for _, keys := range keySets { + for target, key := range keys { + if strings.TrimSpace(key) == "" { + continue + } + if endpoint, err := SSHHostScope(target); err == nil { + endpoints[knownhosts.Normalize(endpoint)] = struct{}{} + } + } + } + if len(endpoints) == 0 { + return nil + } + content, err := readConfiguredSSHKnownHosts() + if err != nil { + return &domain.GitSSHKnownHosts{ReadFailed: true} + } + matches := matchingSSHKnownHosts(content, endpoints) + if len(matches) == 0 { + return nil + } + var entries strings.Builder + for _, endpoint := range slices.Sorted(maps.Keys(matches)) { + pins := matches[endpoint] + entries.Write(pins.revocations) + entries.Write(pins.keys) + } + pins := &domain.GitSSHKnownHosts{Entries: entries.String()} + encoded, _ := json.Marshal(pins) + const claimFieldPrefix = len(`,"gitSshKnownHosts":`) + if len(encoded)+claimFieldPrefix > domain.RunnerSSHKnownHostsWireLimit { + return &domain.GitSSHKnownHosts{TooLarge: true} + } + return pins +} + +func readConfiguredSSHKnownHosts() ([]byte, error) { + path := strings.TrimSpace(os.Getenv(SSHKnownHostsEnv)) + if path == "" { + return nil, nil + } + return os.ReadFile(path) +} + +func sshHostKeys(ctx context.Context, remotes []SSHRemote) ([]byte, error) { + endpoints := make(map[string]struct{}, len(remotes)) + for _, remote := range remotes { + endpoints[knownhosts.Normalize(remote.Endpoint())] = struct{}{} + } + var platformEntries []byte + if platform, _ := ctx.Value(sshKnownHostsContext{}).(*domain.GitSSHKnownHosts); platform != nil { + if platform.ReadFailed { + return nil, fmt.Errorf("无法读取平台 %s 指定的主机密钥文件", SSHKnownHostsEnv) + } + if platform.TooLarge { + return nil, fmt.Errorf("平台下发的 SSH 主机密钥超过大小上限,请检查 %s 中匹配的条目", SSHKnownHostsEnv) + } + platformEntries = []byte(platform.Entries) + } + localEntries, err := readConfiguredSSHKnownHosts() + if err != nil { + return nil, fmt.Errorf("无法读取 %s 指定的主机密钥文件", SSHKnownHostsEnv) + } + platform := matchingSSHKnownHosts(platformEntries, endpoints) + local := matchingSSHKnownHosts(localEntries, endpoints) + builtin := matchingSSHKnownHosts([]byte(builtinKnownHosts), endpoints) + var pinned, bundled []byte + for _, endpoint := range slices.Sorted(maps.Keys(endpoints)) { + localPins, defaults := local[endpoint], builtin[endpoint] + // Platform entries take precedence for trusted keys at their exact + // endpoint. Revocations from both configurations always apply. + pinned = append(pinned, localPins.revocations...) + keys := localPins.keys + if platformPins, ok := platform[endpoint]; ok { + pinned = append(pinned, platformPins.revocations...) + keys = platformPins.keys + } + if len(keys) == 0 && len(defaults.keys) == 0 { + return nil, fmt.Errorf("SSH 主机 %s 未获信任:平台只内置了 github.com 的公钥,其他主机请管理员在 %s 中添加并核对该主机和端口的公钥", endpoint, SSHKnownHostsEnv) + } + pinned = append(pinned, keys...) + bundled = append(bundled, defaults.revocations...) + bundled = append(bundled, defaults.keys...) + } + return append(pinned, bundled...), nil +} + +type sshHostPins struct { + keys []byte + revocations []byte +} + +func matchingSSHKnownHosts(content []byte, endpoints map[string]struct{}) map[string]sshHostPins { + matches := make(map[string]sshHostPins) + for line := range bytes.SplitSeq(content, []byte("\n")) { + marker, hosts, key, _, _, err := ssh.ParseKnownHosts(line) + if err != nil || (marker != "" && marker != "revoked" && marker != "cert-authority") { + continue + } + for _, host := range hosts { + host = strings.ToLower(host) + if _, ok := endpoints[host]; !ok { + continue + } + pins := matches[host] + if marker == "revoked" { + pins.revocations = fmt.Appendf(pins.revocations, "@revoked %s %s", host, ssh.MarshalAuthorizedKey(key)) + } else { + if marker == "cert-authority" { + pins.keys = append(pins.keys, "@cert-authority "...) + } + pins.keys = fmt.Appendf(pins.keys, "%s %s", host, ssh.MarshalAuthorizedKey(key)) + } + matches[host] = pins + } + } + return matches +} diff --git a/internal/httpapi/assistant_process.go b/internal/httpapi/assistant_process.go index 725643a61..620726d42 100644 --- a/internal/httpapi/assistant_process.go +++ b/internal/httpapi/assistant_process.go @@ -69,7 +69,7 @@ func (h *assistantHandlers) conversationExecutor(s *AgentToolSession, root strin }() sum := sha256.Sum256([]byte(fmt.Sprintf("%d/%s", s.ChatOwnerID, s.ChatSessionID))) key := hex.EncodeToString(sum[:]) - request := domain.RunnerWorkspaceRequest{Operation: "conversation_process"} + request := domain.RunnerWorkspaceRequest{Operation: domain.RunnerWorkspaceOperationConversationCLI} workspace := "" if s.RuntimeMode == domain.AssistantModeWorkspace { runID := s.workspaceRun() diff --git a/internal/httpapi/repository_ssh_test.go b/internal/httpapi/repository_ssh_test.go index d6cf9b556..2681dc817 100644 --- a/internal/httpapi/repository_ssh_test.go +++ b/internal/httpapi/repository_ssh_test.go @@ -129,8 +129,10 @@ func TestSSHRepositoryCheckoutAndDelivery(t *testing.T) { // A conversation workspace CLI delivers with its own git: the personal key // is offered by host, whatever remote URL the agent cloned. workspaceCreds := runtime.RunCredentials{GitUserName: "alice", GitUserEmail: "alice@example.invalid", GitSSHHostKeys: map[string]string{net.JoinHostPort(endpoint.Host, endpoint.Port): privateKey}} + workspaceCtx := gitwork.WithSSHKnownHosts(ctx, gitwork.SSHKnownHostsForKeys(workspaceCreds.GitSSHHostKeys)) + t.Setenv(gitwork.SSHKnownHostsEnv, "") workspaceGit := func(args ...string) (string, error) { - return runtime.ExecuteLocalCommand(ctx, root, runtime.CommandSpec{ + return runtime.ExecuteLocalCommand(workspaceCtx, root, runtime.CommandSpec{ Argv: append([]string{"git", "-C", "app"}, args...), CredentialBindings: []runtime.CredentialBinding{runtime.BindGitWorkspace}, }, workspaceCreds, nil) } @@ -139,7 +141,7 @@ func TestSSHRepositoryCheckoutAndDelivery(t *testing.T) { t.Fatalf("workspace git %v: %v (%s)", args, err, out) } } - if pushed, local := sshTestGit(t, bare, "rev-parse", "refs/heads/agent/ssh-push"), sshTestGit(t, checkout, "rev-parse", "HEAD"); pushed != local { + if pushed, local := sshTestGit(t, bare, "--git-dir=.", "rev-parse", "refs/heads/agent/ssh-push"), sshTestGit(t, checkout, "rev-parse", "HEAD"); pushed != local { t.Fatalf("pushed %s, local %s", pushed, local) } if resp := cvBRequest(t, f, http.MethodDelete, keyURL+"/"+keys[0].ID, nil, true); resp.Code != http.StatusOK || f.s.GitSSHKeysForRemotes(f.user.ID, []string{remote})[remote] != "" { @@ -231,9 +233,18 @@ func TestGitSSHRunnerClaimUsesTaskActorCredential(t *testing.T) { } actor, _ = f.s.UserByLoginName(actor.LoginName) const key = "transient-other-host-identity" - const gitlabKey = "transient-gitlab-host-identity" - const gitlabRepo = "git@gitlab.example.org:team/app.git" const otherHostRepo = "git@git.example.org:team/app.git" + gitlabRepo, gitlabKey, bare, knownHosts := localSSHGit(t, nil) + expectedPin, err := os.ReadFile(knownHosts) + if err != nil { + t.Fatal(err) + } + _, unrelatedKey := sshTestIdentity(t) + unrelatedPin := knownhosts.Line([]string{"unrelated.example.org:22"}, unrelatedKey) + "\n" + if err := os.WriteFile(knownHosts, append(expectedPin, unrelatedPin...), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv(gitwork.SSHKnownHostsEnv, knownHosts) if err := f.s.SaveGitSSHKey(actor.ID, "", otherHostRepo, "Other Git host", key); err != nil { t.Fatal(err) } @@ -243,7 +254,7 @@ func TestGitSSHRunnerClaimUsesTaskActorCredential(t *testing.T) { runnerID, session := registerTestRunner(t, f.h) registered, _ := f.s.Runner(runnerID) now := time.Now().UTC() - if err := f.s.RunnerHeartbeat(runnerID, registered.Runtimes, domain.RunnerVersionGitSSH, now, now.Add(time.Hour)); err != nil { + if err := f.s.RunnerHeartbeat(runnerID, registered.Runtimes, domain.RunnerVersionGitSSHKnownHosts, now, now.Add(time.Hour)); err != nil { t.Fatal(err) } task := domain.RunnerTask{ID: "ssh-task", RunnerID: runnerID, Provider: "mock", Status: domain.RunnerTaskQueued, MaxAttempts: 1, Invocation: &domain.RunnerInvocation{ @@ -253,18 +264,54 @@ func TestGitSSHRunnerClaimUsesTaskActorCredential(t *testing.T) { if err := f.s.PutRunnerTask(task); err != nil { t.Fatal(err) } - resp := requestWithHeaders(t, f.h, http.MethodPost, "/api/runners/tasks/claim", map[string]any{"availableRuntimes": []string{"mock"}, "noWait": true}, map[string]string{"Authorization": "Bearer " + session}) + auth := map[string]string{"Authorization": "Bearer " + session} + resp := requestWithHeaders(t, f.h, http.MethodPost, "/api/runners/tasks/claim", map[string]any{ + "availableRuntimes": []string{"mock"}, + "noWait": true, + "supportsGitSshKnownHosts": true, + }, auth) var result struct{ Task domain.RunnerTask } if err := json.Unmarshal(resp.Body.Bytes(), &result); err != nil || resp.Code != http.StatusOK || result.Task.ID != task.ID || result.Task.Invocation == nil || result.Task.Invocation.GitSSHKeys[gitlabRepo] != gitlabKey || result.Task.Invocation.GitSSHKeys[otherHostRepo] != key || result.Task.Invocation.GitSSHUserID != 0 { t.Fatalf("authenticated claim did not hydrate the task actor's keys (HTTP %d)", resp.Code) } + pins := result.Task.GitSSHKnownHosts + if pins == nil || pins.Entries != string(expectedPin) || pins.ReadFailed || resp.Header().Get("Cache-Control") != "no-store" { + t.Fatal("claim did not return just the requested platform host pin with no-store") + } stored, _ := f.s.RunnerTask(task.ID) - if stored.Invocation == nil || len(stored.Invocation.GitSSHKeys) != 0 { - t.Fatal("claimed key was persisted") + if stored.Invocation == nil || len(stored.Invocation.GitSSHKeys) != 0 || stored.GitSSHKnownHosts != nil { + t.Fatal("claimed Git credentials or host pins were persisted") } - if resp := requestWithHeaders(t, f.h, http.MethodGet, "/api/runners/tasks/"+task.ID, nil, map[string]string{"Authorization": "Bearer " + session}); resp.Code != http.StatusOK || strings.Contains(resp.Body.String(), "gitSsh") || strings.Contains(resp.Body.String(), key) { + if resp := requestWithHeaders(t, f.h, http.MethodGet, "/api/runners/tasks/"+task.ID, nil, auth); resp.Code != http.StatusOK || strings.Contains(resp.Body.String(), "gitSsh") || strings.Contains(resp.Body.String(), key) { t.Fatalf("runner status response must omit credential fields (HTTP %d)", resp.Code) } + + // A claim from an older client carries no capability flag, even if the + // persisted runner version was reported by a different process. + task.ID = "legacy-ssh-task" + if err := f.s.PutRunnerTask(task); err != nil { + t.Fatal(err) + } + legacy := requestWithHeaders(t, f.h, http.MethodPost, "/api/runners/tasks/claim", map[string]any{ + "availableRuntimes": []string{"mock"}, + "noWait": true, + }, auth) + var legacyResult struct{ Task domain.RunnerTask } + if err := json.Unmarshal(legacy.Body.Bytes(), &legacyResult); err != nil || legacy.Code != http.StatusOK || legacyResult.Task.ID != task.ID || + legacyResult.Task.GitSSHKnownHosts != nil || legacyResult.Task.Invocation == nil || legacyResult.Task.Invocation.GitSSHKeys[gitlabRepo] != gitlabKey { + t.Fatalf("legacy claim did not preserve the existing SSH payload (HTTP %d)", legacy.Code) + } + + t.Setenv(gitwork.SSHKnownHostsEnv, "") + ctx := gitwork.WithSSHKnownHosts(t.Context(), pins) + ctx = gitwork.WithSSHRemote(gitwork.WithSSHKeys(ctx, result.Task.Invocation.GitSSHKeys), gitlabRepo) + checkout := t.TempDir() + if _, err := gitwork.Run(ctx, checkout, "clone", "--", gitlabRepo, "."); err != nil { + t.Fatalf("clone with the claim's pins and no runner configuration: %v", err) + } + if got, want := sshTestGit(t, checkout, "rev-parse", "HEAD"), sshTestGit(t, bare, "--git-dir=.", "rev-parse", "HEAD"); got != want { + t.Fatalf("cloned HEAD = %s, want %s", got, want) + } } func sshTestGit(t *testing.T, dir string, args ...string) string { diff --git a/internal/httpapi/runner_ws.go b/internal/httpapi/runner_ws.go index dd3347daa..a78cd175a 100644 --- a/internal/httpapi/runner_ws.go +++ b/internal/httpapi/runner_ws.go @@ -15,6 +15,8 @@ import ( "agentflow/internal/domain" "agentflow/internal/engine" + "agentflow/internal/gitwork" + "agentflow/internal/runtime" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" @@ -201,9 +203,30 @@ func (h *runnerControlHub) reconnectExpectedLocked(runnerID string, now time.Tim return ok && now.Sub(dropped) < h.reconnectGrace } +// Resolve pins on the platform serving the socket, including requests relayed +// by a predecessor during handover. +func runnerWorkspaceSSHKnownHosts(request domain.RunnerWorkspaceRequest) *domain.GitSSHKnownHosts { + var preparationKeys, conversationKeys map[string]string + if request.PreparationInvocation != nil { + preparationKeys = request.PreparationInvocation.GitSSHKeys + } + if request.Operation == domain.RunnerWorkspaceOperationConversationCLI { + // Only starts need pins. Skip decoding asset transfers and stdin data. + var conversation struct { + Operation string `json:"operation"` + Command *runtime.ConversationCommand `json:"command"` + } + if json.Unmarshal(request.Conversation, &conversation) == nil && conversation.Operation == "start" && conversation.Command != nil { + conversationKeys = conversation.Command.Credentials.GitSSHHostKeys + } + } + return gitwork.SSHKnownHostsForKeys(preparationKeys, request.TerminalGitSSHKeys, conversationKeys) +} + // send delivers one request; sent reports whether it reached the socket's // queue, after which it must not be retried elsewhere. func (h *runnerControlHub) send(ctx context.Context, conn *runnerControlConn, request domain.RunnerWorkspaceRequest) (domain.RunnerWorkspaceResponse, bool, error) { + request.GitSSHKnownHosts = runnerWorkspaceSSHKnownHosts(request) h.mu.Lock() h.nextID++ requestID := fmt.Sprintf("workspace-%d", h.nextID) diff --git a/internal/httpapi/runner_ws_workspace_test.go b/internal/httpapi/runner_ws_workspace_test.go index 2c862640d..197a4e2c9 100644 --- a/internal/httpapi/runner_ws_workspace_test.go +++ b/internal/httpapi/runner_ws_workspace_test.go @@ -6,6 +6,7 @@ import ( "errors" "net/http" "net/http/httptest" + "os" "path/filepath" "strings" "testing" @@ -13,10 +14,12 @@ import ( "agentflow/internal/domain" "agentflow/internal/engine" + "agentflow/internal/gitwork" "agentflow/internal/repo" "agentflow/internal/runtime" "github.com/gin-gonic/gin" + "golang.org/x/crypto/ssh/knownhosts" ) func quietRunnerHub() *runnerControlHub { @@ -281,6 +284,73 @@ func TestRunnerWorkspaceRPCIsTransient(t *testing.T) { } } +func TestRunnerWorkspaceSendsScopedPlatformSSHKnownHosts(t *testing.T) { + hub := quietRunnerHub() + conn := testRunnerConn("runner-a") + registerTestConn(hub, conn) + _, hostKey := sshTestIdentity(t) + preparationPin := knownhosts.Line([]string{"gitlab.example.org:2222"}, hostKey) + "\n" + terminalPin := knownhosts.Line([]string{"terminal.example.org:22"}, hostKey) + "\n" + otherPin := knownhosts.Line([]string{"other.example.org:22"}, hostKey) + "\n" + path := filepath.Join(t.TempDir(), "known_hosts") + if err := os.WriteFile(path, []byte(preparationPin+terminalPin+otherPin), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv(gitwork.SSHKnownHostsEnv, path) + done := requestAsync(hub, conn.runnerID, domain.RunnerWorkspaceRequest{ + RunID: "run-a", + Operation: domain.RunnerWorkspaceOperationTerminal, + Terminal: &domain.WorkspaceTerminalRequest{Operation: "open"}, + PreparationInvocation: &domain.RunnerInvocation{ + GitSSHKeys: map[string]string{"ssh://git@gitlab.example.org:2222/group/repo.git": "checkout-identity"}, + }, + TerminalGitSSHKeys: map[string]string{"git@terminal.example.org:group/repo.git": "terminal-identity"}, + }) + got := answerOnce(t, hub, conn, domain.RunnerWorkspaceResponse{Status: http.StatusOK}) + if pins := got.GitSSHKnownHosts; pins == nil || pins.Entries != preparationPin+terminalPin || pins.ReadFailed { + t.Fatalf("workspace platform pins = %+v", pins) + } + if result := <-done; result.err != nil || result.response.Status != http.StatusOK { + t.Fatalf("workspace response = %+v", result) + } +} + +func TestRunnerWorkspaceRelayHydratesConversationSSHKnownHosts(t *testing.T) { + predecessor, successor := quietRunnerHub(), quietRunnerHub() + predecessor.successor = successor.requestRelayed + predecessor.handOver() + conn := testRunnerConn("runner-a") + registerTestConn(successor, conn) + + _, hostKey := sshTestIdentity(t) + pin := knownhosts.Line([]string{"gitlab.example.org:2222"}, hostKey) + "\n" + path := filepath.Join(t.TempDir(), "known_hosts") + if err := os.WriteFile(path, []byte(pin), 0o600); err != nil { + t.Fatal(err) + } + t.Setenv(gitwork.SSHKnownHostsEnv, path) + conversation, err := json.Marshal(runtime.ConversationProcessRequest{ + Operation: "start", + Command: &runtime.ConversationCommand{ + Credentials: runtime.RunCredentials{GitSSHHostKeys: map[string]string{"gitlab.example.org:2222": "identity"}}, + }, + }) + if err != nil { + t.Fatal(err) + } + done := requestAsync(predecessor, conn.runnerID, domain.RunnerWorkspaceRequest{ + Operation: domain.RunnerWorkspaceOperationConversationCLI, + Conversation: conversation, + }) + got := answerOnce(t, successor, conn, domain.RunnerWorkspaceResponse{Status: http.StatusOK}) + if pins := got.GitSSHKnownHosts; pins == nil || pins.Entries != pin || pins.ReadFailed { + t.Fatalf("relayed conversation pins = %+v", pins) + } + if result := <-done; result.err != nil || result.response.Status != http.StatusOK { + t.Fatalf("relayed conversation response = %+v", result) + } +} + func waitClosed(t *testing.T, conn *runnerControlConn) { t.Helper() select { diff --git a/internal/httpapi/runners.go b/internal/httpapi/runners.go index e80de3e77..6ac3ee4a7 100644 --- a/internal/httpapi/runners.go +++ b/internal/httpapi/runners.go @@ -16,6 +16,7 @@ import ( "time" "agentflow/internal/domain" + "agentflow/internal/gitwork" "agentflow/internal/repo" "github.com/gin-gonic/gin" @@ -825,10 +826,11 @@ func (h *runnersHandlers) runnerTaskClaim(c *gin.Context) { return } var input struct { - AvailableRuntimes []string `json:"availableRuntimes"` - Capacity int `json:"capacity"` - Preflight bool `json:"preflight"` - NoWait bool `json:"noWait"` + AvailableRuntimes []string `json:"availableRuntimes"` + Capacity int `json:"capacity"` + Preflight bool `json:"preflight"` + NoWait bool `json:"noWait"` + SupportsGitSSHKnownHosts bool `json:"supportsGitSshKnownHosts"` } if !decode(c, &input) { return @@ -854,6 +856,9 @@ func (h *runnersHandlers) runnerTaskClaim(c *gin.Context) { } if task.Invocation != nil && task.Invocation.GitSSHUserID > 0 && runner.SupportsGitSSH() && len(task.Invocation.GitSSHRemotes()) > 0 { task.Invocation.GitSSHKeys = h.repo.GitSSHKeysForRemotes(task.Invocation.GitSSHUserID, task.Invocation.GitSSHRemotes()) + if input.SupportsGitSSHKnownHosts { + task.GitSSHKnownHosts = gitwork.SSHKnownHostsForKeys(task.Invocation.GitSSHKeys) + } task.Invocation.GitSSHUserID = 0 c.Header("Cache-Control", "no-store") } diff --git a/internal/runner/client.go b/internal/runner/client.go index 99dffe231..be80f06aa 100644 --- a/internal/runner/client.go +++ b/internal/runner/client.go @@ -52,6 +52,8 @@ func (c *Client) sessionToken() string { // ---------- request plumbing ---------- +const runnerTaskClaimPath = "/runners/tasks/claim" + func (c *Client) do(ctx context.Context, method, path string, body any, out any) error { var reader io.Reader if body != nil { @@ -82,7 +84,11 @@ func (c *Client) do(ctx context.Context, method, path string, body any, out any) if resp.StatusCode == http.StatusNoContent { return nil } - payload, _ := io.ReadAll(io.LimitReader(resp.Body, 8<<20)) + responseLimit := int64(8 << 20) + if path == runnerTaskClaimPath { + responseLimit += domain.RunnerSSHKnownHostsWireLimit + } + payload, _ := io.ReadAll(io.LimitReader(resp.Body, responseLimit)) if resp.StatusCode >= 400 { var detail struct { Error string `json:"error"` @@ -182,21 +188,23 @@ func (c *Client) Disconnect(ctx context.Context) error { } type ClaimRequest struct { - AvailableRuntimes []string `json:"availableRuntimes"` - Capacity int `json:"capacity"` - Preflight bool `json:"preflight,omitempty"` - NoWait bool `json:"noWait,omitempty"` + AvailableRuntimes []string `json:"availableRuntimes"` + Capacity int `json:"capacity"` + Preflight bool `json:"preflight,omitempty"` + NoWait bool `json:"noWait,omitempty"` + SupportsGitSSHKnownHosts bool `json:"supportsGitSshKnownHosts,omitempty"` } // WaitForTask long-polls without leasing work. Older servers ignore preflight // and return a leased task, which the daemon can still execute. func (c *Client) WaitForTask(ctx context.Context, req ClaimRequest) (bool, *domain.RunnerTask, error) { req.Preflight = true + req.SupportsGitSSHKnownHosts = true var out struct { TaskAvailable bool `json:"taskAvailable"` Task domain.RunnerTask `json:"task"` } - if err := c.do(ctx, http.MethodPost, "/runners/tasks/claim", req, &out); err != nil { + if err := c.do(ctx, http.MethodPost, runnerTaskClaimPath, req, &out); err != nil { return false, nil, err } if out.Task.ID != "" { @@ -209,10 +217,11 @@ func (c *Client) WaitForTask(ctx context.Context, req ClaimRequest) (bool, *doma // is reported as (nil, false, nil); the caller simply polls again. The // context deadline must exceed the server long-poll timeout. func (c *Client) Claim(ctx context.Context, req ClaimRequest) (*domain.RunnerTask, bool, error) { + req.SupportsGitSSHKnownHosts = true var out struct { Task domain.RunnerTask `json:"task"` } - if err := c.do(ctx, http.MethodPost, "/runners/tasks/claim", req, &out); err != nil { + if err := c.do(ctx, http.MethodPost, runnerTaskClaimPath, req, &out); err != nil { return nil, false, err } if out.Task.ID == "" { diff --git a/internal/runner/daemon.go b/internal/runner/daemon.go index a81144fa9..9df44f2a6 100644 --- a/internal/runner/daemon.go +++ b/internal/runner/daemon.go @@ -547,6 +547,7 @@ func (d *Daemon) executeTask(ctx context.Context, task domain.RunnerTask) { ctx, stopLease := d.keepTaskLease(ctx, task, leaseRenewInterval) defer stopLease() ctx = d.withDockerRunnerHelperWait(ctx) + ctx = gitwork.WithSSHKnownHosts(ctx, task.GitSSHKnownHosts) // The read fence ends before the completion report: once the control plane // sees this task terminal (and lets a paused run continue), a follow-up // preparation or snapshot must not still find the gate held. @@ -788,7 +789,7 @@ func (d *Daemon) executeTerminal(ctx context.Context, task domain.RunnerTask) ex return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("prepare git credentials: %v", err)} } defer closeGit() - cleanup, err := runtime.BindGitSSH(sandbox, workspace, gitEnv, task.Invocation.GitSSHKeys) + cleanup, err := runtime.BindGitSSH(ctx, sandbox, workspace, gitEnv, task.Invocation.GitSSHKeys) if err != nil { return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("prepare git credentials: %v", err)} } @@ -963,7 +964,7 @@ func (d *Daemon) executeInvocation(ctx context.Context, task domain.RunnerTask) return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("prepare git credentials: %v", gitErr)}, nil } defer closeGit() - cleanup, gitErr := runtime.BindGitSSH(sandbox, workspace, gitEnv, inv.GitSSHKeys) + cleanup, gitErr := runtime.BindGitSSH(ctx, sandbox, workspace, gitEnv, inv.GitSSHKeys) if gitErr != nil { return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("prepare git credentials: %v", gitErr)}, nil } @@ -1446,7 +1447,7 @@ func (d *Daemon) defaultWorkspace(taskID string) string { } // runnerVersion is reported at registration; it matches the module build. -var runnerVersion = domain.RunnerVersionGitHTTP +var runnerVersion = domain.RunnerVersionGitSSHKnownHosts // Version returns the runner build version reported at registration. func Version() string { return runnerVersion } diff --git a/internal/runner/protocol_cases_test.go b/internal/runner/protocol_cases_test.go index 40775506c..9d01c49af 100644 --- a/internal/runner/protocol_cases_test.go +++ b/internal/runner/protocol_cases_test.go @@ -18,6 +18,37 @@ import ( "github.com/gorilla/websocket" ) +func TestClaimSSHKnownHostsFitsAfterLegacyPayload(t *testing.T) { + response := struct { + Task domain.RunnerTask `json:"task"` + }{Task: domain.RunnerTask{ID: "large-ssh-task"}} + empty, err := json.Marshal(response) + if err != nil { + t.Fatal(err) + } + response.Task.Prompt = strings.Repeat("x", (8<<20)-len(empty)-32) + const pin = "[gitlab.example.org]:2222 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl\n" + response.Task.GitSSHKnownHosts = &domain.GitSSHKnownHosts{Entries: pin} + body, err := json.Marshal(response) + if err != nil || len(body) <= 8<<20 { + t.Fatalf("claim response is %d bytes: %v", len(body), err) + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var req struct { + SupportsGitSSHKnownHosts bool `json:"supportsGitSshKnownHosts"` + } + if err := json.NewDecoder(r.Body).Decode(&req); err != nil || !req.SupportsGitSSHKnownHosts { + t.Errorf("claim did not advertise SSH host pin support: %v", err) + } + _, _ = w.Write(body) + })) + defer server.Close() + task, claimed, err := NewClient(server.URL).Claim(t.Context(), ClaimRequest{AvailableRuntimes: []string{"mock"}}) + if err != nil || !claimed || task.GitSSHKnownHosts == nil || task.GitSSHKnownHosts.Entries != pin { + t.Fatalf("claim did not decode the extended response, claimed = %v, err = %v", claimed, err) + } +} + func TestPreviewArchiveSuccessAndFailures(t *testing.T) { var uploaded []byte server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/internal/runner/workspace.go b/internal/runner/workspace.go index 4889c0fdd..8a06b60bc 100644 --- a/internal/runner/workspace.go +++ b/internal/runner/workspace.go @@ -121,7 +121,8 @@ func (d *Daemon) readRunnerWorkspace(ctx context.Context, request domain.RunnerW return domain.RunnerWorkspaceResponse{Status: http.StatusConflict, Error: "Runner 即将重启,请稍后重试"} } ctx = d.withDockerRunnerHelperWait(ctx) - if request.Operation == "conversation_process" { + ctx = gitwork.WithSSHKnownHosts(ctx, request.GitSSHKnownHosts) + if request.Operation == domain.RunnerWorkspaceOperationConversationCLI { return d.conversationProcess(ctx, request) } if request.Operation == domain.RunnerWorkspaceOperationLoginModels { diff --git a/internal/runtime/git_ssh.go b/internal/runtime/git_ssh.go index 10f77e1ed..3527ffc4f 100644 --- a/internal/runtime/git_ssh.go +++ b/internal/runtime/git_ssh.go @@ -1,6 +1,7 @@ package runtime import ( + "context" "os" "path/filepath" "slices" @@ -11,7 +12,7 @@ import ( // BindGitSSH keeps the SSH agent alive for exactly one command. Only the // public configuration and socket are visible in the sandbox's control dir. -func BindGitSSH(sandbox Sandbox, workspace string, environment map[string]string, keys map[string]string) (func(), error) { +func BindGitSSH(ctx context.Context, sandbox Sandbox, workspace string, environment map[string]string, keys map[string]string) (func(), error) { if len(keys) == 0 { return func() {}, nil } @@ -24,24 +25,24 @@ func BindGitSSH(sandbox Sandbox, workspace string, environment map[string]string clientRoot = "/workspace/.agentflow/runtime" } environment["GIT_ALLOW_PROTOCOL"] = gitwork.AllowedProtocols - return gitwork.StartSSH(environment, keys, root, clientRoot) + return gitwork.StartSSH(ctx, environment, keys, root, clientRoot) } -func bindCommandGitSSH(sandbox Sandbox, workspace string, command CommandSpec, credentials RunCredentials) (func(), error) { +func bindCommandGitSSH(ctx context.Context, sandbox Sandbox, workspace string, command CommandSpec, credentials RunCredentials) (func(), error) { if slices.Contains(command.CredentialBindings, BindGitWorkspace) { - return bindWorkspaceGitSSH(sandbox, filepath.Clean(workspace), command.environment, credentials.GitSSHHostKeys) + return bindWorkspaceGitSSH(ctx, sandbox, filepath.Clean(workspace), command.environment, credentials.GitSSHHostKeys) } if !slices.Contains(command.CredentialBindings, BindGit) { return func() {}, nil } - return BindGitSSH(sandbox, filepath.Clean(workspace), command.environment, credentials.GitSSHKeys) + return BindGitSSH(ctx, sandbox, filepath.Clean(workspace), command.environment, credentials.GitSSHKeys) } // bindWorkspaceGitSSH starts the requester's SSH agent for every configured // host for as long as the workspace CLI runs. A host the administrator has not // pinned in the known hosts file cannot be used; SSH then fails with that // reason while HTTPS with the Git Token keeps working. -func bindWorkspaceGitSSH(sandbox Sandbox, workspace string, environment map[string]string, keys map[string]string) (func(), error) { +func bindWorkspaceGitSSH(ctx context.Context, sandbox Sandbox, workspace string, environment map[string]string, keys map[string]string) (func(), error) { if len(keys) == 0 { return func() {}, nil } @@ -54,7 +55,7 @@ func bindWorkspaceGitSSH(sandbox Sandbox, workspace string, environment map[stri clientRoot = "/workspace/.agentflow/runtime" } environment["GIT_ALLOW_PROTOCOL"] = gitwork.AllowedProtocols - stop, err := gitwork.StartSSHHosts(environment, keys, root, clientRoot) + stop, err := gitwork.StartSSHHosts(ctx, environment, keys, root, clientRoot) if err != nil { environment["GIT_SSH_VARIANT"] = "ssh" environment["GIT_SSH_COMMAND"] = "sh -c 'echo " + shellSingleQuoted("个人 Git SSH 密钥不可用:"+err.Error()+";可改用 HTTPS 地址(使用个人 Git Token)") + " >&2; exit 1' --" @@ -84,7 +85,7 @@ const terminalGitSSHUnavailable = `sh -c 'echo "当前工作区环境无法把 // - docker, seatbelt and anything else share one filesystem view among all // processes of the workspace, so the requester's key is not offered and // SSH remotes fail with a hint to use the platform's submit/push. -func BindTerminalGitSSH(sandbox Sandbox, command *CommandSpec, keys map[string]string) (func(), error) { +func BindTerminalGitSSH(ctx context.Context, sandbox Sandbox, command *CommandSpec, keys map[string]string) (func(), error) { if len(keys) == 0 { return func() {}, nil } @@ -106,7 +107,7 @@ func BindTerminalGitSSH(sandbox Sandbox, command *CommandSpec, keys map[string]s _ = os.RemoveAll(root) return nil, err } - stop, err := gitwork.StartSSH(command.environment, keys, root, "") + stop, err := gitwork.StartSSH(ctx, command.environment, keys, root, "") if err != nil { _ = os.RemoveAll(root) return nil, err diff --git a/internal/runtime/git_ssh_terminal_test.go b/internal/runtime/git_ssh_terminal_test.go index 66a3f5881..774e20801 100644 --- a/internal/runtime/git_ssh_terminal_test.go +++ b/internal/runtime/git_ssh_terminal_test.go @@ -52,7 +52,7 @@ func TestTerminalGitSSHAgentStaysOutOfWorkspace(t *testing.T) { sb = &processSandbox{workspace: workspace, driver: "bwrap"} } command := CommandSpec{environment: map[string]string{}} - stop, err := BindTerminalGitSSH(sb, &command, keys) + stop, err := BindTerminalGitSSH(t.Context(), sb, &command, keys) if err != nil { t.Fatal(err) } @@ -125,7 +125,7 @@ func TestTerminalGitSSHWithoutPrivateFilesystemOffersNoKey(t *testing.T) { for _, sb := range []Sandbox{&dockerSandbox{workspace: t.TempDir()}, &processSandbox{workspace: t.TempDir(), driver: "seatbelt"}} { before, _ := filepath.Glob("/tmp/lf-term-ssh-*") command := CommandSpec{environment: map[string]string{}} - stop, err := BindTerminalGitSSH(sb, &command, keys) + stop, err := BindTerminalGitSSH(t.Context(), sb, &command, keys) if err != nil { t.Fatal(err) } diff --git a/internal/runtime/sandbox_local.go b/internal/runtime/sandbox_local.go index 87106831b..bd526c52b 100644 --- a/internal/runtime/sandbox_local.go +++ b/internal/runtime/sandbox_local.go @@ -110,7 +110,7 @@ func ExecuteLocalCommand(ctx context.Context, workspace string, command CommandS if err != nil { return "", err } - stopSSH, err := bindCommandGitSSH(sandbox, workspace, bound, credentials) + stopSSH, err := bindCommandGitSSH(ctx, sandbox, workspace, bound, credentials) if err != nil { return "", err } diff --git a/internal/runtime/sandbox_manager.go b/internal/runtime/sandbox_manager.go index 65fe2d101..e201db49d 100644 --- a/internal/runtime/sandbox_manager.go +++ b/internal/runtime/sandbox_manager.go @@ -444,7 +444,7 @@ func (s *managedSandbox) Exec(ctx context.Context, command CommandSpec, sink Eve if err != nil { return "", err } - cleanup, err := bindCommandGitSSH(s.Sandbox, s.workspace, bound, credentials) + cleanup, err := bindCommandGitSSH(ctx, s.Sandbox, s.workspace, bound, credentials) if err != nil { return "", err } diff --git a/internal/runtime/workspace_terminal.go b/internal/runtime/workspace_terminal.go index 15e07c166..fb8d890d3 100644 --- a/internal/runtime/workspace_terminal.go +++ b/internal/runtime/workspace_terminal.go @@ -89,7 +89,7 @@ func (p *TerminalPool) HandleSandboxWithGit(ctx context.Context, sandbox Sandbox var bind func(*CommandSpec) error if len(credentials.SSHKeys) > 0 && sandbox != nil { bind = func(command *CommandSpec) error { - stop, err := BindTerminalGitSSH(sandbox, command, credentials.SSHKeys) + stop, err := BindTerminalGitSSH(ctx, sandbox, command, credentials.SSHKeys) if err == nil { stopSSH = stop }