From 63432a423e93604a008165aca948fabc632c4294 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 02:34:02 -0600 Subject: [PATCH 1/2] mcpx e2e: build child env from allowlist, guard registry vars, TestMain backstop (#1992) Replace the inherit-minus-MCPX_* harness environment with a true allowlist builder in internal/testsupport (ChildEnv): the parent contributes PATH, TMPDIR, LANG/LC_*, TERM, SYSTEMROOT and the runtime-cache passthroughs only; XDG_* resolve under the test dir and everything else -- MCPX_*, XDG_*, OPENCODE_*, proxies, NODE_OPTIONS -- is dropped. newEnv builds on it. Add the registry-derived guard: every settings EnvName()/EnvAliases entry is classified as harness-set or safe-under-inherit-nothing, with a behaviour half proving the drop for each one, plus the hostile-parent doctor/settings/recipes regression test. Add the TestMain backstop: fail fast if MCPX_DAEMON_ENDPOINT or MCPX_SOCKET would reach a child, and fail if the caller's real daemon set differs at suite end. --- pkgs/mcpx/internal/e2e/e2e_test.go | 84 ++- pkgs/mcpx/internal/e2e/envguard_test.go | 598 ++++++++++++++++++ pkgs/mcpx/internal/testsupport/childenv.go | 90 +++ .../internal/testsupport/childenv_test.go | 90 +++ 4 files changed, 845 insertions(+), 17 deletions(-) create mode 100644 pkgs/mcpx/internal/e2e/envguard_test.go create mode 100644 pkgs/mcpx/internal/testsupport/childenv.go create mode 100644 pkgs/mcpx/internal/testsupport/childenv_test.go diff --git a/pkgs/mcpx/internal/e2e/e2e_test.go b/pkgs/mcpx/internal/e2e/e2e_test.go index 7eedce283a..b7fbad41e3 100644 --- a/pkgs/mcpx/internal/e2e/e2e_test.go +++ b/pkgs/mcpx/internal/e2e/e2e_test.go @@ -15,6 +15,7 @@ import ( "path/filepath" "slices" "sort" + "strconv" "strings" "sync" "syscall" @@ -22,6 +23,7 @@ import ( "time" "github.com/developing-today/code/pkgs/mcpx/internal/cli" + "github.com/developing-today/code/pkgs/mcpx/internal/daemon" "github.com/developing-today/code/pkgs/mcpx/internal/elicit" "github.com/developing-today/code/pkgs/mcpx/internal/testsupport" @@ -57,7 +59,12 @@ func newEnv(t *testing.T, cfgBody string) *env { dir: dir, mcpx: mcpx, fake: fake, - envVars: append(withoutMCPXVars(os.Environ()), + // The child environment is built from an allowlist, not inherited: + // ChildEnv drops everything the test does not control, so no caller + // variable reaches the binary whatever the caller's shell exported + // (#1992). What the test does control is listed here and wins, + // because os/exec keeps the last value for a duplicated key. + envVars: testsupport.ChildEnv(os.Environ(), dir, // Never the real registry. A test that reaches the internet // fails wherever the internet is slow, which is every CI runner // -- `/v1/registry/search` timed out there while passing on a @@ -3195,25 +3202,68 @@ func TestConcurrentRunsOfOneScriptKeepTheirOwnLauncher(t *testing.T) { } } -// withoutMCPXVars drops every MCPX_ variable the developer running the suite -// happens to have exported. +// TestMain is the backstop for #1992: the suite must never disturb the +// caller's real daemons, whatever the caller's shell exported. // -// The harness sets the ones it needs a line below, and last-wins in exec's -// environment would cover those. It does not cover the ones it deliberately -// leaves unset: a test that asserts a knob is off reads the default only if -// nothing in the ambient environment turned it on, and MCPX_TRACE=1 in the -// shell of somebody debugging mcpx is exactly the case. That fails on one -// machine and nowhere else, which is the worst kind. -// -// PATH and everything else is kept, because the child needs a runtime. -func withoutMCPXVars(env []string) []string { - out := make([]string, 0, len(env)) - for _, kv := range env { - if strings.HasPrefix(kv, "MCPX_") { - continue +// First it fails fast if the parent carries a caller-daemon pointer +// (MCPX_DAEMON_ENDPOINT or MCPX_SOCKET) that would reach a test child: with +// the allowlist builder that cannot happen, so reaching this failure means +// somebody widened the allowlist and every test after it would run against +// the user's daemon instead of its own. Then it records the caller's real +// daemon set, runs the suite, and fails if that set differs at the end -- +// the one assertion that the suite left the machine as it found it, which +// also catches a regression of the #182 daemon-leak fix. +func TestMain(m *testing.M) { + os.Exit(guardedMain(m)) +} + +func guardedMain(m *testing.M) int { + before := callerDaemonSet() + for _, name := range []string{"MCPX_DAEMON_ENDPOINT", "MCPX_SOCKET"} { + if v := os.Getenv(name); v != "" && childWouldInherit(name) { + fmt.Fprintf(os.Stderr, + "e2e: refusing to run: the parent exports %s=%q and the harness would pass it to test children, "+ + "pointing the suite at the caller's daemon (#1992). Unset it, or fix the allowlist in internal/testsupport.\n", + name, v) + return 1 + } + } + code := m.Run() + after := callerDaemonSet() + if !slices.Equal(before, after) { + fmt.Fprintf(os.Stderr, + "e2e: the suite disturbed the caller's real daemons (#1992): before=[%s] after=[%s]. "+ + "Each test owns its MCPX_STATE_DIR, so a difference means a child reached the real state dir; "+ + "a daemon you started or stopped yourself mid-suite reads the same way.\n", + strings.Join(before, " "), strings.Join(after, " ")) + return 1 + } + return code +} + +// childWouldInherit reports whether a parent variable would reach a test +// child through the allowlist builder. The probe carries no harness extras, +// which can only override more, never less: if the name survives here it +// survives in every test. +func childWouldInherit(name string) bool { + prefix := name + "=" + for _, kv := range testsupport.ChildEnv(os.Environ(), "") { + if strings.HasPrefix(kv, prefix) { + return true } - out = append(out, kv) } + return false +} + +// callerDaemonSet snapshots the caller's real daemons: the info records in +// the state dir resolved from the parent environment, which no test may +// touch because every test sets its own MCPX_STATE_DIR. +func callerDaemonSet() []string { + var out []string + for _, d := range daemon.ResolvePaths().ListDaemons() { + out = append(out, d.Socket+" pid="+strconv.Itoa(d.PID)) + } + slices.Sort(out) return out } diff --git a/pkgs/mcpx/internal/e2e/envguard_test.go b/pkgs/mcpx/internal/e2e/envguard_test.go new file mode 100644 index 0000000000..a4d2376437 --- /dev/null +++ b/pkgs/mcpx/internal/e2e/envguard_test.go @@ -0,0 +1,598 @@ +package e2e_test + +import ( + "path/filepath" + "regexp" + "strings" + "testing" + + "github.com/developing-today/code/pkgs/mcpx/internal/settings" +) + +// The guard for #1992, Approach C: every MCPX_* variable the settings +// registry declares reaches a test child either as a value the harness chose +// or not at all. A parent value leaking through would run the suite under +// the developer's settings -- or, for the daemon endpoint and socket, +// against the developer's real daemon -- and fail on one machine and nowhere +// else. +// +// A new setting is covered the day it is added: TestEveryRegistryVariable +// below fails until its variables are classified here, into exactly one of +// the two tables. "Set by the harness" means the harness sets the variable +// somewhere in this package (newEnv for every child, or a test's withEnv for +// its own), so a test that depends on the value chooses it. "Safe" does not +// mean inherited: the allowlist builder drops every MCPX_* variable from the +// parent, so safe means safe under the harness's inherit-nothing policy -- +// the variable keeps its default unless the harness sets it, and no caller +// value can change what a test observes. The behaviour half +// (TestTheHarnessBuildsChildrenFromTheAllowlist) proves the drop for every +// name in both tables at once, so a stale entry in either direction fails +// loudly instead of drifting. + +// harnessSetMCPX is every registry variable the harness sets: newEnv for +// every child, or a test's withEnv/append for its own. +var harnessSetMCPX = map[string]struct{}{ + "MCPX_ARTIFACTS_MAX_BYTES": {}, + "MCPX_AUTONOMY_MAX": {}, + "MCPX_AUTOSTART_ARGS": {}, + "MCPX_AUTOSTART_IDLE_EXIT": {}, + "MCPX_CACHE_DIR": {}, + "MCPX_CONFIG": {}, + "MCPX_DAEMON_AUTOSTART": {}, + "MCPX_DAEMON_ENDPOINT": {}, + "MCPX_DAEMON_INLINE": {}, + "MCPX_DAEMON_WARM": {}, + "MCPX_DAEMON_WATCH_CONFIG": {}, + "MCPX_DEPS_MANIFEST": {}, + "MCPX_DEPS_PACKAGES": {}, + "MCPX_DEPS_SOURCE": {}, + "MCPX_ELICIT_ASK_TIMEOUT": {}, + "MCPX_ELICIT_CONFIRM_DESTRUCTIVE": {}, + "MCPX_HOOKS_AUTONOMY": {}, + "MCPX_LOGGING_FILE": {}, + "MCPX_MCP_PASSTHROUGH": {}, + "MCPX_MCP_SPEC": {}, + "MCPX_OUTPUT_JSON": {}, + "MCPX_PATHS_ADAPTERS": {}, + "MCPX_PATHS_DEPS": {}, + "MCPX_PATHS_STATE": {}, + "MCPX_PERMISSIONS": {}, + "MCPX_PLUMBING_STRICT_UNKNOWN_KEYS": {}, + "MCPX_POLICY_WRITE": {}, + "MCPX_POOL_MAX": {}, + "MCPX_POOL_SCOPE": {}, + "MCPX_POOL_SHARING": {}, + "MCPX_PRESET": {}, + "MCPX_PROMPT_AUTONOMY": {}, + "MCPX_PROTO_NATIVE": {}, + "MCPX_PROTO_TASKS_POLL_INTERVAL": {}, + "MCPX_REGISTRY_LIMIT": {}, + "MCPX_REGISTRY_MAX_PAGES": {}, + "MCPX_REGISTRY_PAGE_SIZE": {}, + "MCPX_REGISTRY_URL": {}, + "MCPX_SCRIPT_CAPTURE_CONSOLE": {}, + "MCPX_SCRIPT_CWD": {}, + "MCPX_SCRIPT_ENV": {}, + "MCPX_SCRIPT_INTERPRETER": {}, + "MCPX_SCRIPT_PERMISSIONS": {}, + "MCPX_SCRIPT_TYPECHECK": {}, + "MCPX_SEARCH_LIMIT": {}, + "MCPX_SPEC_LENIENT": {}, + "MCPX_STATE_DIR": {}, + "MCPX_TRACE": {}, + "MCPX_TRANSPORT_ALLOWED_ORIGINS": {}, + "MCPX_UPSTREAM_ERA_CACHE": {}, + "MCPX_UPSTREAM_PROBE_TIMEOUT": {}, + "MCPX_UPSTREAM_PROTOCOL": {}, +} + +// safeMCPX is every other registry variable: safe under the harness's +// inherit-nothing policy, because the allowlist builder never inherits a +// parent MCPX_* value and the behaviour test below proves the drop for each +// one. A variable the harness must control belongs in harnessSetMCPX, not +// here. +var safeMCPX = map[string]struct{}{ + "MCPX_ARTIFACTS_CHUNK_BYTES": {}, + "MCPX_ARTIFACTS_DELIVERY": {}, + "MCPX_ARTIFACTS_DIR": {}, + "MCPX_ARTIFACTS_ENABLED": {}, + "MCPX_ARTIFACTS_GC_INTERVAL": {}, + "MCPX_ARTIFACTS_IDLE_TTL": {}, + "MCPX_ARTIFACTS_INLINE_MAX_BYTES": {}, + "MCPX_ARTIFACTS_INTERCEPT_IMAGES": {}, + "MCPX_ARTIFACTS_MAX_AGE": {}, + "MCPX_ARTIFACTS_QUOTA": {}, + "MCPX_ARTIFACTS_TTL": {}, + "MCPX_AUTOSTART_BIN": {}, + "MCPX_AUTOSTART_CONNECT_TIMEOUT": {}, + "MCPX_AUTOSTART_LOG_TAIL": {}, + "MCPX_AUTOSTART_PING_TIMEOUT": {}, + "MCPX_AUTOSTART_POLL_INTERVAL": {}, + "MCPX_CATALOG_BIAS": {}, + "MCPX_CATALOG_BUDGET": {}, + "MCPX_CATALOG_INSTRUCTIONS": {}, + "MCPX_CATALOG_PINNED_TOOLS": {}, + "MCPX_COMPLETION_MAX_VALUES": {}, + "MCPX_COMPLETION_PER_MINUTE": {}, + "MCPX_COMPLETION_SERVER_PER_MINUTE": {}, + "MCPX_DAEMON_ADDRESS": {}, + "MCPX_DAEMON_IDLE_EXIT": {}, + "MCPX_DAEMON_INLINE_START_POLL": {}, + "MCPX_DAEMON_INLINE_START_TIMEOUT": {}, + "MCPX_DAEMON_LEASE_TTL": {}, + "MCPX_DAEMON_PORT": {}, + "MCPX_DAEMON_PROBE_TIMEOUT": {}, + "MCPX_DAEMON_REAP_INTERVAL": {}, + "MCPX_DAEMON_REFRESH_TIMEOUT": {}, + "MCPX_DAEMON_ROOTS": {}, + "MCPX_DAEMON_SAVE_INTERVAL": {}, + "MCPX_DAEMON_SOCKET_PROBE_TIMEOUT": {}, + "MCPX_DAEMON_TAKEOVER_TIMEOUT": {}, + "MCPX_DAEMON_TSNET_HOSTNAME": {}, + "MCPX_DAEMON_TSNET_UP_TIMEOUT": {}, + "MCPX_DAEMON_UI": {}, + "MCPX_DAEMON_UI_DIR": {}, + "MCPX_DAEMON_WARM_TIMEOUT": {}, + "MCPX_DEPS_AUTO_INSTALL": {}, + "MCPX_DIAGNOSE_HISTORY": {}, + "MCPX_DIAGNOSE_PREFLIGHT": {}, + "MCPX_DOCTOR_TIMEOUT": {}, + "MCPX_ELICIT_DISAMBIGUATE": {}, + "MCPX_ELICIT_DISAMBIGUATE_DEFAULT": {}, + "MCPX_ELICIT_MAX_AGE": {}, + "MCPX_ELICIT_PENDING_LIMIT": {}, + "MCPX_ELICIT_TTL": {}, + "MCPX_EMBEDDINGS_API_KEY": {}, + "MCPX_EMBEDDINGS_BACKEND": {}, + "MCPX_EMBEDDINGS_MODEL": {}, + "MCPX_EMBEDDINGS_URL": {}, + "MCPX_EMBEDDINGS_WASM_PATH": {}, + "MCPX_EVENTS_HISTORY": {}, + "MCPX_EVENTS_RECONNECT": {}, + "MCPX_EXEC_MAX_OUTPUT_BYTES": {}, + "MCPX_EXEC_OUTPUT": {}, + "MCPX_EXEC_TIMEOUT": {}, + "MCPX_EXEC_WHERE": {}, + "MCPX_HOOKS": {}, + "MCPX_HOOKS_TIMEOUT": {}, + "MCPX_HTTP_BODY_LIMIT": {}, + "MCPX_HTTP_CALL_BODY_LIMIT": {}, + "MCPX_HTTP_CONTROL_BODY_LIMIT": {}, + "MCPX_HTTP_IDLE_CONNS": {}, + "MCPX_HTTP_IDLE_CONN_TIMEOUT": {}, + "MCPX_HTTP_READ_HEADER_TIMEOUT": {}, + "MCPX_HTTP_REMOTE_IDLE_CONNS": {}, + "MCPX_HTTP_REQUEST_TIMEOUT": {}, + "MCPX_HTTP_SHUTDOWN_GRACE": {}, + "MCPX_HTTP_SSE_PING": {}, + "MCPX_HTTP_SSE_RETRY": {}, + "MCPX_HTTP_STREAM_BUFFER_INIT": {}, + "MCPX_HTTP_STREAM_BUFFER_MAX": {}, + "MCPX_LOGGING_DIR": {}, + "MCPX_LOGGING_FORMAT": {}, + "MCPX_LOGGING_INCLUDE": {}, + "MCPX_LOGGING_KEEP": {}, + "MCPX_LOGGING_LEVEL": {}, + "MCPX_LOGGING_MAX_AGE": {}, + "MCPX_LOGGING_MAX_BYTES": {}, + "MCPX_LOGGING_MAX_LINES": {}, + "MCPX_LOGGING_SOURCE": {}, + "MCPX_LOG_LEVEL": {}, + "MCPX_LOGSTORE_FOLLOW_BACKLOG": {}, + "MCPX_LOGSTORE_QUERY_LIMIT": {}, + "MCPX_MCP_PAGE_SIZE": {}, + "MCPX_MODEL_API_KEY_ENV": {}, + "MCPX_MODEL_BASE_URL": {}, + "MCPX_MODEL_BUDGET": {}, + "MCPX_MODEL_DEFAULT": {}, + "MCPX_MODEL_PROVIDER": {}, + "MCPX_MODEL_TIMEOUT": {}, + "MCPX_OUTPUT_COLOR": {}, + "MCPX_PATHS_APIS": {}, + "MCPX_PATHS_CACHE": {}, + "MCPX_PATHS_CONFIG": {}, + "MCPX_PATHS_PLACEHOLDERS": {}, + "MCPX_PATHS_PLUGINS": {}, + "MCPX_PATHS_SCRIPTS": {}, + "MCPX_PLUGIN_ANNOTATE": {}, + "MCPX_PLUGIN_BACKEND": {}, + "MCPX_PLUGIN_BIN": {}, + "MCPX_PLUGIN_BIN_ARGS": {}, + "MCPX_PLUGIN_DAEMON_TOOLS": {}, + "MCPX_PLUGIN_DISCOVERY_RETRY": {}, + "MCPX_PLUGIN_ENV": {}, + "MCPX_PLUGIN_HEADLESS": {}, + "MCPX_PLUGIN_INSTRUCTIONS": {}, + "MCPX_PLUGIN_REMEMBER": {}, + "MCPX_PLUGIN_TOOLS": {}, + "MCPX_PLUGIN_TOOL_TIMING": {}, + "MCPX_PLUMBING_ALLOW_TS_JS_OVERLAP": {}, + "MCPX_PLUMBING_INDEX_ON_QUERY": {}, + "MCPX_PLUMBING_LAUNCHER_PLACEHOLDER_REPEAT": {}, + "MCPX_PLUMBING_SOURCE_DIR_ALLOWED": {}, + "MCPX_PLUMBING_SOURCE_DIR_RECURSIVE": {}, + "MCPX_PLUMBING_SOURCE_PROBE_PATHS": {}, + "MCPX_PLUMBING_VALIDATE_PATHS": {}, + "MCPX_POOL_CALL_TIMEOUT": {}, + "MCPX_POOL_IDLE_TIMEOUT": {}, + "MCPX_POOL_MIN": {}, + "MCPX_POOL_START_TIMEOUT": {}, + "MCPX_PRESETS": {}, + "MCPX_PROMPT_CATALOG_BUDGET": {}, + "MCPX_PROMPT_MAX_TOKENS": {}, + "MCPX_PROMPT_RUN_TIMEOUT": {}, + "MCPX_PROMPT_SAMPLE": {}, + "MCPX_PROMPT_SAMPLE_TIMEOUT": {}, + "MCPX_PROTO_ASK_POLL": {}, + "MCPX_PROTO_ASK_ROUNDS": {}, + "MCPX_PROTO_ASK_TIMEOUT": {}, + "MCPX_PROTO_ASK_TTL": {}, + "MCPX_PROTO_ELICITATION_PER_MINUTE": {}, + "MCPX_PROTO_MCP_PATH": {}, + "MCPX_PROTO_MESSAGES_LIST_MAX_AGE": {}, + "MCPX_PROTO_MESSAGES_READ_MAX_AGE": {}, + "MCPX_PROTO_MESSAGES_TASK_AFTER": {}, + "MCPX_PROTO_SAMPLING_PER_MINUTE": {}, + "MCPX_PROTO_SAMPLING_SESSION_PER_MINUTE": {}, + "MCPX_PROTO_SERVE_MCP": {}, + "MCPX_PROTO_SESSION_IDLE": {}, + "MCPX_PROTO_STATE_TTL": {}, + "MCPX_PROTO_TASKS_CANCEL_GRACE": {}, + "MCPX_PROTO_TASKS_MAX_PER_REQUESTOR": {}, + "MCPX_PROTO_TASKS_MAX_TTL": {}, + "MCPX_PROTO_TASKS_PER_MINUTE": {}, + "MCPX_RECIPES_LIMIT": {}, + "MCPX_RECIPES_MATCH_MARGIN": {}, + "MCPX_RECIPES_MIN_SCORE": {}, + "MCPX_REGISTRY_TIMEOUT": {}, + "MCPX_REPAIR_AUTONOMY": {}, + "MCPX_RETENTION_MAX_TOOLS": {}, + "MCPX_RETENTION_STICKY_WINDOW": {}, + "MCPX_SCRIPT_BEFORE": {}, + "MCPX_SCRIPT_IMPORTS": {}, + "MCPX_SCRIPT_IMPORTS_ON_DYNAMIC": {}, + "MCPX_SCRIPT_LAUNCHER": {}, + "MCPX_SCRIPT_ON_ERROR": {}, + "MCPX_SCRIPT_ON_SUCCESS": {}, + "MCPX_SCRIPT_PREFIX": {}, + "MCPX_SCRIPT_PROFILES": {}, + "MCPX_SCRIPT_RUNTIME": {}, + "MCPX_SCRIPT_RUNTIMES": {}, + "MCPX_SCRIPT_RUNTIME_ORDER": {}, + "MCPX_SCRIPT_SUFFIX": {}, + "MCPX_SEARCH_SEMANTIC": {}, + "MCPX_SERVE_MODE": {}, + "MCPX_SESSION_RELEASE_TIMEOUT": {}, + "MCPX_SPEC_PRECEDENCE": {}, + "MCPX_STATS_TOP": {}, + "MCPX_TASKS_RESULT_WAIT": {}, + "MCPX_TASKS_TTL": {}, + "MCPX_TRANSPORT_BEARER_TOKEN": {}, + "MCPX_TRANSPORT_SESSION_META_KEYS": {}, + "MCPX_TRANSPORT_SSE_KEEP_ALIVE": {}, + "MCPX_TRANSPORT_STDIO_DRAIN": {}, +} + +// registryEnvNames is every MCPX_* spelling one setting answers to: its +// derived name and its aliases. +func registryEnvNames(set settings.Setting) []string { + return append([]string{set.EnvName()}, set.EnvAliases...) +} + +// TestEveryRegistryVariableIsHarnessSetOrSafe is the static half of the +// guard: every variable the registry declares is classified into exactly one +// of the two tables above. Adding a setting without classifying it fails +// here; removing one without deleting its entry fails here too. +func TestEveryRegistryVariableIsHarnessSetOrSafe(t *testing.T) { + sch, err := settings.New(settings.Registry()) + if err != nil { + t.Fatalf("the registry should be internally consistent: %v", err) + } + seen := map[string]bool{} + for _, set := range sch.All() { + for _, name := range registryEnvNames(set) { + seen[name] = true + _, inHarness := harnessSetMCPX[name] + _, inSafe := safeMCPX[name] + switch { + case inHarness && inSafe: + t.Errorf("%s (%s) is in both tables; it belongs in exactly one", name, set.Path) + case !inHarness && !inSafe: + t.Errorf("%s (%s) is in neither table; classify it: harnessSetMCPX if the harness sets it, safeMCPX if the default governs", name, set.Path) + } + } + } + for name := range harnessSetMCPX { + if !seen[name] { + t.Errorf("harnessSetMCPX names %s, which no setting declares; delete the stale entry", name) + } + } + for name := range safeMCPX { + if !seen[name] { + t.Errorf("safeMCPX names %s, which no setting declares; delete the stale entry", name) + } + } +} + +// alwaysSetMCPX is what newEnv sets for every child, so a parent value for +// one of these must never survive: the behaviour test plants canaries for +// them and requires the harness value to win. +var alwaysSetMCPX = []string{ + "MCPX_REGISTRY_URL", + "MCPX_STATE_DIR", + "MCPX_CACHE_DIR", + "MCPX_CONFIG", + "MCPX_AUTOSTART_IDLE_EXIT", + "MCPX_POLICY_WRITE", +} + +// callerLeakParent are caller variable classes the old inherit-everything +// harness leaked into tests: XDG base directories, outer session scoping, +// proxies and NODE_OPTIONS. (HOME is planted separately: it must exist for +// testsupport.TempDir to resolve it.) +func callerLeakParent() []string { + return []string{ + "XDG_CONFIG_HOME=/nonexistent-mcpx-guard/XDG_CONFIG_HOME", + "XDG_DATA_HOME=/nonexistent-mcpx-guard/XDG_DATA_HOME", + "XDG_STATE_HOME=/nonexistent-mcpx-guard/XDG_STATE_HOME", + "XDG_CACHE_HOME=/nonexistent-mcpx-guard/XDG_CACHE_HOME", + "XDG_RUNTIME_DIR=/nonexistent-mcpx-guard/XDG_RUNTIME_DIR", + "OPENCODE_TEST_GUARD=e2e-guard-canary", + "OPENCODE_DB=/nonexistent-mcpx-guard/opencode.db", + "HTTP_PROXY=http://nonexistent-mcpx-guard:9", + "HTTPS_PROXY=http://nonexistent-mcpx-guard:9", + "ALL_PROXY=http://nonexistent-mcpx-guard:9", + "NO_PROXY=nonexistent-mcpx-guard", + "http_proxy=http://nonexistent-mcpx-guard:9", + "https_proxy=http://nonexistent-mcpx-guard:9", + "all_proxy=http://nonexistent-mcpx-guard:9", + "no_proxy=nonexistent-mcpx-guard", + "NODE_OPTIONS=--require /nonexistent-mcpx-guard", + } +} + +// TestTheHarnessBuildsChildrenFromTheAllowlist is the behaviour half of the +// guard: with a hostile parent -- a canary for every registry variable plus +// every caller variable class the old harness leaked -- a fresh child +// carries the harness values where the harness sets them, the test dir's +// XDG roots, and nothing else from the caller. +func TestTheHarnessBuildsChildrenFromTheAllowlist(t *testing.T) { + sch, err := settings.New(settings.Registry()) + if err != nil { + t.Fatalf("the registry should be internally consistent: %v", err) + } + for _, set := range sch.All() { + for _, name := range registryEnvNames(set) { + switch name { + case "MCPX_DAEMON_ENDPOINT": + t.Setenv(name, "http://127.0.0.1:1") + case "DENO_DIR", "BUN_INSTALL_CACHE_DIR", "NPM_CONFIG_CACHE": + // Allowlisted runtime-cache passthroughs by design; the + // harness sets the first two explicitly, so no canary. + default: + t.Setenv(name, "e2e-guard-canary") + } + } + } + // MCPX_SOCKET is not a registry variable -- no setting declares it -- but + // it is the daemon's raw socket override, read at the connect/bind call + // sites, so it gets the same canary treatment. + t.Setenv("MCPX_SOCKET", "/nonexistent-mcpx-guard.sock") + // The runtime caches are allowlisted passthroughs: the child must see + // the parent's values, chosen here rather than inherited by accident. + t.Setenv("DENO_DIR", "/nonexistent-mcpx-guard/deno") + t.Setenv("BUN_INSTALL_CACHE_DIR", "/nonexistent-mcpx-guard/bun") + t.Setenv("NPM_CONFIG_CACHE", "/nonexistent-mcpx-guard/npm") + for _, kv := range callerLeakParent() { + name, value, _ := strings.Cut(kv, "=") + t.Setenv(name, value) + } + // HOME must exist: testsupport.TempDir resolves it, so the canary is a + // real directory that is still unmistakably not the test's own home. + fakeHome := t.TempDir() + t.Setenv("HOME", fakeHome) + + e := newEnv(t, oneServer) + // Last wins in os/exec, so the map holds what the child resolves -- but + // the counts prove the parent value is gone, not merely overridden: an + // inherited-then-overridden duplicate would still be a leak to catch. + got := map[string]string{} + counts := map[string]int{} + for _, kv := range e.envVars { + name, value, ok := strings.Cut(kv, "=") + if !ok { + t.Fatalf("harness env entry %q has no =", kv) + } + got[name] = value + counts[name]++ + } + + // The harness-controlled variables win over any parent value, exactly + // once each. + for _, name := range alwaysSetMCPX { + v, ok := got[name] + if !ok { + t.Errorf("%s: newEnv must set it for every child", name) + continue + } + if counts[name] != 1 { + t.Errorf("%s appears %d times: the parent value reaches the child and is only overridden", name, counts[name]) + } + if strings.Contains(v, "canary") || strings.Contains(v, "nonexistent") || v == "http://127.0.0.1:1" { + t.Errorf("%s=%q: the parent canary reached the child", name, v) + } + } + if v := got["MCPX_STATE_DIR"]; v != filepath.Join(e.dir, "state") { + t.Errorf("MCPX_STATE_DIR=%q, want the test's own state dir", v) + } + if v := got["MCPX_CACHE_DIR"]; v != filepath.Join(e.dir, "cache") { + t.Errorf("MCPX_CACHE_DIR=%q, want the test's own cache dir", v) + } + if v := got["MCPX_CONFIG"]; v != filepath.Join(e.dir, ".mcpx.json") { + t.Errorf("MCPX_CONFIG=%q, want the test's own config", v) + } + + // No other registry variable reaches the child, including the ones + // individual tests set for themselves: a fresh child leaves them at + // their defaults. MCPX_SOCKET is not registry-declared but is checked + // the same way. + always := map[string]bool{} + for _, name := range alwaysSetMCPX { + always[name] = true + } + for _, set := range sch.All() { + for _, name := range registryEnvNames(set) { + if always[name] { + continue + } + if v, ok := got[name]; ok { + t.Errorf("%s=%q: a parent registry value reached the child", name, v) + } + } + } + if v, ok := got["MCPX_SOCKET"]; ok { + t.Errorf("MCPX_SOCKET=%q: the caller's socket override reached the child", v) + } + + // The runtime-cache passthroughs carry the parent's values. + for name, want := range map[string]string{ + "DENO_DIR": "/nonexistent-mcpx-guard/deno", + "BUN_INSTALL_CACHE_DIR": "/nonexistent-mcpx-guard/bun", + "NPM_CONFIG_CACHE": "/nonexistent-mcpx-guard/npm", + } { + if got[name] != want { + t.Errorf("%s=%q, want the parent passthrough %q", name, got[name], want) + } + } + + // XDG base directories resolve under the test's own dir. + for name, want := range map[string]string{ + "XDG_CONFIG_HOME": filepath.Join(e.dir, ".config"), + "XDG_DATA_HOME": filepath.Join(e.dir, ".local", "share"), + "XDG_STATE_HOME": filepath.Join(e.dir, ".local", "state"), + "XDG_CACHE_HOME": filepath.Join(e.dir, ".cache"), + "XDG_RUNTIME_DIR": filepath.Join(e.dir, "run"), + } { + if got[name] != want { + t.Errorf("%s=%q, want %q", name, got[name], want) + } + } + + // No other caller variable class reaches the child. + if got["HOME"] != filepath.Join(e.dir, "home") { + t.Errorf("HOME=%q, want the test's own home", got["HOME"]) + } + if got["HOME"] == fakeHome { + t.Errorf("HOME=%q: the parent home reached the child", got["HOME"]) + } + for name := range got { + upper := strings.ToUpper(name) + switch { + case strings.HasPrefix(upper, "OPENCODE_"): + t.Errorf("%s=%q: outer session scoping reached the child", name, got[name]) + case upper == "NODE_OPTIONS": + t.Errorf("%s=%q: caller node flags reached the child", name, got[name]) + case strings.HasSuffix(upper, "_PROXY") || upper == "HTTP_PROXY" || upper == "HTTPS_PROXY" || upper == "ALL_PROXY": + t.Errorf("%s=%q: a caller proxy reached the child", name, got[name]) + case strings.HasPrefix(name, "XDG_"): + switch name { + case "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CACHE_HOME", "XDG_RUNTIME_DIR": + default: + t.Errorf("%s=%q: a caller XDG variable reached the child", name, got[name]) + } + } + } + if got["PATH"] == "" { + t.Error("PATH is empty: the child would find no runtime") + } +} + +// TestTheHarnessInheritsNoMCPXVariable is the regression test #1992 +// prescribes: with a hostile parent -- a daemon endpoint and socket pointing +// at the caller's daemon, a bogus script runtime, a HOME that does not +// exist -- the suite's own commands stay local, default and home-free. +func TestTheHarnessInheritsNoMCPXVariable(t *testing.T) { + t.Setenv("MCPX_DAEMON_ENDPOINT", "http://127.0.0.1:1") + t.Setenv("MCPX_SOCKET", "/nonexistent-mcpx-guard.sock") + t.Setenv("MCPX_SCRIPT_RUNTIME", "bogus-runtime") + // HOME must exist: testsupport.TempDir resolves it, so the canary is a + // real directory that is still unmistakably not the test's own home. + fakeHome := t.TempDir() + t.Setenv("HOME", fakeHome) + e := newEnv(t, oneServer) + // The harness's own stub registry is a 127.0.0.1 URL, so the endpoint + // canary is checked with the stub exempted: a stub port starting with + // 1 would otherwise read as a leak. + stub := gotRegistryURL(t, e.envVars) + + canaries := []string{"/nonexistent-mcpx-guard", "bogus-runtime"} + // The endpoint canary needs a port boundary: the stub registry is + // itself a 127.0.0.1 URL, and a stub port starting with 1 would + // otherwise read as a leak -- as would any 127.0.0.1:1xxx port a + // diagnostic prints. Port 1 followed by a non-digit is only the canary. + scrubbed := func(out string) string { return strings.ReplaceAll(out, stub, "") } + endpointCanary := regexp.MustCompile(`127\.0\.0\.1:1([^0-9]|$)`) + leaksEndpoint := func(out string) bool { return endpointCanary.MatchString(scrubbed(out)) } + for _, kv := range e.envVars { + if strings.HasPrefix(kv, "MCPX_REGISTRY_URL=") { + if kv != "MCPX_REGISTRY_URL="+stub { + t.Fatalf("MCPX_REGISTRY_URL entry %q is not the harness stub", kv) + } + continue + } + if kv == "HOME="+fakeHome { + t.Fatalf("the parent HOME reached the child environment in %q", kv) + } + if leaksEndpoint(kv) { + t.Fatalf("parent canary %q reached the child environment in %q", "http://127.0.0.1:1", kv) + } + for _, c := range canaries { + if strings.Contains(kv, c) { + t.Fatalf("parent canary %q reached the child environment in %q", c, kv) + } + } + } + // settings list and recipes are read-only and deterministic: they must + // succeed, and stay canary-free. doctor's exit depends on machine + // health, not isolation, so it must only stay canary-free. + for _, args := range [][]string{{"settings", "list"}, {"recipes"}} { + out, err := e.try(args...) + if err != nil { + t.Fatalf("mcpx %s failed: %v\n%s", strings.Join(args, " "), err, out) + } + if leaksEndpoint(out) { + t.Errorf("mcpx %s shows the parent endpoint canary:\n%s", strings.Join(args, " "), out) + } + for _, c := range canaries { + if strings.Contains(scrubbed(out), c) { + t.Errorf("mcpx %s shows the parent canary %q:\n%s", strings.Join(args, " "), c, out) + } + } + } + out, _ := e.try("doctor", "-v") + if leaksEndpoint(out) { + t.Errorf("mcpx doctor -v shows the parent endpoint canary:\n%s", out) + } + for _, c := range canaries { + if strings.Contains(scrubbed(out), c) { + t.Errorf("mcpx doctor -v shows the parent canary %q:\n%s", c, out) + } + } +} + +// gotRegistryURL is the harness stub URL newEnv sets for every child. +func gotRegistryURL(t *testing.T, env []string) string { + t.Helper() + seen := 0 + var stub string + for _, kv := range env { + if v, ok := strings.CutPrefix(kv, "MCPX_REGISTRY_URL="); ok { + seen++ + stub = v + } + } + if seen != 1 || !strings.HasPrefix(stub, "http://127.0.0.1:") { + t.Fatalf("want exactly one harness stub registry URL, got %d entries (last %q)", seen, stub) + } + return stub +} diff --git a/pkgs/mcpx/internal/testsupport/childenv.go b/pkgs/mcpx/internal/testsupport/childenv.go new file mode 100644 index 0000000000..28d24fe37f --- /dev/null +++ b/pkgs/mcpx/internal/testsupport/childenv.go @@ -0,0 +1,90 @@ +// The test child environment: an allowlist, not an inheritance. +// +// Every end-to-end test starts a real mcpx binary as a child process. That +// child used to inherit the developer's whole environment minus MCPX_* +// variables, so anything else the caller's shell exported -- XDG_* paths +// into the user's real config, OPENCODE_* session scoping from the opencode +// plugin, proxies, NODE_OPTIONS -- reached the test, and the suite's result +// depended on whose machine ran it (#1992). The concrete caller-daemon hole +// (MCPX_DAEMON_ENDPOINT/MCPX_SOCKET) is closed by dropping MCPX_*; this file +// closes the rest by failing open in the other direction: a child gets only +// what is listed here, plus what the harness sets explicitly for that test. +// +// A new caller variable class is isolated by default. Widening the allowlist +// is a deliberate edit here, held to the guard in internal/e2e. +package testsupport + +import ( + "path/filepath" + "strings" +) + +// ChildEnv builds the environment for a test child process from an +// allowlist (#1992, Approach C). +// +// parent is the caller's environment, usually os.Environ. dir is the test's +// own directory, usually testsupport.TempDir: the XDG base directories are +// pinned underneath it so no caller XDG_* path, and no real $HOME reached +// through them, is visible to the child. extra is what the test controls -- +// MCPX_STATE_DIR, HOME, the stub registry and the like -- and wins, because +// os/exec keeps the last value for a duplicated key. +// +// The parent contributes only: +// - PATH, so the child finds its runtimes (deno, bun, node) and sh; +// - TMPDIR (TEMP/TMP), LANG, LANGUAGE, LC_* and TERM, so locale-aware +// output and the temp directory behave as on the caller's machine; +// - SYSTEMROOT, so the same builder stays correct if the suite ever runs +// on Windows (#136); +// - the runtime-cache passthroughs DENO_DIR, BUN_INSTALL_CACHE_DIR and +// NPM_CONFIG_CACHE, so a test reuses the caller's warm caches instead +// of paying for a cold download per test. +// +// Everything else is dropped, in particular: every MCPX_* variable (a +// parent value would run the suite under the caller's settings, or point it +// at the caller's daemon); every XDG_* variable (replaced below); every +// OPENCODE_* variable (outer session scoping must not leak into the test's); +// proxy variables in either case (a proxy between the test and its own stub +// registry is a failure that only happens on some machines); NODE_OPTIONS +// (a caller's --require flag would load into every script the test runs); +// and TLS, credential and identity variables, which the isolated suite has +// no business reading. +func ChildEnv(parent []string, dir string, extra ...string) []string { + out := make([]string, 0, len(parent)+len(extra)+5) + for _, kv := range parent { + name, _, ok := strings.Cut(kv, "=") + if !ok { + continue + } + if !inheritedFromParent(name) { + continue + } + out = append(out, kv) + } + // XDG base directories under the test's own dir. The child resolves + // state, cache, config and the runtime dir from these (or from the + // MCPX_* overrides in extra), so nothing under the caller's real home + // or real runtime dir is shared with the test. + out = append(out, + "XDG_CONFIG_HOME="+filepath.Join(dir, ".config"), + "XDG_DATA_HOME="+filepath.Join(dir, ".local", "share"), + "XDG_STATE_HOME="+filepath.Join(dir, ".local", "state"), + "XDG_CACHE_HOME="+filepath.Join(dir, ".cache"), + "XDG_RUNTIME_DIR="+filepath.Join(dir, "run"), + ) + out = append(out, extra...) + return out +} + +// inheritedFromParent reports whether a parent variable passes the allowlist +// into a test child. Anything not named here is dropped, whatever its +// prefix; in particular no MCPX_* variable is ever inherited. +func inheritedFromParent(name string) bool { + switch name { + case "PATH", "TMPDIR", "TEMP", "TMP", + "LANG", "LANGUAGE", "TERM", + "DENO_DIR", "BUN_INSTALL_CACHE_DIR", "NPM_CONFIG_CACHE", + "SYSTEMROOT": + return true + } + return strings.HasPrefix(name, "LC_") +} diff --git a/pkgs/mcpx/internal/testsupport/childenv_test.go b/pkgs/mcpx/internal/testsupport/childenv_test.go new file mode 100644 index 0000000000..b5ff151dc5 --- /dev/null +++ b/pkgs/mcpx/internal/testsupport/childenv_test.go @@ -0,0 +1,90 @@ +package testsupport + +import ( + "path/filepath" + "strings" + "testing" +) + +func TestChildEnvIsAnAllowlist(t *testing.T) { + dir := filepath.Join("/tests", "case") + got := ChildEnv([]string{ + "PATH=/bin", + "TMPDIR=/tmp", + "LANG=C.UTF-8", + "LC_ALL=C", + "TERM=xterm-256color", + "SYSTEMROOT=C:\\Windows", + "DENO_DIR=/cache/deno", + "BUN_INSTALL_CACHE_DIR=/cache/bun", + "NPM_CONFIG_CACHE=/cache/npm", + "HOME=/caller/home", + "MCPX_TRACE=1", + "MCPX_DAEMON_ENDPOINT=http://127.0.0.1:9", + "MCPX_SOCKET=/caller/daemon.sock", + "XDG_CONFIG_HOME=/caller/config", + "XDG_DATA_HOME=/caller/data", + "XDG_STATE_HOME=/caller/state", + "XDG_CACHE_HOME=/caller/cache", + "XDG_RUNTIME_DIR=/caller/run", + "OPENCODE_SESSION_ID=caller-session", + "HTTP_PROXY=http://proxy:9", + "NODE_OPTIONS=--require caller.js", + "AWS_ACCESS_KEY_ID=not-for-tests", + "GITHUB_TOKEN=not-for-tests", + }, dir, "HOME="+filepath.Join(dir, "home"), "MCPX_CONFIG="+filepath.Join(dir, "config")) + + env := envMap(got) + for _, name := range []string{ + "PATH", "TMPDIR", "LANG", "LC_ALL", "TERM", "SYSTEMROOT", + "DENO_DIR", "BUN_INSTALL_CACHE_DIR", "NPM_CONFIG_CACHE", + "HOME", "MCPX_CONFIG", + } { + if env[name] == "" { + t.Errorf("allowlisted or explicitly set variable %s is missing", name) + } + } + for _, name := range []string{ + "MCPX_TRACE", "MCPX_DAEMON_ENDPOINT", "MCPX_SOCKET", + "OPENCODE_SESSION_ID", "HTTP_PROXY", "NODE_OPTIONS", + "AWS_ACCESS_KEY_ID", "GITHUB_TOKEN", + } { + if _, ok := env[name]; ok { + t.Errorf("caller variable %s reached the child", name) + } + } + for name, want := range map[string]string{ + "XDG_CONFIG_HOME": filepath.Join(dir, ".config"), + "XDG_DATA_HOME": filepath.Join(dir, ".local", "share"), + "XDG_STATE_HOME": filepath.Join(dir, ".local", "state"), + "XDG_CACHE_HOME": filepath.Join(dir, ".cache"), + "XDG_RUNTIME_DIR": filepath.Join(dir, "run"), + } { + if env[name] != want { + t.Errorf("%s=%q, want %q", name, env[name], want) + } + } +} + +func TestChildEnvExtraWinsOverParent(t *testing.T) { + got := ChildEnv([]string{"PATH=/bin", "TMPDIR=/caller/tmp"}, "/tests/case", + "TMPDIR=/tests/tmp", "MCPX_STATE_DIR=/tests/state") + env := envMap(got) + if env["TMPDIR"] != "/tests/tmp" { + t.Errorf("TMPDIR=%q, want extra value", env["TMPDIR"]) + } + if env["MCPX_STATE_DIR"] != "/tests/state" { + t.Errorf("MCPX_STATE_DIR=%q, want extra value", env["MCPX_STATE_DIR"]) + } +} + +func envMap(env []string) map[string]string { + out := make(map[string]string, len(env)) + for _, kv := range env { + name, value, ok := strings.Cut(kv, "=") + if ok { + out[name] = value + } + } + return out +} From a20bbf7185d1e323c38eedba681c52a20fc4440b Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 15:56:02 -0600 Subject: [PATCH 2/2] mcpx(testsupport): pin the build env so a hostile caller env cannot break test builds FakeMCPBinary/MCPXBinary ran go build with the ambient env. The e2e env guard test plants XDG_CACHE_HOME, proxies and HOME pointing at a nonexistent root, and the build died three ways: build cache under the planted XDG root, module downloads through the planted proxy, and a cold GOPATH/module cache under the planted HOME (which the test then could not clean up). buildEnv now pins GOCACHE/XDG_CACHE_HOME under the build temp dir, drops proxy variables (the ChildEnv reasoning), and resets HOME from the passwd entry so builds use the invoking users warm caches. TestTheHarnessBuildsChildrenFromTheAllowlist and TestEveryRegistryVariableIsHarnessSetOrSafe now pass; internal/testsupport tests and go build ./... pass. --- pkgs/mcpx/internal/testsupport/build.go | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pkgs/mcpx/internal/testsupport/build.go b/pkgs/mcpx/internal/testsupport/build.go index ec2e16e858..de68bf26e9 100644 --- a/pkgs/mcpx/internal/testsupport/build.go +++ b/pkgs/mcpx/internal/testsupport/build.go @@ -4,7 +4,9 @@ package testsupport import ( "os" "os/exec" + "os/user" "path/filepath" + "strings" "sync" "testing" ) @@ -19,6 +21,53 @@ var ( mcpxErr error ) +// buildEnv returns the environment for a test-support build. The build +// cache is pinned under the build's own temp dir: a caller (or a guard +// test planting hostile values, see the e2e env guard) must never be +// able to point the build at a path of its choosing — with an ambient +// XDG_CACHE_HOME under a nonexistent root, go build dies with +// "failed to initialize build cache" before any test logic runs. +// GOCACHE is set explicitly as well as XDG_CACHE_HOME so the pin holds +// even where a caller's GOCACHE was already set. +func buildEnv(dir string) []string { + cache := filepath.Join(dir, ".cache") + env := make([]string, 0, len(os.Environ())+2) + for _, kv := range os.Environ() { + name, _, _ := strings.Cut(kv, "=") + if name == "GOCACHE" || name == "XDG_CACHE_HOME" || name == "HOME" || isProxyVar(name) { + continue + } + env = append(env, kv) + } + env = append(env, + "XDG_CACHE_HOME="+cache, + "GOCACHE="+filepath.Join(cache, "go-build"), + ) + // HOME is reset from the passwd entry, not inherited: a planted + // HOME (the guard test sets one) would move GOPATH and the module + // cache under the caller's chosen root, making the build download + // every module into a temp dir it then cannot clean up. The build + // uses the invoking user's real home and its warm caches. + if u, err := user.Current(); err == nil && u.HomeDir != "" { + env = append(env, "HOME="+u.HomeDir) + } else if home, herr := os.UserHomeDir(); herr == nil { + env = append(env, "HOME="+home) + } + return env +} + +// isProxyVar reports whether name is a proxy variable in either case. +// A build inherits no proxy: the same reasoning as ChildEnv — a proxy +// planted by a caller (or a guard test) between the build and the +// module proxy turns a hermetic build into a network failure. +func isProxyVar(name string) bool { + switch strings.ToUpper(name) { + case "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY": + return true + } + return false +} + // FakeMCPBinary compiles the fake MCP server once per test binary and returns // its path. func FakeMCPBinary(t *testing.T) string { @@ -32,6 +81,7 @@ func FakeMCPBinary(t *testing.T) string { binary = filepath.Join(dir, "fakemcp") cmd := exec.Command("go", "build", "-o", binary, "./internal/testsupport/fakemcp") cmd.Dir = repoRoot() + cmd.Env = buildEnv(dir) out, err := cmd.CombinedOutput() if err != nil { buildErr = &buildFailure{err: err, out: string(out)} @@ -62,6 +112,7 @@ func MCPXBinary(t *testing.T) string { mcpxBin = filepath.Join(dir, "mcpx") cmd := exec.Command("go", "build", "-o", mcpxBin, "./cmd/mcpx") cmd.Dir = repoRoot() + cmd.Env = buildEnv(dir) out, err := cmd.CombinedOutput() if err != nil { mcpxErr = &buildFailure{err: err, out: string(out)}