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(surveys): take hosted survey api host from the page origin (#102471)

Tom Piccirello850afe5f9eb8
diff --git a/posthog/templates/surveys/public_survey.html b/posthog/templates/surveys/public_survey.html
index 7f20d80a6ed4..3beb8cf7e7d9 100644
--- a/posthog/templates/surveys/public_survey.html
+++ b/posthog/templates/surveys/public_survey.html
@@ -418,7 +418,10 @@
const searchParams = new URLSearchParams(window.location.search);
const config = {
- api_host: projectConfig.api_host,
+ // A managed reverse proxy serves this page on the customer's domain and routes that domain's other
+ // paths to ingestion, so the SDK uses the origin in the address bar. The server does not render this
+ // value, because it could only read the domain from a request header that any client can set.
+ api_host: window.location.origin,
disable_surveys_automatic_display: true,
debug: searchParams.get('__posthog_debug__') === 'true',
// Disable all non-essential features
diff --git a/products/surveys/backend/api/survey.py b/products/surveys/backend/api/survey.py
index 1ade1bf83c02..af23fd53f70c 100644
--- a/products/surveys/backend/api/survey.py
+++ b/products/surveys/backend/api/survey.py
@@ -3770,7 +3770,6 @@ def public_survey_page(request: HttpRequest, survey_id: str) -> HttpResponse:
# Build project config
project_config = {
- "api_host": request.build_absolute_uri("/").rstrip("/"),
"token": survey.team.api_token,
}
diff --git a/products/surveys/backend/api/test/test_external_surveys.py b/products/surveys/backend/api/test/test_external_surveys.py
index a859b930f84d..3b258b2c1aa3 100644
--- a/products/surveys/backend/api/test/test_external_surveys.py
+++ b/products/surveys/backend/api/test/test_external_surveys.py
@@ -347,15 +347,18 @@ class TestExternalSurveys(APIBaseTest):
assert config_match is not None
project_config = json.loads(config_match.group(1))
- assert "api_host" in project_config
assert "token" in project_config
- @override_settings(SITE_URL=EXTERNAL_SITE_URL)
+ @override_settings(SITE_URL=EXTERNAL_SITE_URL, USE_X_FORWARDED_HOST=True)
def test_assets_load_from_app_origin_behind_reverse_proxy(self):
survey = self.create_external_survey()
# Simulate serving the page through a reverse-proxy domain
- response = self.client.get(f"/external_surveys/{survey.id}/", HTTP_HOST="surveys.proxy-domain.example.com")
+ response = self.client.get(
+ f"/external_surveys/{survey.id}/",
+ HTTP_HOST="surveys.proxy-domain.example.com",
+ HTTP_X_FORWARDED_HOST="forwarded.proxy-domain.example.com",
+ )
assert response.status_code == 200
content = response.content.decode()
@@ -366,10 +369,10 @@ class TestExternalSurveys(APIBaseTest):
for href in self.get_stylesheet_hrefs(content)
)
assert 'href="/static/' not in content
- # Capture must keep following the request host so events route through the proxy
- project_config = self.get_json_script_value(content, "project-config")
- assert isinstance(project_config, dict)
- assert project_config["api_host"] == "http://surveys.proxy-domain.example.com"
+ # The page script takes the capture host from the browser URL. A client can set these
+ # headers, so neither may reach the page and choose where the SDK loads from.
+ assert "proxy-domain.example.com" not in content
+ assert "api_host: window.location.origin," in content
# Error pages are served through proxies too and must not emit relative asset links
error_response = self.client.get("/external_surveys/not-a-uuid/", HTTP_HOST="surveys.proxy-domain.example.com")