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(vercel): keep localhost vite URL on https requests (#56378)

Matt Brooker742cb16dbbd7
diff --git a/ee/api/vercel/README.md b/ee/api/vercel/README.md
index 60ce28c2c83e..1f6a73749ee6 100644
--- a/ee/api/vercel/README.md
+++ b/ee/api/vercel/README.md
@@ -141,9 +141,10 @@ You can verify by checking `curl -s -o /dev/null -w "%{http_code}" http://localh
### `JS_URL` doesn't affect Vite dev scripts
-In development, the Vite dev server script tags (`@vite/client`, `@react-refresh`, `src/index.tsx`) are hardcoded to `http://localhost:8234` in `posthog/utils.py`.
+In development, the Vite dev server script tags (`@vite/client`, `@react-refresh`, `src/index.tsx`) are emitted by `get_js_url()` in `posthog/utils.py`.
+When the page is served over HTTPS (e.g. via ngrok), the URL stays as `http://localhost:8234` so the browser can load Vite assets directly — browsers exempt `localhost` from mixed-content blocking.
+For HTTP requests where `JS_URL=http://localhost:8234`, the host is rewritten to the request origin so non-localhost dev environments (Docker containers, sandboxes) can still reach Vite.
`JS_URL` only sets `window.JS_URL` for the production bundle loader.
-Browsers exempt `localhost` from mixed content blocking, so this works even when the page is served over HTTPS via ngrok.
### 1Password shows "<concealed by 1Password>" in logs
diff --git a/posthog/test/test_utils.py b/posthog/test/test_utils.py
index cadc615d0a4b..ddc44ac24af8 100644
--- a/posthog/test/test_utils.py
+++ b/posthog/test/test_utils.py
@@ -33,6 +33,7 @@ from posthog.utils import (
get_default_event_info,
get_default_event_name,
get_ip_address,
+ get_js_url,
get_short_user_agent,
load_data_from_request,
refresh_requested_by_client,
@@ -165,6 +166,75 @@ class TestFormatUrls(TestCase):
self.assertEqual("https://www.testserver", format_query_params_absolute_url(request))
+class TestGetJsUrl(TestCase):
+ factory = RequestFactory()
+
+ @parameterized.expand(
+ [
+ (
+ "http_rewrites_host",
+ True,
+ "http://localhost:8234",
+ "dev-container:8000",
+ False,
+ "http://dev-container:8234",
+ ),
+ (
+ "https_keeps_localhost",
+ True,
+ "http://localhost:8234",
+ "my-tunnel.ngrok-free.dev",
+ True,
+ "http://localhost:8234",
+ ),
+ (
+ "non_localhost_unchanged",
+ True,
+ "https://cdn.example.com/static",
+ "dev-container:8000",
+ False,
+ "https://cdn.example.com/static",
+ ),
+ (
+ "non_debug_unchanged",
+ False,
+ "http://localhost:8234",
+ "dev-container:8000",
+ False,
+ "http://localhost:8234",
+ ),
+ (
+ "http_rewrites_ipv6_host",
+ True,
+ "http://localhost:8234",
+ "[::1]:8000",
+ False,
+ "http://[::1]:8234",
+ ),
+ (
+ "http_rewrites_host_without_port",
+ True,
+ "http://localhost:8234",
+ "dev-container",
+ False,
+ "http://dev-container:8234",
+ ),
+ ]
+ )
+ def test_get_js_url(
+ self, _name: str, debug: bool, js_url: str, http_host: str, is_https: bool, expected: str
+ ) -> None:
+ settings_kwargs: dict = {"DEBUG": debug, "JS_URL": js_url}
+ if is_https:
+ settings_kwargs["SECURE_PROXY_SSL_HEADER"] = ("HTTP_X_FORWARDED_PROTO", "https")
+ with self.settings(**settings_kwargs):
+ if is_https:
+ request = self.factory.get("/", HTTP_HOST=http_host, HTTP_X_FORWARDED_PROTO="https")
+ else:
+ request = self.factory.get("/", HTTP_HOST=http_host)
+ self.assertEqual(expected, get_js_url(request))
+
+
class TestGeneralUtils(TestCase):
def test_available_timezones(self):
timezones = get_available_timezones_with_offsets()
diff --git a/posthog/utils.py b/posthog/utils.py
index 2dc0b983efa7..176212e5771d 100644
--- a/posthog/utils.py
+++ b/posthog/utils.py
@@ -382,13 +382,19 @@ def get_js_url(request: HttpRequest) -> str:
"""
from urllib.parse import urlparse
+ from django.http.request import split_domain_port
+
parsed = urlparse(settings.JS_URL)
- if settings.DEBUG and parsed.hostname == "localhost":
+ if settings.DEBUG and parsed.hostname == "localhost" and not request.is_secure():
# Rewrite the JS_URL hostname to match the request origin so the browser
# can reach the Vite dev server when accessed via a non-localhost address
- # (e.g. from a Docker container or remote host).
+ # (e.g. from a Docker container or remote host). Skipped when the request
+ # is HTTPS (e.g. ngrok) — browsers exempt localhost from mixed-content blocking,
+ # so keeping http://localhost:8234 lets the browser load Vite assets directly.
+ # split_domain_port keeps IPv6 hosts wrapped in brackets so the URL stays valid.
+ domain, _ = split_domain_port(request.get_host())
# nosemgrep: python.flask.security.audit.directly-returned-format-string.directly-returned-format-string
- return f"http://{request.get_host().split(':')[0]}:{parsed.port}"
+ return f"http://{domain}:{parsed.port}"
return settings.JS_URL