Files
ARTEX/server/execpriv_test.go
T
BaaRai 04a7ab3c39 security(server): keep server secrets from agent processes
GHSA-prm8-vw2v-wjwh: the agent command executor ran as root in the server's
container and PID namespace, so it could read /app/keys and recover the
server secrets from /proc/<pid>/environ.

Deliverable 1, executor identity. Agent processes (Bash, the interactive
shell, custom tools, stdio MCP servers) run as the uid/gid set by
-exec-uid and -exec-gid (default 10001). Custom tools use
SysProcAttr.Credential. Norma starts Bash and the shell itself, so a PATH
shim (the server binary named bash) drops the identity before exec; MCP
stdio commands run through the same binary in drop mode. The task and chat
workspace trees are shared with the executor group (setgid, group rw,
umask 002). The key files and the config file become root-only, and
/app/keys is 0700 in the image. An explicit -exec-uid that cannot be applied
stops the start; the default logs once that same-uid isolation is
unavailable when the server is not root.

Deliverable 2, sealed environment. On Linux, SealProcessEnv re-executes the
server once with the secret variables removed from its environment block,
passing the values through an inherited pipe. The new image restores them
into the Go environment, so readers see the same configuration. Off Linux it
logs that the environment is not sealed.

Deliverable 3, approval gate. Custom tools, MCP servers and skill files
(create_/update_ of custom tool and MCP, create_skill, update_skill_file)
meet built-in ask rules seeded once (intercept_persistence_rules_v1). A user
allow rule cannot skip them; a user deny still wins. The rules are data the
agent cannot edit.

Tests: resolution and refusal of root identities, drop command wrapping,
sealed-environment encoding, and an end-to-end handoff test on Linux that
checks the kernel environment block and a child process hold no secret
names and that the values survive (empty and multi-line included). Approval
matching is tested in intercept.
2026-11-20 05:35:53 +01:00

124 lines
3.7 KiB
Go

package server
import (
"reflect"
"strings"
"testing"
)
func TestResolveExecPrivilegeNegativeUIDDisables(t *testing.T) {
p, err := ResolveExecPrivilege(-1, DefaultExecGID, true)
if err != nil {
t.Fatalf("explicit disable must not fail: %v", err)
}
if p.Enabled {
t.Fatal("negative uid must disable the drop")
}
}
func TestResolveExecPrivilegeRefusesRootIdentity(t *testing.T) {
for _, c := range [][2]int{{0, DefaultExecGID}, {DefaultExecUID, 0}} {
if _, err := ResolveExecPrivilege(c[0], c[1], true); err == nil {
t.Fatalf("uid %d gid %d: root identity must be refused", c[0], c[1])
}
}
}
// Without root the drop cannot be applied: an explicit request is a startup error
// (fail closed), the default degrades to a disabled privilege.
func TestResolveExecPrivilegeWithoutRoot(t *testing.T) {
if serverIsRoot() && execPrivilegeSupported() {
t.Skip("needs a process that cannot switch identity")
}
p, err := ResolveExecPrivilege(DefaultExecUID, DefaultExecGID, false)
if err != nil || p.Enabled {
t.Fatalf("default request must degrade quietly: enabled=%v err=%v", p.Enabled, err)
}
if _, err := ResolveExecPrivilege(DefaultExecUID, DefaultExecGID, true); err == nil {
t.Fatal("explicit request must fail closed")
}
}
func TestResolveExecPrivilegeAsRoot(t *testing.T) {
if !serverIsRoot() || !execPrivilegeSupported() {
t.Skip("needs a root process on a platform with credentials")
}
p, err := ResolveExecPrivilege(DefaultExecUID, DefaultExecGID, true)
if err != nil {
t.Fatal(err)
}
if !p.Enabled || p.UID != DefaultExecUID || p.GID != DefaultExecGID {
t.Fatalf("got %+v", p)
}
}
func TestExecDropCommandDisabledKeepsCommand(t *testing.T) {
cmd, args, err := execDropCommand(ExecPrivilege{}, "npx", []string{"-y", "pkg"})
if err != nil {
t.Fatal(err)
}
if cmd != "npx" || !reflect.DeepEqual(args, []string{"-y", "pkg"}) {
t.Fatalf("disabled drop must not rewrite the command: %q %v", cmd, args)
}
}
func TestExecDropCommandWrapsForShim(t *testing.T) {
p := ExecPrivilege{Enabled: true, UID: 10001, GID: 10002}
_, args, err := execDropCommand(p, "npx", []string{"-y", "pkg"})
if err != nil {
t.Fatal(err)
}
want := []string{execDropArg, "10001", "10002", "--", "npx", "-y", "pkg"}
if !reflect.DeepEqual(args, want) {
t.Fatalf("args = %v, want %v", args, want)
}
}
func TestWithExecPrivilegeSetsServerField(t *testing.T) {
s := &Server{}
p := ExecPrivilege{Enabled: true, UID: 10001, GID: 10001}
WithExecPrivilege(p)(s)
if s.execPriv != p {
t.Fatalf("execPriv = %+v, want %+v", s.execPriv, p)
}
}
func TestSealedEnvRoundTripKeepsEmptyAndMultiline(t *testing.T) {
in := []string{
"ARTEX_PG_DSN=postgres://u:p@h/db?sslmode=disable\nsecond line\ttab",
"ARTEX_SECRET_KEY=",
"ANTHROPIC_API_KEY=sk-ant-test",
"OPENAI_API_KEY==leading-equals",
}
out, err := decodeSealedEnv(encodeSealedEnv(in))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(out, in) {
t.Fatalf("round trip changed the values:\n got %q\nwant %q", out, in)
}
}
func TestSealedEnvRejectsForeignNames(t *testing.T) {
if _, err := decodeSealedEnv([]byte("PATH=/bin\x00")); err == nil {
t.Fatal("a name outside secretEnv must be rejected")
}
if _, err := decodeSealedEnv([]byte("ANTHROPIC_API_KEY=x")); err == nil {
t.Fatal("an unterminated entry must be rejected")
}
}
func TestWithoutSecretEnvDropsOnlySecrets(t *testing.T) {
env := []string{"PATH=/bin", "ARTEX_PG_DSN=x", "HOME=/root", "OPENAI_API_KEY=", "ARTEX_SECRET_KEY=y"}
got := withoutSecretEnv(env)
want := []string{"PATH=/bin", "HOME=/root"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v, want %v", got, want)
}
for _, kv := range got {
if isSecretName(strings.SplitN(kv, "=", 2)[0]) {
t.Fatalf("secret left in %q", kv)
}
}
}