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(notebooks): fall back to SITE_URL for sandbox callback URLs in prod (#73018)

Sinan Kucukkoselerc9a75348a92d
diff --git a/products/notebooks/backend/sql_v2.py b/products/notebooks/backend/sql_v2.py
index d6b5991f1d0e..aa6ba984fce0 100644
--- a/products/notebooks/backend/sql_v2.py
+++ b/products/notebooks/backend/sql_v2.py
@@ -125,9 +125,16 @@ def mint_command_token(secret: str, run_id: str, ttl_seconds: int = _COMMAND_TOK
def _backend_base_url() -> str:
- # The sandbox reaches the host backend here. Docker maps localhost -> host.docker.internal,
- # so default to that for local dev; SANDBOX_API_URL overrides (e.g. ngrok for Modal).
- base = getattr(settings, "SANDBOX_API_URL", None) or "http://host.docker.internal:8000"
+ # The sandbox reaches the host backend here. SANDBOX_API_URL overrides (e.g. ngrok for Modal
+ # from local dev). In dev the kernel runs in local Docker, where the host is
+ # host.docker.internal (SITE_URL would be an unreachable localhost); in prod the kernel runs
+ # remotely and must use the public SITE_URL.
+ if settings.SANDBOX_API_URL:
+ base = settings.SANDBOX_API_URL
+ elif settings.DEBUG:
+ base = "http://host.docker.internal:8000"
+ else:
+ base = settings.SITE_URL
return base.rstrip("/")
diff --git a/products/notebooks/backend/test/test_sql_v2.py b/products/notebooks/backend/test/test_sql_v2.py
index 2704cf6460d9..9f992515d743 100644
--- a/products/notebooks/backend/test/test_sql_v2.py
+++ b/products/notebooks/backend/test/test_sql_v2.py
@@ -16,7 +16,7 @@ from unittest.mock import patch
from django.core import signing
from django.core.cache import cache
-from django.test import SimpleTestCase
+from django.test import SimpleTestCase, override_settings
from parameterized import parameterized
@@ -41,6 +41,7 @@ from products.notebooks.backend.sandbox.kernel.data_plane import (
from products.notebooks.backend.sql_v2 import (
SQLV2KernelNotRunning,
SQLV2PageError,
+ build_callback_url,
dispatch_sql_v2_run,
ensure_sql_v2_server,
fetch_sql_v2_page,
@@ -82,6 +83,19 @@ def _restrict_query_access(test: APIBaseTest) -> None:
cache.clear()
+class TestSQLV2BackendBaseURL(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("explicit_override", "https://tunnel.example.dev/", True, "https://tunnel.example.dev"),
+ ("dev_fallback", None, True, "http://host.docker.internal:8000"),
+ ("prod_fallback", None, False, "https://us.posthog.com"),
+ ]
+ )
+ def test_callback_url_base(self, _name: str, sandbox_api_url: str | None, debug: bool, expected_base: str) -> None:
+ with override_settings(SANDBOX_API_URL=sandbox_api_url, DEBUG=debug, SITE_URL="https://us.posthog.com"):
+ assert build_callback_url("run-1") == f"{expected_base}/internal/notebooks/runs/run-1/result/"
+
+
class TestSQLV2Callback(APIBaseTest):
def setUp(self):
super().setUp()