: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(toolbar): vendor finder and cap selector combination explosion (#63458)
posthog[bot]9823b19e145d
diff --git a/frontend/package.json b/frontend/package.jsonindex 6a21e2900ed5..2260869c0d7e 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -60,7 +60,6 @@"@floating-ui/react": "^0.27.19","@marsidev/react-turnstile": "^1.4.2","@mathjax/src": "^4.1.2",- "@medv/finder": "^4.0.2","@memlab/lens": "2.0.0","@microlink/react-json-view": "^1.31.18","@microsoft/fetch-event-source": "^2.0.1",diff --git a/frontend/src/toolbar/utils.ts b/frontend/src/toolbar/utils.tsindex b52a4ba8a7b9..eea66c9f5b41 100644--- a/frontend/src/toolbar/utils.ts+++ b/frontend/src/toolbar/utils.ts@@ -1,4 +1,3 @@-import { finder } from '@medv/finder'import { querySelectorAllDeep } from 'query-selector-shadow-dom'import { CSSProperties } from 'react'@@ -8,6 +7,7 @@ import { patch } from '~/toolbar/patch'import { toolbarLogger } from '~/toolbar/toolbarLogger'import { captureToolbarException } from '~/toolbar/toolbarPosthogJS'import { ActionStepForm, ElementRect } from '~/toolbar/types'+import { finder } from '~/toolbar/vendor/finder'import { ActionStepType } from '~/types'import { ActionStepPropertyKey } from './actions/ActionStep'@@ -166,6 +166,14 @@ function computeElementQuery(element: HTMLElement, dataAttributes: string[]): st// that aren't in the PostHog preferred list - they were returned early abovereturn name.startsWith('data-')},+ // the combination guard tripped and cut the candidate search short -+ // the selector may degrade to a brittle positional path - record host+ // and depth so we can see how often and where pathological DOMs hit this+ onCombinationsCapped: ({ levels }) =>+ toolbarLogger.warn('element_selector', 'finder hit maxCombinations cap, candidate search truncated', {+ host: window.location.host,+ levels,+ }),})return slashDotDataAttrUnescape(foundSelector)} catch (error) {diff --git a/frontend/src/toolbar/vendor/finder/index.test.ts b/frontend/src/toolbar/vendor/finder/index.test.tsnew file mode 100644index 000000000000..6520b7f3db55--- /dev/null+++ b/frontend/src/toolbar/vendor/finder/index.test.ts@@ -0,0 +1,103 @@+import { finder } from './index'++// A small class-heavy tree whose first level alone carries enough word-like+// classes that `maxCombinations: 1` trips the guard immediately.+const CAPPABLE_HTML = `+ <section class="alpha bravo charlie">+ <article class="delta echo foxtrot">+ <span class="golf hotel india">target</span>+ </article>+ </section>+`++describe('vendored finder', () => {+ afterEach(() => {+ document.body.innerHTML = ''+ })++ function deepestChild(): Element {+ let element: Element = document.body+ while (element.firstElementChild) {+ element = element.firstElementChild+ }+ return element+ }++ it.each([+ {+ name: 'generates a unique selector that matches the element',+ html: `+ <div class="container">+ <ul class="menu">+ <li class="item">first</li>+ <li class="target">second</li>+ </ul>+ </div>+ `,+ targetSelector: '.target',+ config: undefined,+ },+ {+ name: 'honours an explicit maxCombinations cap and still returns a working selector',+ // A cap of 1 forces the guard to trip on the first level, so finder must+ // fall back rather than enumerate selectors — but still produce a match.+ html: CAPPABLE_HTML,+ targetSelector: '.golf',+ config: { maxCombinations: 1 },+ },+ ])('$name', ({ html, targetSelector, config }) => {+ document.body.innerHTML = html+ const target = document.querySelector(targetSelector)!++ const selector = config ? finder(target, config) : finder(target)++ expect(document.querySelectorAll(selector)).toHaveLength(1)+ expect(document.querySelector(selector)).toBe(target)+ })++ it('does not blow up on many classes across many levels (antonmedv/finder#85)', () => {+ // Every level carries many word-like classes, so the naive cross-product+ // of candidate selectors reaches hundreds of millions of paths. Without+ // the combination guard this exhausts memory / times the test out; with+ // it, finder falls back to a positional path and returns promptly.+ const classes = ['alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot', 'golf', 'hotel', 'india', 'juliet']+ let html = 'target'+ for (let depth = 0; depth < 12; depth++) {+ html = `<div class="${classes.join(' ')}">${html}</div>`+ }+ document.body.innerHTML = html+ const target = deepestChild()++ const selector = finder(target)++ expect(document.querySelectorAll(selector)).toHaveLength(1)+ expect(document.querySelector(selector)).toBe(target)+ }, 5_000)++ it.each([+ { name: 'does not fire onCombinationsCapped under the default cap', config: {}, expectedCalls: 0 },+ {+ name: 'fires onCombinationsCapped exactly once when the cap trips',+ config: { maxCombinations: 1 },+ expectedCalls: 1,+ },+ ])('$name', ({ config, expectedCalls }) => {+ document.body.innerHTML = CAPPABLE_HTML+ const target = document.querySelector('.golf')!++ const cappedSpy = jest.fn()+ finder(target, { ...config, onCombinationsCapped: cappedSpy })++ expect(cappedSpy).toHaveBeenCalledTimes(expectedCalls)+ })++ it('passes the level count to onCombinationsCapped when the cap trips', () => {+ document.body.innerHTML = CAPPABLE_HTML+ const target = document.querySelector('.golf')!++ const cappedSpy = jest.fn()+ finder(target, { maxCombinations: 1, onCombinationsCapped: cappedSpy })++ expect(cappedSpy).toHaveBeenCalledWith({ levels: expect.any(Number) })+ })+})diff --git a/frontend/src/toolbar/vendor/finder/index.ts b/frontend/src/toolbar/vendor/finder/index.tsnew file mode 100644index 000000000000..713a92e9427f--- /dev/null+++ b/frontend/src/toolbar/vendor/finder/index.ts@@ -0,0 +1,440 @@+// Vendored from @medv/[email protected] — https://github.com/antonmedv/finder+// Ported in place (TypeScript, no formatting reflow) so we can carry a fix that+// is still unmerged upstream.+//+// PostHog modifications:+// - Guard against combinatorial explosion before materializing the candidate+// paths in `search()`. On DOMs with many classes across many levels the+// cross-product of `combinations(stack)` can reach hundreds of millions of+// entries and exhaust memory / overflow the stack *before* the timeout and+// `maxNumberOfPathChecks` guards in `finder()` ever run. We compute the+// product of the level sizes (cheap) and stop expanding once it exceeds+// `maxCombinations`. Adapted from upstream antonmedv/finder#84, the fix for+// antonmedv/finder#85.+// - Append candidates with an iterative `for...of` push instead of+// `paths.push(...combinations(stack))`. Spreading a generator's output as+// function arguments is itself bounded by the engine's argument-count limit+// (~65k), so the spread form would reintroduce a crash if `maxCombinations`+// were ever raised past that ceiling — the same failure class the guard+// above exists to prevent. The iterative push has no such ceiling and emits+// identical candidates in identical order.+// - Added an optional `onCombinationsCapped` callback so callers can observe+// when the guard trips (cutting the candidate search short, which may force+// a positional fallback) without coupling this vendored file to any PostHog+// logging. It receives the level count reached so callers can record how+// deep the offending element was.+//+// MIT License+//+// Copyright (c) 2018 Anton Medvedev+//+// Permission is hereby granted, free of charge, to any person obtaining a copy+// of this software and associated documentation files (the "Software"), to deal+// in the Software without restriction, including without limitation the rights+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+// copies of the Software, and to permit persons to whom the Software is+// furnished to do so, subject to the following conditions:+//+// The above copyright notice and this permission notice shall be included in all+// copies or substantial portions of the Software.+//+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+// SOFTWARE.++type Knot = {+ name: string+ penalty: number+ level?: number+}++const acceptedAttrNames = new Set(['role', 'name', 'aria-label', 'rel', 'href'])++/** Check if attribute name and value are word-like. */+export function attr(name: string, value: string): boolean {+ let nameIsOk = acceptedAttrNames.has(name)+ nameIsOk ||= name.startsWith('data-') && wordLike(name)++ let valueIsOk = wordLike(value) && value.length < 100+ valueIsOk ||= value.startsWith('#') && wordLike(value.slice(1))++ return nameIsOk && valueIsOk+}++/** Check if id name is word-like. */+export function idName(name: string): boolean {+ return wordLike(name)+}++/** Check if class name is word-like. */+export function className(name: string): boolean {+ return wordLike(name)+}++/** Check if tag name is word-like. */+export function tagName(_name: string): boolean {+ return true+}++/** Configuration options for the finder. */+export type Options = {+ /** The root element to start the search from. */+ root: Element+ /** Function that determines if an id name may be used in a selector. */+ idName: (name: string) => boolean+ /** Function that determines if a class name may be used in a selector. */+ className: (name: string) => boolean+ /** Function that determines if a tag name may be used in a selector. */+ tagName: (name: string) => boolean+ /** Function that determines if an attribute may be used in a selector. */+ attr: (name: string, value: string) => boolean+ /** Timeout to search for a selector. */+ timeoutMs: number+ /** Minimum length of levels in fining selector. */+ seedMinLength: number+ /** Minimum length for optimising selector. */+ optimizedMinLength: number+ /** Maximum number of path checks. */+ maxNumberOfPathChecks: number+ /**+ * PostHog addition: maximum size of the candidate cross-product to+ * materialize per search level. Caps the combinatorial blow-up described in+ * antonmedv/finder#85 before it can exhaust memory. Defaults to a value far+ * above what normal elements ever need, so it only trips on pathological DOMs.+ */+ maxCombinations: number+ /**+ * PostHog addition: invoked when `maxCombinations` is exceeded and the+ * candidate search is cut short — which may force a positional fallback, but+ * a stable selector found before the cap can still win. Receives the number+ * of levels (ancestor depth) reached when the guard tripped. Optional and+ * side-effect-only; lets callers emit a signal without this vendored file+ * depending on any logging.+ */+ onCombinationsCapped?: (info: { levels: number }) => void+}++/** Finds unique CSS selectors for the given element. */+export function finder(input: Element, options?: Partial<Options>): string {+ if (input.nodeType !== Node.ELEMENT_NODE) {+ throw new Error(`Can't generate CSS selector for non-element node type.`)+ }+ if (input.tagName.toLowerCase() === 'html') {+ return 'html'+ }+ const defaults: Options = {+ root: document.body,+ idName: idName,+ className: className,+ tagName: tagName,+ attr: attr,+ timeoutMs: 1000,+ seedMinLength: 3,+ optimizedMinLength: 2,+ maxNumberOfPathChecks: Infinity,+ maxCombinations: 50_000,+ }++ const startTime = new Date()+ const config = { ...defaults, ...options }+ const rootDocument = findRootDocument(config.root, defaults)++ let foundPath: Knot[] | undefined+ let count = 0+ for (const candidate of search(input, config, rootDocument)) {+ const elapsedTimeMs = new Date().getTime() - startTime.getTime()+ if (elapsedTimeMs > config.timeoutMs || count >= config.maxNumberOfPathChecks) {+ const fPath = fallback(input, rootDocument)+ if (!fPath) {+ throw new Error(`Timeout: Can't find a unique selector after ${config.timeoutMs}ms`)+ }+ return selector(fPath)+ }+ count+++ if (unique(candidate, rootDocument)) {+ foundPath = candidate+ break+ }+ }++ if (!foundPath) {+ // PostHog: when the combinatorial guard in `search()` cuts the walk short+ // there may be no unique short selector. Fall back to a positional+ // nth-of-type path (matching upstream antonmedv/finder#84's "return+ // fallback" behaviour) so callers still get a working selector instead of+ // an exception.+ const fPath = fallback(input, rootDocument)+ if (fPath) {+ return selector(fPath)+ }+ throw new Error(`Selector was not found.`)+ }++ const optimized = [...optimize(foundPath, input, config, rootDocument, startTime)]+ optimized.sort(byPenalty)+ if (optimized.length > 0) {+ return selector(optimized[0])+ }+ return selector(foundPath)+}++function* search(input: Element, config: Options, rootDocument: Element | Document): Generator<Knot[]> {+ const stack: Knot[][] = []+ let paths: Knot[][] = []+ let current: Element | null = input+ let i = 0+ while (current && current !== rootDocument) {+ const level = tie(current, config)+ for (const node of level) {+ node.level = i+ }+ stack.push(level)+ current = current.parentElement+ i++++ // PostHog: bail before the cross-product explodes (antonmedv/finder#85).+ // The product of level sizes is the number of paths `combinations(stack)`+ // would emit; computing it is O(levels) and lets us stop before the+ // O(product) materialization below allocates an unbounded array.+ const numCombinations = stack.reduce((product, levelKnots) => product * levelKnots.length, 1)+ if (numCombinations > config.maxCombinations) {+ config.onCombinationsCapped?.({ levels: stack.length })+ break+ }++ for (const candidate of combinations(stack)) {+ paths.push(candidate)+ }++ if (i >= config.seedMinLength) {+ paths.sort(byPenalty)+ for (const candidate of paths) {+ yield candidate+ }+ paths = []+ }+ }++ paths.sort(byPenalty)+ for (const candidate of paths) {+ yield candidate+ }+}++function wordLike(name: string): boolean {+ if (/^[a-z-]{3,}$/i.test(name)) {+ const words = name.split(/-|[A-Z]/)+ for (const word of words) {+ if (word.length <= 2) {+ return false+ }+ if (/[^aeiou]{4,}/i.test(word)) {+ return false+ }+ }+ return true+ }+ return false+}++function tie(element: Element, config: Options): Knot[] {+ const level: Knot[] = []++ const elementId = element.getAttribute('id')+ if (elementId && config.idName(elementId)) {+ level.push({+ name: '#' + CSS.escape(elementId),+ penalty: 0,+ })+ }++ for (let i = 0; i < element.classList.length; i++) {+ const name = element.classList[i]+ if (config.className(name)) {+ level.push({+ name: '.' + CSS.escape(name),+ penalty: 1,+ })+ }+ }++ for (let i = 0; i < element.attributes.length; i++) {+ const attr = element.attributes[i]+ if (config.attr(attr.name, attr.value)) {+ level.push({+ name: `[${CSS.escape(attr.name)}="${CSS.escape(attr.value)}"]`,+ penalty: 2,+ })+ }+ }++ const tagName = element.tagName.toLowerCase()+ if (config.tagName(tagName)) {+ level.push({+ name: tagName,+ penalty: 5,+ })++ const index = indexOf(element, tagName)+ if (index !== undefined) {+ level.push({+ name: nthOfType(tagName, index),+ penalty: 10,+ })+ }+ }++ const nth = indexOf(element)+ if (nth !== undefined) {+ level.push({+ name: nthChild(tagName, nth),+ penalty: 50,+ })+ }++ return level+}++function selector(path: Knot[]): string {+ let node = path[0]+ let query = node.name+ for (let i = 1; i < path.length; i++) {+ const level = path[i].level || 0+ if (node.level === level - 1) {+ query = `${path[i].name} > ${query}`+ } else {+ query = `${path[i].name} ${query}`+ }+ node = path[i]+ }+ return query+}++function penalty(path: Knot[]): number {+ return path.map((node) => node.penalty).reduce((acc, i) => acc + i, 0)+}++function byPenalty(a: Knot[], b: Knot[]): number {+ return penalty(a) - penalty(b)+}++function indexOf(input: Element, tagName?: string): number | undefined {+ const parent = input.parentNode+ if (!parent) {+ return undefined+ }+ let child = parent.firstChild+ if (!child) {+ return undefined+ }+ let i = 0+ while (child) {+ if (+ child.nodeType === Node.ELEMENT_NODE &&+ (tagName === undefined || (child as Element).tagName.toLowerCase() === tagName)+ ) {+ i+++ }+ if (child === input) {+ break+ }+ child = child.nextSibling+ }+ return i+}++function fallback(input: Element, rootDocument: Element | Document): Knot[] | undefined {+ let i = 0+ let current: Element | null = input+ const path: Knot[] = []+ while (current && current !== rootDocument) {+ const tagName = current.tagName.toLowerCase()+ const index = indexOf(current, tagName)+ if (index === undefined) {+ return+ }+ path.push({+ name: nthOfType(tagName, index),+ penalty: NaN,+ level: i,+ })+ current = current.parentElement+ i+++ }+ if (unique(path, rootDocument)) {+ return path+ }+}++function nthChild(tagName: string, index: number): string {+ if (tagName === 'html') {+ return 'html'+ }+ return `${tagName}:nth-child(${index})`+}++function nthOfType(tagName: string, index: number): string {+ if (tagName === 'html') {+ return 'html'+ }+ return `${tagName}:nth-of-type(${index})`+}++function* combinations(stack: Knot[][], path: Knot[] = []): Generator<Knot[]> {+ if (stack.length > 0) {+ for (const node of stack[0]) {+ yield* combinations(stack.slice(1, stack.length), path.concat(node))+ }+ } else {+ yield path+ }+}++function findRootDocument(rootNode: Element | Document, defaults: Options): Element | Document {+ if (rootNode.nodeType === Node.DOCUMENT_NODE) {+ return rootNode+ }+ if (rootNode === defaults.root) {+ return rootNode.ownerDocument as Document+ }+ return rootNode+}++function unique(path: Knot[], rootDocument: Element | Document): boolean {+ const css = selector(path)+ switch (rootDocument.querySelectorAll(css).length) {+ case 0:+ throw new Error(`Can't select any node with this selector: ${css}`)+ case 1:+ return true+ default:+ return false+ }+}++function* optimize(+ path: Knot[],+ input: Element,+ config: Options,+ rootDocument: Element | Document,+ startTime: Date+): Generator<Knot[]> {+ if (path.length > 2 && path.length > config.optimizedMinLength) {+ for (let i = 1; i < path.length - 1; i++) {+ const elapsedTimeMs = new Date().getTime() - startTime.getTime()+ if (elapsedTimeMs > config.timeoutMs) {+ return+ }+ const newPath = [...path]+ newPath.splice(i, 1)+ if (unique(newPath, rootDocument) && rootDocument.querySelector(selector(newPath)) === input) {+ yield newPath+ yield* optimize(newPath, input, config, rootDocument, startTime)+ }+ }+ }+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex b2ad3f4a68b6..4300b199917b 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -565,9 +565,6 @@ importers:'@mathjax/src':specifier: ^4.1.2version: 4.1.2- '@medv/finder':- specifier: ^4.0.2- version: 4.0.2'@memlab/lens':specifier: 2.0.0version: 2.0.0@@ -7464,9 +7461,6 @@ packages:'@types/react': 18.3.27react: 18.3.1- '@medv/[email protected]':- resolution: {integrity: sha512-RraNY9SCcx4KZV0Dh6BEW6XEW2swkqYca74pkFFRw6hHItSHiy+O/xMnpbofjYbzXj0tSpBGthUF1hHTsr3vIQ==}-'@memlab/[email protected]':resolution: {integrity: sha512-2aoX/v0PI0qgW65+ZnhbWimFFSGDRTm8p8qQ04zNDTrqtlOWUuugT2yu3XeExVSLhLlrMcqL0PsuZ/SbOcbggw==}@@ -14541,10 +14535,6 @@ packages:resolution: {integrity: sha512-r8VyPX7XL8U01Xgnb1CjZ3XV+z90cXIJ9JPE/R9SEC9vpw2P6CfsRPJmp20DppC5N7ZAMCmjYkJIa744Iyg96w==}- resolution: {integrity: sha512-/ce7+jQ1PQ6rVXwe+jKEg5hW5ciicHwIQUagZkp6IufBoY3YDgdTTY1azVs0qoRgVmvsNB+rbjLJxDAeHHtwsQ==}- engines: {node: '>=10.13.0'}-resolution: {integrity: sha512-6QEuw3zoX1SJQc7b87aBXke/no+mG2bTBgw29gWMQonLmpEkWoCAVkl+M49e48AZlWzxiDzDZzYdp6kobcyLww==}engines: {node: '>=10.13.0'}@@ -27069,8 +27059,6 @@ snapshots:'@types/react': 18.3.27react: 18.3.1- '@medv/[email protected]': {}-'@memlab/[email protected]': {}'@mermaid-js/[email protected]':@@ -35427,11 +35415,6 @@ snapshots:fast-json-parse: 1.0.3objectorarray: 1.0.5- dependencies:- graceful-fs: 4.2.11- tapable: 2.3.0-dependencies:graceful-fs: 4.2.11@@ -45650,7 +45633,7 @@ snapshots:acorn-import-phases: 1.0.4([email protected])browserslist: 4.28.1chrome-trace-event: 1.0.3- enhanced-resolve: 5.20.0+ enhanced-resolve: 5.22.1es-module-lexer: 2.0.0eslint-scope: 5.1.1events: 3.3.0