: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): improve heatmap key handling and link href validation (#66916)
Tom Piccirello1c57c79a0cdd
diff --git a/frontend/src/lib/lemon-ui/Link/Link.test.tsx b/frontend/src/lib/lemon-ui/Link/Link.test.tsxnew file mode 100644index 000000000000..b31dd96b8d3c--- /dev/null+++ b/frontend/src/lib/lemon-ui/Link/Link.test.tsx@@ -0,0 +1,32 @@+import '@testing-library/jest-dom'++import { render, screen } from '@testing-library/react'++import { Link } from './Link'++describe('Link', () => {+ it('never resolves a javascript: target to an executable href, even with client-side routing disabled', () => {+ // disableClientSideRouting short-circuits the routing rewrite that would otherwise neutralize the+ // scheme, so the scheme block must hold regardless of it (e.g. if the flag is set via prototype pollution).+ render(+ <Link to="javascript:alert(document.domain)" disableClientSideRouting>+ click me+ </Link>+ )++ const anchor = screen.getByText('click me').closest('a')+ expect(anchor?.getAttribute('href') ?? '').not.toMatch(/^javascript:/i)+ })++ it('blocks javascript: targets regardless of casing and whitespace', () => {+ render(+ <Link to=" JavaScript:alert(1)" disableClientSideRouting>+ sneaky+ </Link>+ )++ const anchor = screen.getByText('sneaky').closest('a')+ const href = anchor?.getAttribute('href') ?? ''+ expect(href.replace(/\s/g, '').toLowerCase()).not.toMatch(/^javascript:/)+ })+})diff --git a/frontend/src/lib/lemon-ui/Link/Link.tsx b/frontend/src/lib/lemon-ui/Link/Link.tsxindex cea0ba02fa8d..fad0bbec0e07 100644--- a/frontend/src/lib/lemon-ui/Link/Link.tsx+++ b/frontend/src/lib/lemon-ui/Link/Link.tsx@@ -94,6 +94,14 @@ const isDirectLink = (url: string): boolean => {return /^(mailto:|https?:\/\/|:\/\/)/.test(url)}+const hasDangerousScheme = (url: string): boolean => {+ // Browsers ignore leading control chars/whitespace and any tabs/newlines embedded in the scheme,+ // so strip them all before matching. javascript:/vbscript: targets must never become an href —+ // not even when disableClientSideRouting would otherwise skip the routing rewrite.+ const normalized = url.replace(/[\u0000-\u0020]/g, '').toLowerCase()+ return /^(javascript|vbscript):/.test(normalized)+}+/** Resolve a `to` target into a concrete href string. */function resolveHref(to: LinkPrimitiveProps['to'], disableClientSideRouting?: boolean): string | undefined {if (!to) {@@ -102,6 +110,9 @@ function resolveHref(to: LinkPrimitiveProps['to'], disableClientSideRouting?: boif (typeof to !== 'string') {return '#'}+ if (hasDangerousScheme(to)) {+ return '#'+ }return isDirectLink(to) || disableClientSideRouting ? to : addProjectIdIfMissing(to)}diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Heatmap/TwoDimensionalHeatmap.test.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Heatmap/TwoDimensionalHeatmap.test.tsxindex 80fbb308e459..4804ac36f3ae 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Heatmap/TwoDimensionalHeatmap.test.tsx+++ b/frontend/src/queries/nodes/DataVisualization/Components/Heatmap/TwoDimensionalHeatmap.test.tsx@@ -53,7 +53,11 @@ const response = {],}-const setup = (nullLabel = '(header null)', nullValue = ''): ReturnType<typeof dataVisualizationLogic.build> => {+const setup = (+ nullLabel = '(header null)',+ nullValue = '',+ responseOverride: typeof response = response+): ReturnType<typeof dataVisualizationLogic.build> => {logicCounter += 1const testKey = `test-two-dimensional-heatmap-${logicCounter}`const query = makeQuery(nullLabel, nullValue)@@ -65,7 +69,7 @@ const setup = (nullLabel = '(header null)', nullValue = ''): ReturnType<typeof dconst logic = dataVisualizationLogic(props)logic.mount()- dataNodeLogic({ key: testKey, query: query.source, dataNodeCollectionId }).actions.setResponse(response)+ dataNodeLogic({ key: testKey, query: query.source, dataNodeCollectionId }).actions.setResponse(responseOverride)render(<Provider>@@ -81,12 +85,35 @@ const setup = (nullLabel = '(header null)', nullValue = ''): ReturnType<typeof ddescribe('TwoDimensionalHeatmap', () => {afterEach(() => {cleanup()+ // Guard against any pollution leaking into other tests if a regression reintroduces it+ delete (Object.prototype as any).disableClientSideRouting})beforeEach(() => {initKeaTests()})+ it('does not pollute Object.prototype when row labels contain __proto__', async () => {+ // Attacker-controlled query results: a Y-axis label of `__proto__` and an X-axis label that+ // is a sensitive global key. With a plain-object accumulator, cellValues['__proto__'][key] = 1+ // writes straight to Object.prototype, which the Link component then reads to bypass URL sanitization.+ const maliciousResponse = {+ columns: ['region', 'segment', 'count'],+ types: [+ ['region', 'String'],+ ['segment', 'String'],+ ['count', 'Int64'],+ ] as [string, string][],+ results: [['disableClientSideRouting', '__proto__', 1]],+ }++ setup('(header null)', '', maliciousResponse)++ expect(await screen.findByText('disableClientSideRouting')).toBeInTheDocument()+ expect(({} as any).disableClientSideRouting).toBeUndefined()+ expect('disableClientSideRouting' in Object.prototype).toBe(false)+ })+it('uses the null label in headers and the null value in cell contents', async () => {setup('(header null)', '(cell null)')diff --git a/frontend/src/queries/nodes/DataVisualization/Components/Heatmap/TwoDimensionalHeatmap.tsx b/frontend/src/queries/nodes/DataVisualization/Components/Heatmap/TwoDimensionalHeatmap.tsxindex ff5e2a90925d..2111d561f4a6 100644--- a/frontend/src/queries/nodes/DataVisualization/Components/Heatmap/TwoDimensionalHeatmap.tsx+++ b/frontend/src/queries/nodes/DataVisualization/Components/Heatmap/TwoDimensionalHeatmap.tsx@@ -62,7 +62,9 @@ const buildHeatmapData = (): HeatmapData => {const xValues: string[] = []const yValues: string[] = []- const cellValues: Record<string, Record<string, number | null>> = {}+ // Prototype-less: row labels are attacker-controlled and used as keys, so a `__proto__`+ // label on a plain object would write through to Object.prototype (prototype pollution).+ const cellValues: Record<string, Record<string, number | null>> = Object.create(null)const numericValues: number[] = []let duplicateCellCount = 0@@ -101,7 +103,7 @@ const buildHeatmapData = (}if (!cellValues[yLabel]) {- cellValues[yLabel] = {}+ cellValues[yLabel] = Object.create(null)}const cellKey = `${yLabel}||${xLabel}`