Files
ARTEX/agent/rundir_test.go
T
BaaRai e73cdeda93 security(agent): share only run-dir leaves; main agent gets its own leaf
The task and chat workspace trees were shared whole with the executor
group, and the main agent and the planner worked in tasks/<id>, the parent
of every worker directory: the executor could rename, remove or replace a
run directory, its own included, and plant entries beside them.

- Layout: no agent working directory is an ancestor of another. The main
  agent and the planner work in tasks/<id>/main, a worker in
  tasks/<id>/i<N>, a chat in sessions/<id>. With the privilege drop on,
  tasks/, sessions/ and tasks/<id> are root:root 0755 and only the leaves
  are root:<exec gid> 2775.
- PrepareExecPrivilege no longer shares tasks/ and sessions/. It sets the
  parents, shares the leaves found on disk with what they hold, and
  registers the run-directory maker (agent.SetRunDirMaker). ensureRunDir
  and the chat session directory go through it: each level is created
  with Mkdir and set through a descriptor opened with O_NOFOLLOW, parents
  before the leaf, so a link planted where a run directory goes is
  refused. Without the drop both stay a plain MkdirAll.
- Uploads for the main agent land in tasks/<id>/main/uploads, and the
  attachment paths given to it use the same directory; session uploads
  create sessions/<id> by the same rule.
- Task archives still pack and delete the whole task directory, so the
  package holds files/tasks/<id>/main and i<N>. The package keeps neither
  owners nor directory modes: the restore puts the layout back
  (installTaskArchiveFiles takes the privilege).
- Existing data is not migrated. Files a task wrote directly in
  tasks/<id>/ stay there with their owner and mode; the main agent starts
  in an empty tasks/<id>/main and its file tools no longer reach them.

Tests: TestRunDirLayout and TestIsRunDirName (agent); root-only
TestRunDirLayoutAsRoot, TestRunDirSiblingsProtectedAsRoot,
TestRunDirLayoutOnExistingTreeAsRoot and
TestTaskArchiveRestoreRunDirOwnershipAsRoot (server). The archive tests
use the main/ and i<N> leaves. SECURITY.md states the invariant, names the
race that remains and drops ensureRunDir from the residual list.
2026-11-20 05:35:55 +01:00

62 lines
2.0 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
)
// The main agent and the planner work in tasks/<id>/main, a worker in
// tasks/<id>/i<N>, a chat in sessions/<id>: no run directory is an ancestor of
// another, and each is created through the server's maker when one is set.
func TestRunDirLayout(t *testing.T) {
base := t.TempDir()
mainDir := ensureRunDir(base, 7, 0)
worker := ensureRunDir(base, 7, 12)
session := EnsureSessionDir(base, "conv-3")
task := filepath.Join(base, "tasks", "7")
if mainDir != filepath.Join(task, "main") || worker != filepath.Join(task, "i12") || session != filepath.Join(base, "sessions", "conv-3") {
t.Fatalf("run directories: main %s, worker %s, session %s", mainDir, worker, session)
}
dirs := []string{mainDir, worker, session}
for _, dir := range dirs {
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
t.Fatalf("%s was not created: %v", dir, err)
}
for _, other := range dirs {
if other != dir && strings.HasPrefix(other, dir+string(filepath.Separator)) {
t.Errorf("%s is an ancestor of %s", dir, other)
}
}
}
var made []string
SetRunDirMaker(func(b, leaf string) error {
if b != base {
t.Errorf("maker base = %s, want %s", b, base)
}
made = append(made, leaf)
return nil
})
t.Cleanup(func() { SetRunDirMaker(nil) })
want := []string{EnsureRunDir(base, 8, 0), EnsureRunDir(base, 8, 5), EnsureSessionDir(base, "conv-4")}
if strings.Join(made, "|") != strings.Join(want, "|") {
t.Errorf("maker calls = %v, want %v", made, want)
}
if _, err := os.Stat(filepath.Join(base, "tasks", "8")); !os.IsNotExist(err) {
t.Errorf("a run directory was created beside the maker: %v", err)
}
}
func TestIsRunDirName(t *testing.T) {
for name, want := range map[string]bool{
"main": true, "i1": true, "i120": true,
"": false, "i": false, "i1a": false, "i-1": false, "I1": false, "uploads": false, "cmd-output": false, "main2": false,
} {
if got := IsRunDirName(name); got != want {
t.Errorf("IsRunDirName(%q) = %v, want %v", name, got, want)
}
}
}