From 57d3bf67e72098405b800132be4ca37d9d53dfbc Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:17:40 +0100 Subject: [PATCH 1/6] fix(intercept): fail closed when the rule store cannot be read A failed rule load left the enabled-tools set nil, so IsToolEnabled answered false for every tool and the guard returned before any rule ran; Match, MatchBuiltin and MatchPersistence answered "no match" on the same error. A database outage therefore let every tool call run unchecked. Now, when the rules or the enabled-tools setting cannot be read, IsToolEnabled answers true so the call reaches the rule path, and the three matchers return a deny. The error is logged once per outage. A failed read of the enabled-tools setting counts as a failed load instead of silently selecting the defaults. A deny or ask rule whose pattern does not compile is no longer skipped: it matches every call as a deny until it is fixed or disabled. A broken allow rule still allows nothing. --- guard/guard_test.go | 40 +++++++++++ intercept/intercept.go | 58 +++++++++++---- intercept/persistence.go | 2 +- intercept/store_error_test.go | 132 ++++++++++++++++++++++++++++++++++ 4 files changed, 219 insertions(+), 13 deletions(-) create mode 100644 intercept/store_error_test.go diff --git a/guard/guard_test.go b/guard/guard_test.go index e49dced4b..966e18e83 100644 --- a/guard/guard_test.go +++ b/guard/guard_test.go @@ -2,6 +2,7 @@ package guard import ( "context" + "database/sql" "encoding/json" "strings" "testing" @@ -240,4 +241,43 @@ func TestShortBlockReasonUnchanged(t *testing.T) { } } +// A rule store that cannot be read blocks the call end to end, in the full guard +// (workers, chat) and in the target-only guard (planner, main agent), instead of +// letting it run unchecked. The handle is closed before its first query, so no +// server is contacted. +func TestUnreadableRuleStoreBlocksCalls(t *testing.T) { + closed := func() *intercept.Interceptor { + sqlDB, err := sql.Open("pgx", "postgres://u:p@127.0.0.1:1/unreachable") + if err != nil { + t.Fatal(err) + } + if err := sqlDB.Close(); err != nil { + t.Fatal(err) + } + return intercept.New(&db.DB{DB: sqlDB}) + } + for name, g := range map[string]*Guard{ + "full guard": NewWithInterceptor(closed()), + "target-only guard": NewTargetOnly(closed()), + } { + for _, call := range []struct{ tool, input string }{ + {"Bash", `{"command":"ls -la"}`}, + {"Monitor", `{"command":"tail -f /dev/null"}`}, + {"create_mcp", `{"name":"x","command":"sh"}`}, + } { + block, msg := pre(g, context.Background(), call.tool, call.input) + if !block || !strings.HasPrefix(msg, "[ARTEX platform control, not target defense]") || + !strings.Contains(msg, "intercept rules could not be loaded") { + t.Errorf("%s: %s block=%v msg=%q, want a block that names the unreadable rule store", name, call.tool, block, msg) + } + } + } + // The blocked call is recorded as a block, never as an allow. + g := NewWithInterceptor(closed()) + pre(g, context.Background(), "Bash", `{"command":"ls -la"}`) + if a := g.Audit(); len(a) != 1 || a[0].Action != "block" { + t.Errorf("audit = %+v, want one block entry", a) + } +} + var _ = hook.PreToolUse diff --git a/intercept/intercept.go b/intercept/intercept.go index c9078e9ba..a4f20fe2e 100644 --- a/intercept/intercept.go +++ b/intercept/intercept.go @@ -16,6 +16,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "net" "regexp" "strconv" @@ -111,15 +112,17 @@ func TargetScopeFromContext(ctx context.Context) (s TargetScope, ok bool) { } // compiledRule is an InterceptRule with the regex pre-compiled (nil for string rules) -// and the argv tokens pre-parsed (nil for non-argv rules). +// and the argv tokens pre-parsed (nil for non-argv rules). bad marks a deny or +// ask rule whose pattern does not compile: it matches every call, as a deny. type compiledRule struct { db.InterceptRule re *regexp.Regexp argv []string + bad bool } // compileRule prepares a rule for matching. ok is false for a rule whose -// pattern does not compile; such a rule is skipped rather than crashing. +// pattern does not compile; see loadLocked for what happens to it. func compileRule(r db.InterceptRule) (compiledRule, bool) { cr := compiledRule{InterceptRule: r} switch r.MatchType { @@ -205,6 +208,7 @@ type Interceptor struct { mu sync.RWMutex cached []compiledRule // sorted by priority DESC; nil means not loaded yet enabledTools map[string]bool // nil means not loaded yet + loadFailed bool // the last load failed; its error has been logged pending *pendingManager reviewer Reviewer // nil = freeform LLM fallback judge not wired completer Completer // nil = closed-question judge not wired @@ -289,11 +293,24 @@ func (i *Interceptor) Invalidate() { i.mu.Unlock() } +// loadLocked reads the rules and the enabled-tools setting. It installs both or, +// when either read fails, neither: the callers then fail closed (IsToolEnabled +// answers true, the matchers deny). The error is logged once per outage. func (i *Interceptor) loadLocked() error { rules, err := i.db.ListInterceptRules() + var val string + var ok bool + if err == nil { + val, ok, err = i.db.GetSetting("intercept_enabled_tools") + } if err != nil { + if !i.loadFailed { + i.loadFailed = true + log.Printf("[intercept] the rule store cannot be read; every tool call is denied until it can: %v", err) + } return err } + i.loadFailed = false var out []compiledRule for _, r := range rules { if !r.Enabled { @@ -301,18 +318,28 @@ func (i *Interceptor) loadLocked() error { } cr, ok := compileRule(r) if !ok { - continue // skip rules with a bad pattern rather than crashing + if r.Action != "deny" && r.Action != "ask" { + continue // an allow rule with a bad pattern allows nothing + } + // A deny or ask rule that cannot be evaluated must not fail open. + cr.bad, cr.Action = true, "deny" + cr.Message = "intercept rule [" + r.Name + "] has a pattern that does not compile; calls are blocked until the rule is fixed or disabled" } out = append(out, cr) } i.cached = out - // Load enabled-tools set from settings, falling back to hard-coded defaults. - val, ok, _ := i.db.GetSetting("intercept_enabled_tools") + // Enabled-tools set from settings, falling back to hard-coded defaults. i.enabledTools = enabledToolsFromSetting(val, ok) return nil } +// storeUnreadableDecision is what the matchers answer when the rules cannot be +// loaded: a deny, so that a call is never let through unchecked. +func storeUnreadableDecision() Decision { + return Decision{Action: "deny", Message: "the intercept rules could not be loaded, so the call cannot be checked; it is blocked until the rule store is readable again"} +} + // enabledToolsFromSetting maps the stored intercept_enabled_tools value to the // gate's set. A malformed value must not silently disable the gate, so it falls // back to the same defaults as an unset setting. @@ -357,7 +384,8 @@ func (i *Interceptor) rules() ([]compiledRule, error) { // IsToolEnabled returns true if the named tool is in the intercept-enabled set // (i.e. it should enter the rule-matching path). Uses the same double-check lock -// pattern as rules(). +// pattern as rules(). When the set cannot be loaded it answers true for every +// tool, so the call reaches the rule path, where Match denies it. func (i *Interceptor) IsToolEnabled(name string) bool { i.mu.RLock() if i.enabledTools != nil { @@ -370,7 +398,9 @@ func (i *Interceptor) IsToolEnabled(name string) bool { i.mu.Lock() defer i.mu.Unlock() if i.enabledTools == nil { - _ = i.loadLocked() + if err := i.loadLocked(); err != nil { + return true + } } return i.enabledTools[name] } @@ -676,11 +706,12 @@ func judgeActionLabel(action string) string { // Match evaluates the rule list (priority DESC) against a tool call. // Returns (Decision, true) for the first matching enabled rule, or -// (Decision{}, false) if no rule matches. +// (Decision{}, false) if no rule matches. When the rules cannot be loaded it +// returns a deny. func (i *Interceptor) Match(toolName string, input []byte) (Decision, bool) { rules, err := i.rules() - if err != nil || len(rules) == 0 { - return Decision{}, false + if err != nil { + return storeUnreadableDecision(), true } for _, r := range rules { if ruleMatches(r, toolName, input) { @@ -712,7 +743,7 @@ const builtinRulePrefix = "[built-in]" // enter the rule system. It returns the first match in priority order. User rules, // allow and ask rules, and the judge are not consulted. The planner and the main // agent use it (#124, F11). An interceptor without a database and without loaded -// rules matches nothing. +// rules matches nothing. When the rules cannot be loaded it returns a deny. func (i *Interceptor) MatchBuiltin(toolName string, input []byte) (Decision, bool) { i.mu.RLock() loaded := i.cached != nil @@ -725,7 +756,7 @@ func (i *Interceptor) MatchBuiltin(toolName string, input []byte) (Decision, boo } rules, err := i.rules() if err != nil { - return Decision{}, false + return storeUnreadableDecision(), true } for _, r := range rules { if r.Action != "deny" || !strings.HasPrefix(r.Name, builtinRulePrefix) || !ruleMatches(r, toolName, input) { @@ -772,6 +803,9 @@ func (i *Interceptor) SetRulesForTest(rules []db.InterceptRule) { } func ruleMatches(r compiledRule, toolName string, input []byte) bool { + if r.bad { + return true + } if r.MatchType == "argv" { // Bash only, on the parsed command; match_target is always tool_input. // Allow rules stay strictly limited to a single simple command with diff --git a/intercept/persistence.go b/intercept/persistence.go index 2d63e18e4..ed12f473b 100644 --- a/intercept/persistence.go +++ b/intercept/persistence.go @@ -38,7 +38,7 @@ func (i *Interceptor) MatchPersistence(toolName string, input []byte) (Decision, } rules, err := i.rules() if err != nil { - return Decision{}, false + return storeUnreadableDecision(), true } var ask *compiledRule for idx := range rules { diff --git a/intercept/store_error_test.go b/intercept/store_error_test.go new file mode 100644 index 000000000..f26decc58 --- /dev/null +++ b/intercept/store_error_test.go @@ -0,0 +1,132 @@ +package intercept + +import ( + "bytes" + "database/sql" + "log" + "strings" + "testing" + + "github.com/Autumn-27/artex/db" +) + +// closedStore returns a store whose every read fails. No server is contacted: +// the handle is closed before its first query. +func closedStore(t *testing.T) *db.DB { + t.Helper() + sqlDB, err := sql.Open("pgx", "postgres://u:p@127.0.0.1:1/unreachable") + if err != nil { + t.Fatal(err) + } + if err := sqlDB.Close(); err != nil { + t.Fatal(err) + } + return &db.DB{DB: sqlDB} +} + +// A rule store that cannot be read must not open the gate: every tool enters the +// rule path, and every matcher denies. The error is logged once, not per call. +func TestStoreErrorFailsClosed(t *testing.T) { + var logged bytes.Buffer + prev := log.Writer() + log.SetOutput(&logged) + t.Cleanup(func() { log.SetOutput(prev) }) + + ic := New(closedStore(t)) + input := []byte(`{"command":"ls -la"}`) + for _, tool := range []string{"Bash", "ExecuteExtraTool", "Monitor", "Read", "my_custom_tool"} { + if !ic.IsToolEnabled(tool) { + t.Errorf("IsToolEnabled(%q) = false with an unreadable store, want true", tool) + } + } + for name, match := range map[string]func(string, []byte) (Decision, bool){ + "Match": ic.Match, + "MatchBuiltin": ic.MatchBuiltin, + "MatchPersistence": ic.MatchPersistence, + } { + tool := "Bash" + if name == "MatchPersistence" { + tool = "create_mcp" + } + dec, ok := match(tool, input) + if !ok || dec.Action != "deny" || !strings.Contains(dec.Message, "could not be loaded") { + t.Errorf("%s with an unreadable store = %+v, %v; want a deny that names the cause", name, dec, ok) + } + } + if n := strings.Count(logged.String(), "rule store cannot be read"); n != 1 { + t.Errorf("the store error was logged %d times, want once:\n%s", n, logged.String()) + } + if !strings.Contains(logged.String(), "sql: database is closed") { + t.Errorf("the log line does not carry the error:\n%s", logged.String()) + } +} + +// A malformed intercept_enabled_tools value read from the store keeps the +// defaults: ExecuteExtraTool and Monitor stay gated, read-only tools stay out. +func TestMalformedEnabledToolsSettingKeepsDefaults(t *testing.T) { + d := targetTestDB(t) + const key = "intercept_enabled_tools" + prev, had, err := d.GetSetting(key) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if had { + _ = d.SetSetting(key, prev) + return + } + _, _ = d.Exec(`DELETE FROM settings WHERE key=$1`, key) + }) + for _, val := range []string{"{not json", `"Bash"`, `{"Bash":true}`} { + if err := d.SetSetting(key, val); err != nil { + t.Fatal(err) + } + ic := New(d) + for _, tool := range defaultEnabledTools { + if !ic.IsToolEnabled(tool) { + t.Errorf("setting %q: default tool %q is not gated", val, tool) + } + } + if ic.IsToolEnabled("Read") { + t.Errorf("setting %q: Read is gated, want the defaults only", val) + } + } +} + +// A deny or ask rule whose pattern does not compile cannot be evaluated; it +// matches as a deny instead of being skipped. A broken allow rule allows nothing. +func TestUncompilableRuleFailsClosed(t *testing.T) { + d := targetTestDB(t) + create := func(name, matchType, pattern, action string, priority int) db.InterceptRule { + t.Helper() + r, err := d.CreateInterceptRule(name, "tool_input", matchType, pattern, action, "", priority, true, false, 0, "deny") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = d.DeleteInterceptRule(r.ID) }) + return r + } + input := []byte(`{"command":"ls -la"}`) + + allow := create("broken allow", "regex", "(", "allow", 1000002) + if dec, ok := New(d).Match("Bash", input); ok && dec.RuleID == allow.ID { + t.Fatalf("a broken allow rule matched: %+v", dec) + } + for n, tc := range []struct{ matchType, pattern, action string }{ + {"regex", "(", "ask"}, + {"regex", "[z-a]", "deny"}, + {"argv", `"unterminated`, "deny"}, + } { + r := create("broken "+tc.action, tc.matchType, tc.pattern, tc.action, 1000001-n) + ic := New(d) + dec, ok := ic.Match("Bash", input) + if !ok || dec.Action != "deny" || dec.RuleID != r.ID || !strings.Contains(dec.Message, "does not compile") { + t.Errorf("%s %s rule %q: Match = %+v, %v; want a deny by rule %d", tc.matchType, tc.action, tc.pattern, dec, ok, r.ID) + } + // A user rule, broken or not, is not a built-in rule. + if dec, ok := ic.MatchBuiltin("Bash", input); ok { + t.Errorf("MatchBuiltin matched a broken user rule: %+v", dec) + } + _ = d.DeleteInterceptRule(r.ID) + } +} From ee35fb4b29d4ce534eb12e6134209fcb2148428f Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:22:54 +0100 Subject: [PATCH 2/6] fix(server): refuse cookie-authenticated state changes from a foreign origin requireAuth accepted the artex_token cookie on its own for every method and checked neither Origin nor Sec-Fetch-Site. The handlers decode a JSON body whatever its Content-Type, so a page on another origin of the same site (another port of the host, a sibling subdomain) could send a text/plain POST that the browser sends without a preflight and with the cookie: POST /api/mcp stored a server entry with a command. A POST, PUT, PATCH or DELETE whose session comes from the cookie must now be same-origin: Sec-Fetch-Site is same-origin, or Origin names the scheme, host and port the request was sent to. Anything else, including a request with neither header, is answered 403. Requests that carry a bearer token are not checked, nor are GET and HEAD. The /api/auth/* routes stay outside requireAuth, so setup and login are unchanged. The web console sends the bearer token on every state-changing request. --- server/auth.go | 55 +++++++++++ server/csrf_test.go | 223 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 278 insertions(+) create mode 100644 server/csrf_test.go diff --git a/server/auth.go b/server/auth.go index 2d5155052..768cfcc75 100644 --- a/server/auth.go +++ b/server/auth.go @@ -8,6 +8,7 @@ import ( "log" "math/big" "net/http" + "net/url" "os" "path/filepath" "strconv" @@ -282,8 +283,55 @@ func extractToken(r *http.Request) string { return r.URL.Query().Get("token") } +// cookieAuthenticated reports whether extractToken takes the session from the +// artex_token cookie. The browser attaches that cookie on its own, to requests +// a foreign page makes as well; a bearer or query token is put there by the caller. +func cookieAuthenticated(r *http.Request) bool { + if strings.HasPrefix(r.Header.Get("Authorization"), "Bearer ") { + return false + } + c, err := r.Cookie("artex_token") + return err == nil && c.Value != "" +} + +// sameOrigin reports whether the browser states that the request comes from a +// page of this server: Sec-Fetch-Site is same-origin, or Origin names the +// scheme, host and port the request was sent to. Scripts can set neither +// header. A request that carries neither is not same-origin. +func sameOrigin(r *http.Request) bool { + if r.Header.Get("Sec-Fetch-Site") == "same-origin" { + return true + } + o, err := url.Parse(r.Header.Get("Origin")) + if err != nil || o.Host == "" { + return false + } + scheme := "http" + if r.TLS != nil { + scheme = "https" + } + return o.Scheme == scheme && hostWithPort(o.Host, scheme) == hostWithPort(r.Host, scheme) +} + +// hostWithPort returns host in lower case with its port, the scheme's default +// when host names none. +func hostWithPort(host, scheme string) string { + u := url.URL{Host: host} + port := u.Port() + if port == "" { + port = "80" + if scheme == "https" { + port = "443" + } + } + return strings.ToLower(u.Hostname()) + ":" + port +} + // requireAuth wraps h with JWT validation. // /api/auth/*, /api/health and /api/ready are exempt. +// A state-changing request whose session is the cookie must be same-origin +// (see sameOrigin), so that a foreign page cannot act with the operator's +// session. Requests that carry a bearer token, and GET and HEAD, are not checked. func (s *Server) requireAuth(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { p := r.URL.Path @@ -300,6 +348,13 @@ func (s *Server) requireAuth(h http.Handler) http.Handler { writeErr(w, 401, "token is invalid or expired") return } + switch r.Method { + case http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete: + if cookieAuthenticated(r) && !sameOrigin(r) { + writeErr(w, 403, "cross-origin request refused: a cookie session can change state only from the console's own origin; send the token as Authorization: Bearer instead") + return + } + } h.ServeHTTP(w, r) }) } diff --git a/server/csrf_test.go b/server/csrf_test.go new file mode 100644 index 000000000..bc7f265f1 --- /dev/null +++ b/server/csrf_test.go @@ -0,0 +1,223 @@ +package server + +import ( + "context" + "crypto/tls" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/Autumn-27/artex/db" +) + +// csrfRequest builds a request to http://example.com (the httptest default +// host) with the given headers. A "Cookie" value is the artex_token cookie. +func csrfRequest(method, path, body string, headers map[string]string) *http.Request { + r := httptest.NewRequest(method, path, strings.NewReader(body)) + for k, v := range headers { + if k == "Cookie" { + r.AddCookie(&http.Cookie{Name: "artex_token", Value: v}) + continue + } + r.Header.Set(k, v) + } + return r +} + +// A session that arrives as the artex_token cookie is sent by the browser on +// its own, so a state-changing request that carries it must come from the +// console's own origin. requireAuth is exercised directly, without a database. +func TestCookieStateChangeNeedsSameOrigin(t *testing.T) { + s := &Server{jwtKey: []byte("0123456789abcdef0123456789abcdef")} + tok, err := signJWT(s.jwtKey) + if err != nil { + t.Fatal(err) + } + reached := false + h := s.requireAuth(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + reached = true + w.WriteHeader(204) + })) + // The request host is example.com over plain HTTP, so its origin is + // http://example.com (port 80). + for _, tc := range []struct { + name string + method string + headers map[string]string + want int + }{ + {"cookie, matching Origin", "POST", map[string]string{"Cookie": tok, "Origin": "http://example.com"}, 204}, + {"cookie, matching Origin with the default port written out", "POST", map[string]string{"Cookie": tok, "Origin": "http://EXAMPLE.com:80"}, 204}, + {"cookie, Sec-Fetch-Site same-origin, no Origin", "POST", map[string]string{"Cookie": tok, "Sec-Fetch-Site": "same-origin"}, 204}, + // A proxy that terminates TLS or rewrites Host changes what Origin is + // compared with; the browser's own same-origin statement still passes. + {"cookie, Origin differs, Sec-Fetch-Site same-origin", "POST", map[string]string{"Cookie": tok, "Origin": "https://console.example.org", "Sec-Fetch-Site": "same-origin"}, 204}, + {"cookie, foreign Origin", "POST", map[string]string{"Cookie": tok, "Origin": "https://evil.example"}, 403}, + {"cookie, foreign Origin, Sec-Fetch-Site cross-site", "POST", map[string]string{"Cookie": tok, "Origin": "https://evil.example", "Sec-Fetch-Site": "cross-site"}, 403}, + {"cookie, same site on another port", "POST", map[string]string{"Cookie": tok, "Origin": "http://example.com:3000", "Sec-Fetch-Site": "same-site"}, 403}, + {"cookie, same host over another scheme", "POST", map[string]string{"Cookie": tok, "Origin": "https://example.com"}, 403}, + {"cookie, sibling subdomain", "POST", map[string]string{"Cookie": tok, "Origin": "http://a.example.com", "Sec-Fetch-Site": "same-site"}, 403}, + {"cookie, host that only starts with the request host", "POST", map[string]string{"Cookie": tok, "Origin": "http://example.com.evil.example"}, 403}, + {"cookie, opaque Origin", "POST", map[string]string{"Cookie": tok, "Origin": "null"}, 403}, + {"cookie, neither header", "POST", map[string]string{"Cookie": tok}, 403}, + {"cookie, Sec-Fetch-Site none", "POST", map[string]string{"Cookie": tok, "Sec-Fetch-Site": "none"}, 403}, + {"cookie, foreign Origin, PUT", "PUT", map[string]string{"Cookie": tok, "Origin": "https://evil.example"}, 403}, + {"cookie, foreign Origin, PATCH", "PATCH", map[string]string{"Cookie": tok, "Origin": "https://evil.example"}, 403}, + {"cookie, foreign Origin, DELETE", "DELETE", map[string]string{"Cookie": tok, "Origin": "https://evil.example"}, 403}, + {"cookie, neither header, DELETE", "DELETE", map[string]string{"Cookie": tok}, 403}, + // Reads are not state changes. + {"cookie, foreign Origin, GET", "GET", map[string]string{"Cookie": tok, "Origin": "https://evil.example"}, 204}, + {"cookie, neither header, GET", "GET", map[string]string{"Cookie": tok}, 204}, + {"cookie, neither header, HEAD", "HEAD", map[string]string{"Cookie": tok}, 204}, + // A bearer token is not sent by the browser on its own. + {"bearer, foreign Origin", "POST", map[string]string{"Authorization": "Bearer " + tok, "Origin": "https://evil.example"}, 204}, + {"bearer, neither header", "POST", map[string]string{"Authorization": "Bearer " + tok}, 204}, + {"bearer and cookie, foreign Origin", "DELETE", map[string]string{"Authorization": "Bearer " + tok, "Cookie": tok, "Origin": "https://evil.example"}, 204}, + // A bad bearer token does not fall back to the cookie. + {"bad bearer and cookie, matching Origin", "POST", map[string]string{"Authorization": "Bearer nope", "Cookie": tok, "Origin": "http://example.com"}, 401}, + {"no token", "POST", map[string]string{"Origin": "http://example.com"}, 401}, + } { + reached = false + rec := httptest.NewRecorder() + h.ServeHTTP(rec, csrfRequest(tc.method, "/api/mcp", "", tc.headers)) + if rec.Code != tc.want || reached != (tc.want == 204) { + t.Errorf("%s: status=%d handler reached=%v, want %d", tc.name, rec.Code, reached, tc.want) + } + } + + // The token in the query string is a credential the caller holds, like the + // bearer token; with the cookie also present the cookie is the one used. + rec := httptest.NewRecorder() + h.ServeHTTP(rec, csrfRequest("POST", "/api/mcp?token="+tok, "", map[string]string{"Origin": "https://evil.example"})) + if rec.Code != 204 { + t.Errorf("query token, foreign Origin: status=%d, want 204", rec.Code) + } + rec = httptest.NewRecorder() + h.ServeHTTP(rec, csrfRequest("POST", "/api/mcp?token="+tok, "", map[string]string{"Cookie": tok, "Origin": "https://evil.example"})) + if rec.Code != 403 { + t.Errorf("query token and cookie, foreign Origin: status=%d, want 403", rec.Code) + } + + // Over TLS on a non-default port the origin is https://host:port. + for origin, want := range map[string]int{ + "https://console.test:8443": 204, + "http://console.test:8443": 403, + "https://console.test": 403, + "https://console.test:8444": 403, + } { + r := csrfRequest("POST", "https://console.test:8443/api/mcp", "", map[string]string{"Cookie": tok, "Origin": origin}) + r.TLS = &tls.ConnectionState{} + rec := httptest.NewRecorder() + h.ServeHTTP(rec, r) + if rec.Code != want { + t.Errorf("TLS request, Origin %s: status=%d, want %d", origin, rec.Code, want) + } + } + // An IPv6 literal host. + r := csrfRequest("POST", "http://[::1]:8787/api/mcp", "", map[string]string{"Cookie": tok, "Origin": "http://[::1]:8787"}) + rec = httptest.NewRecorder() + h.ServeHTTP(rec, r) + if rec.Code != 204 { + t.Errorf("IPv6 host, matching Origin: status=%d, want 204", rec.Code) + } +} + +// The same rule through the real mux, on an endpoint that stores a command +// (POST /api/mcp): a foreign page must not be able to add an MCP server with +// the operator's cookie. Setup and login need no session and keep working. +func TestCookieCSRFThroughRealMux(t *testing.T) { + m, err := NewManager(t.TempDir(), "") + if err != nil { + db.SkipPGUnavailable(t, err) + } + if m.pg == nil { + db.SkipNoPG(t) + } + defer m.Close() + td := t.TempDir() + ctx, cancel := context.WithCancel(context.Background()) + s := New(ctx, m, td, td, td) + defer stopTestServer(t, s, cancel) + + stamp := time.Now().UnixNano() + s.authHashKey = fmt.Sprintf("test.password_hash.%d", stamp) + s.authVersionKey = fmt.Sprintf("test.password_version.%d", stamp) + prefix := fmt.Sprintf("csrf-%d-", stamp) + defer func() { // deferred, not t.Cleanup: it must run before m.Close + _, _ = m.pg.Exec(`DELETE FROM settings WHERE key=$1 OR key=$2`, s.authHashKey, s.authVersionKey) + _, _ = m.pg.Exec(`DELETE FROM mcp_servers WHERE name LIKE $1`, prefix+"%") + }() + s.setupMu.Lock() + s.setupToken = "setup-ok" + s.setupMu.Unlock() + h := s.Handler() + + token := func(rec *httptest.ResponseRecorder) string { + t.Helper() + var out struct { + Token string `json:"token"` + } + if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &out) != nil || out.Token == "" { + t.Fatalf("status=%d body=%s, want a token", rec.Code, rec.Body.String()) + } + return out.Token + } + + // Setup from a client that sends neither Origin nor Sec-Fetch-Site (curl). + rec := httptest.NewRecorder() + h.ServeHTTP(rec, csrfRequest("POST", "/api/auth/init", `{"password":"correct-horse-1","setup_token":"setup-ok"}`, nil)) + stale := token(rec) + // Login the same way, then from the browser: the login page posts from the + // console's own origin and may still hold the cookie of an earlier session. + login := `{"username":"ARTEX","password":"correct-horse-1"}` + rec = httptest.NewRecorder() + h.ServeHTTP(rec, csrfRequest("POST", "/api/auth/login", login, nil)) + token(rec) + rec = httptest.NewRecorder() + h.ServeHTTP(rec, csrfRequest("POST", "/api/auth/login", login, map[string]string{"Cookie": stale})) + token(rec) + rec = httptest.NewRecorder() + h.ServeHTTP(rec, csrfRequest("POST", "/api/auth/login", login, map[string]string{"Cookie": stale, "Origin": "http://example.com", "Sec-Fetch-Site": "same-origin"})) + tok := token(rec) + + for _, tc := range []struct { + name string + headers map[string]string + want int + }{ + {"same-origin", map[string]string{"Cookie": tok, "Origin": "http://example.com", "Content-Type": "application/json"}, 200}, + // What a foreign page can send without a preflight: a text/plain body. + {"cross-origin", map[string]string{"Cookie": tok, "Origin": "https://evil.example", "Content-Type": "text/plain"}, 403}, + {"other-port", map[string]string{"Cookie": tok, "Origin": "http://example.com:3000", "Sec-Fetch-Site": "same-site", "Content-Type": "text/plain"}, 403}, + {"no-headers", map[string]string{"Cookie": tok}, 403}, + {"bearer-cross-origin", map[string]string{"Authorization": "Bearer " + tok, "Origin": "https://evil.example"}, 200}, + } { + name := prefix + tc.name + body := fmt.Sprintf(`{"name":%q,"transport":"stdio","command":"sh","args":["-c","id"],"enabled":false}`, name) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, csrfRequest("POST", "/api/mcp", body, tc.headers)) + rows, err := m.pg.ListMCP() + if err != nil { + t.Fatal(err) + } + stored := false + for _, row := range rows { + stored = stored || row.Name == name + } + if rec.Code != tc.want || stored != (tc.want == 200) { + t.Errorf("%s: status=%d MCP server stored=%v, want %d (body %s)", tc.name, rec.Code, stored, tc.want, strings.TrimSpace(rec.Body.String())) + } + } + + // Reads with the cookie alone still work (the log page and the event + // streams send nothing else). + rec = httptest.NewRecorder() + h.ServeHTTP(rec, csrfRequest("GET", "/api/mcp", "", map[string]string{"Cookie": tok})) + if rec.Code != 200 { + t.Errorf("GET /api/mcp with the cookie: status=%d, want 200", rec.Code) + } +} From 8eff7b907cec8594ac1ea115a22845ed7675ef89 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:29:44 +0100 Subject: [PATCH 3/6] fix(server): keep the setup token and proxy credentials out of the log sink The log sink keeps every standard-logger line in a ring, persists it to server_logs and serves it through /api/logs, /api/logs/history and /api/logs/stream to any session. Two secrets went through it. The one-time setup token was logged with log.Printf. It is now printed on stderr directly, with the same wording, so `docker compose logs` and the journal still show it; the sink gets a line that says where it was printed. A global proxy with user:password was logged in full when the browser MCP was synced and when a stored value failed validation at startup, and was written with its credentials into the --proxy-server argument of the browser MCP row, which GET /api/mcp lists. The log lines and the row now carry the proxy without its userinfo. A stored address that does not parse is not written to the row at all. As a second line of defence the sink masks the password of any scheme://user:password@ it is given, in the ring and on stderr, and rows persisted earlier are masked when they are read back. --- server/auth.go | 12 +- server/log_hygiene_test.go | 323 +++++++++++++++++++++++++++++++++++++ server/logsink.go | 22 ++- server/manager.go | 22 ++- server/server.go | 2 +- 5 files changed, 373 insertions(+), 8 deletions(-) create mode 100644 server/log_hygiene_test.go diff --git a/server/auth.go b/server/auth.go index 768cfcc75..82d545b92 100644 --- a/server/auth.go +++ b/server/auth.go @@ -5,6 +5,7 @@ import ( "crypto/subtle" "encoding/hex" "fmt" + "io" "log" "math/big" "net/http" @@ -376,9 +377,15 @@ func (s *Server) authStatus(w http.ResponseWriter, r *http.Request) { writeJSON(w, 200, map[string]any{"initialized": hash != ""}) } +// setupTokenOut is where the setup token is printed: the stderr of the process. +// It is a variable so that tests can capture it. +var setupTokenOut io.Writer = os.Stderr + // initSetupToken generates the one-time setup token when no admin password is set -// and writes it to the server log. Without it, anyone who can reach the port +// and prints it on the server console. Without it, anyone who can reach the port // before the operator could claim the instance through POST /api/auth/init. +// The token goes to stderr directly, not through the standard logger: the log +// sink keeps every line for /api/logs and persists it. func (s *Server) initSetupToken() { if s.m == nil || s.m.pg == nil { return @@ -411,7 +418,8 @@ func (s *Server) initSetupToken() { s.setupMu.Lock() s.setupToken = token s.setupMu.Unlock() - log.Printf("[auth] no admin password is set. Setup token for the /setup page: %s", token) + fmt.Fprintln(setupTokenOut, time.Now().Format("2006/01/02 15:04:05"), "[auth] no admin password is set. Setup token for the /setup page:", token) + log.Printf("[auth] no admin password is set. The setup token for the /setup page was printed on the server console (stderr); it is not kept in this log") } // loadPasswordVersion reads auth.password_version. A missing key leaves the diff --git a/server/log_hygiene_test.go b/server/log_hygiene_test.go new file mode 100644 index 000000000..b6025a3f5 --- /dev/null +++ b/server/log_hygiene_test.go @@ -0,0 +1,323 @@ +package server + +import ( + "bytes" + "context" + "fmt" + "log" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" + + "github.com/Autumn-27/artex/db" +) + +// lockedBuffer is a bytes.Buffer that goroutines still logging in the +// background can write to while the test reads it. +type lockedBuffer struct { + mu sync.Mutex + b bytes.Buffer +} + +func (l *lockedBuffer) Write(p []byte) (int, error) { + l.mu.Lock() + defer l.mu.Unlock() + return l.b.Write(p) +} + +func (l *lockedBuffer) String() string { + l.mu.Lock() + defer l.mu.Unlock() + return l.b.String() +} + +// captureLogSink routes the standard logger through a fresh log sink for the +// test, as StartLogCapture does in the binary, and makes it the sink the +// /api/logs endpoints read. passthrough receives what the sink forwards to +// stderr. +func captureLogSink(t *testing.T) (sink *logSinkT, passthrough *lockedBuffer) { + t.Helper() + passthrough = &lockedBuffer{} + sink = &logSinkT{cap: 3000, subs: map[chan LogLine]struct{}{}, out: passthrough} + prevSink, prevOut := logSink, log.Writer() + logSink = sink + log.SetOutput(sink) + t.Cleanup(func() { + log.SetOutput(prevOut) + logSink = prevSink + }) + return sink, passthrough +} + +// sinkText is every line the sink holds, joined. +func sinkText(s *logSinkT) string { + lines, _ := s.recent(0, 0) + var b strings.Builder + for _, l := range lines { + b.WriteString(l.Text) + b.WriteByte('\n') + } + return b.String() +} + +// The setup token claims the instance, and /api/logs serves the log sink to +// any session; the sink (ring, persisted rows, stream) must never hold it. +func TestSetupTokenStaysOutOfLogSink(t *testing.T) { + m, err := NewManager(t.TempDir(), "") + if err != nil { + db.SkipPGUnavailable(t, err) + } + defer m.Close() + sink, passthrough := captureLogSink(t) + console := &lockedBuffer{} + prevConsole := setupTokenOut + setupTokenOut = console + t.Cleanup(func() { setupTokenOut = prevConsole }) + + s := &Server{m: m, jwtKey: []byte("0123456789abcdef0123456789abcdef"), + authHashKey: fmt.Sprintf("test.password_hash.%d", time.Now().UnixNano())} + s.initSetupToken() + if s.setupToken == "" { + t.Fatal("no setup token generated while no password is set") + } + // The console line is the one the documented `grep "Setup token"` finds. + if got := console.String(); !strings.Contains(got, "Setup token for the /setup page: "+s.setupToken+"\n") { + t.Errorf("the server console does not show the setup token: %q", strings.ReplaceAll(got, s.setupToken, "")) + } + if got := sinkText(sink); strings.Contains(got, s.setupToken) { + t.Errorf("the log sink holds the setup token:\n%s", got) + } + if got := passthrough.String(); strings.Contains(got, s.setupToken) { + t.Errorf("the sink forwarded the setup token:\n%s", got) + } + // The operator is still told where to find it. + if got := sinkText(sink); !strings.Contains(got, "[auth] no admin password is set") || !strings.Contains(got, "server console") { + t.Errorf("the log sink does not say where the setup token was printed:\n%s", got) + } +} + +// A global proxy with user:password is a secret (GET /api/settings masks it). +// Setting it must not put the password into the log sink, the /api/logs and +// /api/logs/history output, the browser MCP row, or the MCP listing. +func TestGlobalProxyCredentialsStayOutOfLogsAndMCPRow(t *testing.T) { + m, err := NewManager(t.TempDir(), "") + if err != nil { + db.SkipPGUnavailable(t, err) + } + if m.pg == nil { + db.SkipNoPG(t) + } + defer m.Close() + sink, passthrough := captureLogSink(t) + td := t.TempDir() + ctx, cancel := context.WithCancel(context.Background()) + s := New(ctx, m, td, td, td) // wires the fresh sink to this database + defer stopTestServer(t, s, cancel) + h := s.Handler() + tok, err := signJWT(s.jwtKey) + if err != nil { + t.Fatal(err) + } + do := func(method, path, body string) *httptest.ResponseRecorder { + r := httptest.NewRequest(method, path, strings.NewReader(body)) + r.Header.Set("Authorization", "Bearer "+tok) + rec := httptest.NewRecorder() + h.ServeHTTP(rec, r) + return rec + } + + // Unique per run, so rows an earlier run left in server_logs cannot match. + stamp := time.Now().UnixNano() + user, password := fmt.Sprintf("proxyuser%d", stamp), fmt.Sprintf("s3cr3t%d", stamp) + host := fmt.Sprintf("proxy-%d.test:3128", stamp) + defer func() { // deferred, not t.Cleanup: it must run before m.Close + if err := m.SetGlobalProxy(""); err != nil { + t.Errorf("clear the global proxy: %v", err) + } + }() + if rec := do("PUT", "/api/settings", fmt.Sprintf(`{"global_proxy":"http://%s:%s@%s"}`, user, password, host)); rec.Code != 200 { + t.Fatalf("PUT /api/settings: status=%d body=%s", rec.Code, rec.Body.String()) + } + if got := m.GlobalProxy(); !strings.Contains(got, password) { + t.Fatalf("the global proxy was not stored with its credentials: %q", got) + } + + // The browser MCP row carries the proxy, without the credentials. + browser := listMCPByName(t, m.pg, browserMCPName) + if args := decodeStrSlice(browser.Args); len(args) < 2 || args[len(args)-2] != "--proxy-server" || args[len(args)-1] != "http://"+host { + t.Errorf("browser MCP args = %s, want them to end with --proxy-server http://%s", browser.Args, host) + } + // The sync line is in the sink, and persisted, before the surfaces are read. + deadline := time.Now().Add(10 * time.Second) + for persisted := false; !persisted; { + rows, err := m.pg.RecentLogs(200) + if err != nil { + t.Fatal(err) + } + for _, row := range rows { + persisted = persisted || strings.Contains(row.Text, host) + } + if !persisted && time.Now().After(deadline) { + t.Fatalf("no log line that names %s was persisted", host) + } + time.Sleep(20 * time.Millisecond) + } + + surfaces := map[string]string{ + "log sink": sinkText(sink), + "stderr passthrough": passthrough.String(), + "GET /api/logs": do("GET", "/api/logs?limit=3000", "").Body.String(), + "GET /api/logs/history": do("GET", "/api/logs/history?limit=500", "").Body.String(), + "browser MCP row args": string(browser.Args), + "GET /api/mcp": do("GET", "/api/mcp", "").Body.String(), + } + for name, got := range surfaces { + if strings.Contains(got, password) { + t.Errorf("%s holds the proxy password", name) + } + if !strings.Contains(got, host) { + t.Errorf("%s does not name the proxy host %s, so this check proves nothing", name, host) + } + } + if args := string(browser.Args); strings.Contains(args, user) { + t.Errorf("browser MCP args hold the proxy user: %s", args) + } +} + +// A stored global proxy that fails validation is reported at startup; the line +// must not quote the address with its credentials either. +func TestInvalidStoredGlobalProxyIsLoggedWithoutCredentials(t *testing.T) { + m, err := NewManager(t.TempDir(), "") + if err != nil { + db.SkipPGUnavailable(t, err) + } + defer m.Close() + stamp := time.Now().UnixNano() + password := fmt.Sprintf("s3cr3t%d", stamp) + prev, had, err := m.pg.GetSetting(settingGlobalProxy) + if err != nil { + t.Fatal(err) + } + defer func() { // deferred, not t.Cleanup: it must run before m.Close + var err error + if had { + err = m.pg.SetSetting(settingGlobalProxy, prev) + } else { + _, err = m.pg.Exec(`DELETE FROM settings WHERE key=$1`, settingGlobalProxy) + } + if err != nil { + t.Errorf("restore the global proxy setting: %v", err) + } + m.syncBrowserMCPProxy() + }() + for _, stored := range []string{ + "ftp://proxyuser:" + password + "@badproxy.test:21", // unsupported scheme + "proxyuser:" + password + "@badproxy.test:3128", // no scheme: does not parse + } { + if err := m.pg.SetSetting(settingGlobalProxy, stored); err != nil { + t.Fatal(err) + } + sink, passthrough := captureLogSink(t) + // The recording proxy is on, so the stored value is validated at startup. + m2, err := NewManager(t.TempDir(), "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + got := sinkText(sink) + browser := listMCPByName(t, m.pg, browserMCPName) + m2.Close() + if !strings.Contains(got, "[proxy] global proxy") { + t.Errorf("stored %q: the invalid proxy was not reported:\n%s", strings.ReplaceAll(stored, password, "***"), got) + } + for name, text := range map[string]string{"log sink": got, "stderr passthrough": passthrough.String(), "browser MCP row args": string(browser.Args)} { + if strings.Contains(text, password) { + t.Errorf("stored %q: %s holds the proxy password", strings.ReplaceAll(stored, password, "***"), name) + } + } + } +} + +func TestProxyWithoutCredentials(t *testing.T) { + for raw, want := range map[string]string{ + "": "", + "http://127.0.0.1:8788": "http://127.0.0.1:8788", + "http://user:pass@proxy.test:3128": "http://proxy.test:3128", + "socks5://user@proxy.test:1080": "socks5://proxy.test:1080", + " https://u:p%40ss@proxy.test:8443/ ": "https://proxy.test:8443/", + "http://user:pa:ss@[2001:db8::1]:3128": "http://[2001:db8::1]:3128", + "user:pass@proxy.test:3128": "", // no scheme: does not parse + "http://user:pass@proxy.test:notaport": "", + "http://user:pa ss@proxy.test:3128": "", + "//user:pass@proxy.test:3128": "//proxy.test:3128", + } { + if got := proxyWithoutCredentials(raw); got != want { + t.Errorf("proxyWithoutCredentials(%q) = %q, want %q", raw, got, want) + } + } +} + +func TestMaskURLCredentials(t *testing.T) { + for in, want := range map[string]string{ + "plain line, no URL": "plain line, no URL", + "proxy http://user:pass@proxy.test:3128 attached": "proxy http://user:xxxxx@proxy.test:3128 attached", + `global proxy "socks5://u:p@h:1080" invalid`: `global proxy "socks5://u:xxxxx@h:1080" invalid`, + "dsn postgres://artex:pw@/db?host=/tmp/sock": "dsn postgres://artex:xxxxx@/db?host=/tmp/sock", + "a password with @ and : http://user:p@ss:w0rd@h:1/x": "a password with @ and : http://user:xxxxx@h:1/x", + "two http://a:b@h1 and https://c:d@h2/p": "two http://a:xxxxx@h1 and https://c:xxxxx@h2/p", + "an empty user http://:secret@h:1": "an empty user http://:xxxxx@h:1", + // No password: nothing to mask, and nothing mistaken for one. + "user only ssh://git@github.com/org/repo": "user only ssh://git@github.com/org/repo", + "host and port http://127.0.0.1:8787/api?mail=a@b.test": "host and port http://127.0.0.1:8787/api?mail=a@b.test", + "query without a path http://h:8787?mail=a@b.test": "query without a path http://h:8787?mail=a@b.test", + "IPv6 http://[::1]:8787/x@y": "IPv6 http://[::1]:8787/x@y", + "see https://docs.test:8443 or mail ops@corp.test": "see https://docs.test:8443 or mail ops@corp.test", + } { + if got := maskURLCredentials(in); got != want { + t.Errorf("maskURLCredentials(%q) = %q, want %q", in, got, want) + } + } +} + +// The sink masks what it keeps and what it forwards to stderr. +func TestLogSinkMasksURLCredentials(t *testing.T) { + sink, passthrough := captureLogSink(t) + log.Printf("[proxy] upstream %s refused", "http://user:hunter2@proxy.test:3128") + for name, got := range map[string]string{"ring": sinkText(sink), "stderr passthrough": passthrough.String()} { + if strings.Contains(got, "hunter2") || !strings.Contains(got, "[proxy] upstream http://user:xxxxx@proxy.test:3128 refused") { + t.Errorf("%s = %q, want the password masked", name, got) + } + } +} + +// Rows persisted before the sink masked credentials are masked when they are +// read back: by GET /api/logs/history and by the restore into the ring. +func TestLogsHistoryMasksCredentialsInOldRows(t *testing.T) { + m, err := NewManager(t.TempDir(), "") + if err != nil { + db.SkipPGUnavailable(t, err) + } + defer m.Close() + password := fmt.Sprintf("oldpw%d", time.Now().UnixNano()) + id, err := m.pg.InsertLog("info", "mcp", "[mcp] browser MCP now has the capture proxy http://olduser:"+password+"@old.test:3128 attached (CA )") + if err != nil { + t.Fatal(err) + } + defer func() { _, _ = m.pg.Exec(`DELETE FROM server_logs WHERE id=$1`, id) }() // before m.Close + + s := &Server{m: m} + rec := httptest.NewRecorder() + s.getLogsHistory(rec, httptest.NewRequest("GET", "/api/logs/history?limit=500", nil)) + if body := rec.Body.String(); rec.Code != 200 || strings.Contains(body, password) || !strings.Contains(body, "http://olduser:xxxxx@old.test:3128") { + t.Errorf("GET /api/logs/history: status=%d, want the old row with its password masked; password present=%v", rec.Code, strings.Contains(body, password)) + } + + sink := &logSinkT{cap: 3000, subs: map[chan LogLine]struct{}{}, out: &lockedBuffer{}} + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + sink.SetDB(ctx, m.pg) + if got := sinkText(sink); strings.Contains(got, password) || !strings.Contains(got, "http://olduser:xxxxx@old.test:3128") { + t.Errorf("restored ring: want the old row with its password masked; password present=%v", strings.Contains(got, password)) + } +} diff --git a/server/logsink.go b/server/logsink.go index 7a6ec4624..cff3c687c 100644 --- a/server/logsink.go +++ b/server/logsink.go @@ -5,6 +5,7 @@ import ( "io" "log" "os" + "regexp" "strings" "sync" "time" @@ -75,7 +76,7 @@ func (s *logSinkT) SetDB(ctx context.Context, pg *db.DB) { TS: l.CreatedAt.Format(time.RFC3339), Level: l.Level, Tag: l.Tag, - Text: l.Text, + Text: maskURLCredentials(l.Text), // rows written before the sink masked them }) } // Prepend history before any in-memory startup logs already in ring. @@ -117,10 +118,25 @@ func (s *logSinkT) SetDB(ctx context.Context, pg *db.DB) { }) } +// urlCredentials matches scheme://user:password@ up to the last @ of the +// authority. +var urlCredentials = regexp.MustCompile(`([a-zA-Z][a-zA-Z0-9+.-]*://[^/\s:@?#]*):[^/\s?#]*@`) + +// maskURLCredentials replaces the password of every scheme://user:password@ in +// text. It is a second line of defence for the log sink, which /api/logs +// serves: a log call should not format a secret in the first place. +func maskURLCredentials(text string) string { + if !strings.Contains(text, "://") { + return text + } + return urlCredentials.ReplaceAllString(text, "${1}:xxxxx@") +} + // Write implements io.Writer for the log package: one call per log.Printf line. func (s *logSinkT) Write(p []byte) (int, error) { - _, _ = s.out.Write(p) // keep the terminal output - line := strings.TrimRight(string(p), "\n") + text := maskURLCredentials(string(p)) + _, _ = io.WriteString(s.out, text) // keep the terminal output + line := strings.TrimRight(text, "\n") if strings.TrimSpace(line) != "" { s.add(parseLog(line)) } diff --git a/server/manager.go b/server/manager.go index c763fdb39..bd68b8d70 100644 --- a/server/manager.go +++ b/server/manager.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "log" + "net/url" "os" "path/filepath" "sort" @@ -527,7 +528,9 @@ func NewManager(dir, proxyAddr string) (*Manager, error) { } if m.traffic != nil { if err := m.traffic.SetUpstreamProxy(m.globalProxy); err != nil { - log.Printf("[proxy] global proxy %q invalid, ignored: %v", m.globalProxy, err) + // Neither the stored value nor err (which quotes it) is logged: both + // can carry the proxy credentials. + log.Printf("[proxy] global proxy %q invalid, ignored; set it again in Settings (shown without credentials, empty when it cannot be parsed)", proxyWithoutCredentials(m.globalProxy)) } } m.enrich = enrich.New(m.assets, m.ProxyAddr, 4) @@ -755,7 +758,10 @@ func (m *Manager) syncBrowserMCPProxy() { return // user removed/renamed it — leave it alone } - proxy := m.ProxyAddr() // "" when capture off + // "" when capture is off and no global proxy is set. The global proxy can + // carry user:password; the row is listed by GET /api/mcp and the address is + // logged below, so the credentials are left out of both. + proxy := proxyWithoutCredentials(m.ProxyAddr()) cert := m.ProxyCACert() // "" when capture off args := stripProxyArgs(decodeStrSlice(srv.Args)) @@ -780,6 +786,18 @@ func (m *Manager) syncBrowserMCPProxy() { } } +// proxyWithoutCredentials returns a proxy URL without its user:password, for +// log lines and stored rows. An address that does not parse is returned as "": +// its credentials cannot be told apart from the rest. +func proxyWithoutCredentials(raw string) string { + u, err := url.Parse(strings.TrimSpace(raw)) + if err != nil || u.Host == "" { + return "" + } + u.User = nil + return u.String() +} + // stripProxyArgs removes any --proxy-server/--proxy-bypass flags (both "--flag val" // and "--flag=val" forms) so they can be re-added cleanly from current state, // without mutating the input slice. diff --git a/server/server.go b/server/server.go index 154682153..241ad179d 100644 --- a/server/server.go +++ b/server/server.go @@ -3347,7 +3347,7 @@ func (s *Server) getLogsHistory(w http.ResponseWriter, r *http.Request) { TS: r.CreatedAt.Format(time.RFC3339), Level: r.Level, Tag: r.Tag, - Text: r.Text, + Text: maskURLCredentials(r.Text), // rows written before the sink masked them }) } writeJSON(w, 200, map[string]any{"items": items, "has_more": len(rows) == limit}) From 6765d01b945e6bedd57926ef9ea7fdc10528dc71 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:34:55 +0100 Subject: [PATCH 4/6] fix(server): keep the capture CA, index and blob store out of workspace deletes The workspace file manager refused traffic/_ca and traffic/_index by path, but DELETE /api/workspace/delete on their parent, traffic, removed the whole tree: the capture CA private key, the exchange index and the blobs went with it. The blob store, traffic/_blobs, was not denied at all and could be listed, read and downloaded. traffic/_blobs joins the denied prefixes, and a delete is refused with 400 when a denied prefix lies beneath the entry it would remove. A symlink to such a directory is still removed as a link. --- server/workspace.go | 32 ++++++++++++++- server/workspace_deny_test.go | 77 +++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/server/workspace.go b/server/workspace.go index 82d339d1e..19044e525 100644 --- a/server/workspace.go +++ b/server/workspace.go @@ -39,8 +39,9 @@ func (s *Server) wsResolveConfined(rel string) (confinedPath, bool) { // workspaceDeniedPrefixes are platform-internal subtrees under the data dir // that the workspace file manager must not expose. traffic/_ca holds the -// capture proxy CA private key; traffic/_index holds the SQLite exchange index. -var workspaceDeniedPrefixes = []string{"traffic/_ca", "traffic/_index"} +// capture proxy CA private key; traffic/_index holds the SQLite exchange index; +// traffic/_blobs holds the captured bodies the index refers to by hash. +var workspaceDeniedPrefixes = []string{"traffic/_ca", "traffic/_index", "traffic/_blobs"} // workspacePathDenied reports whether realPath lands in a platform-internal // subtree. It compares the canonicalized path relative to the real root, so a @@ -69,6 +70,27 @@ func workspacePathDenied(realRoot, realPath string) bool { return false } +// workspaceHoldsDenied reports whether a platform-internal subtree lies beneath +// path, so that removing path would remove it too. Segments are normalised as +// in workspacePathDenied. +func workspaceHoldsDenied(realRoot, path string) bool { + rel, err := filepath.Rel(realRoot, path) + if err != nil { + return true // fail closed + } + segs := strings.Split(filepath.ToSlash(rel), "/") + for i, seg := range segs { + segs[i] = strings.ToLower(strings.TrimRight(seg, ". ")) + } + rel = strings.Join(segs, "/") + for _, prefix := range workspaceDeniedPrefixes { + if strings.HasPrefix(prefix, rel+"/") { + return true + } + } + return false +} + // wsRel renders an absolute path back as a workspace-relative path (forward slashes). func (s *Server) wsRel(abs string) string { base, err := resolveRealRoot(s.m.dir) @@ -235,6 +257,12 @@ func (s *Server) wsDelete(w http.ResponseWriter, r *http.Request) { writeErr(w, 400, "cannot delete the workspace root directory") return } + // entryPath is what RemoveAll removes: a symlink is removed as a link, a + // directory with everything beneath it. + if workspaceHoldsDenied(cp.realRoot, cp.entryPath) { + writeErr(w, 400, "cannot delete a directory that holds platform data (the capture CA, index and blob store)") + return + } if !cp.exists { writeErr(w, 404, "path does not exist") return diff --git a/server/workspace_deny_test.go b/server/workspace_deny_test.go index 70e08fd23..447407fc4 100644 --- a/server/workspace_deny_test.go +++ b/server/workspace_deny_test.go @@ -1,6 +1,9 @@ package server import ( + "net/http" + "net/http/httptest" + "net/url" "os" "path/filepath" "testing" @@ -66,3 +69,77 @@ func TestWorkspacePathDenied(t *testing.T) { } } } + +// Deleting a directory that holds a platform-internal subtree would remove the +// capture CA key and the exchange index with it, and the blob store is as +// internal as the index. Neither is reachable through the file manager. +func TestWorkspaceDeleteKeepsPlatformPathsAndBlobsAreDenied(t *testing.T) { + dir := t.TempDir() + for _, p := range []string{"traffic/_ca", "traffic/_index", "traffic/_blobs/sha256/ab", "traffic/host", "other/sub"} { + if err := os.MkdirAll(filepath.Join(dir, p), 0o755); err != nil { + t.Fatal(err) + } + } + kept := []string{"traffic/_ca/mitmproxy-ca.pem", "traffic/_index/index.sqlite", "traffic/_blobs/sha256/ab/abcd.bin", "traffic/host/note.txt"} + for _, p := range append(kept, "other/sub/note.txt") { + if err := os.WriteFile(filepath.Join(dir, p), []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + } + s := &Server{m: &Manager{dir: dir}} + call := func(h http.HandlerFunc, method, target string) int { + rec := httptest.NewRecorder() + h(rec, httptest.NewRequest(method, target, nil)) + return rec.Code + } + + // The parent of the denied subtrees, in the spellings that reach it. + for _, rel := range []string{"traffic", "traffic/", "TRAFFIC", "traffic."} { + if code := call(s.wsDelete, "DELETE", "/api/workspace/delete?path="+url.QueryEscape(rel)); code != 400 { + t.Errorf("DELETE %q: status=%d, want 400", rel, code) + } + } + for _, p := range kept { + if _, err := os.Stat(filepath.Join(dir, p)); err != nil { + t.Errorf("%s is gone after the refused deletes: %v", p, err) + } + } + + // The blob store is denied like the CA and the index. + for _, rel := range []string{"traffic/_blobs", "traffic/_blobs/sha256/ab/abcd.bin", "TRAFFIC/_BLOBS/sha256"} { + for name, h := range map[string]http.HandlerFunc{"list": s.wsList, "read": s.wsRead, "download": s.wsDownload} { + if code := call(h, "GET", "/api/workspace/"+name+"?path="+url.QueryEscape(rel)); code != 400 { + t.Errorf("%s %q: status=%d, want 400", name, rel, code) + } + } + if code := call(s.wsDelete, "DELETE", "/api/workspace/delete?path="+url.QueryEscape(rel)); code != 400 { + t.Errorf("DELETE %q: status=%d, want 400", rel, code) + } + } + if _, err := os.Stat(filepath.Join(dir, "traffic/_blobs/sha256/ab/abcd.bin")); err != nil { + t.Errorf("the blob is gone: %v", err) + } + + // A symlink to the traffic directory is removed as a link; its target stays. + if err := os.Symlink(filepath.Join(dir, "traffic"), filepath.Join(dir, "alias")); err == nil { + if code := call(s.wsDelete, "DELETE", "/api/workspace/delete?path=alias"); code != 200 { + t.Errorf("DELETE of a symlink to traffic: status=%d, want 200", code) + } + if _, err := os.Lstat(filepath.Join(dir, "alias")); err == nil { + t.Error("the symlink was not removed") + } + if _, err := os.Stat(filepath.Join(dir, "traffic/_ca/mitmproxy-ca.pem")); err != nil { + t.Errorf("deleting the symlink removed the CA key: %v", err) + } + } + + // Ordinary trees, and the traffic of one host, are still deletable. + for _, rel := range []string{"traffic/host", "other"} { + if code := call(s.wsDelete, "DELETE", "/api/workspace/delete?path="+url.QueryEscape(rel)); code != 200 { + t.Errorf("DELETE %q: status=%d, want 200", rel, code) + } + if _, err := os.Stat(filepath.Join(dir, rel)); err == nil { + t.Errorf("%s still exists after its delete", rel) + } + } +} From a1998685f9b8f6281764ffb6ae9fb84b7ef66411 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:34:55 +0100 Subject: [PATCH 5/6] fix(server): block carrier-grade NAT and reserved ranges; stop echoing upstream model errors POST /api/llm/models treated every address outside RFC 1918, loopback, link-local, multicast and the metadata addresses as public. It now also refuses 100.64.0.0/10, 198.18.0.0/15, 192.0.0.0/24, 240.0.0.0/4, NAT64 64:ff9b::/96 and 6to4 2002::/16, for literal hosts, resolved names, redirects and at dial time. An endpoint listed in ARTEX_LLM_PRIVATE_ENDPOINTS may still sit in 100.64.0.0/10 or 198.18.0.0/15, as it may in RFC 1918 space; the other ranges are never allowed. A non-200 answer was returned with the first 512 bytes of its body, which let a caller read what any reachable host answers. The answer now carries the status only. --- SECURITY.md | 4 +- server/llm_models_reserved_test.go | 140 +++++++++++++++++++++++++++++ server/server_mgmt.go | 43 +++++++-- 3 files changed, 178 insertions(+), 9 deletions(-) create mode 100644 server/llm_models_reserved_test.go diff --git a/SECURITY.md b/SECURITY.md index 80e58924f..f84a5c7c2 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -45,9 +45,9 @@ Accepted gap: when a task has no allow-list, there is no allow rule to apply (bl ## Outbound Model Listing (`POST /api/llm/models`) -`POST /api/llm/models` fetches model lists from the provider endpoint and optional HTTP proxy supplied in the request. By default, ARTEX allows only `http` and `https` URLs whose hosts resolve exclusively to public IP addresses (blocking loopback, RFC1918 private networks, IPv6 unique-local `fc00::/7`, link-local/cloud metadata such as `169.254.169.254`, unspecified, and multicast addresses), and enforces the same checks on redirects and at dial time. The built-in local Ollama default (`http://127.0.0.1:11434/v1`) is permitted only when the request leaves `base_url` empty for the `ollama` provider. +`POST /api/llm/models` fetches model lists from the provider endpoint and optional HTTP proxy supplied in the request. By default, ARTEX allows only `http` and `https` URLs whose hosts resolve exclusively to public IP addresses (blocking loopback, RFC1918 private networks, IPv6 unique-local `fc00::/7`, link-local/cloud metadata such as `169.254.169.254`, unspecified, and multicast addresses, carrier-grade NAT `100.64.0.0/10`, the benchmarking range `198.18.0.0/15`, `192.0.0.0/24`, the reserved block `240.0.0.0/4`, NAT64 `64:ff9b::/96` and 6to4 `2002::/16`), and enforces the same checks on redirects and at dial time. A non-200 answer from the endpoint is reported by its status only; its body is not returned. The built-in local Ollama default (`http://127.0.0.1:11434/v1`) is permitted only when the request leaves `base_url` empty for the `ollama` provider. -Operators running self-hosted LLM gateways or Ollama on a LAN or Docker bridge can allow specific endpoints with `ARTEX_LLM_PRIVATE_ENDPOINTS`, a comma-separated list of exact `host:port` entries (for example, `127.0.0.1:11434,host.docker.internal:11434,192.168.1.10:11434`). A URL whose `host:port` matches an entry may resolve to a loopback or private address while scheme restrictions, link-local/cloud-metadata blocks, and redirect validation still apply; everything else stays refused. Leaving `ARTEX_LLM_PRIVATE_ENDPOINTS` unset or empty keeps the strict public-only policy. +Operators running self-hosted LLM gateways or Ollama on a LAN or Docker bridge can allow specific endpoints with `ARTEX_LLM_PRIVATE_ENDPOINTS`, a comma-separated list of exact `host:port` entries (for example, `127.0.0.1:11434,host.docker.internal:11434,192.168.1.10:11434`). A URL whose `host:port` matches an entry may resolve to a loopback or private address, or to an address in `100.64.0.0/10` or `198.18.0.0/15` (an overlay network, a fake-IP resolver), while scheme restrictions, link-local/cloud-metadata blocks, the other reserved ranges above, and redirect validation still apply; everything else stays refused. Leaving `ARTEX_LLM_PRIVATE_ENDPOINTS` unset or empty keeps the strict public-only policy. ## Agent Process Isolation diff --git a/server/llm_models_reserved_test.go b/server/llm_models_reserved_test.go new file mode 100644 index 000000000..884d85038 --- /dev/null +++ b/server/llm_models_reserved_test.go @@ -0,0 +1,140 @@ +package server + +import ( + "context" + "net" + "net/http" + "net/http/httptest" + "net/netip" + "strings" + "sync/atomic" + "testing" +) + +// Ranges that are not public Internet addresses but are neither RFC 1918 nor +// loopback: carrier-grade NAT, the benchmarking range, the IETF protocol +// assignments, the reserved class E block, and the two IPv6 prefixes that +// embed an IPv4 address (NAT64, 6to4). +var reservedModelAddrs = map[string][]string{ + "cgnat": {"100.64.0.1", "100.127.255.254", "::ffff:100.64.0.1"}, + "benchmark": {"198.18.0.1", "198.19.255.254"}, + "ietf_protocol": {"192.0.0.1", "192.0.0.170"}, + "reserved": {"240.0.0.1", "250.1.2.3", "255.255.255.255"}, + "nat64": {"64:ff9b::a9fe:a9fe", "64:ff9b::7f00:1", "64:ff9b::5db8:d822"}, + "six_to_four": {"2002:a9fe:a9fe::1", "2002:7f00:1::1", "2002:5db8:d822::1"}, +} + +func TestLLMModelsRefusesReservedRanges(t *testing.T) { + t.Setenv(LLMPrivateEndpointsEnv, "") + s := &Server{} + for name, addrs := range reservedModelAddrs { + for _, a := range addrs { + addr := netip.MustParseAddr(a) + if !s.isBlockedModelAddr(addr, false) { + t.Errorf("%s: %s is allowed, want it blocked", name, a) + } + // As a literal host: refused before any request, without the address. + host := a + if addr.Is6() { + host = "[" + a + "]" + } + resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{ + "provider": "openai", "base_url": "http://" + host + "/v1", "api_key": "sk-test", + }) + if errMsg, _ := resp["error"].(string); resp["ok"] != false || errMsg == "" || strings.Contains(errMsg, a) { + t.Errorf("%s: base_url %s answered %v, want a refusal that does not echo the address", name, a, resp) + } + // As the address a public-looking name resolves to. + rs := &Server{modelLookupIP: func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("93.184.216.34"), addr}, nil + }} + resp = postJSON(t, rs.pgListModels, "/api/llm/models", map[string]any{ + "provider": "openai", "base_url": "http://models.example/v1", "api_key": "sk-test", + }) + if errMsg, _ := resp["error"].(string); resp["ok"] != false || errMsg != "endpoint URL host is not allowed" { + t.Errorf("%s: a name resolving to %s answered %v, want the host refused", name, a, resp) + } + } + } + // The addresses next to each range stay public. + for _, a := range []string{"100.63.255.255", "100.128.0.1", "198.17.255.255", "198.20.0.1", "192.0.1.1", "93.184.216.34", "64:ff9a::1", "2001:4860:4860::8888", "2003::1"} { + if s.isBlockedModelAddr(netip.MustParseAddr(a), false) { + t.Errorf("%s is blocked, want it allowed", a) + } + } +} + +// The dial-time guard refuses the same ranges, so a name that changes its +// answer between the check and the connection gains nothing. +func TestLLMModelsDialGuardRefusesReservedRanges(t *testing.T) { + t.Setenv(LLMPrivateEndpointsEnv, "") + g := newModelURLGuard(&Server{}) + for _, a := range []string{"100.64.0.1", "198.18.0.1", "192.0.0.1", "240.0.0.1", "64:ff9b::a9fe:a9fe", "2002:a9fe:a9fe::1"} { + if err := g.blockDial("tcp", net.JoinHostPort(a, "443"), nil); err == nil { + t.Errorf("dial to %s is allowed, want it refused", a) + } + } + if err := g.blockDial("tcp", "93.184.216.34:443", nil); err != nil { + t.Errorf("dial to a public address refused: %v", err) + } +} + +// An endpoint the operator listed in ARTEX_LLM_PRIVATE_ENDPOINTS may sit in +// carrier-grade NAT space (an overlay network) or in the benchmarking range +// (a fake-IP resolver), as it may in RFC 1918 space. The other reserved ranges +// and the metadata address inside 100.64.0.0/10 are never allowed. +func TestLLMModelsPrivateEndpointListCoversSharedRanges(t *testing.T) { + s := &Server{} + for a, wantBlocked := range map[string]bool{ + "100.64.0.1": false, + "198.18.0.1": false, + "10.0.0.1": false, + "100.100.100.200": true, // Alibaba Cloud metadata + "192.0.0.1": true, + "240.0.0.1": true, + "64:ff9b::a9fe:a9fe": true, + "2002:a9fe:a9fe::1": true, + "169.254.169.254": true, + } { + if got := s.isBlockedModelAddr(netip.MustParseAddr(a), true); got != wantBlocked { + t.Errorf("allow-listed %s: blocked=%v, want %v", a, got, wantBlocked) + } + } +} + +// A non-200 answer is reported by its status alone. Echoing the body turned +// the endpoint into a reader of whatever the contacted host returns. +func TestLLMModelsDoesNotEchoUpstreamErrorBody(t *testing.T) { + t.Setenv(LLMPrivateEndpointsEnv, "") + const secret = "internal-admin-token-5f2c" + var hits atomic.Int32 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + hits.Add(1) + w.WriteHeader(http.StatusForbidden) + _, _ = w.Write([]byte(`{"message":"denied","debug":"` + secret + `"}`)) + })) + t.Cleanup(upstream.Close) + + s := &Server{modelLookupIP: func(context.Context, string) ([]netip.Addr, error) { + return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil + }} + s.modelDialContext = dialTestServerForHosts( + map[string]string{"api.public.example": upstream.Listener.Addr().String()}, + (&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext, + ) + for _, provider := range []string{"openai", "anthropic", "ollama"} { + resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{ + "provider": provider, "base_url": "http://api.public.example/v1", "api_key": "sk-public", + }) + errMsg, _ := resp["error"].(string) + if resp["ok"] != false || !strings.Contains(errMsg, "403") { + t.Errorf("%s: answer %v, want ok=false with the status", provider, resp) + } + if strings.Contains(errMsg, secret) || strings.Contains(errMsg, "debug") || strings.Contains(errMsg, "denied") { + t.Errorf("%s: the upstream body was echoed: %q", provider, errMsg) + } + } + if hits.Load() == 0 { + t.Fatal("the upstream was never contacted, so the test proves nothing") + } +} diff --git a/server/server_mgmt.go b/server/server_mgmt.go index 446ed5f16..426f12e53 100644 --- a/server/server_mgmt.go +++ b/server/server_mgmt.go @@ -2389,6 +2389,32 @@ func sameRedirectHost(a, b *url.URL) bool { return ha != "" && ha == hb } +// Ranges that are not public Internet addresses and that +// intercept.IsNonPublicAddr does not cover. The shared ones are in use on +// private networks (overlay networks, fake-IP resolvers), so an allow-listed +// endpoint may sit there; the reserved ones are never a model endpoint. +var ( + modelSharedPrefixes = []netip.Prefix{ + netip.MustParsePrefix("100.64.0.0/10"), // carrier-grade NAT (RFC 6598) + netip.MustParsePrefix("198.18.0.0/15"), // benchmarking (RFC 2544) + } + modelReservedPrefixes = []netip.Prefix{ + netip.MustParsePrefix("192.0.0.0/24"), // IETF protocol assignments (RFC 6890) + netip.MustParsePrefix("240.0.0.0/4"), // reserved, and the limited broadcast address + netip.MustParsePrefix("64:ff9b::/96"), // NAT64: the low 32 bits name an IPv4 host + netip.MustParsePrefix("2002::/16"), // 6to4: bits 16 to 47 name an IPv4 host + } +) + +func prefixesContain(prefixes []netip.Prefix, addr netip.Addr) bool { + for _, p := range prefixes { + if p.Contains(addr) { + return true + } + } + return false +} + func (s *Server) isBlockedModelAddr(addr netip.Addr, allowPrivate bool) bool { if !addr.IsValid() { return true @@ -2397,18 +2423,19 @@ func (s *Server) isBlockedModelAddr(addr netip.Addr, allowPrivate bool) bool { if s != nil && s.allowLoopbackModels && addr.IsLoopback() { return false } - if !intercept.IsNonPublicAddr(addr) { + shared := prefixesContain(modelSharedPrefixes, addr) + if !intercept.IsNonPublicAddr(addr) && !shared && !prefixesContain(modelReservedPrefixes, addr) { return false } if allowPrivate { - // Allow-listed self-hosted endpoints may resolve to loopback or RFC1918 / - // unique-local private addresses, but never link-local, unspecified, - // multicast, or instance-metadata addresses (such as 169.254.169.254 or - // fd00:ec2::254). + // Allow-listed self-hosted endpoints may resolve to loopback, RFC1918 / + // unique-local private addresses, or the shared ranges above, but never + // link-local, unspecified, multicast, reserved, or instance-metadata + // addresses (such as 169.254.169.254, 100.100.100.200 or fd00:ec2::254). if addr.IsLoopback() { return false } - if addr.IsPrivate() && intercept.ValidateExemptionHost(addr.String()) == nil { + if (addr.IsPrivate() || shared) && intercept.ValidateExemptionHost(addr.String()) == nil { return false } } @@ -2756,7 +2783,9 @@ func (s *Server) pgListModels(w http.ResponseWriter, r *http.Request) { body, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<20)) resp.Body.Close() if resp.StatusCode != http.StatusOK { - lastErr = fmt.Sprintf("API returned %d: %s", resp.StatusCode, string(body[:min(len(body), 512)])) + // The status only: echoing the body would let the caller read what + // any host this endpoint can reach answers. + lastErr = fmt.Sprintf("API returned %d %s (the response body is not shown)", resp.StatusCode, http.StatusText(resp.StatusCode)) continue } // Both OpenAI and Anthropic return {"data": [{"id": "..."},...]}. From 6bb2fe199e313f6ee20f9d0e6ae86bf7141ed9ab Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:34:25 +0100 Subject: [PATCH 6/6] fix(server): judge NAT64 and 6to4 addresses by their embedded IPv4 POST /api/llm/models refused 64:ff9b::/96 and 2002::/16 as a whole. A DNS64 resolver answers with a 64:ff9b::/96 address for every IPv4-only name, so on a NAT64 network every public provider was refused and no setting brought it back. An address in 64:ff9b::/96 (the IPv4 address in the low 32 bits) or in 2002::/16 (the IPv4 address in bits 16 to 47) is now judged as the IPv4 address it embeds, by the rule a bare IPv4 address gets: private, loopback, link-local, metadata, reserved, carrier-grade NAT and unspecified addresses stay refused, a public one is accepted. The rule is the same for literal hosts, resolved names, redirects and at dial time, and an endpoint listed in ARTEX_LLM_PRIVATE_ENDPOINTS gets behind such an address what it gets as a bare IPv4 address. --- SECURITY.md | 2 +- server/llm_models_embedded_ipv4_test.go | 179 ++++++++++++++++++++++++ server/llm_models_reserved_test.go | 8 +- server/server_mgmt.go | 22 ++- 4 files changed, 204 insertions(+), 7 deletions(-) create mode 100644 server/llm_models_embedded_ipv4_test.go diff --git a/SECURITY.md b/SECURITY.md index f84a5c7c2..adbeb3bcb 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -45,7 +45,7 @@ Accepted gap: when a task has no allow-list, there is no allow rule to apply (bl ## Outbound Model Listing (`POST /api/llm/models`) -`POST /api/llm/models` fetches model lists from the provider endpoint and optional HTTP proxy supplied in the request. By default, ARTEX allows only `http` and `https` URLs whose hosts resolve exclusively to public IP addresses (blocking loopback, RFC1918 private networks, IPv6 unique-local `fc00::/7`, link-local/cloud metadata such as `169.254.169.254`, unspecified, and multicast addresses, carrier-grade NAT `100.64.0.0/10`, the benchmarking range `198.18.0.0/15`, `192.0.0.0/24`, the reserved block `240.0.0.0/4`, NAT64 `64:ff9b::/96` and 6to4 `2002::/16`), and enforces the same checks on redirects and at dial time. A non-200 answer from the endpoint is reported by its status only; its body is not returned. The built-in local Ollama default (`http://127.0.0.1:11434/v1`) is permitted only when the request leaves `base_url` empty for the `ollama` provider. +`POST /api/llm/models` fetches model lists from the provider endpoint and optional HTTP proxy supplied in the request. By default, ARTEX allows only `http` and `https` URLs whose hosts resolve exclusively to public IP addresses (blocking loopback, RFC1918 private networks, IPv6 unique-local `fc00::/7`, link-local/cloud metadata such as `169.254.169.254`, unspecified, and multicast addresses, carrier-grade NAT `100.64.0.0/10`, the benchmarking range `198.18.0.0/15`, `192.0.0.0/24` and the reserved block `240.0.0.0/4`; a NAT64 `64:ff9b::/96` or 6to4 `2002::/16` address is judged by the IPv4 address it embeds), and enforces the same checks on redirects and at dial time. A non-200 answer from the endpoint is reported by its status only; its body is not returned. The built-in local Ollama default (`http://127.0.0.1:11434/v1`) is permitted only when the request leaves `base_url` empty for the `ollama` provider. Operators running self-hosted LLM gateways or Ollama on a LAN or Docker bridge can allow specific endpoints with `ARTEX_LLM_PRIVATE_ENDPOINTS`, a comma-separated list of exact `host:port` entries (for example, `127.0.0.1:11434,host.docker.internal:11434,192.168.1.10:11434`). A URL whose `host:port` matches an entry may resolve to a loopback or private address, or to an address in `100.64.0.0/10` or `198.18.0.0/15` (an overlay network, a fake-IP resolver), while scheme restrictions, link-local/cloud-metadata blocks, the other reserved ranges above, and redirect validation still apply; everything else stays refused. Leaving `ARTEX_LLM_PRIVATE_ENDPOINTS` unset or empty keeps the strict public-only policy. diff --git a/server/llm_models_embedded_ipv4_test.go b/server/llm_models_embedded_ipv4_test.go new file mode 100644 index 000000000..80aa9c942 --- /dev/null +++ b/server/llm_models_embedded_ipv4_test.go @@ -0,0 +1,179 @@ +package server + +import ( + "context" + "net" + "net/http" + "net/http/httptest" + "net/netip" + "sync/atomic" + "testing" +) + +// A NAT64 address (64:ff9b::/96, the IPv4 address in the low 32 bits) and a +// 6to4 address (2002::/16, the IPv4 address in bits 16 to 47) reach the IPv4 +// host they embed, so they are judged as that host is: a DNS64 resolver hands +// out 64:ff9b::/96 answers for every public IPv4-only provider. +func TestLLMModelsJudgesNAT64And6to4ByEmbeddedIPv4(t *testing.T) { + t.Setenv(LLMPrivateEndpointsEnv, "") + s := &Server{} + for _, tc := range []struct { + addr, what string + wantBlocked bool + }{ + {"64:ff9b::a9fe:a9fe", "NAT64 of 169.254.169.254 (metadata)", true}, + {"2002:a9fe:a9fe::1", "6to4 of 169.254.169.254 (metadata)", true}, + {"64:ff9b::5db8:d822", "NAT64 of 93.184.216.34 (public)", false}, + {"2002:5db8:d822::1", "6to4 of 93.184.216.34 (public)", false}, + {"64:ff9b::7f00:1", "NAT64 of 127.0.0.1 (loopback)", true}, + {"2002:7f00:1::", "6to4 of 127.0.0.1 (loopback)", true}, + {"64:ff9b::a00:1", "NAT64 of 10.0.0.1 (private)", true}, + {"2002:c0a8:101::1", "6to4 of 192.168.1.1 (private)", true}, + {"64:ff9b::6440:1", "NAT64 of 100.64.0.1 (carrier-grade NAT)", true}, + {"2002:c612:1::1", "6to4 of 198.18.0.1 (benchmarking)", true}, + {"64:ff9b::6464:64c8", "NAT64 of 100.100.100.200 (metadata)", true}, + {"2002:f000:1::1", "6to4 of 240.0.0.1 (reserved)", true}, + {"64:ff9b::c000:1", "NAT64 of 192.0.0.1 (IETF protocol assignments)", true}, + {"64:ff9b::", "NAT64 of 0.0.0.0 (unspecified)", true}, + {"2002::1", "6to4 of 0.0.0.0 (unspecified)", true}, + {"2002:0808:0808:ffff:ffff:ffff:ffff:ffff", "6to4 of 8.8.8.8 (public), any low bits", false}, + {"2001:4860:4860::8888", "plain public IPv6", false}, + {"2606:4700:4700::1111", "plain public IPv6", false}, + // The digits 2002 a9fe a9fe appear, but the first 16 bits are not 2002. + {"200:2a9f:ea9f:e000::1", "2002 shifted by four bits, outside 2002::/16", false}, + {"20:2a9:fea9:fe00::1", "2002 shifted by eight bits, outside 2002::/16", false}, + {"2001:2002:a9fe:a9fe::1", "2002 in the second group, outside 2002::/16", false}, + {"2003:a9fe:a9fe::1", "the prefix next to 2002::/16", false}, + // Inside 64:ff9b::/64 but outside the /96 that carries an IPv4 address. + {"64:ff9b::1:a9fe:a9fe", "outside 64:ff9b::/96", false}, + } { + t.Run(tc.addr, func(t *testing.T) { + if got := s.isBlockedModelAddr(netip.MustParseAddr(tc.addr), false); got != tc.wantBlocked { + t.Errorf("%s: blocked=%v, want %v", tc.what, got, tc.wantBlocked) + } + }) + } +} + +// The dial-time guard applies the same rule to the address it is handed. +func TestLLMModelsDialGuardJudgesEmbeddedIPv4(t *testing.T) { + t.Setenv(LLMPrivateEndpointsEnv, "") + g := newModelURLGuard(&Server{}) + for a, wantRefused := range map[string]bool{ + "64:ff9b::a9fe:a9fe": true, + "2002:a9fe:a9fe::1": true, + "64:ff9b::7f00:1": true, + "2002:7f00:1::": true, + "64:ff9b::5db8:d822": false, + "2002:5db8:d822::1": false, + } { + t.Run(a, func(t *testing.T) { + err := g.blockDial("tcp", net.JoinHostPort(a, "443"), nil) + if refused := err != nil; refused != wantRefused { + t.Errorf("dial to %s: refused=%v (%v), want %v", a, refused, err, wantRefused) + } + }) + } +} + +// The URL check runs on the endpoint and on every redirect target: a literal +// or a resolved NAT64 or 6to4 address passes when its IPv4 host is public and +// is refused when it is not. +func TestLLMModelsURLGuardJudgesEmbeddedIPv4(t *testing.T) { + t.Setenv(LLMPrivateEndpointsEnv, "") + answers := map[string][]netip.Addr{ + "dns64.example": {netip.MustParseAddr("64:ff9b::5db8:d822")}, + "metadata.example": {netip.MustParseAddr("93.184.216.34"), netip.MustParseAddr("64:ff9b::a9fe:a9fe")}, + "loopback.example": {netip.MustParseAddr("2002:7f00:1::")}, + } + g := newModelURLGuard(&Server{modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) { + return answers[host], nil + }}) + for _, role := range []string{"endpoint", "redirect"} { + for rawURL, wantRefused := range map[string]bool{ + "https://[64:ff9b::5db8:d822]/v1": false, + "https://[2002:5db8:d822::1]/v1": false, + "https://dns64.example/v1": false, + "http://[64:ff9b::a9fe:a9fe]/latest/meta-data/": true, + "http://[2002:a9fe:a9fe::1]/latest/meta-data/": true, + "http://[64:ff9b::7f00:1]:8080/": true, + "http://[2002:7f00:1::]:8080/": true, + "https://metadata.example/v1": true, + "https://loopback.example/v1": true, + "https://[2001:4860:4860::8888]/v1": false, + "https://[200:2a9f:ea9f:e000::1]/latest/metadata": false, + } { + t.Run(role+" "+rawURL, func(t *testing.T) { + _, err := g.validateURL(context.Background(), rawURL, role) + if refused := err != nil; refused != wantRefused { + t.Errorf("refused=%v (%v), want %v", refused, err, wantRefused) + } + }) + } + } +} + +// End to end: a provider reached through a NAT64 address is listed, and a +// NAT64 address of the metadata host is refused before any request. +func TestLLMModelsListsThroughNAT64Address(t *testing.T) { + t.Setenv(LLMPrivateEndpointsEnv, "") + var hits atomic.Int32 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + hits.Add(1) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":[{"id":"gpt-test"}]}`)) + })) + t.Cleanup(upstream.Close) + + s := &Server{} + // The route is taken only for the NAT64 address itself; every other dial + // goes through the production guard. + s.modelDialContext = dialTestServerForHosts( + map[string]string{"[64:ff9b::5db8:d822]:80": upstream.Listener.Addr().String()}, + (&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext, + ) + resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{ + "provider": "openai", "base_url": "http://[64:ff9b::5db8:d822]/v1", "api_key": "sk-test", + }) + rawModels, _ := resp["models"].([]any) + if resp["ok"] != true || hits.Load() != 1 || len(rawModels) != 1 || rawModels[0] != "gpt-test" { + t.Errorf("NAT64 address of a public host: answer %v after %d requests, want the model list", resp, hits.Load()) + } + + hits.Store(0) + resp = postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{ + "provider": "openai", "base_url": "http://[64:ff9b::a9fe:a9fe]/v1", "api_key": "sk-test", + }) + if resp["ok"] != false || resp["error"] != "endpoint URL host is not allowed" || hits.Load() != 0 { + t.Errorf("NAT64 address of the metadata host: answer %v after %d requests, want a refusal and no request", resp, hits.Load()) + } +} + +// An endpoint listed in ARTEX_LLM_PRIVATE_ENDPOINTS gets, behind a NAT64 or +// 6to4 address, what it gets as a bare IPv4 address: private, loopback and +// shared ranges are allowed, link-local, metadata and reserved ones never. +func TestLLMModelsPrivateEndpointListCoversEmbeddedIPv4(t *testing.T) { + s := &Server{} + for _, tc := range []struct { + addr, what string + wantBlocked bool + }{ + {"64:ff9b::a00:1", "NAT64 of 10.0.0.1", false}, + {"2002:a00:1::1", "6to4 of 10.0.0.1", false}, + {"64:ff9b::7f00:1", "NAT64 of 127.0.0.1", false}, + {"64:ff9b::6440:1", "NAT64 of 100.64.0.1", false}, + {"2002:c612:1::1", "6to4 of 198.18.0.1", false}, + {"64:ff9b::a9fe:a9fe", "NAT64 of 169.254.169.254", true}, + {"2002:a9fe:a9fe::1", "6to4 of 169.254.169.254", true}, + {"64:ff9b::6464:64c8", "NAT64 of 100.100.100.200", true}, + {"2002:f000:1::1", "6to4 of 240.0.0.1", true}, + {"64:ff9b::c000:1", "NAT64 of 192.0.0.1", true}, + {"64:ff9b::", "NAT64 of 0.0.0.0", true}, + } { + t.Run(tc.addr, func(t *testing.T) { + if got := s.isBlockedModelAddr(netip.MustParseAddr(tc.addr), true); got != tc.wantBlocked { + t.Errorf("allow-listed %s: blocked=%v, want %v", tc.what, got, tc.wantBlocked) + } + }) + } +} diff --git a/server/llm_models_reserved_test.go b/server/llm_models_reserved_test.go index 884d85038..2a2c58315 100644 --- a/server/llm_models_reserved_test.go +++ b/server/llm_models_reserved_test.go @@ -13,15 +13,15 @@ import ( // Ranges that are not public Internet addresses but are neither RFC 1918 nor // loopback: carrier-grade NAT, the benchmarking range, the IETF protocol -// assignments, the reserved class E block, and the two IPv6 prefixes that -// embed an IPv4 address (NAT64, 6to4). +// assignments, the reserved class E block, and NAT64 and 6to4 addresses that +// embed a non-public IPv4 address. var reservedModelAddrs = map[string][]string{ "cgnat": {"100.64.0.1", "100.127.255.254", "::ffff:100.64.0.1"}, "benchmark": {"198.18.0.1", "198.19.255.254"}, "ietf_protocol": {"192.0.0.1", "192.0.0.170"}, "reserved": {"240.0.0.1", "250.1.2.3", "255.255.255.255"}, - "nat64": {"64:ff9b::a9fe:a9fe", "64:ff9b::7f00:1", "64:ff9b::5db8:d822"}, - "six_to_four": {"2002:a9fe:a9fe::1", "2002:7f00:1::1", "2002:5db8:d822::1"}, + "nat64": {"64:ff9b::a9fe:a9fe", "64:ff9b::7f00:1"}, + "six_to_four": {"2002:a9fe:a9fe::1", "2002:7f00:1::1"}, } func TestLLMModelsRefusesReservedRanges(t *testing.T) { diff --git a/server/server_mgmt.go b/server/server_mgmt.go index 426f12e53..732f99a71 100644 --- a/server/server_mgmt.go +++ b/server/server_mgmt.go @@ -2401,11 +2401,26 @@ var ( modelReservedPrefixes = []netip.Prefix{ netip.MustParsePrefix("192.0.0.0/24"), // IETF protocol assignments (RFC 6890) netip.MustParsePrefix("240.0.0.0/4"), // reserved, and the limited broadcast address - netip.MustParsePrefix("64:ff9b::/96"), // NAT64: the low 32 bits name an IPv4 host - netip.MustParsePrefix("2002::/16"), // 6to4: bits 16 to 47 name an IPv4 host } + // IPv6 prefixes whose addresses reach the IPv4 host they embed. A DNS64 + // resolver answers with NAT64 addresses for every IPv4-only name, so the + // prefixes are not blocked as a whole: the embedded address is judged. + modelNAT64Prefix = netip.MustParsePrefix("64:ff9b::/96") // the low 32 bits name an IPv4 host + model6to4Prefix = netip.MustParsePrefix("2002::/16") // bits 16 to 47 name an IPv4 host ) +// modelEmbeddedIPv4 returns the IPv4 address a NAT64 or 6to4 address embeds. +func modelEmbeddedIPv4(addr netip.Addr) (netip.Addr, bool) { + b := addr.As16() + switch { + case modelNAT64Prefix.Contains(addr): + return netip.AddrFrom4([4]byte(b[12:16])), true + case model6to4Prefix.Contains(addr): + return netip.AddrFrom4([4]byte(b[2:6])), true + } + return netip.Addr{}, false +} + func prefixesContain(prefixes []netip.Prefix, addr netip.Addr) bool { for _, p := range prefixes { if p.Contains(addr) { @@ -2420,6 +2435,9 @@ func (s *Server) isBlockedModelAddr(addr netip.Addr, allowPrivate bool) bool { return true } addr = addr.Unmap().WithZone("") + if v4, ok := modelEmbeddedIPv4(addr); ok { + addr = v4 + } if s != nil && s.allowLoopbackModels && addr.IsLoopback() { return false }