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): keep non-http launch targets out of app routes (#104748)

posthog[bot]8c5ccf180c32
diff --git a/frontend/src/lib/lemon-ui/Link/Link.test.tsx b/frontend/src/lib/lemon-ui/Link/Link.test.tsx
index 67e96c170370..6bbb36484615 100644
--- a/frontend/src/lib/lemon-ui/Link/Link.test.tsx
+++ b/frontend/src/lib/lemon-ui/Link/Link.test.tsx
@@ -1,6 +1,7 @@
import '@testing-library/jest-dom'
import { fireEvent, render, screen } from '@testing-library/react'
+import { router } from 'kea-router'
import { Link } from './Link'
@@ -30,6 +31,30 @@ describe('Link', () => {
expect(href.replace(/\s/g, '').toLowerCase()).not.toMatch(/^javascript:/)
})
+ const schemeCases: [scheme: string, target: string][] = [
+ ['chrome-extension', 'chrome-extension://abcdefghijklmnop/index.html'],
+ ['capacitor', 'capacitor://localhost/home'],
+ ['https', 'https://example.com/page'],
+ ]
+
+ it.each(schemeCases)('keeps a %s: target out of the app routes', (scheme, target) => {
+ // The href alone is not enough: `router.actions.push` rejects a cross-origin URL with a
+ // `SecurityError`, so the click must reach the browser instead of the router.
+ const unmountRouter = router.mount()
+ const push = jest.spyOn(router.actions, 'push').mockImplementation(() => {})
+ const text = `launch ${scheme}`
+ render(<Link to={target}>{text}</Link>)
+
+ const anchor = screen.getByText(text).closest('a')
+ expect(anchor).toHaveAttribute('href', target)
+
+ fireEvent.click(anchor!)
+ expect(push).not.toHaveBeenCalled()
+
+ push.mockRestore()
+ unmountRouter()
+ })
+
// The command palette depends on modifier clicks NOT reaching the passed onClick, so it
// handles them in the capture phase instead. If this ever starts forwarding them, that
// palette handler and this onClick would both run, opening a new tab and navigating in place.
diff --git a/frontend/src/lib/lemon-ui/Link/Link.tsx b/frontend/src/lib/lemon-ui/Link/Link.tsx
index a826dad8d4bc..84be4c6a2ec2 100644
--- a/frontend/src/lib/lemon-ui/Link/Link.tsx
+++ b/frontend/src/lib/lemon-ui/Link/Link.tsx
@@ -90,8 +90,10 @@ const isPostHogDomain = (url: string): boolean => {
return /^https:\/\/((www|app|eu)\.)?posthog\.com/.test(url)
}
+// Any scheme-qualified target belongs to the browser, not to the app router. A target the router
+// claims gets the project prefix, and `/project/<id>/chrome-extension://…` matches no route.
const isDirectLink = (url: string): boolean => {
- return /^(mailto:|https?:\/\/|:\/\/)/.test(url)
+ return /^([a-zA-Z][a-zA-Z\d+\-.]*:|:\/\/)/.test(url)
}
/** Resolve a `to` target into a concrete href string. */
@@ -143,7 +145,9 @@ export const LinkPrimitive: React.FC<LinkPrimitiveProps & React.RefAttributes<HT
},
ref
) => {
- const externalLink = isExternalLink(to)
+ // `isExternalLink` knows http and mailto only, and `router.actions.push` rejects any other
+ // scheme with a `SecurityError`, so the scheme test widens the same exclusion.
+ const browserOwnedLink = isExternalLink(to) || (typeof to === 'string' && isDirectLink(to))
const { elementProps: draggableProps } = useLinkDrag(typeof to === 'string' ? to : undefined)
const onClick = (event: React.MouseEvent<HTMLElement>): void => {
@@ -159,7 +163,7 @@ export const LinkPrimitive: React.FC<LinkPrimitiveProps & React.RefAttributes<HT
return
}
- if (!target && to && !externalLink && !disableClientSideRouting && !shouldForcePageLoad(to)) {
+ if (!target && to && !browserOwnedLink && !disableClientSideRouting && !shouldForcePageLoad(to)) {
event.preventDefault()
if (to && to !== '#' && !preventClick) {
if (Array.isArray(to)) {