mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 01:10:54 +08:00
The SDK file tools Read, Write, Edit, MultiEdit, LS, Glob and Grep run in the server process, which is root in the image, and took any absolute or relative path: Read returned /app/keys/jwt.key or the config file, and Write could plant a skill or rewrite the config past the approval gate. - agent/filetools.go: defaultTools replaces the seven tools for every agent kind (chat, main agent, planner, worker). Same names, prompts and schemas; every path goes through an os.Root on the run's working directory, opened component by component below the data directory, so absolute paths elsewhere, "..", symlinks leading out, a run directory swapped for a symlink and files with extra hard links are refused. Grep no longer starts ripgrep. The LLM connection test passes an empty tool list instead of nil (nil meant the unconfined defaults). - traffic: chmod the go-mitmproxy CA private key to 0600 after the proxy creates or loads it; the CA certificate stays readable. - server: the executor gets HOME and XDG_CACHE_HOME in <data>/exec-home, owned by it, on the bash shim, the MCP drop shim and the custom-tool credential path. - server: ARTEX_LLM_PROXY and ARTEX_LLM_BASE_URL can carry user:pass@; they are scrubbed and sealed with the other startup secrets. - docker: no-new-privileges for the artex service, setuid/setgid bits stripped in the image, PLAYWRIGHT_BROWSERS_PATH kept at root's cache. - db: the persistence approval seed inserts each rule only when no rule of that name exists, so a re-run does not duplicate rules. - Root-gated tests run the bash shim, a custom tool and an MCP stdio command as the executor and check the uid, a 0600 root file, the server's /proc environ and a cache write.
174 lines
5.8 KiB
Go
174 lines
5.8 KiB
Go
//go:build linux
|
|
|
|
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
actool "github.com/Autumn-27/norma/tool"
|
|
)
|
|
|
|
// Root tests of the three executor paths: the Bash tool through the PATH shim, a
|
|
// custom tool through SysProcAttr.Credential, and an MCP stdio command through the
|
|
// drop shim. Each runs the same probe as the agent would and must show the executor
|
|
// identity, no access to a 0600 root file or to the server's /proc environ, and a
|
|
// writable HOME and cache.
|
|
|
|
type execRootFixture struct {
|
|
priv ExecPrivilege
|
|
home, secret, work string
|
|
}
|
|
|
|
func newExecRootFixture(t *testing.T) *execRootFixture {
|
|
t.Helper()
|
|
if os.Geteuid() != 0 {
|
|
t.Skip("needs root")
|
|
}
|
|
old := syscall.Umask(0o022)
|
|
syscall.Umask(old)
|
|
t.Cleanup(func() { syscall.Umask(old) })
|
|
// InstallBashShim and PrepareExecPrivilege set these process-wide; restore them.
|
|
for _, k := range []string{"PATH", envExecBash, envExecUID, envExecGID, envExecHome} {
|
|
t.Setenv(k, os.Getenv(k))
|
|
}
|
|
// A data directory like /app/data: root-owned and traversable.
|
|
dataDir, err := os.MkdirTemp("", "artex-exec-root-")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.RemoveAll(dataDir) })
|
|
if err := os.Chmod(dataDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
priv, err := ResolveExecPrivilege(DefaultExecUID, DefaultExecGID, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := PrepareExecPrivilege(priv, dataDir, "", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
secret := filepath.Join(dataDir, "root-only.key")
|
|
if err := os.WriteFile(secret, []byte("root-secret\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.ReadFile(secret); err != nil {
|
|
t.Fatalf("root must read its own key: %v", err)
|
|
}
|
|
work := filepath.Join(dataDir, "tasks", "1")
|
|
if err := os.MkdirAll(work, 0o775); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &execRootFixture{priv: priv, home: filepath.Join(dataDir, execHomeDir), secret: secret, work: work}
|
|
}
|
|
|
|
// probe is the shell script each path runs as the executor.
|
|
func (f *execRootFixture) probe() string {
|
|
return fmt.Sprintf(`echo "uid=$(id -u) gid=$(id -g) groups=$(id -G)"
|
|
if cat '%s' >/dev/null 2>&1; then echo secret=read; else echo secret=denied; fi
|
|
if cat /proc/%d/environ >/dev/null 2>&1; then echo environ=read; else echo environ=denied; fi
|
|
echo "home=$HOME cache=$XDG_CACHE_HOME"
|
|
if mkdir -p "$XDG_CACHE_HOME/probe" && echo ok > "$XDG_CACHE_HOME/probe/x"; then echo cache=written; else echo cache=failed; fi
|
|
`, f.secret, os.Getpid())
|
|
}
|
|
|
|
func (f *execRootFixture) check(t *testing.T, path, out string) {
|
|
t.Helper()
|
|
for _, want := range []string{
|
|
fmt.Sprintf("uid=%d gid=%d groups=%d", DefaultExecUID, DefaultExecGID, DefaultExecGID),
|
|
"secret=denied",
|
|
"environ=denied",
|
|
"home=" + f.home + " cache=" + filepath.Join(f.home, ".cache"),
|
|
"cache=written",
|
|
} {
|
|
if !strings.Contains(out, want) {
|
|
t.Errorf("%s: want %q in the probe output:\n%s", path, want, out)
|
|
}
|
|
}
|
|
fi, err := os.Stat(filepath.Join(f.home, ".cache", "probe", "x"))
|
|
if err != nil {
|
|
t.Fatalf("%s: cache file missing: %v", path, err)
|
|
}
|
|
if st := fi.Sys().(*syscall.Stat_t); st.Uid != DefaultExecUID {
|
|
t.Errorf("%s: cache file owned by uid %d, want %d", path, st.Uid, DefaultExecUID)
|
|
}
|
|
t.Logf("%s probe output:\n%s", path, out)
|
|
}
|
|
|
|
// Norma's Bash tool runs exec.Command("bash"): the PATH shim drops and execs bash.
|
|
func TestExecutorBashShimAsRoot(t *testing.T) {
|
|
f := newExecRootFixture(t)
|
|
if err := InstallBashShim(f.priv); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.RemoveAll(filepath.SplitList(os.Getenv("PATH"))[0]) })
|
|
in, _ := json.Marshal(map[string]string{"command": f.probe()})
|
|
res, err := actool.NewBash().Call(context.Background(), in, &actool.ToolContext{WorkingDir: f.work})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.check(t, "bash shim", res.Flatten())
|
|
}
|
|
|
|
// A custom script tool runs through execPython, which applies the credential.
|
|
func TestExecutorCustomToolAsRoot(t *testing.T) {
|
|
f := newExecRootFixture(t)
|
|
py, err := exec.LookPath("python3")
|
|
if err != nil {
|
|
t.Fatalf("python3 is needed for the custom-tool path: %v", err)
|
|
}
|
|
code := `import os, subprocess
|
|
print(subprocess.run(["sh", "-c", os.environ["PROBE"]], capture_output=True, text=True).stdout)`
|
|
out, err := execPython(context.Background(), py, "probe", code, nil, f.work, []string{"PROBE=" + f.probe()}, 30*time.Second, f.priv)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.check(t, "custom tool", out)
|
|
}
|
|
|
|
// An MCP stdio command is started as `<server> __artex-exec-drop uid gid -- cmd`;
|
|
// mcp.NewStdioClient runs it with exec.Command and the server environment.
|
|
func TestExecutorMCPStdioAsRoot(t *testing.T) {
|
|
f := newExecRootFixture(t)
|
|
command, args, err := execDropCommand(f.priv, "sh", []string{"-c", f.probe()})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := exec.Command(command, args...)
|
|
c.Dir = f.work
|
|
out, err := c.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%v\n%s", err, out)
|
|
}
|
|
f.check(t, "mcp stdio", string(out))
|
|
}
|
|
|
|
// applyExecCredential sets the credential and the executor's HOME; the later env
|
|
// entry wins in exec.Cmd. Runs without root: nothing is started.
|
|
func TestApplyExecCredentialSetsExecutorHome(t *testing.T) {
|
|
t.Setenv(envExecHome, "/data/exec-home")
|
|
c := exec.Command("true")
|
|
c.Env = []string{"HOME=/root", "KEEP=1"}
|
|
applyExecCredential(c, ExecPrivilege{Enabled: true, UID: 10001, GID: 10002})
|
|
if cr := c.SysProcAttr.Credential; cr == nil || cr.Uid != 10001 || cr.Gid != 10002 {
|
|
t.Fatalf("credential = %+v", c.SysProcAttr.Credential)
|
|
}
|
|
want := []string{"HOME=/root", "KEEP=1", "HOME=/data/exec-home", "XDG_CACHE_HOME=/data/exec-home/.cache"}
|
|
if strings.Join(c.Env, "|") != strings.Join(want, "|") {
|
|
t.Fatalf("env = %v, want %v", c.Env, want)
|
|
}
|
|
disabled := exec.Command("true")
|
|
applyExecCredential(disabled, ExecPrivilege{})
|
|
if disabled.SysProcAttr != nil || disabled.Env != nil {
|
|
t.Fatal("a disabled privilege must leave the command unchanged")
|
|
}
|
|
}
|