: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(search): validate cmd+k shift+enter href before opening in new tab (#60566)
Rafael Audibert63d6bdc0982d
diff --git a/frontend/src/lib/components/Search/Search.tsx b/frontend/src/lib/components/Search/Search.tsxindex 1793e1b41801..149608a47c34 100644--- a/frontend/src/lib/components/Search/Search.tsx+++ b/frontend/src/lib/components/Search/Search.tsx@@ -26,6 +26,7 @@ import { ButtonPrimitive } from 'lib/ui/Button/ButtonPrimitives'import { ContextMenu, ContextMenuContent, ContextMenuGroup, ContextMenuTrigger } from 'lib/ui/ContextMenu/ContextMenu'import { Label } from 'lib/ui/Label/Label'import { WrappingLoadingSkeleton } from 'lib/ui/WrappingLoadingSkeleton/WrappingLoadingSkeleton'+import { getRelativeNextPath } from 'lib/utils'import { cn } from 'lib/utils/css-classes'import { urls } from 'scenes/urls'import { userLogic } from 'scenes/userLogic'@@ -666,7 +667,10 @@ function SearchInput({ autoFocus, className }: SearchInputProps): JSX.Element {e.stopPropagation()const item = highlightedItemRef.currentif (item?.href) {- window.open(item.href, '_blank', 'noopener,noreferrer')+ const safePath = getRelativeNextPath(item.href, window.location)+ if (safePath) {+ window.open(safePath, '_blank', 'noopener,noreferrer')+ }}}if (e.key === 'Tab' && showAskAiLink && searchValue.trim()) {diff --git a/frontend/src/lib/utils.test.ts b/frontend/src/lib/utils.test.tsindex f2f9015e4376..6ece06cfa1af 100644--- a/frontend/src/lib/utils.test.ts+++ b/frontend/src/lib/utils.test.ts@@ -1206,6 +1206,16 @@ describe('lib/utils', () => {it('returns null for encoded protocol-relative URL', () => {expect(getRelativeNextPath('%2F%2Fevil.com%2Ftest', location)).toBeNull()})++ it.each([+ ['/\\evil.com/path', '/-then-backslash'],+ ['/\\\\evil.com/path', '/-then-two-backslashes'],+ ['%2F%5Cevil.com%2Fpath', 'encoded /-then-backslash'],+ ])('returns null for backslash external bypass (%s — %s)', (input) => {+ // Browsers normalize backslashes in special-scheme URLs per WHATWG, so /\\evil.com+ // resolves to //evil.com and escapes the origin.+ expect(getRelativeNextPath(input, location)).toBeNull()+ })})describe('parseTagsFilter()', () => {diff --git a/frontend/src/lib/utils.tsx b/frontend/src/lib/utils.tsxindex a322ca009120..6e286f07c15d 100644--- a/frontend/src/lib/utils.tsx+++ b/frontend/src/lib/utils.tsx@@ -2470,9 +2470,19 @@ export function getRelativeNextPath(nextPath: string | null | undefined, locatioreturn null}- // Root-relative path+ // 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('/')) {- return decoded+ try {+ const url = new URL(decoded, location.origin)+ if (url.origin !== location.origin) {+ return null+ }+ return url.pathname + url.search + url.hash+ } catch {+ return null+ }}// Try to parse as a full URL