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(toolbar): accept relative api_host reverse-proxy configs (#72854)

Xander396fb800f41b
diff --git a/frontend/src/toolbar/toolbarConfigLogic.test.ts b/frontend/src/toolbar/toolbarConfigLogic.test.ts
index e31eb3d17e18..cb58791f1454 100644
--- a/frontend/src/toolbar/toolbarConfigLogic.test.ts
+++ b/frontend/src/toolbar/toolbarConfigLogic.test.ts
@@ -1323,6 +1323,18 @@ describe('toolbar toolbarConfigLogic', () => {
expect(logic.values.apiHost).toBe('https://proxy.example.com/ingest')
})
+ it('resolves a relative reverse-proxy api_host against the current origin', () => {
+ // posthog-js commonly points api_host at a relative reverse-proxy path
+ // like /ingest (Next.js/Vercel rewrites). The toolbar must keep that
+ // prefix rather than reject it and fall back to the bare origin.
+ const logic = toolbarConfigLogic.build({
+ posthog: { config: { api_host: '/ingest' } } as any,
+ } as any)
+ logic.mount()
+ expect(logic.values.apiHost).toBe(`${window.location.origin}/ingest`)
+ expect(logic.values.apiHostResolution.source).toBe('posthog_api_host')
+ })
+
it.each([
['apiURL', { apiURL: '' }],
['api_host', { apiURL: '', posthog: { config: { api_host: '' } } as any }],
@@ -1375,13 +1387,23 @@ describe('toolbar toolbarConfigLogic', () => {
['https://proxy.example.com/ingest/', 'https://proxy.example.com/ingest'],
['https://us.i.posthog.com', 'https://us.i.posthog.com'],
['https://us.i.posthog.com/', 'https://us.i.posthog.com'],
+ // relative reverse-proxy configs resolve against the current origin
+ ['/ingest', `${window.location.origin}/ingest`],
+ ['/ingest/', `${window.location.origin}/ingest`],
// rejected
['javascript:alert(1)', null],
['https://user:pass@host/path', null],
['https://[email protected]/path', null],
+ ['//evil.com', null], // protocol-relative would hijack the origin
+ ['/\\evil.com', null], // backslash protocol-relative
+ // tab/newline/CR are stripped by the URL parser, turning these into
+ // protocol-relative refs that would otherwise hijack the origin
+ ['/\t/evil.com', null],
+ ['/\n/evil.com', null],
+ ['/\r/evil.com', null],
['', null],
[undefined, null],
- ['not a url', null],
+ ['not a url', null], // non-path garbage is not treated as relative
])('canonicalizeApiHost(%p) === %p', (input, expected) => {
expect(canonicalizeApiHost(input as any)).toBe(expected)
})
diff --git a/frontend/src/toolbar/toolbarConfigLogic.ts b/frontend/src/toolbar/toolbarConfigLogic.ts
index 8c2e1a44a45e..630bfd9d867c 100644
--- a/frontend/src/toolbar/toolbarConfigLogic.ts
+++ b/frontend/src/toolbar/toolbarConfigLogic.ts
@@ -544,16 +544,29 @@ export function canonicalizeUiHost(candidate: string | undefined | null): string
/**
* Sanitize an apiHost candidate. Like canonicalizeUiHost, but preserves the URL
- * path so reverse-proxy deployments (e.g. `https://proxy/ingest`) keep working
- * — apiHost is concatenated with `/static/toolbar.css` and `/i/v1/logs`, so the
- * path prefix must survive. Query and fragment are still dropped.
+ * path so reverse-proxy deployments keep working, whether configured absolutely
+ * (`https://proxy/ingest`) or as a relative path (`/ingest`, resolved against the
+ * current origin). apiHost is concatenated with `/static/toolbar.css` and
+ * `/i/v1/logs`, so the path prefix must survive. Query and fragment are dropped.
*/
export function canonicalizeApiHost(candidate: string | undefined | null): string | null {
if (!candidate) {
return null
}
try {
- const parsed = new URL(candidate)
+ // Path-absolute values like "/ingest" are relative reverse-proxy configs;
+ // resolve them against the current origin. Protocol-relative refs
+ // ("//evil.com", "/\evil.com") fall through to base-less parsing and are
+ // rejected, as is any non-path garbage.
+ const trimmed = candidate.trim()
+ const isPathAbsolute = /^\/(?![/\\])/.test(trimmed)
+ const parsed = isPathAbsolute ? new URL(trimmed, window.location.origin) : new URL(candidate)
+ // The regex can't see tab/newline/CR that the WHATWG parser strips mid-string,
+ // so "/\t/evil.com" would normalize to a network-path ref and hijack the origin.
+ // Verify the resolved origin instead of trusting the textual guard.
+ if (isPathAbsolute && parsed.origin !== window.location.origin) {
+ return null
+ }
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
return null
}