Files
ARTEX/server/envseal_linux_test.go
T
BaaRai eb5d3d73c1 security(agent): confine the file tools and close the executor review
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.
2026-11-20 05:35:54 +01:00

134 lines
3.5 KiB
Go

//go:build linux
package server
import (
"bytes"
"encoding/hex"
"fmt"
"os"
"os/exec"
"strings"
"testing"
)
const sealHelperEnv = "ARTEX_SEAL_TEST_HELPER"
// sealHelper is the server's start sequence in miniature: seal, scrub, then report
// what the kernel environment block and a child process see. It runs in the
// re-executed image, where SealProcessEnv restores the values from the handoff.
func sealHelper() {
SealProcessEnv()
proc, err := os.ReadFile("/proc/self/environ")
if err != nil {
fmt.Printf("SEAL error %v\n", err)
return
}
fmt.Printf("SEAL proc %x\n", proc)
for _, k := range secretEnv {
v, ok := os.LookupEnv(k)
fmt.Printf("SEAL value %s %x %t\n", k, v, ok)
}
ScrubSecretEnv()
child, err := exec.Command("env").Output()
if err != nil {
fmt.Printf("SEAL error %v\n", err)
return
}
fmt.Printf("SEAL child %x\n", child)
fmt.Println("SEAL done")
}
// TestSealedEnvironmentHandoff starts the test binary with the secrets in its
// environment, as the server is started. The kernel environment block of the sealed
// image and the environment of a child must hold none of the secret names, and the
// values restored in the process must equal the originals, including empty and
// multi-line ones.
func TestSealedEnvironmentHandoff(t *testing.T) {
if os.Getenv(sealHelperEnv) == "1" {
sealHelper()
return
}
want := map[string]string{
"ARTEX_PG_DSN": "postgres://artex:p@ss@db.internal/artex?sslmode=disable\nsecond line\twith tab",
"ARTEX_SECRET_KEY": "",
"ANTHROPIC_API_KEY": "sk-ant-test",
"OPENAI_API_KEY": "=leading-equals",
// Proxy and endpoint URLs can carry credentials in their userinfo.
"ARTEX_LLM_PROXY": "http://proxyuser:proxypass@proxy.internal:3128",
"ARTEX_LLM_BASE_URL": "https://apiuser:apipass@llm.internal/v1",
}
env := os.Environ()
for k, v := range want {
env = append(env, k+"="+v)
}
env = append(env, sealHelperEnv+"=1")
cmd := exec.Command(os.Args[0], "-test.run=^TestSealedEnvironmentHandoff$")
cmd.Env = env
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("helper failed: %v\n%s", err, out)
}
var proc, child []byte
got := map[string]string{}
seen := map[string]bool{}
done := false
for _, line := range strings.Split(string(out), "\n") {
if !strings.HasPrefix(line, "SEAL ") {
continue
}
f := strings.Split(line, " ")
switch f[1] {
case "error":
t.Fatalf("helper: %s", line)
case "proc":
proc = mustHex(t, f[2])
case "child":
child = mustHex(t, f[2])
case "value":
// SEAL value NAME <hex> <present>; the hex part is empty for an empty value.
name, present := f[2], f[len(f)-1]
hexPart := ""
if len(f) == 5 {
hexPart = f[3]
}
if present != "true" {
t.Fatalf("%s is not set after sealing", name)
}
got[name] = string(mustHex(t, hexPart))
seen[name] = true
case "done":
done = true
}
}
if !done {
t.Fatalf("helper did not finish:\n%s", out)
}
for name := range want {
if !seen[name] {
t.Errorf("%s missing from the helper report", name)
}
if got[name] != want[name] {
t.Errorf("%s restored as %q, want %q", name, got[name], want[name])
}
}
for name := range want {
if bytes.Contains(proc, []byte(name+"=")) {
t.Errorf("kernel environment block still holds %s", name)
}
if bytes.Contains(child, []byte(name+"=")) {
t.Errorf("child process environment holds %s", name)
}
}
}
func mustHex(t *testing.T, s string) []byte {
t.Helper()
b, err := hex.DecodeString(s)
if err != nil {
t.Fatalf("bad hex %q: %v", s, err)
}
return b
}