mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 17:30:56 +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.
100 lines
3.2 KiB
Go
100 lines
3.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/artex/agent"
|
|
)
|
|
|
|
// A child process spawned after startup must not see the startup secrets, while
|
|
// other variables still pass through.
|
|
func TestScrubSecretEnvHidesSecretsFromChildren(t *testing.T) {
|
|
sh, err := exec.LookPath("sh")
|
|
if err != nil {
|
|
t.Skip("no sh in PATH")
|
|
}
|
|
for _, k := range secretEnv {
|
|
t.Setenv(k, "leak-"+k)
|
|
}
|
|
t.Setenv("ARTEX_SCRUB_KEEP", "kept")
|
|
|
|
ScrubSecretEnv()
|
|
|
|
out, err := exec.Command(sh, "-c", "env").Output()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
env := "\n" + string(out)
|
|
for _, k := range secretEnv {
|
|
if strings.Contains(env, "\n"+k+"=") {
|
|
t.Errorf("%s still reaches child processes", k)
|
|
}
|
|
}
|
|
if !strings.Contains(env, "\nARTEX_SCRUB_KEEP=kept\n") {
|
|
t.Errorf("non-secret variable did not reach the child process")
|
|
}
|
|
}
|
|
|
|
// New must apply the options it receives. An earlier version accepted
|
|
// ...Option and never called them, so the binary kept the secrets in the
|
|
// environment while the unit test above still passed.
|
|
func TestNewAppliesSecretScrubOption(t *testing.T) {
|
|
// t.Setenv restores both after the test: the scrub unsets them for the rest
|
|
// of this process, and later tests still need the DSN.
|
|
if dsn := os.Getenv("ARTEX_PG_DSN"); dsn != "" {
|
|
t.Setenv("ARTEX_PG_DSN", dsn)
|
|
}
|
|
t.Setenv("ARTEX_SECRET_KEY", os.Getenv("ARTEX_SECRET_KEY"))
|
|
t.Setenv("ANTHROPIC_API_KEY", "scrub-probe")
|
|
t.Setenv("OPENAI_API_KEY", "scrub-probe")
|
|
|
|
m, err := NewManager(t.TempDir(), "")
|
|
if err != nil {
|
|
t.Skipf("no manager: %v", err)
|
|
}
|
|
t.Cleanup(func() { m.Close() })
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
_ = New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir(), WithSecretScrub())
|
|
|
|
if os.Getenv("ANTHROPIC_API_KEY") != "" || os.Getenv("OPENAI_API_KEY") != "" {
|
|
t.Fatal("New did not apply WithSecretScrub: the secrets are still in the environment")
|
|
}
|
|
}
|
|
|
|
// The LLM proxy and endpoint URLs can carry user:pass@ credentials. They are
|
|
// scrubbed and sealed like the keys, and a child spawned after the scrub sees
|
|
// neither, while agent.FromEnv still read them before it.
|
|
func TestSecretEnvCoversCredentialURLs(t *testing.T) {
|
|
sh, err := exec.LookPath("sh")
|
|
if err != nil {
|
|
t.Skip("no sh in PATH")
|
|
}
|
|
for _, k := range []string{"ARTEX_LLM_PROXY", "ARTEX_LLM_BASE_URL"} {
|
|
if !isSecretName(k) {
|
|
t.Errorf("%s is not in secretEnv", k)
|
|
}
|
|
}
|
|
t.Setenv("ARTEX_LLM_PROVIDER", "openai")
|
|
t.Setenv("OPENAI_API_KEY", "sk-test")
|
|
t.Setenv("ARTEX_LLM_PROXY", "http://proxyuser:proxypass@proxy.internal:3128")
|
|
t.Setenv("ARTEX_LLM_BASE_URL", "https://apiuser:apipass@llm.internal/v1")
|
|
cfg, ok := agent.FromEnv()
|
|
if !ok || cfg.Proxy != "http://proxyuser:proxypass@proxy.internal:3128" || cfg.BaseURL != "https://apiuser:apipass@llm.internal/v1" {
|
|
t.Fatalf("FromEnv must read the URLs before the scrub: ok=%v proxy=%q base=%q", ok, cfg.Proxy, cfg.BaseURL)
|
|
}
|
|
ScrubSecretEnv()
|
|
out, err := exec.Command(sh, "-c", "env").Output()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(out), "proxypass") || strings.Contains(string(out), "apipass") {
|
|
t.Fatalf("a child process still sees the proxy or endpoint credentials:\n%s", out)
|
|
}
|
|
}
|