: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(frontend): validate table cell data and tree menu links (#127334)
Paul D'Ambradb7c0c34042f
diff --git a/frontend/src/layout/panel-layout/ProjectTree/menus/BrowserLikeMenuItems.test.tsx b/frontend/src/layout/panel-layout/ProjectTree/menus/BrowserLikeMenuItems.test.tsxnew file mode 100644index 000000000000..b2edefcb1606--- /dev/null+++ b/frontend/src/layout/panel-layout/ProjectTree/menus/BrowserLikeMenuItems.test.tsx@@ -0,0 +1,41 @@+import { cleanup, fireEvent, render, screen } from '@testing-library/react'++import { DropdownMenu, DropdownMenuContent } from 'lib/ui/DropdownMenu/DropdownMenu'++import { BrowserLikeMenuItems } from './BrowserLikeMenuItems'++describe('BrowserLikeMenuItems', () => {+ afterEach(() => {+ cleanup()+ jest.restoreAllMocks()+ })++ it.each([+ ['javascript:void(0)', false],+ [' JaVaScRiPt:void(0)', false],+ ['java\tscr\nipt:void(0)', false],+ ['vbscript:MsgBox(1)', false],+ ['/project/1/insights', true],+ ['https://example.com/report', true],+ ])('checks the scheme before opening %s', (href, allowed) => {+ const open = jest.spyOn(window, 'open').mockImplementation(() => null)+ const onClick = jest.fn()+ render(+ <DropdownMenu defaultOpen>+ <DropdownMenuContent>+ <BrowserLikeMenuItems href={href} onClick={onClick} />+ </DropdownMenuContent>+ </DropdownMenu>+ )++ fireEvent.click(screen.getByText('Open link in new browser tab'))++ if (allowed) {+ expect(open).toHaveBeenCalledWith(href, '_blank')+ expect(onClick).toHaveBeenCalledTimes(1)+ } else {+ expect(open).not.toHaveBeenCalled()+ expect(onClick).not.toHaveBeenCalled()+ }+ })+})diff --git a/frontend/src/layout/panel-layout/ProjectTree/menus/BrowserLikeMenuItems.tsx b/frontend/src/layout/panel-layout/ProjectTree/menus/BrowserLikeMenuItems.tsxindex 7f1aa8d199e2..ad98653a0aae 100644--- a/frontend/src/layout/panel-layout/ProjectTree/menus/BrowserLikeMenuItems.tsx+++ b/frontend/src/layout/panel-layout/ProjectTree/menus/BrowserLikeMenuItems.tsx@@ -1,6 +1,7 @@import { lemonToast } from 'lib/lemon-ui/LemonToast'import { ButtonPrimitive } from 'lib/ui/Button/ButtonPrimitives'import { DropdownMenuItem } from 'lib/ui/DropdownMenu/DropdownMenu'+import { hasDangerousScheme } from 'lib/utils/url'import { CustomMenuProps } from '../types'@@ -21,6 +22,9 @@ export function BrowserLikeMenuItems({asChildonClick={(e) => {e.stopPropagation()+ if (hasDangerousScheme(href)) {+ return+ }window.open(href, '_blank')onClick?.()}}diff --git a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.stories.tsx b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.stories.tsxindex d7c81e9d72f3..cbd53a3ca964 100644--- a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.stories.tsx+++ b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.stories.tsx@@ -5,6 +5,8 @@ import { IconTrash } from '@posthog/icons'import { useDelayedOnMountEffect } from 'lib/hooks/useOnMountEffect'+import { fn } from 'storybook/test'+import { IconLink } from '../icons'import { LemonButton } from '../LemonButton'import { More } from '../LemonButton/More'@@ -18,6 +20,7 @@ const meta: Meta<LemonTableProps<any>> = {title: 'Lemon UI/Lemon Table',component: LemonTable as any,tags: ['autodocs'],+ args: { onRow: fn(() => ({})) },}export default metadiff --git a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsxindex 05ea43ee6c0b..087b25b19d4f 100644--- a/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx+++ b/frontend/src/lib/lemon-ui/LemonTable/LemonTable.test.tsx@@ -35,6 +35,77 @@ describe('LemonTable', () => {afterEach(cleanup)+ it.each([+ { children: 'plain data', props: { title: 'data title' } },+ { props: { dangerouslySetInnerHTML: { __html: '<b>plain data</b>' } } },+ ['alpha', 'beta'],+ ])('renders raw object values as text: %j', (value) => {+ const { container } = render(+ <LemonTable dataSource={[{ value }]} columns={[{ title: 'Value', dataIndex: 'value' }]} />+ )++ const cell = container.querySelector('tbody td')!+ expect(cell.textContent).toBe(JSON.stringify(value))+ expect(cell.getAttribute('title')).toBeNull()+ expect(cell.querySelector('b')).toBeNull()+ })++ it.each([+ [+ 'circular reference',+ () => {+ const value: { self?: unknown } = {}+ value.self = value+ return value+ },+ ],+ ['nested bigint', () => ({ nested: { count: 1n } })],+ [+ 'throwing toJSON',+ () => ({+ toJSON: () => {+ throw new Error('serialization failed')+ },+ }),+ ],+ ])('renders %s as safe text and continues to the next row', (_, createValue) => {+ const { container } = render(+ <LemonTable+ dataSource={[{ value: createValue() }, { value: { status: 'next row' } }]}+ columns={[{ title: 'Value', dataIndex: 'value' }]}+ />+ )++ expect(Array.from(container.querySelectorAll('tbody td'), (cell) => cell.textContent)).toEqual([+ '[Unserializable value]',+ '{"status":"next row"}',+ ])+ })++ it('keeps React elements from raw cell values', () => {+ render(+ <LemonTable+ dataSource={[{ value: <span>formatted value</span> }]}+ columns={[{ title: 'Value', dataIndex: 'value' }]}+ />+ )++ expect(screen.getByText('formatted value').tagName).toBe('SPAN')+ })++ it('keeps cell properties from a column renderer', () => {+ const { container } = render(+ <LemonTable+ dataSource={[{ value: 'alpha' }]}+ columns={[{ title: 'Value', render: () => ({ children: 'rendered', props: { title: 'cell title' } }) }]}+ />+ )++ const cell = container.querySelector('tbody td')!+ expect(cell.textContent).toBe('rendered')+ expect(cell.getAttribute('title')).toBe('cell title')+ })+const renderedOrder = (): string[] => screen.getAllByTestId('cell-name').map((el) => el.textContent ?? '')it.each([false, true].flatMap((virtualized) => [false, true].map((pivot) => ({ virtualized, pivot }))))(diff --git a/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx b/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsxindex 1342667ed357..565c7efd3a1e 100644--- a/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx+++ b/frontend/src/lib/lemon-ui/LemonTable/TableRow.tsx@@ -30,6 +30,14 @@ export interface TableRowProps<T extends Record<string, any>> {onCellContextMenu?: (event: React.MouseEvent<HTMLTableCellElement>) => void}+function stringifyRawCellValue(value: unknown): string {+ try {+ return JSON.stringify(value) ?? '[Unserializable value]'+ } catch {+ return '[Unserializable value]'+ }+}+function TableRowRaw<T extends Record<string, any>>({record,recordIndex,@@ -124,7 +132,9 @@ function TableRowRaw<T extends Record<string, any>>({const value = column.dataIndex != null ? record[column.dataIndex] : undefinedconst rawContents = column.render? column.render(value as T[keyof T], record, recordIndex, rowCount)- : value+ : isTableCellRepresentation(value)+ ? stringifyRawCellValue(value)+ : value// Wrap with cell actions if definedconst cellActionsOverlay = column.cellActions