server: start degraded where agent isolation cannot be built

The isolation self-test stopped every start it failed, so a non-root
server (the systemd unit), dev.sh, Windows and macOS no longer started
without -dev-insecure-no-isolation.

EnforceIsolation now decides from the platform and the privilege drop:

- self-test passed: isolated;
- Linux with the privilege drop on (root, the Docker image): a failure
  stops the start, unless -dev-insecure-no-isolation is set;
- elsewhere (not Linux, not root, -exec-uid=-1): the server starts
  degraded, without a flag.

A degraded start logs a banner with the self-test failure and what is
exposed, and GET /api/health reports the isolation status to a
signed-in caller; the web UI shows a red banner on every page while it
is degraded. The systemd deployment guide and dev.sh describe the mode.
This commit is contained in:
BaaRai
2026-11-22 09:50:09 +01:00
parent dd80fea140
commit f83659cd52
10 changed files with 196 additions and 46 deletions
+64 -24
View File
@@ -12,14 +12,42 @@ import (
// starts, and checks from inside it that it runs as the executor, cannot read the
// server's secrets or its process, sees only the allowlisted environment and cannot
// open a database session without the server's password; it also checks that the
// file tools cannot read the secrets. EnforceIsolation then decides the start: a
// failed or impossible self-test stops it, unless the development escape
// -dev-insecure-no-isolation is set, which is off by default and logs a warning
// banner that says what is exposed.
// file tools cannot read the secrets. EnforceIsolation then decides the start:
//
// - The self-test passes: the server starts isolated.
// - Linux with the privilege drop on (the server is root, as in the Docker image):
// isolation is expected there and the self-test shows it is broken, so the start
// stops. The development escape -dev-insecure-no-isolation, off by default, lets
// it go on degraded.
// - Anywhere else (not Linux, the server is not root, or -exec-uid=-1): isolation
// cannot be built on that host, so the server starts degraded without any flag.
// The systemd unit (non-root), dev.sh, Windows and macOS start as before.
//
// A degraded start logs a warning banner that says what is exposed, and reports the
// state in GET /api/health to a signed-in caller, which the web UI shows as a
// banner on every page.
// DevNoIsolationFlag is the command-line flag of the development escape.
const DevNoIsolationFlag = "dev-insecure-no-isolation"
// The isolation modes the server reports.
const (
IsolationIsolated = "isolated"
IsolationDegraded = "degraded"
)
// IsolationStatus is the agent isolation the server runs with.
type IsolationStatus struct {
Mode string `json:"mode"`
// Reason is the self-test failure of a degraded start.
Reason string `json:"reason,omitempty"`
}
// WithIsolationStatus records the result of EnforceIsolation for GET /api/health.
func WithIsolationStatus(st IsolationStatus) Option {
return func(s *Server) { s.isolation = st }
}
// IsolationTargets are the server's paths and database the self-test checks the
// executor cannot reach.
type IsolationTargets struct {
@@ -27,46 +55,58 @@ type IsolationTargets struct {
KeyDir string
ConfigPath string
SkillDir string
// DSN is the server's database DSN; only its host, port, user and database
// DSN is the server's database DSN; only its hosts, ports, user and database
// name reach the probe, never its password.
DSN string
}
// EnforceIsolation decides the start from the self-test result. A nil result
// passes. A failure stops the start with an error, unless devEscape is set: the
// warning banner is then logged and the start goes on.
func EnforceIsolation(selfTest error, devEscape bool) error {
// EnforceIsolation decides the start from the self-test result, the platform goos
// and the privilege drop p (see the rules above). It returns the status to report,
// or an error when the start must stop.
func EnforceIsolation(goos string, p ExecPrivilege, selfTest error, devEscape bool) (IsolationStatus, error) {
if selfTest == nil {
log.Printf("[isolation] self-test passed: agent processes and file tools run as the executor and cannot reach the server's secrets")
if devEscape {
log.Printf("[isolation] -%s is set and not needed: the self-test passed", DevNoIsolationFlag)
}
return nil
return IsolationStatus{Mode: IsolationIsolated}, nil
}
if !devEscape {
return fmt.Errorf("agent isolation self-test failed: %v. The server does not start without agent isolation: fix the cause above (run the server as root with -exec-uid, the Docker default), or pass -%s for development only", selfTest, DevNoIsolationFlag)
if isolationRequired(goos, p) && !devEscape {
return IsolationStatus{}, fmt.Errorf("agent isolation self-test failed: %v. A Linux server running as root with the privilege drop does not start without agent isolation: fix the cause above, or pass -%s for development only", selfTest, DevNoIsolationFlag)
}
logNoIsolationBanner(selfTest)
return nil
logDegradedBanner(selfTest, isolationRequired(goos, p))
return IsolationStatus{Mode: IsolationDegraded, Reason: selfTest.Error()}, nil
}
// noIsolationBanner is the warning the development escape logs at every start.
func noIsolationBanner(cause error) []string {
// isolationRequired reports whether the host can isolate agents, so that a failed
// self-test means broken isolation: Linux, with the privilege drop on (root).
func isolationRequired(goos string, p ExecPrivilege) bool {
return goos == "linux" && p.Enabled
}
// degradedBanner is the warning a degraded start logs. escaped is set when the start
// went on only through the development escape.
func degradedBanner(cause error, escaped bool) []string {
bar := strings.Repeat("!", 78)
return []string{
bar,
"!! AGENT ISOLATION IS OFF: -" + DevNoIsolationFlag + " is set. DEVELOPMENT ONLY.",
"!! Self-test failure: " + cause.Error(),
why := "!! This host cannot isolate agents: that needs Linux and a root server with the" +
"\n!! privilege drop (-exec-uid), as in the Docker image."
if escaped {
why = "!! -" + DevNoIsolationFlag + " is set. DEVELOPMENT ONLY."
}
lines := []string{bar, "!! AGENT ISOLATION IS DEGRADED."}
lines = append(lines, strings.Split(why, "\n")...)
return append(lines,
"!! Self-test failure: "+cause.Error(),
"!! Agent shells, custom tools, MCP stdio servers and the file tools may read the",
"!! server's keys (JWT signing key, secret key), its config, its database",
"!! credentials and its process, and may change skills and tools on disk.",
"!! Do not use this flag on a server that holds real targets or credentials.",
"!! Do not use this mode on a server that holds real targets or credentials.",
bar,
}
)
}
func logNoIsolationBanner(cause error) {
for _, line := range noIsolationBanner(cause) {
func logDegradedBanner(cause error, escaped bool) {
for _, line := range degradedBanner(cause, escaped) {
log.Print("[isolation] " + line)
}
}
+77 -16
View File
@@ -2,8 +2,10 @@ package server
import (
"bytes"
"encoding/json"
"errors"
"log"
"net/http/httptest"
"strings"
"testing"
)
@@ -20,35 +22,94 @@ func captureLog(t *testing.T, fn func()) string {
return buf.String()
}
// A failed self-test stops the start; the escape, off by default, lets it go on
// with the warning banner; a passing self-test starts either way.
// The start decision, branch by branch.
func TestEnforceIsolation(t *testing.T) {
cause := errors.New("Bash runs as uid 0, not the executor uid 10001")
root := ExecPrivilege{Enabled: true, UID: 10001, GID: 10001} // drop on: the server is root
off := ExecPrivilege{} // not root, or -exec-uid=-1
const banner = "AGENT ISOLATION IS DEGRADED"
var err error
out := captureLog(t, func() { err = EnforceIsolation(cause, false) })
var (
st IsolationStatus
err error
)
// Hard failure: Linux, root, self-test failed, no escape. No banner: no start.
out := captureLog(t, func() { st, err = EnforceIsolation("linux", root, cause, false) })
if err == nil || !strings.Contains(err.Error(), cause.Error()) || !strings.Contains(err.Error(), "-"+DevNoIsolationFlag) {
t.Fatalf("without the escape: err = %v, want the cause and the flag named", err)
t.Fatalf("linux root: err = %v, want the cause and the flag named", err)
}
if strings.Contains(out, "AGENT ISOLATION IS OFF") {
t.Fatalf("the banner belongs to the escape only:\n%s", out)
if st.Mode != "" || strings.Contains(out, banner) {
t.Fatalf("linux root: status %+v, banner in\n%s", st, out)
}
out = captureLog(t, func() { err = EnforceIsolation(cause, true) })
if err != nil {
t.Fatalf("with the escape the start goes on: %v", err)
// Linux, root, with the development escape: degraded, banner names the flag.
out = captureLog(t, func() { st, err = EnforceIsolation("linux", root, cause, true) })
if err != nil || st.Mode != IsolationDegraded || st.Reason != cause.Error() {
t.Fatalf("linux root with the escape: status %+v err %v, want degraded", st, err)
}
for _, want := range []string{"AGENT ISOLATION IS OFF", "-" + DevNoIsolationFlag, "DEVELOPMENT ONLY", cause.Error(), "keys", "database"} {
for _, want := range []string{banner, "-" + DevNoIsolationFlag, "DEVELOPMENT ONLY", cause.Error(), "keys", "database"} {
if !strings.Contains(out, want) {
t.Errorf("banner lacks %q:\n%s", want, out)
t.Errorf("escape banner lacks %q:\n%s", want, out)
}
}
for _, escape := range []bool{false, true} {
out = captureLog(t, func() { err = EnforceIsolation(nil, escape) })
if err != nil || !strings.Contains(out, "self-test passed") || strings.Contains(out, "AGENT ISOLATION IS OFF") {
t.Fatalf("passing self-test (escape %v): err=%v\n%s", escape, err, out)
// Degraded without any flag: Linux not root (the systemd unit, dev.sh), Windows,
// macOS (root or not). The banner says why and what is exposed.
for _, c := range []struct {
goos string
p ExecPrivilege
}{{"linux", off}, {"windows", off}, {"darwin", off}, {"darwin", root}} {
out = captureLog(t, func() { st, err = EnforceIsolation(c.goos, c.p, cause, false) })
if err != nil || st.Mode != IsolationDegraded || st.Reason != cause.Error() {
t.Fatalf("%s drop=%v: status %+v err %v, want a degraded start", c.goos, c.p.Enabled, st, err)
}
for _, want := range []string{banner, "cannot isolate agents", cause.Error(), "keys", "database"} {
if !strings.Contains(out, want) {
t.Errorf("%s drop=%v: banner lacks %q:\n%s", c.goos, c.p.Enabled, want, out)
}
}
if strings.Contains(out, "DEVELOPMENT ONLY") {
t.Errorf("%s drop=%v: the degraded banner is not the escape's:\n%s", c.goos, c.p.Enabled, out)
}
}
// A passing self-test starts isolated, escape or not, without a banner.
for _, escape := range []bool{false, true} {
out = captureLog(t, func() { st, err = EnforceIsolation("linux", root, nil, escape) })
if err != nil || st.Mode != IsolationIsolated || !strings.Contains(out, "self-test passed") || strings.Contains(out, banner) {
t.Fatalf("passing self-test (escape %v): status %+v err=%v\n%s", escape, st, err, out)
}
}
}
// GET /api/health reports the isolation status to a signed-in caller only: the web
// UI shows the degraded banner from it.
func TestHealthReportsIsolation(t *testing.T) {
key := []byte("0123456789abcdef0123456789abcdef")
s := &Server{jwtKey: key}
WithIsolationStatus(IsolationStatus{Mode: IsolationDegraded, Reason: "the server is not root"})(s)
w := httptest.NewRecorder()
s.health(w, httptest.NewRequest("GET", "/api/health", nil))
if got := strings.TrimSpace(w.Body.String()); got != `{"ok":true}` {
t.Fatalf("no token: %s, want no isolation status", got)
}
tok, err := signJWT(key)
if err != nil {
t.Fatal(err)
}
r := httptest.NewRequest("GET", "/api/health", nil)
r.Header.Set("Authorization", "Bearer "+tok)
w = httptest.NewRecorder()
s.health(w, r)
var body struct {
Isolation *IsolationStatus `json:"isolation"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil || body.Isolation == nil ||
body.Isolation.Mode != IsolationDegraded || body.Isolation.Reason != "the server is not root" {
t.Fatalf("signed in: %s, want the degraded status", w.Body.String())
}
}
+6 -1
View File
@@ -54,7 +54,8 @@ type Server struct {
scrubSecrets bool // drop ARTEX_PG_DSN and the API keys from the environment once read (binary only)
execPriv ExecPrivilege // identity agent processes run under; zero keeps the server's own
execPriv ExecPrivilege // identity agent processes run under; zero keeps the server's own
isolation IsolationStatus // result of the startup isolation self-test (isolation.go); zero when not run
setupMu sync.Mutex
setupToken string // one-time token POST /api/auth/init requires; empty when none is active
@@ -1101,6 +1102,10 @@ func (s *Server) health(w http.ResponseWriter, r *http.Request) {
return
}
out := map[string]any{"ok": true, "service": "artex", "version": BuildVersion}
// The web UI shows a banner on every page for a degraded isolation (isolation.go).
if s.isolation.Mode != "" {
out["isolation"] = s.isolation
}
// Opt-in only (ARTEX_UPDATE_CHECK); off, this returns "" without any request.
if latest := updates.newerVersion(r.Context(), time.Now(), BuildVersion); latest != "" {
out["latest_version"] = latest