: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(cloud-agent): block unsigned git commits in agent sandbox (#59396)
Alessandro Pogliaghi2374e704267a
diff --git a/products/tasks/backend/sandbox/images/Dockerfile.sandbox-base b/products/tasks/backend/sandbox/images/Dockerfile.sandbox-baseindex 275bb9ff7b67..37bd0bf3b6ec 100644--- a/products/tasks/backend/sandbox/images/Dockerfile.sandbox-base+++ b/products/tasks/backend/sandbox/images/Dockerfile.sandbox-base@@ -6,6 +6,9 @@ FROM ubuntu:24.04ENV DEBIAN_FRONTEND=noninteractiveENV TZ=UTC+# Remove gh CLI telemetry+ENV GH_TELEMETRY=false+# Install system packages (expanded for coding environments)RUN apt-get update && \apt-get install -y --no-install-recommends \@@ -59,7 +62,7 @@ RUN curl -fsSL https://deb.nodesource.com/setup_24.x | bash - && \RUN npm install -g yarn pnpm typescript ts-node nodemon# Install GitHub CLI without adding another apt repository refresh.-ARG GH_CLI_VERSION=2.89.0+ARG GH_CLI_VERSION=2.92.0RUN set -eux; \arch="$(dpkg --print-architecture)"; \curl -fsSL -o /tmp/gh.deb \@@ -114,6 +117,11 @@ RUN chmod +x /tmp/install-skills.sh && \RUN git config --global user.email "[email protected]" && \git config --global user.name "PostHog Code"+# Block `git commit`/`git push` so unsigned commits cannot leave the sandbox+COPY products/tasks/backend/sandbox/images/git-guard.sh /opt/posthog/bin/git+RUN chmod +x /opt/posthog/bin/git+ENV PATH="/opt/posthog/bin:${PATH}"+# This is required for the Claude Code SDK to allow --dangerously-skip-permissions as the root userENV IS_SANDBOX=1diff --git a/products/tasks/backend/sandbox/images/Dockerfile.sandbox-notebook b/products/tasks/backend/sandbox/images/Dockerfile.sandbox-notebookindex b97742d04af7..706ef0e84cd8 100644--- a/products/tasks/backend/sandbox/images/Dockerfile.sandbox-notebook+++ b/products/tasks/backend/sandbox/images/Dockerfile.sandbox-notebook@@ -88,6 +88,11 @@ RUN cd /scripts && \RUN git config --global user.email "[email protected]" && \git config --global user.name "PostHog Code"+# Block `git commit`/`git push` so unsigned commits cannot leave the sandbox.+COPY products/tasks/backend/sandbox/images/git-guard.sh /opt/posthog/bin/git+RUN chmod +x /opt/posthog/bin/git+ENV PATH="/opt/posthog/bin:${PATH}"+# This is required for the Claude Code SDK to allow --dangerously-skip-permissions as the root userENV IS_SANDBOX=1diff --git a/products/tasks/backend/sandbox/images/git-guard.sh b/products/tasks/backend/sandbox/images/git-guard.shnew file mode 100644index 000000000000..57e22f3d8473--- /dev/null+++ b/products/tasks/backend/sandbox/images/git-guard.sh@@ -0,0 +1,57 @@+#!/bin/sh+# git guard.+#+# Installed first on PATH inside the cloud sandbox image. Blocks `git commit`+# and `git push` so unsigned commits cannot leave the sandbox.+# All other git subcommands (including the tool's own add/fetch/reset/+# ls-remote/update-ref) pass straight through to the real git.+#+# Escape hatch: set POSTHOG_ALLOW_UNSIGNED_GIT=1 to bypass (debugging only).++native_git=""+for candidate in /usr/bin/git /usr/local/bin/git /bin/git; do+ if [ -x "$candidate" ] && [ "$candidate" != "/opt/posthog/bin/git" ]; then+ native_git="$candidate"+ break+ fi+done+if [ -z "$native_git" ]; then+ echo "git-guard: could not locate the real git binary" >&2+ exit 127+fi++if [ "${POSTHOG_ALLOW_UNSIGNED_GIT:-}" = "1" ]; then+ exec "$native_git" "$@"+fi++# Find the subcommand, skipping git-level global options (and the ones that take+# a value), so `git -C path commit` is caught the same as `git commit`.+subcommand=""+skip_next=0+for arg in "$@"; do+ if [ "$skip_next" = "1" ]; then+ skip_next=0+ continue+ fi+ case "$arg" in+ -C|-c|--git-dir|--work-tree|--namespace|--exec-path)+ skip_next=1+ ;;+ --git-dir=*|--work-tree=*|--namespace=*|--exec-path=*|-*)+ ;;+ *)+ subcommand="$arg"+ break+ ;;+ esac+done++case "$subcommand" in+ commit|push)+ echo "git $subcommand is disabled in PostHog Code: commits must be signed." >&2+ echo "Stage changes with 'git add', then call the git_signed_commit tool." >&2+ exit 1+ ;;+esac++exec "$native_git" "$@"diff --git a/products/tasks/backend/services/modal_sandbox.py b/products/tasks/backend/services/modal_sandbox.pyindex acd8862ab013..9edbd9cd00a1 100644--- a/products/tasks/backend/services/modal_sandbox.py+++ b/products/tasks/backend/services/modal_sandbox.py@@ -92,6 +92,7 @@ LOCAL_MODAL_DOCKERFILES = {SandboxTemplate.NOTEBOOK_BASE: Path("products/tasks/backend/sandbox/images/Dockerfile.sandbox-notebook"),}LOCAL_MODAL_INSTALL_SKILLS_SCRIPT = Path("products/tasks/backend/sandbox/images/install-skills.sh")+LOCAL_MODAL_GIT_GUARD_SCRIPT = Path("products/tasks/backend/sandbox/images/git-guard.sh")_image_ref_cache: TTLCache = TTLCache(maxsize=2, ttl=300)@@ -245,6 +246,12 @@ def _prepare_local_modal_build_context(template: SandboxTemplate) -> tuple[str,destination_dockerfile_path.parent.mkdir(parents=True, exist_ok=True)shutil.copy2(source_dockerfile_path, destination_dockerfile_path)+ # Both base and notebook Dockerfiles COPY the git guard, so include it in+ # every local build context.+ destination_git_guard_path = context_dir / LOCAL_MODAL_GIT_GUARD_SCRIPT+ destination_git_guard_path.parent.mkdir(parents=True, exist_ok=True)+ shutil.copy2(base_dir / LOCAL_MODAL_GIT_GUARD_SCRIPT, destination_git_guard_path)+if template == SandboxTemplate.DEFAULT_BASE:source_install_script_path = base_dir / LOCAL_MODAL_INSTALL_SKILLS_SCRIPTdestination_install_script_path = context_dir / LOCAL_MODAL_INSTALL_SKILLS_SCRIPT