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(auth): preserve encoded query params in login next redirect (#70437)

Ben Leac0f354aad2b0
diff --git a/frontend/src/lib/utils/url.test.ts b/frontend/src/lib/utils/url.test.ts
index 57b923c302df..3be855ab2f0a 100644
--- a/frontend/src/lib/utils/url.test.ts
+++ b/frontend/src/lib/utils/url.test.ts
@@ -159,6 +159,20 @@ describe('url utils', () => {
expect(getRelativeNextPath('%2Ftest%2Ffoo%3Fbar%3Dbaz%23hash', location)).toBe('/test/foo?bar=baz#hash')
})
+ it('preserves percent-encoded characters in nested query params', () => {
+ // e.g. docs "Run in PostHog" links: ?next=/sql?open_query=SELECT%0A... — decoding
+ // again would turn %0A into a raw newline that new URL() strips, corrupting the SQL
+ expect(getRelativeNextPath('/sql?open_query=SELECT%0A++properties.%24mcp+AS+tool', location)).toBe(
+ '/sql?open_query=SELECT%0A++properties.%24mcp+AS+tool'
+ )
+ })
+
+ it('preserves percent-encoded query params in same-origin absolute URL', () => {
+ expect(getRelativeNextPath('https://us.posthog.com/sql?open_query=SELECT%0A%2A', location)).toBe(
+ '/sql?open_query=SELECT%0A%2A'
+ )
+ })
+
it('returns null for encoded protocol-relative URL', () => {
expect(getRelativeNextPath('%2F%2Fevil.com%2Ftest', location)).toBeNull()
})
diff --git a/frontend/src/lib/utils/url.ts b/frontend/src/lib/utils/url.ts
index 6737d9fe902f..7b20b7981c3c 100644
--- a/frontend/src/lib/utils/url.ts
+++ b/frontend/src/lib/utils/url.ts
@@ -189,28 +189,18 @@ export function parseGithubRepoURL(url: string): Record<string, string> {
return { user, repo, type, path }
}
-export function getRelativeNextPath(nextPath: string | null | undefined, location: Location): string | null {
- if (!nextPath || typeof nextPath !== 'string') {
- return null
- }
- let decoded: string
- try {
- decoded = decodeURIComponent(nextPath)
- } catch {
- decoded = nextPath
- }
-
+function resolveSameOriginPath(candidate: string, location: Location): string | null {
// Protocol-relative URLs (e.g., //evil.com/test) are not allowed
- if (decoded.startsWith('//')) {
+ if (candidate.startsWith('//')) {
return null
}
// Root-relative path — resolve against the current origin and verify it doesn't escape.
// Browsers normalize backslashes in special-scheme URLs per WHATWG, so a raw startsWith('/')
// check would accept '/\\evil.com/path', which the browser then loads as '//evil.com/path'.
- if (decoded.startsWith('/')) {
+ if (candidate.startsWith('/')) {
try {
- const url = new URL(decoded, location.origin)
+ const url = new URL(candidate, location.origin)
if (url.origin !== location.origin) {
return null
}
@@ -222,7 +212,7 @@ export function getRelativeNextPath(nextPath: string | null | undefined, locatio
// Try to parse as a full URL
try {
- const url = new URL(decoded)
+ const url = new URL(candidate)
if ((url.protocol === 'http:' || url.protocol === 'https:') && url.origin === location.origin) {
return url.pathname + url.search + url.hash
}
@@ -231,3 +221,26 @@ export function getRelativeNextPath(nextPath: string | null | undefined, locatio
return null
}
}
+
+export function getRelativeNextPath(nextPath: string | null | undefined, location: Location): string | null {
+ if (!nextPath || typeof nextPath !== 'string') {
+ return null
+ }
+
+ // Callers pass a value that URL parsing already decoded once (kea-router searchParams or
+ // URLSearchParams.get), so use it verbatim: decoding again would corrupt percent-encoded
+ // characters belonging to nested query params — e.g. `next=/sql?open_query=SELECT%0A...`,
+ // where a second decode turns %0A into a raw newline that new URL() then silently strips,
+ // gluing the SQL into one line.
+ const verbatim = resolveSameOriginPath(nextPath, location)
+ if (verbatim !== null) {
+ return verbatim
+ }
+
+ // Fall back to decoding once for values that arrive still fully encoded (e.g. %2Ftest%2Ffoo).
+ try {
+ return resolveSameOriginPath(decodeURIComponent(nextPath), location)
+ } catch {
+ return null
+ }
+}
diff --git a/frontend/src/scenes/authentication/login/loginLogic.test.ts b/frontend/src/scenes/authentication/login/loginLogic.test.ts
index 393a005489a8..ece5f3876f7c 100644
--- a/frontend/src/scenes/authentication/login/loginLogic.test.ts
+++ b/frontend/src/scenes/authentication/login/loginLogic.test.ts
@@ -78,6 +78,13 @@ describe('loginLogic', () => {
['//foo.bar', '/'],
['/bla?haha', '/bla?haha'],
['/bla?haha#hoho', '/bla?haha#hoho'],
+ // Percent-encoded chars nested in next's own query params must survive the redirect
+ // (e.g. docs "Run in PostHog" links carrying %0A newlines in open_query); the router
+ // round-trip normalizes form-encoded "+" spaces to "%20", which decodes the same
+ [
+ '/sql?open_query=SELECT%0A++properties.%24mcp+AS+tool',
+ '/sql?open_query=SELECT%0A%20%20properties.%24mcp%20AS%20tool',
+ ],
]
for (const [next, result] of matches) {