Skip to content

Commit

Permalink
fix: Option to redirect app.posthog.com traffic to us.posthog.com (#1…
Browse files Browse the repository at this point in the history
…9377)

* fix: Use app. as capture endpoint when using us.posthog.com

* fix

* fix

* Update query snapshots

* Update posthog/urls.py

Co-authored-by: Raquel Smith <[email protected]>

* fix

* fixi

* Update UI snapshots for `chromium` (2)

* Update query snapshots

* fix

* Update UI snapshots for `chromium` (2)

* safety

* use dynamic settings

* fix test

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Raquel Smith <[email protected]>
  • Loading branch information
3 people authored Dec 17, 2023
1 parent 7e8fc23 commit 4703409
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion frontend/src/lib/components/JSSnippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export function JSSnippet(): JSX.Element {
return (
<CodeSnippet language={Language.HTML}>{`<script>
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys onSessionId".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
posthog.init('${currentTeam?.api_token}',{api_host:'${window.location.origin}'})
posthog.init('${currentTeam?.api_token}',{api_host:'${
window.location.origin === 'https://us.posthog.com' ? 'https://app.posthog.com' : window.location.origin
}'})
</script>`}</CodeSnippet>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ describe('redirectToLoggedInInstance', () => {
['handles the sneaky string', ' ', null],
['handles not URLs', 'yo ho ho', null],
['handles EU', 'https://eu.posthog.com', 'eu'],
['handles US', 'https://app.posthog.com', 'app'],
['handles app', 'https://app.posthog.com', null],
['handles US', 'https://us.posthog.com', 'us'],
['handles leading quotes', '"https://eu.posthog.com', 'eu'],
['handles trailing quotes', 'https://eu.posthog.com"', 'eu'],
['handles wrapping quotes', '"https://eu.posthog.com"', 'eu'],
['handles ports', 'https://app.posthog.com:8123', 'app'],
['handles longer urls', 'https://app.posthog.com:1234?query=parameter#hashParam', 'app'],
['handles ports', 'https://us.posthog.com:8123', 'us'],
['handles longer urls', 'https://eu.posthog.com:1234?query=parameter#hashParam', 'eu'],
])('%s', (_name, cookie, expected) => {
expect(cleanedCookieSubdomain(cookie)).toEqual(expected)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const PH_CURRENT_INSTANCE = 'ph_current_instance'

const REDIRECT_TIMEOUT = 2500

type Subdomain = 'eu' | 'app'
type Subdomain = 'eu' | 'us'

export function cleanedCookieSubdomain(loggedInInstance: string | null): Subdomain | null {
try {
Expand All @@ -42,10 +42,10 @@ export function cleanedCookieSubdomain(loggedInInstance: string | null): Subdoma
// convert to URL, so that we can be sure we're dealing with a valid URL
const hostname = new URL(url).hostname
switch (hostname) {
case 'app.posthog.com':
return 'app'
case 'eu.posthog.com':
return 'eu'
case 'us.posthog.com':
return 'us'
default:
return null
}
Expand All @@ -58,7 +58,7 @@ export function cleanedCookieSubdomain(loggedInInstance: string | null): Subdoma

function regionFromSubdomain(subdomain: Subdomain): 'EU' | 'US' {
switch (subdomain) {
case 'app':
case 'us':
return 'US'
case 'eu':
return 'EU'
Expand Down
6 changes: 6 additions & 0 deletions posthog/settings/dynamic_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@
"The number of rows that the heatmap query tries to sample.",
int,
),
"REDIRECT_APP_TO_US": (
get_from_env("REDIRECT_APP_TO_US", False, type_cast=str_to_bool),
"Temporary option to redirect all app traffic from app.posthog.com to us.posthog.com.",
bool,
),
}

SETTINGS_ALLOWING_API_OVERRIDE = (
Expand Down Expand Up @@ -225,6 +230,7 @@
"SENTRY_AUTH_TOKEN",
"SENTRY_ORGANIZATION",
"HEATMAP_SAMPLE_N",
"REDIRECT_APP_TO_US",
)

# SECRET_SETTINGS can only be updated but will never be exposed through the API (we do store them plain text in the DB)
Expand Down
8 changes: 7 additions & 1 deletion posthog/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Any, Callable, List, Optional, cast
from posthog.models.instance_setting import get_instance_setting
from urllib.parse import urlparse

from django.conf import settings
from django.http import HttpRequest, HttpResponse, HttpResponseServerError
from django.http import HttpRequest, HttpResponse, HttpResponseServerError, HttpResponseRedirect
from django.template import loader
from django.urls import URLPattern, include, path, re_path
from django.views.decorators.csrf import (
Expand All @@ -17,6 +18,7 @@
SpectacularSwaggerView,
)
from revproxy.views import ProxyView
from django.utils.http import url_has_allowed_host_and_scheme
from sentry_sdk import last_event_id
from two_factor.urls import urlpatterns as tf_urls

Expand Down Expand Up @@ -90,6 +92,10 @@ def handler500(request):

@ensure_csrf_cookie
def home(request, *args, **kwargs):
if request.get_host().split(":")[0] == "app.posthog.com" and get_instance_setting("REDIRECT_APP_TO_US"):
url = "https://us.posthog.com{}".format(request.get_full_path())
if url_has_allowed_host_and_scheme(url, "us.posthog.com", True):
return HttpResponseRedirect(url)
return render_template("index.html", request)


Expand Down

0 comments on commit 4703409

Please sign in to comment.