Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(dev): strip read-denied $HOME dirs from sandbox PATH (#64768)

Julian Bez8fd775901af9
diff --git a/bin/dev-sandbox b/bin/dev-sandbox
index 6c289d9f8897..86e36a85e315 100755
--- a/bin/dev-sandbox
+++ b/bin/dev-sandbox
@@ -134,6 +134,31 @@ if [[ "$rendered" -nt "$stamp" ]]; then
fi
fi
+# Drop $HOME PATH entries the sandbox denies, so bare-command spawns keep working.
+# node/libuv resolve a bare command name with execvP, which walks $PATH and aborts
+# the ENTIRE search on the first EPERM — and Seatbelt returns EPERM (not ENOENT) for
+# a dir that exists but is read-denied. So a single unreadable $HOME dir on PATH
+# (fnm/nvm/asdf/pyenv shims under ~/.local, ~/.nvm, …) breaks every bare-name spawn
+# before it can reach a usable binary — e.g. pnpm's lifecycle scripts during install
+# fail with `spawn EPERM`. Keep only the repo's own bins (node_modules/.bin) and
+# non-$HOME dirs (system, the flox/nix store) — the full toolchain the dev stack
+# actually uses. $HOME interpreters were already unusable inside the sandbox by
+# design (contents unreadable → exec fails); this just makes the search skip them
+# instead of dying on them.
+sandboxed_path=""
+_oldifs="$IFS"
+set -f # PATH entries are literal paths; never glob-expand them
+IFS=':'
+for _pdir in $PATH; do
+ case "$_pdir" in
+ "$project"/* | "$mainrepo"/*) ;; # repo bins (node_modules/.bin) — keep
+ "$HOME"/* | "") continue ;; # read-denied $HOME shim dir / empty — drop
+ esac
+ sandboxed_path+="${sandboxed_path:+:}$_pdir"
+done
+IFS="$_oldifs"
+set +f
+
# Strip ambient personal tokens the dev stack doesn't need from the child env.
# The application's own config env (DB URLs, OBJECT_STORAGE_* keys, 1Password-
# injected API keys) is left intact — only credentials that happen to live in the
@@ -156,4 +181,5 @@ exec env \
-u CARGO_REGISTRY_TOKEN \
-u HUGGING_FACE_HUB_TOKEN \
-u HF_TOKEN \
+ PATH="$sandboxed_path" \
"${sandbox[@]}" /bin/bash -c "$cmd"
diff --git a/bin/dev-sandbox-selftest b/bin/dev-sandbox-selftest
index 323eabe34409..adf490367ca2 100755
--- a/bin/dev-sandbox-selftest
+++ b/bin/dev-sandbox-selftest
@@ -189,6 +189,24 @@ else
die "rendered profile not found (expected after earlier sandbox runs)"
fi
+# 13. PATH is sanitized: read-denied $HOME dirs are dropped from the child PATH so a
+# bare-command spawn keeps working. node/libuv resolve bare names via posix_spawnp,
+# which aborts the whole PATH search on the first EPERM Seatbelt returns for an
+# existing-but-denied dir — so one unreadable $HOME shim dir (nvm/fnm/asdf/pyenv)
+# ahead of the real binary breaks every bare spawn (e.g. pnpm lifecycle scripts).
+# Assert a $HOME dir is dropped while system + repo bins survive. String-only, so
+# the probe dir need not exist.
+repo="$(pwd -P)"
+shim="$HOME/.dev_sandbox_selftest_shim/bin"
+child_path="$(PATH="$shim:/usr/bin:/bin:$repo/bin" $SANDBOX 'printf %s "$PATH"' 2>/dev/null)"
+if [[ ":$child_path:" == *":$shim:"* ]]; then
+ die "PATH not sanitized: read-denied \$HOME dir survived in child PATH"
+elif [[ ":$child_path:" != *":/usr/bin:"* ]] || [[ ":$child_path:" != *":$repo/bin:"* ]]; then
+ die "PATH over-sanitized: dropped a system or repo dir (got: $child_path)"
+else
+ pass "PATH sanitized: denied \$HOME dir dropped, system + repo bins kept"
+fi
+
echo
if [[ $fail -eq 0 ]]; then
echo "PASS — all dev-sandbox self-tests passed"