mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-04 05:33:55 +08:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a4fa45245 | ||
|
|
aeb094d5ef | ||
|
|
eb05248804 |
@@ -6,6 +6,7 @@ All notable changes to this project are documented in this file. The format is b
|
||||
|
||||
### Security
|
||||
|
||||
- **`ask_operator` reads run-directory images through a task-rooted handle.** The copy path resolved the asking run directory with `EvalSymlinks` and then opened the resolved path, so a worker that replaced its run directory with a symlink to `/` made the confinement root `/`: an absolute `image_path` to a symlink named `*.png` could have the panel copy `/proc/1/environ` or `/app/keys/jwt.key` into `tasks/<id>/uploads`, where the worker reads it. The read and the upload write are now both anchored with `os.Root` at `tasks/<id>`; the run directory must be a real directory, no path component may be a symlink, the opened file must be the one `Lstat` saw, and the copy is bounded to 10 MB. Existing `..`, outside-path and non-image refusals are unchanged.
|
||||
- **Task archives no longer follow a directory swapped for a symlink while the task is packed.** The packer, running as root, listed each directory of the staged run tree and then opened its files by full path, so code running as the executor uid that kept a working directory inside the run tree could replace a listed directory with a symlink (for example to `/app/keys`) and have root-only files such as the session signing key copied into the archive, then read them back from the run directory after a restore. The packer now walks and opens the payload through an `os.Root` anchored at it, so no lookup can leave the payload, and refuses the archive when a file it opens is not the regular file the walk listed (device and inode compared). Symlinks already present in the run tree are still skipped and never followed. Behaviour change: an archive whose run tree changes this way while it is packed fails instead of completing, and the task stays in place.
|
||||
- **Agent sessions no longer offer Norma's background-task tools.** Norma adds `Monitor`, `TaskOutput`, `TaskStop` and `TaskList` to every session that does not opt out, and only the connection test and the goal decomposer opted out. `Monitor` starts a shell command in the panel process, not through the sandbox that `Bash` uses, so in `ARTEX_SANDBOX=required` mode a worker, planner, chat or main-agent model could run commands in the panel container, outside the task's worker, and read or write other tasks' workspaces and transcripts. Every agent session is now built through one constructor. It turns these tools off while the sandbox is required, and always for a session that has neither `Bash` nor the interactive shell, so an orchestration agent cannot gain a shell through `Monitor`. Sessions that do have `Bash` or `shell_*` keep them when the sandbox is off: their background runs, PTY sessions and the pause kill switch depend on the same task manager. The constructor also turns a nil tool list into an empty one, because Norma reads nil as "use my own default tools" and installs its unconfined `Bash` and file tools; the tool catalogue resolver also returns an empty list instead of nil when the tools table cannot be read, the case of the Auto chat agent, whose base list is empty.
|
||||
- **The target gate honors operator-approved egress grants.** The gate compared every target with pins only, while the sandbox proxy allowed every port of an operator-approved CIDR grant: a host already inside a granted task or company scope still raised per-port and bare-host approval prompts (pins match `host:port`, and a request naming the host without a port is covered only by a port-0 pin). `CheckTargets` now also consults the grants the operator approved for the task (its approved, unexpanded `task_scope` rows and the entries frozen into `task_scope_egress` by approved company grants, normalized as the proxy allowlist is): a domain matching a granted domain, an address inside a granted CIDR, or a range fully inside one passes as covered by `scope_grant`, recorded once per process and target key as a system-decided allowed row with the target outcome `scope_grant`. Pins, stored exemptions, block rules and human approvals are unchanged: anything outside a grant is decided as before, and task allow rules play no role, as everywhere in the gate. Related to #830, the mirror gap on the network side.
|
||||
|
||||
+112
-49
@@ -170,10 +170,11 @@ func askOperatorActivity(text string, att []chatAttachment, derived []string) db
|
||||
return a
|
||||
}
|
||||
|
||||
// resolveAskOperatorImage maps image_path onto a regular file inside runDir, the
|
||||
// run directory of the agent that asks (its CWD). Relative paths are resolved from it.
|
||||
// ".." segments, paths that EvalSymlinks outside runDir, and non-images are refused.
|
||||
func resolveAskOperatorImage(runDir, src string) (string, error) {
|
||||
// askOperatorImageRel maps image_path onto a path relative to the task root.
|
||||
// Relative paths resolve from runName, the run directory of the agent that asks
|
||||
// (i<intentID>, or main). An absolute path must be inside that run directory.
|
||||
// ".." segments, paths outside the run directory and non-images are refused.
|
||||
func askOperatorImageRel(taskDir, runName, src string) (string, error) {
|
||||
src = strings.TrimSpace(src)
|
||||
ext := strings.ToLower(filepath.Ext(src))
|
||||
if !askOperatorImageExt[ext] {
|
||||
@@ -182,34 +183,54 @@ func resolveAskOperatorImage(runDir, src string) (string, error) {
|
||||
if slices.Contains(strings.Split(filepath.ToSlash(src), "/"), "..") {
|
||||
return "", fmt.Errorf("image_path must stay inside the run directory")
|
||||
}
|
||||
if !filepath.IsAbs(src) {
|
||||
src = filepath.Join(runDir, src)
|
||||
var rel string
|
||||
if filepath.IsAbs(src) {
|
||||
r, err := filepath.Rel(taskDir, filepath.Clean(src))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("image_path must stay inside the run directory")
|
||||
}
|
||||
rel = r
|
||||
} else {
|
||||
rel = filepath.Join(runName, src)
|
||||
}
|
||||
realRoot, err := filepath.EvalSymlinks(runDir)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("image not readable")
|
||||
}
|
||||
real, err := filepath.EvalSymlinks(src)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("image not readable")
|
||||
}
|
||||
rel, err := filepath.Rel(realRoot, real)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
if !filepath.IsLocal(rel) || (rel != runName && !strings.HasPrefix(rel, runName+string(filepath.Separator))) {
|
||||
return "", fmt.Errorf("image_path must stay inside the run directory")
|
||||
}
|
||||
return real, nil
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
// openAskOperatorImage opens the image at path and requires the opened file to be
|
||||
// the regular file lst describes, lst being the Lstat of path. Between the
|
||||
// EvalSymlinks of resolveAskOperatorImage and this open, the agent can replace the
|
||||
// file with a symlink: Lstat does not follow one and the open does, so the two then
|
||||
// name different files and the image is refused.
|
||||
func openAskOperatorImage(path string, lst os.FileInfo) (*os.File, error) {
|
||||
// askOperatorRejectSymlinks refuses a symlink in any component of rel below the
|
||||
// task root. os.Root follows links that stay inside the root, so a link from the
|
||||
// run directory to a sibling would otherwise be read.
|
||||
func askOperatorRejectSymlinks(root *os.Root, rel string) error {
|
||||
parts := strings.Split(rel, string(filepath.Separator))
|
||||
prefix := parts[0]
|
||||
for i, part := range parts {
|
||||
if i > 0 {
|
||||
prefix = filepath.Join(prefix, part)
|
||||
}
|
||||
info, err := root.Lstat(prefix)
|
||||
if err != nil {
|
||||
return fmt.Errorf("image not readable")
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
return fmt.Errorf("image_path must stay inside the run directory")
|
||||
}
|
||||
if i < len(parts)-1 && !info.IsDir() {
|
||||
return fmt.Errorf("image not readable")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// openAskOperatorImage opens rel under the task root and requires the opened file
|
||||
// to be the regular file lst describes. The root refuses a path outside the task
|
||||
// and a symlink that leaves it; Lstat does not follow the last component.
|
||||
func openAskOperatorImage(root *os.Root, rel string, lst os.FileInfo) (*os.File, error) {
|
||||
if !lst.Mode().IsRegular() {
|
||||
return nil, fmt.Errorf("image_path must be a regular file")
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
f, err := root.Open(rel)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("image not readable")
|
||||
}
|
||||
@@ -220,18 +241,22 @@ func openAskOperatorImage(path string, lst os.FileInfo) (*os.File, error) {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// askOperatorUploadsDir returns tasks/<id>/uploads, created when absent. A symlink
|
||||
// or anything else that is not a directory is refused: the copy would be written
|
||||
// through it.
|
||||
func askOperatorUploadsDir(taskDir string) (string, error) {
|
||||
dir := filepath.Join(taskDir, "uploads")
|
||||
if err := os.Mkdir(dir, 0o755); err != nil && !errors.Is(err, fs.ErrExist) {
|
||||
return "", err
|
||||
// askOperatorUploadsRoot returns tasks/<id>/uploads as a root anchored at the
|
||||
// task directory. A symlink or anything else that is not a directory is refused:
|
||||
// the copy would be written through it.
|
||||
func askOperatorUploadsRoot(taskRoot *os.Root) (*os.Root, error) {
|
||||
if err := taskRoot.Mkdir("uploads", 0o755); err != nil && !errors.Is(err, fs.ErrExist) {
|
||||
return nil, err
|
||||
}
|
||||
if fi, err := os.Lstat(dir); err != nil || !fi.IsDir() {
|
||||
return "", fmt.Errorf("the task uploads directory is not a directory")
|
||||
fi, err := taskRoot.Lstat("uploads")
|
||||
if err != nil || fi.Mode()&os.ModeSymlink != 0 || !fi.IsDir() {
|
||||
return nil, fmt.Errorf("the task uploads directory is not a directory")
|
||||
}
|
||||
return dir, nil
|
||||
uploads, err := taskRoot.OpenRoot("uploads")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the task uploads directory is not a directory")
|
||||
}
|
||||
return uploads, nil
|
||||
}
|
||||
|
||||
// askOperatorStageTries bounds the names tried for one staged copy.
|
||||
@@ -257,33 +282,57 @@ func stageAskOperatorImage(dataDir string, taskID, intentID int64, src string) (
|
||||
if src == "" {
|
||||
return nil, nil
|
||||
}
|
||||
taskDir := filepath.Join(dataDir, "tasks", strconv.FormatInt(taskID, 10))
|
||||
resolved, err := resolveAskOperatorImage(agent.EnsureRunDir(dataDir, taskID, intentID), src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
taskRel := filepath.Join("tasks", strconv.FormatInt(taskID, 10))
|
||||
runName := "main"
|
||||
if intentID > 0 {
|
||||
runName = "i" + strconv.FormatInt(intentID, 10)
|
||||
}
|
||||
st, err := os.Lstat(resolved)
|
||||
// Every lookup is anchored at the task directory: a run directory or an
|
||||
// intermediate directory swapped for a symlink cannot leave the task.
|
||||
dataRoot, err := os.OpenRoot(dataDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("image not readable")
|
||||
}
|
||||
in, err := openAskOperatorImage(resolved, st)
|
||||
defer dataRoot.Close()
|
||||
taskRoot, err := dataRoot.OpenRoot(taskRel)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("image not readable")
|
||||
}
|
||||
defer taskRoot.Close()
|
||||
runInfo, err := taskRoot.Lstat(runName)
|
||||
if err != nil || runInfo.Mode()&os.ModeSymlink != 0 || !runInfo.IsDir() {
|
||||
return nil, fmt.Errorf("image_path must stay inside the run directory")
|
||||
}
|
||||
rel, err := askOperatorImageRel(filepath.Join(dataDir, taskRel), runName, src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := askOperatorRejectSymlinks(taskRoot, rel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
st, err := taskRoot.Lstat(rel)
|
||||
if err != nil || !st.Mode().IsRegular() {
|
||||
return nil, fmt.Errorf("image not readable")
|
||||
}
|
||||
if st.Size() > askOperatorMaxImage {
|
||||
return nil, fmt.Errorf("image too large (%s, max 10MB)", humanBytes(st.Size()))
|
||||
}
|
||||
in, err := openAskOperatorImage(taskRoot, rel, st)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer in.Close()
|
||||
if st.Size() > askOperatorMaxImage {
|
||||
return nil, fmt.Errorf("image too large (%s, max 10MB)", humanBytes(st.Size()))
|
||||
}
|
||||
dir, err := askOperatorUploadsDir(taskDir)
|
||||
uploads, err := askOperatorUploadsRoot(taskRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ext := strings.ToLower(filepath.Ext(resolved))
|
||||
defer uploads.Close()
|
||||
ext := strings.ToLower(filepath.Ext(rel))
|
||||
var name string
|
||||
var out *os.File
|
||||
for try := 1; ; try++ {
|
||||
name = askOperatorStageName(ext)
|
||||
out, err = os.OpenFile(filepath.Join(dir, name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o640)
|
||||
out, err = uploads.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o640)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
@@ -291,9 +340,23 @@ func stageAskOperatorImage(dataDir string, taskID, intentID int64, src string) (
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
defer out.Close()
|
||||
if _, err := io.Copy(out, in); err != nil {
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = out.Close()
|
||||
_ = uploads.Remove(name)
|
||||
}
|
||||
}()
|
||||
n, err := io.Copy(out, io.LimitReader(in, askOperatorMaxImage+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []chatAttachment{{Name: name, Path: "uploads/" + name, Size: st.Size()}}, nil
|
||||
if n > askOperatorMaxImage {
|
||||
return nil, fmt.Errorf("image too large (%s, max 10MB)", humanBytes(n))
|
||||
}
|
||||
if err := out.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
committed = true
|
||||
return []chatAttachment{{Name: name, Path: "uploads/" + name, Size: n}}, nil
|
||||
}
|
||||
|
||||
@@ -282,16 +282,22 @@ func TestStageAskOperatorImageNeverOverwrites(t *testing.T) {
|
||||
// elsewhere.
|
||||
func TestOpenAskOperatorImageRefusesSwap(t *testing.T) {
|
||||
dir, taskDir := askOperatorRunDirs(t)
|
||||
root, err := os.OpenRoot(taskDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = root.Close() })
|
||||
src := filepath.Join(taskDir, "i3", "shot.png")
|
||||
rel := filepath.Join("i3", "shot.png")
|
||||
outside := filepath.Join(dir, "outside.png")
|
||||
if err := os.WriteFile(outside, []byte("outside"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lst, err := os.Lstat(src)
|
||||
lst, err := root.Lstat(rel)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f, err := openAskOperatorImage(src, lst)
|
||||
f, err := openAskOperatorImage(root, rel, lst)
|
||||
if err != nil {
|
||||
t.Fatalf("unchanged file: %v", err)
|
||||
}
|
||||
@@ -305,32 +311,95 @@ func TestOpenAskOperatorImageRefusesSwap(t *testing.T) {
|
||||
if err := os.Rename(other, src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f, err := openAskOperatorImage(src, lst); err == nil {
|
||||
if f, err := openAskOperatorImage(root, rel, lst); err == nil {
|
||||
f.Close()
|
||||
t.Fatal("opened a file that replaced the checked one")
|
||||
}
|
||||
|
||||
// A symlink to a file outside the run directory takes the path.
|
||||
// A symlink to a file outside the run directory takes the path. os.Root
|
||||
// refuses the absolute target because it leaves the task root.
|
||||
if err := os.Remove(src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(outside, src); err != nil {
|
||||
t.Skipf("no symlink here: %v", err)
|
||||
}
|
||||
if f, err := openAskOperatorImage(src, lst); err == nil {
|
||||
if f, err := openAskOperatorImage(root, rel, lst); err == nil {
|
||||
f.Close()
|
||||
t.Fatal("opened the target of a symlink that replaced the checked file")
|
||||
}
|
||||
// The swap came before the Lstat: the last component is a symlink.
|
||||
if lst, err = os.Lstat(src); err != nil {
|
||||
if lst, err = root.Lstat(rel); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f, err := openAskOperatorImage(src, lst); err == nil {
|
||||
if f, err := openAskOperatorImage(root, rel, lst); err == nil {
|
||||
f.Close()
|
||||
t.Fatal("opened a path whose last component is a symlink")
|
||||
}
|
||||
}
|
||||
|
||||
// A run directory replaced by a symlink must not turn the task root into the
|
||||
// confinement root: the read stays anchored at tasks/<id>.
|
||||
func TestStageAskOperatorImageRejectsRunDirSymlink(t *testing.T) {
|
||||
dir, taskDir := askOperatorRunDirs(t)
|
||||
runDir := filepath.Join(taskDir, "i3")
|
||||
moved := filepath.Join(taskDir, "i3.real")
|
||||
if err := os.Rename(runDir, moved); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = os.Remove(runDir)
|
||||
_ = os.Rename(moved, runDir)
|
||||
})
|
||||
if err := os.Symlink("/", runDir); err != nil {
|
||||
t.Skipf("no symlink here: %v", err)
|
||||
}
|
||||
// taskDir/shot.png is outside the run directory but inside the task. With the
|
||||
// run directory symlinked to /, the old confinement root resolved to / and
|
||||
// accepted it; the task root must not move.
|
||||
outsideRun := filepath.Join(taskDir, "shot.png")
|
||||
if err := os.WriteFile(outsideRun, []byte("outside the run dir"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if att, err := stageAskOperatorImage(dir, 7, 3, outsideRun); err == nil {
|
||||
t.Fatalf("staged %v through a symlinked run directory", att)
|
||||
}
|
||||
if att, err := stageAskOperatorImage(dir, 7, 3, "shot.png"); err == nil {
|
||||
t.Fatalf("staged %v through a symlinked run directory", att)
|
||||
}
|
||||
}
|
||||
|
||||
// A symlink inside the run directory must not lead to a sibling run directory.
|
||||
func TestStageAskOperatorImageRejectsSiblingSymlink(t *testing.T) {
|
||||
dir, taskDir := askOperatorRunDirs(t)
|
||||
link := filepath.Join(taskDir, "i3", "sibling")
|
||||
if err := os.Symlink(filepath.Join("..", "main"), link); err != nil {
|
||||
t.Skipf("no symlink here: %v", err)
|
||||
}
|
||||
if att, err := stageAskOperatorImage(dir, 7, 3, filepath.Join("sibling", "shot.png")); err == nil {
|
||||
t.Fatalf("staged %v through a sibling symlink", att)
|
||||
}
|
||||
}
|
||||
|
||||
// uploads/ swapped for a symlink must not redirect the copy outside the task.
|
||||
func TestStageAskOperatorImageRejectsUploadsSymlink(t *testing.T) {
|
||||
dir, taskDir := askOperatorRunDirs(t)
|
||||
outside := t.TempDir()
|
||||
if err := os.Symlink(outside, filepath.Join(taskDir, "uploads")); err != nil {
|
||||
t.Skipf("no symlink here: %v", err)
|
||||
}
|
||||
if att, err := stageAskOperatorImage(dir, 7, 3, "shot.png"); err == nil {
|
||||
t.Fatalf("staged %v through a symlinked uploads directory", att)
|
||||
}
|
||||
entries, err := os.ReadDir(outside)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("wrote %d file(s) outside the task", len(entries))
|
||||
}
|
||||
}
|
||||
|
||||
// The emitted activity must carry the {text, attachments} Detail shape the
|
||||
// transcript parser expects, so the operator sees the question with the
|
||||
// attachment card for the screenshot.
|
||||
|
||||
Reference in New Issue
Block a user