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(stamphog): keep the review payload inside the checkout's own directories (#125413)

Georges-Antoine Assi588c3a6b4919
diff --git a/products/stamphog/backend/temporal/activities.py b/products/stamphog/backend/temporal/activities.py
index 95efeba295a8..09d12aef3ebf 100644
--- a/products/stamphog/backend/temporal/activities.py
+++ b/products/stamphog/backend/temporal/activities.py
@@ -2364,11 +2364,18 @@ def _review_payload_command() -> str:
attacker-controlled files beside our engine (e.g. tools/pr-approval-agent/yaml.py), which Python
would import ahead of uv's installed dependency, which is arbitrary code execution with the
sandbox's LLM creds. Overwriting only our known modules would leave those shadow files in place.
+
+ The command fails before it removes or extracts anything when a parent directory of a payload
+ destination is a symlink, such as a PR that commits `tools` as a link. tar follows a symlink in
+ a parent path, so the trusted files would land wherever the PR points, beside files the PR chose.
"""
repo_dir = shlex.quote(STAMPHOG_SANDBOX_REPO_DIR)
policy_paths = " ".join(shlex.quote(path) for path in (*STAMPHOG_POLICY_PATHS, *STAMPHOG_OPTIONAL_POLICY_PATHS))
+ destinations = (*STAMPHOG_POLICY_PATHS, *STAMPHOG_OPTIONAL_POLICY_PATHS, _ENGINE_RELATIVE_DIR, _OWNERS_RELATIVE_DIR)
+ parent_dirs = sorted({parent.as_posix() for path in destinations for parent in PurePosixPath(path).parents} - {"."})
+ no_symlinked_parents = " && ".join(f"test ! -L {shlex.quote(parent)}" for parent in parent_dirs)
return (
- f"cd {repo_dir} && rm -f {policy_paths} && "
+ f"cd {repo_dir} && {no_symlinked_parents} && rm -f {policy_paths} && "
f"rm -rf {shlex.quote(_ENGINE_RELATIVE_DIR)} {shlex.quote(_OWNERS_RELATIVE_DIR)} && "
f"tar -xzf {shlex.quote(STAMPHOG_SANDBOX_PAYLOAD_PATH)} --no-same-owner && "
f"rm -f {shlex.quote(STAMPHOG_SANDBOX_PAYLOAD_PATH)}"
diff --git a/products/stamphog/backend/tests/test_policy_defaults.py b/products/stamphog/backend/tests/test_policy_defaults.py
index 069343e4789c..ad2df008b659 100644
--- a/products/stamphog/backend/tests/test_policy_defaults.py
+++ b/products/stamphog/backend/tests/test_policy_defaults.py
@@ -24,6 +24,7 @@ from products.stamphog.backend.temporal.activities import ( # noqa: E402
_clone_pr,
_effective_policy_files,
_prefetch_review_blobs,
+ _review_payload_archive,
_review_payload_command,
)
from products.stamphog.backend.temporal.constants import ( # noqa: E402
@@ -158,6 +159,27 @@ def test_review_payload_command_wipes_optional_paths_from_pr_head() -> None:
assert extract
[email protected]("tools_is_symlink", [False, True])
+def test_review_payload_lands_only_in_a_real_tools_directory(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, tools_is_symlink: bool
+) -> None:
+ checkout = tmp_path / "checkout"
+ planted_dir = checkout / "a" / "b" / "c"
+ planted_dir.mkdir(parents=True)
+ if tools_is_symlink:
+ (checkout / "tools").symlink_to("a/b/c")
+ payload = tmp_path / "payload.tar.gz"
+ payload.write_bytes(_review_payload_archive({}, "{}"))
+ monkeypatch.setattr(activities, "STAMPHOG_SANDBOX_REPO_DIR", str(checkout))
+ monkeypatch.setattr(activities, "STAMPHOG_SANDBOX_PAYLOAD_PATH", str(payload))
+
+ result = subprocess.run(["bash", "-c", _review_payload_command()], capture_output=True, text=True)
+
+ assert (result.returncode == 0) is not tools_is_symlink
+ assert not (planted_dir / "pr-approval-agent").exists()
+ assert (checkout / "tools" / "pr-approval-agent" / "review_local.py").is_file() is not tools_is_symlink
+
+
def test_clone_and_prefetch_carry_the_credential_on_every_github_fetch() -> None:
executed: list[str] = []
diff --git a/products/stamphog/packages/pr-approval-agent/gates.py b/products/stamphog/packages/pr-approval-agent/gates.py
index 345335d595cb..d7f80c68de16 100644
--- a/products/stamphog/packages/pr-approval-agent/gates.py
+++ b/products/stamphog/packages/pr-approval-agent/gates.py
@@ -31,12 +31,16 @@ if TYPE_CHECKING:
# ownership source.
#
# There are two candidate locations, and the code puts only the FIRST one that exists on the path.
-# The first entry covers a run from this repo, where the resolver is at packages/owners-yaml and the
-# engine's own home is under products/stamphog/packages/. The sibling `owners/` covers two cases:
-# the vendored layout that downstream repos copy, and the review sandbox, which receives this
-# directory at <checkout>/tools/pr-approval-agent with the resolver beside it at
-# <checkout>/tools/owners. Neither of those layouts has a first entry to find, so they fall through
-# to the sibling.
+# The sibling `owners/` covers two cases: the vendored layout that downstream repos copy, and the
+# review sandbox, which receives this directory at <checkout>/tools/pr-approval-agent with the
+# resolver beside it at <checkout>/tools/owners. The second entry covers a run from this repo, where
+# the resolver is at packages/owners-yaml and the engine's own home is under
+# products/stamphog/packages/, which has no `owners/` sibling.
+#
+# The sibling goes first because the sandbox extracts it from the same trusted payload as this file,
+# so it is always beside the engine there. The repo-root offset is a path in the PR checkout. If a PR
+# symlink moves the engine deeper in the tree, that offset lands on a directory the PR controls, and
+# checking it first would import the PR's code in place of the trusted resolver.
#
# Both entries are fixed offsets from this file, and not a discovered repo root.
# `policy.repo_root()` walks up to find a `.stamphog/` directory, and the PR head is untrusted. A PR
@@ -46,9 +50,8 @@ if TYPE_CHECKING:
def _owners_pkg_candidates(engine_file: Path) -> tuple[Path, ...]:
engine_dir = engine_file.resolve().parent
# engine_dir is products/stamphog/packages/pr-approval-agent in this repo, so the repo root is
- # four levels up. In the sandbox and in a vendored copy that offset points outside the tree and
- # finds nothing, which is what makes the sibling the answer there.
- return (engine_dir.parents[3] / "packages" / "owners-yaml", engine_dir.parent / "owners")
+ # four levels up.
+ return (engine_dir.parent / "owners", engine_dir.parents[3] / "packages" / "owners-yaml")
_OWNERS_PKG_CANDIDATES = _owners_pkg_candidates(Path(__file__))
diff --git a/products/stamphog/packages/pr-approval-agent/test_gates.py b/products/stamphog/packages/pr-approval-agent/test_gates.py
index 5dc0a2f3766f..3c3b6dedd0f4 100644
--- a/products/stamphog/packages/pr-approval-agent/test_gates.py
+++ b/products/stamphog/packages/pr-approval-agent/test_gates.py
@@ -751,30 +751,40 @@ def test_owners_candidates_are_fixed_offsets_from_this_file() -> None:
# controls. The sandbox would then import that directory, and the sandbox holds the run's LLM
# credentials. Both candidates must stay fixed offsets from the engine's own file.
engine_dir = Path(gates.__file__).resolve().parent
- assert gates._OWNERS_PKG_CANDIDATES[0] == engine_dir.parents[3] / "packages" / "owners-yaml"
- assert gates._OWNERS_PKG_CANDIDATES[1] == engine_dir.parent / "owners"
+ assert gates._OWNERS_PKG_CANDIDATES == (
+ engine_dir.parent / "owners",
+ engine_dir.parents[3] / "packages" / "owners-yaml",
+ )
@pytest.mark.parametrize(
- "engine_file, resolver_dir",
+ "engine_file, resolver_dir, planted_dirs",
[
pytest.param(
"root/products/stamphog/packages/pr-approval-agent/gates.py",
"root/packages/owners-yaml",
+ (),
id="monorepo",
),
- pytest.param("root/tools/pr-approval-agent/gates.py", "root/tools/owners", id="sandbox-and-vendored"),
+ pytest.param("root/tools/pr-approval-agent/gates.py", "root/tools/owners", (), id="sandbox-and-vendored"),
+ pytest.param(
+ "root/a/b/c/pr-approval-agent/gates.py",
+ "root/a/b/c/owners",
+ ("root/packages/owners-yaml",),
+ id="sandbox-engine-moved-by-pr-symlink",
+ ),
],
)
def test_the_owners_package_is_found_in_both_engine_layouts(
- tmp_path: Path, engine_file: str, resolver_dir: str
+ tmp_path: Path, engine_file: str, resolver_dir: str, planted_dirs: tuple[str, ...]
) -> None:
expected = tmp_path / resolver_dir
- (expected / "owners_yaml").mkdir(parents=True)
+ for package_dir in (resolver_dir, *planted_dirs):
+ (tmp_path / package_dir / "owners_yaml").mkdir(parents=True)
engine_path = tmp_path / engine_file
engine_path.parent.mkdir(parents=True)
engine_path.touch()
found = [candidate for candidate in gates._owners_pkg_candidates(engine_path) if candidate.is_dir()]
- assert found == [expected.resolve()]
+ assert found[0] == expected.resolve()