mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 09:20:53 +08:00
GHSA-prm8-vw2v-wjwh: the agent command executor ran as root in the server's container and PID namespace, so it could read /app/keys and recover the server secrets from /proc/<pid>/environ. Deliverable 1, executor identity. Agent processes (Bash, the interactive shell, custom tools, stdio MCP servers) run as the uid/gid set by -exec-uid and -exec-gid (default 10001). Custom tools use SysProcAttr.Credential. Norma starts Bash and the shell itself, so a PATH shim (the server binary named bash) drops the identity before exec; MCP stdio commands run through the same binary in drop mode. The task and chat workspace trees are shared with the executor group (setgid, group rw, umask 002). The key files and the config file become root-only, and /app/keys is 0700 in the image. An explicit -exec-uid that cannot be applied stops the start; the default logs once that same-uid isolation is unavailable when the server is not root. Deliverable 2, sealed environment. On Linux, SealProcessEnv re-executes the server once with the secret variables removed from its environment block, passing the values through an inherited pipe. The new image restores them into the Go environment, so readers see the same configuration. Off Linux it logs that the environment is not sealed. Deliverable 3, approval gate. Custom tools, MCP servers and skill files (create_/update_ of custom tool and MCP, create_skill, update_skill_file) meet built-in ask rules seeded once (intercept_persistence_rules_v1). A user allow rule cannot skip them; a user deny still wins. The rules are data the agent cannot edit. Tests: resolution and refusal of root identities, drop command wrapping, sealed-environment encoding, and an end-to-end handoff test on Linux that checks the kernel environment block and a child process hold no secret names and that the values survive (empty and multi-line included). Approval matching is tested in intercept.
99 lines
3.2 KiB
Go
99 lines
3.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/artex/mcphttp"
|
|
"github.com/Autumn-27/norma/mcp"
|
|
actool "github.com/Autumn-27/norma/tool"
|
|
)
|
|
|
|
// mcpClient is the shared surface of a connected MCP server (stdio, Streamable HTTP,
|
|
// or legacy SSE),
|
|
// so tools/list and cleanup are handled uniformly regardless of transport.
|
|
type mcpClient interface {
|
|
Tools(context.Context) ([]actool.CoreTool, error)
|
|
Close() error
|
|
}
|
|
|
|
// connectMCP dials one MCP server per its transport. Callers must Close the client.
|
|
func connectMCP(ctx context.Context, m *db.MCPServer, priv ExecPrivilege) (mcpClient, error) {
|
|
switch m.Transport {
|
|
case "stdio":
|
|
if m.Command == "" {
|
|
return nil, fmt.Errorf("stdio transport is missing a command")
|
|
}
|
|
// The stdio command runs as the executor identity (see execpriv.go).
|
|
command, args, err := execDropCommand(priv, m.Command, jsonStrSlice(m.Args))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return mcp.NewStdioClient(ctx, m.Name, command, jsonStrMap(m.Env), args...)
|
|
case "http":
|
|
if m.URL == "" {
|
|
return nil, fmt.Errorf("http transport is missing a URL")
|
|
}
|
|
// env map doubles as HTTP headers (e.g. Authorization).
|
|
return mcphttp.New(ctx, m.Name, m.URL, jsonStrMap(m.Env), m.Insecure, m.TimeoutSeconds)
|
|
case "sse":
|
|
if m.URL == "" {
|
|
return nil, fmt.Errorf("sse transport is missing a URL")
|
|
}
|
|
return mcphttp.NewSSE(ctx, m.Name, m.URL, jsonStrMap(m.Env), m.Insecure)
|
|
default:
|
|
return nil, fmt.Errorf("unknown transport %q", m.Transport)
|
|
}
|
|
}
|
|
|
|
// discoverAndCacheMCP connects to one MCP, lists its tools, and persists the tool
|
|
// names to mcp_tools_cache so the UI shows them without a live connection.
|
|
func (s *Server) discoverAndCacheMCP(ctx context.Context, m *db.MCPServer) error {
|
|
cl, err := connectMCP(ctx, m, s.execPriv)
|
|
if err != nil {
|
|
_ = s.m.pg.UpdateMCPHealth(m.ID, false, err.Error())
|
|
return err
|
|
}
|
|
defer cl.Close()
|
|
ts, err := cl.Tools(ctx)
|
|
if err != nil {
|
|
_ = s.m.pg.UpdateMCPHealth(m.ID, false, err.Error())
|
|
return err
|
|
}
|
|
_ = s.m.pg.UpdateMCPHealth(m.ID, true, "")
|
|
tools := make([]db.MCPTool, 0, len(ts))
|
|
for _, t := range ts {
|
|
tools = append(tools, db.MCPTool{Name: t.Name(), Description: t.Description()})
|
|
}
|
|
if err := s.m.pg.SaveMCPTools(m.ID, tools); err != nil {
|
|
return err
|
|
}
|
|
log.Printf("[mcp] %s discovered %d tools and cached them", m.Name, len(tools))
|
|
return nil
|
|
}
|
|
|
|
// discoverEmptyMCPsOnStartup fills the tool cache for any enabled MCP that has none
|
|
// yet (notably the seeded browser MCP on first run). Runs sequentially in one
|
|
// goroutine so we never spawn many stdio servers at once, and never blocks
|
|
// startup. Best-effort: a failure leaves the cache empty to retry next start.
|
|
func (s *Server) discoverEmptyMCPsOnStartup() {
|
|
servers, err := s.m.pg.ListMCP()
|
|
if err != nil {
|
|
log.Printf("[mcp] startup auto-discovery: failed to read the list: %v", err)
|
|
return
|
|
}
|
|
for _, m := range servers {
|
|
if !m.Enabled || len(m.Tools) > 0 {
|
|
continue
|
|
}
|
|
ctx, cancel := context.WithTimeout(s.ctx, 90*time.Second)
|
|
if err := s.discoverAndCacheMCP(ctx, m); err != nil {
|
|
log.Printf("[mcp] startup auto-discovery of %s failed: %v", m.Name, err)
|
|
}
|
|
cancel()
|
|
}
|
|
}
|