Skip to content
Open
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
52 changes: 52 additions & 0 deletions internal/agent/terminal_dir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: MPL-2.0

package agent

import (
"context"
"io/fs"
"os"
"path/filepath"
"testing"
)

// localMachine is an SSH machine whose files are this computer's: enough
// for the path checks, which only look and resolve links.
type localMachine struct{ Machine }

func (localMachine) Lstat(_ context.Context, name string) (fs.FileInfo, error) {
return os.Lstat(name)
}

func (localMachine) RealPath(_ context.Context, name string) (string, error) {
return filepath.EvalSymlinks(name)
}

// A terminal's folder on an SSH machine is checked as on the server: a link
// in the project that leads outside it is refused (#526). The desktop only
// compared the joined path's prefix, and accepted it.
func TestTerminalDirOnAMachine(t *testing.T) {
workspace, outside := t.TempDir(), t.TempDir()
workspace, _ = filepath.EvalSymlinks(workspace)
os.Mkdir(filepath.Join(workspace, "src"), 0755)
if err := os.Symlink(outside, filepath.Join(workspace, "out")); err != nil {
t.Skip("no symbolic links here:", err)
}
machine := localMachine{}
ctx := context.Background()
if dir, err := TerminalDir(ctx, machine, workspace, ""); err != nil || dir != workspace {
t.Errorf("no cwd: %q %v", dir, err)
}
if dir, err := TerminalDir(ctx, machine, workspace, "src"); err != nil || dir != filepath.Join(workspace, "src") {
t.Errorf("a folder of the project: %q %v", dir, err)
}
if dir, err := TerminalDir(ctx, machine, workspace, "out"); err == nil {
t.Errorf("a link out of the project was accepted: %q", dir)
}
if dir, err := TerminalDir(ctx, machine, workspace, "../"+filepath.Base(outside)); err == nil {
t.Errorf("a path out of the project was accepted: %q", dir)
}
if dir, err := TerminalDir(ctx, nil, workspace, "out"); err == nil {
t.Errorf("on this computer, a link out of the project was accepted: %q", dir)
}
}
14 changes: 14 additions & 0 deletions internal/agent/terminal_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ func NoTerminal(id string, open []string) error {
return fmt.Errorf("No terminal with session_id %s in this chat; its terminals: %s", id, strings.Join(open, ", "))
}

// TerminalDir is the folder terminal_tabs opens a terminal in: the
// workspace, or cwd within it. On an SSH machine (machine not nil), links
// are resolved there (MachinePath), so one cannot lead outside the
// workspace; the desktop only compared the joined path's prefix (#526).
func TerminalDir(ctx context.Context, machine Machine, workspace, cwd string) (string, error) {
switch {
case cwd == "":
return workspace, nil
case machine != nil:
return MachinePath(ctx, machine, workspace, cwd)
}
return WorkspacePath(workspace, cwd)
}

// TerminalReadLimit is how much terminal_read returns: max_bytes, within
// bounds, or 8 KiB.
func TerminalReadLimit(maxBytes *int) int64 {
Expand Down
8 changes: 4 additions & 4 deletions internal/server/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,12 @@ func (server *headlessServer) terminalTools(chat *serverChat) agent.PanelToolHan
if machine == nil && chat.Config.MachineID != "" {
err = fmt.Errorf("This project's SSH machine is no longer on the server")
}
if err == nil && args.Cwd != "" {
if err == nil {
var on agent.Machine
if machine != nil {
dir, err = agent.MachinePath(ctx, machine, dir, args.Cwd)
} else {
dir, err = agent.WorkspacePath(dir, args.Cwd)
on = machine
}
dir, err = agent.TerminalDir(ctx, on, dir, args.Cwd)
}
if err == nil {
err = ctx.Err()
Expand Down
45 changes: 25 additions & 20 deletions internal/ui/terminal_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"image/draw"
"image/png"
"io"
"path"
"strings"
"time"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -102,6 +100,31 @@ func (ui *desktopUI) terminalTools(chat *conversation) agent.PanelToolHandler {
if err := decoder.Decode(&args); err != nil {
return agent.PanelToolResult{}, err
}
// The folder of a new terminal is resolved first, outside the UI
// thread: on an SSH machine it is looked up there.
dir := ""
if name == "terminal_tabs" && args.Action == "open" {
var workspace string
var machine agent.Machine
missing := false
ui.onUI(ctx, func() {
workspace = chat.Config.Workspace
if id := chat.Config.MachineID; id != "" {
if found := ui.machineFor(id); found != nil {
machine = found
} else {
missing = true
}
}
})
if missing {
return agent.PanelToolResult{}, fmt.Errorf("This conversation's SSH machine is no longer in Settings → General")
}
var err error
if dir, err = agent.TerminalDir(ctx, machine, workspace, args.Cwd); err != nil {
return agent.PanelToolResult{}, err
}
}
var term *panelTerminal
var problem error
var list []map[string]any
Expand Down Expand Up @@ -144,24 +167,6 @@ func (ui *desktopUI) terminalTools(chat *conversation) agent.PanelToolHandler {
case "list":
return
case "open":
dir := chat.Config.Workspace
switch {
case args.Cwd == "":
case chat.Config.MachineID != "": // a folder of the project on the machine
dir = path.Join(dir, args.Cwd)
if path.IsAbs(args.Cwd) {
dir = path.Clean(args.Cwd)
}
if dir != chat.Config.Workspace && !strings.HasPrefix(dir, strings.TrimSuffix(chat.Config.Workspace, "/")+"/") {
problem = fmt.Errorf("The path is outside the workspace")
return
}
default:
dir, problem = agent.WorkspacePath(dir, args.Cwd)
if problem != nil {
return
}
}
tab, err := panel.terminalTab(dir, chat.Config.MachineID)
if err != nil {
problem = err
Expand Down
Loading