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.

chore(dev): sandbox manually-run uv/pnpm installs (#64746)

Tom Piccirello0c5efce1851d
diff --git a/.flox/env/manifest.toml b/.flox/env/manifest.toml
index 3b737ef9be60..89b9b3b59790 100644
--- a/.flox/env/manifest.toml
+++ b/.flox/env/manifest.toml
@@ -119,6 +119,8 @@ bash = '''
[ -f "$FLOX_ENV_CACHE/completions/hogli.bash" ] && source "$FLOX_ENV_CACHE/completions/hogli.bash"
# Load phw worktree helper
[ -f "$FLOX_ENV_PROJECT/bin/phw" ] && source "$FLOX_ENV_PROJECT/bin/phw" >/dev/null
+ # Route manually-run dependency installs (uv sync, pnpm install, ...) through the dev sandbox on macOS
+ [ -f "$FLOX_ENV_PROJECT/bin/dev-sandbox-shims" ] && source "$FLOX_ENV_PROJECT/bin/dev-sandbox-shims"
'''
zsh = '''
if [[ -f "$HOME/.zshrc" ]]; then
@@ -146,6 +148,8 @@ zsh = '''
autoload -Uz compinit && compinit -i
# Load phw worktree helper
[[ -f "$FLOX_ENV_PROJECT/bin/phw" ]] && source "$FLOX_ENV_PROJECT/bin/phw" >/dev/null
+ # Route manually-run dependency installs (uv sync, pnpm install, ...) through the dev sandbox on macOS
+ [[ -f "$FLOX_ENV_PROJECT/bin/dev-sandbox-shims" ]] && source "$FLOX_ENV_PROJECT/bin/dev-sandbox-shims"
'''
fish = '''
set -e GOROOT
diff --git a/.github/workflows/dev-sandbox-selftest.yml b/.github/workflows/dev-sandbox-selftest.yml
index 13f41a4a1f58..3425ac942c67 100644
--- a/.github/workflows/dev-sandbox-selftest.yml
+++ b/.github/workflows/dev-sandbox-selftest.yml
@@ -10,12 +10,16 @@ on:
- bin/dev-sandbox
- bin/dev-sandbox.sb
- bin/dev-sandbox-selftest
+ - bin/dev-sandbox-shims
+ - bin/dev-sandbox-shims-selftest
- .github/workflows/dev-sandbox-selftest.yml
pull_request:
paths:
- bin/dev-sandbox
- bin/dev-sandbox.sb
- bin/dev-sandbox-selftest
+ - bin/dev-sandbox-shims
+ - bin/dev-sandbox-shims-selftest
- .github/workflows/dev-sandbox-selftest.yml
concurrency:
@@ -35,3 +39,15 @@ jobs:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Run dev sandbox self-test
run: bin/dev-sandbox-selftest
+
+ shims-selftest:
+ name: Verify dev sandbox shims routing
+ # Pure string-logic test — no sandbox-exec needed, so it runs on Linux.
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ permissions:
+ contents: read
+ steps:
+ - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
+ - name: Run dev sandbox shims self-test
+ run: bin/dev-sandbox-shims-selftest
diff --git a/bin/dev-sandbox-shims b/bin/dev-sandbox-shims
new file mode 100644
index 000000000000..c71a15508bff
--- /dev/null
+++ b/bin/dev-sandbox-shims
@@ -0,0 +1,77 @@
+# Route manually-run dependency installs through bin/dev-sandbox.
+#
+# Sourced by the flox profile (bash/zsh), this defines uv()/pnpm() shell
+# functions that wrap dependency-mutating subcommands — the install-time moment
+# when untrusted package build/postinstall scripts execute — in the macOS
+# Seatbelt sandbox, so a poisoned lockfile can't read credentials (~/.ssh,
+# ~/.aws, ...) outside the repo. Everything else passes straight through.
+#
+# Wrapped: uv {sync, add, lock, pip install, pip sync}
+# pnpm {install, i, add, update, up}
+# Untouched: uv run, uv pip list, uv tool ...; pnpm dev/exec/<script> ...
+#
+# macOS-only: the uv()/pnpm() functions are defined only on Darwin (the sandbox
+# is a no-op elsewhere). Opt out with POSTHOG_DEV_SANDBOX=0. Bypass a single call
+# with `command uv ...` / `\uv ...`. Only the interactive shell is affected —
+# scripts, Makefiles, and the already-sandboxed dev stack are untouched.
+#
+# Limitation: the subcommand is detected as the first non-flag token (so the
+# common `pnpm --filter=x install` works). A global flag that takes a *separate*
+# value (`pnpm -F x install`) hides the subcommand and fails open — the install
+# runs, just unsandboxed. The `=`-joined form is the norm in this repo.
+
+# Pure decision logic — defined on every platform so it stays unit-testable on
+# Linux CI (see bin/dev-sandbox-shims-selftest). Returns 0 = sandbox, 1 = passthrough.
+_ph_sandbox_should_wrap() {
+ [ "${POSTHOG_DEV_SANDBOX:-}" = "0" ] && return 1
+ local tool="$1"
+ shift
+ # First two non-flag tokens: sub1 is the subcommand, sub2 disambiguates `uv pip ...`.
+ local sub1="" sub2="" tok
+ for tok in "$@"; do
+ case "$tok" in
+ -*) continue ;;
+ *)
+ if [ -z "$sub1" ]; then
+ sub1="$tok"
+ else
+ sub2="$tok"
+ break
+ fi
+ ;;
+ esac
+ done
+ case "$tool" in
+ uv)
+ case "$sub1" in
+ sync | add | lock) return 0 ;;
+ pip) [ "$sub2" = "install" ] || [ "$sub2" = "sync" ] && return 0 ;;
+ esac
+ ;;
+ pnpm)
+ case "$sub1" in
+ install | i | add | update | up) return 0 ;;
+ esac
+ ;;
+ esac
+ return 1
+}
+
+# The user-facing shims only make sense where the sandbox actually sandboxes.
+if [ "$(uname -s)" = "Darwin" ]; then
+ _PH_SANDBOX_PROJECT="${FLOX_ENV_PROJECT:-}"
+
+ _ph_sandbox_run() {
+ local tool="$1"
+ shift
+ if [ -n "$_PH_SANDBOX_PROJECT" ] && _ph_sandbox_should_wrap "$tool" "$@"; then
+ printf '\033[2m↪ running %s under dev sandbox (POSTHOG_DEV_SANDBOX=0 to disable)\033[0m\n' "$tool" >&2
+ "$_PH_SANDBOX_PROJECT/bin/dev-sandbox" "$(printf '%q ' "$tool" "$@")"
+ else
+ command "$tool" "$@"
+ fi
+ }
+
+ uv() { _ph_sandbox_run uv "$@"; }
+ pnpm() { _ph_sandbox_run pnpm "$@"; }
+fi
diff --git a/bin/dev-sandbox-shims-selftest b/bin/dev-sandbox-shims-selftest
new file mode 100755
index 000000000000..022906536e80
--- /dev/null
+++ b/bin/dev-sandbox-shims-selftest
@@ -0,0 +1,62 @@
+#!/usr/bin/env bash
+# Self-test for bin/dev-sandbox-shims routing logic. Pure string logic, so it
+# runs anywhere (no sandbox-exec / macOS needed) — unlike bin/dev-sandbox-selftest.
+set -euo pipefail
+
+here="$(cd "$(dirname "$0")" && pwd -P)"
+# shellcheck source=/dev/null
+source "$here/dev-sandbox-shims"
+
+fail=0
+check() { # check <wrap|pass> <tool> <args...>
+ local expected="$1" got
+ shift
+ local tool="$1"
+ shift
+ if _ph_sandbox_should_wrap "$tool" "$@"; then got="wrap"; else got="pass"; fi
+ if [ "$got" != "$expected" ]; then
+ echo "FAIL: expected $expected, got $got for: $tool $*" >&2
+ fail=1
+ else
+ echo "ok: $tool $* -> $got"
+ fi
+}
+
+# uv: dependency-mutating subcommands are sandboxed
+check wrap uv sync
+check wrap uv add requests
+check wrap uv lock
+check wrap uv pip install requests
+check wrap uv pip sync
+check wrap uv --verbose sync
+# uv: everything else passes through
+check pass uv run pytest
+check pass uv pip list
+check pass uv tool install ruff
+check pass uv
+
+# pnpm: dependency-mutating subcommands are sandboxed
+check wrap pnpm install
+check wrap pnpm i
+check wrap pnpm add lodash
+check wrap pnpm update
+check wrap pnpm up
+check wrap pnpm --filter=@posthog/frontend install
+# pnpm: everything else passes through
+check pass pnpm dev
+check pass pnpm exec tsc
+check pass pnpm run install # a script named "install" is not `pnpm install`
+check pass pnpm
+
+# POSTHOG_DEV_SANDBOX=0 disables all wrapping
+echo "-- with POSTHOG_DEV_SANDBOX=0 --"
+POSTHOG_DEV_SANDBOX=0
+check pass uv sync
+check pass pnpm install
+unset POSTHOG_DEV_SANDBOX
+
+if [ "$fail" -ne 0 ]; then
+ echo "dev-sandbox-shims self-test FAILED" >&2
+ exit 1
+fi
+echo "dev-sandbox-shims self-test passed"
diff --git a/hogli.yaml b/hogli.yaml
index 50555be092c5..89e0aaf94d6d 100644
--- a/hogli.yaml
+++ b/hogli.yaml
@@ -1071,6 +1071,10 @@ environment:
bin_script: inject_mcp_intents.py
description: 'Used when developing MCP Analytics locally and trying to load some intents'
hidden: true
+ dev:sandbox:shims:selftest:
+ bin_script: dev-sandbox-shims-selftest
+ description: 'Verify the uv/pnpm dev-sandbox shims route install subcommands correctly'
+ hidden: true
setup:gateway:e2e:
bin_script: setup-gateway-e2e
description: 'Provision the local ai-gateway e2e: enable the team, mint a phs_, publish the credential blob, fund the ledger'