feat: 加固 Agent CLI 执行边界并更新模型目录

收紧 SSH 命令护栏、权限校验与远程执行会话,并同步主流大模型供应商列表。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Logan.LiandCursor committed 2026-09-14 09:43:14 +08:00
1 parent 67660ae1d6
commit 2fd1624bec
18 files changed
+480 -49

No files matched your search

+31 -7
View File
@@ -29,13 +29,21 @@ func NewPersistentClient(cfg SSHConfig) (*PersistentClient, error) {
// Exec 在长连接上执行命令;连接断开时自动重连一次 // Logan.Li
func (p *PersistentClient) Exec(command string, timeout time.Duration) (*ExecResult, error) {
return p.ExecContext(context.Background(), command, timeout)
}
// ExecContext 在长连接上执行命令,并在调用方取消时关闭当前远端 session。 // Logan.Li
func (p *PersistentClient) ExecContext(ctx context.Context, command string, timeout time.Duration) (*ExecResult, error) {
p.mu.Lock()
defer p.mu.Unlock()
result, err := p.execLocked(command, timeout)
result, err := p.execLockedContext(ctx, command, timeout)
if err == nil {
return result, nil
}
if ctx.Err() != nil {
return result, ctx.Err()
}
// 连接级失败:重连后重试一次 // Logan.Li
if p.client != nil {
_ = p.client.Close()
@@ -46,11 +54,16 @@ func (p *PersistentClient) Exec(command string, timeout time.Duration) (*ExecRes
return result, err
}
p.client = client
return p.execLocked(command, timeout)
return p.execLockedContext(ctx, command, timeout)
}
// execLocked 假定已持锁,在当前 client 上执行 // Logan.Li
func (p *PersistentClient) execLocked(command string, timeout time.Duration) (*ExecResult, error) {
return p.execLockedContext(context.Background(), command, timeout)
}
// execLockedContext 假定已持锁,并将调用方上下文传递给当前 SSH session。 // Logan.Li
func (p *PersistentClient) execLockedContext(ctx context.Context, command string, timeout time.Duration) (*ExecResult, error) {
if p.client == nil {
client, err := connect(p.cfg)
if err != nil {
@@ -58,7 +71,7 @@ func (p *PersistentClient) execLocked(command string, timeout time.Duration) (*E
}
p.client = client
}
return execWithClient(p.client, p.cfg, command, timeout)
return execWithClientContext(ctx, p.client, p.cfg, command, timeout)
}
// Close 关闭长连接 // Logan.Li
@@ -75,6 +88,11 @@ func (p *PersistentClient) Close() error {
// execWithClient 在已有 SSH client 上执行命令(供 Exec / PersistentClient 共用) // Logan.Li
func execWithClient(client *ssh.Client, cfg SSHConfig, command string, timeout time.Duration) (*ExecResult, error) {
return execWithClientContext(context.Background(), client, cfg, command, timeout)
}
// execWithClientContext 在已有 SSH client 上执行命令,并响应调用方取消。 // Logan.Li
func execWithClientContext(parent context.Context, client *ssh.Client, cfg SSHConfig, command string, timeout time.Duration) (*ExecResult, error) {
start := time.Now()
result := &ExecResult{Host: cfg.Host, Port: cfg.Port}
@@ -90,7 +108,7 @@ func execWithClient(client *ssh.Client, cfg SSHConfig, command string, timeout t
timeout = 60 * time.Second
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
ctx, cancel := context.WithTimeout(parent, timeout)
defer cancel()
type execOut struct {
@@ -117,10 +135,16 @@ func execWithClient(client *ssh.Client, cfg SSHConfig, command string, timeout t
select {
case <-ctx.Done():
_ = session.Signal(ssh.SIGKILL)
_ = session.Close()
result.Duration = time.Since(start).Milliseconds()
result.Error = fmt.Sprintf("执行超时(%v)", timeout)
if parent.Err() != nil {
result.Error = "执行已取消"
} else {
result.Error = fmt.Sprintf("执行超时(%v)", timeout)
}
result.ExitCode = -1
return result, fmt.Errorf("exec timeout after %v", timeout)
return result, ctx.Err()
case out := <-ch:
result.Duration = time.Since(start).Milliseconds()
result.Output = string(out.output)
@@ -141,4 +165,4 @@ func execWithClient(client *ssh.Client, cfg SSHConfig, command string, timeout t
}
}
return result, nil
}
}