Skip to content

Commit

Permalink
fix(autocapture): prevent potential memory exhaustion when generating…
Browse files Browse the repository at this point in the history
… combinations (#863)

* fix(autocapture): prevent potential memory exhaustion when generating combinations for CSS Selector
  • Loading branch information
Dogfalo authored Sep 5, 2024
1 parent 30a2da3 commit 6fd6495
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/plugin-autocapture-browser/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export const getSelector = (element: Element, logger?: Logger): string => {
try {
selector = finder(element, {
className: (name: string) => name !== constants.AMPLITUDE_VISUAL_TAGGING_HIGHLIGHT_CLASS,
maxNumberOfTries: 1000,
});
return selector;
} catch (error) {
Expand Down
7 changes: 5 additions & 2 deletions packages/plugin-autocapture-browser/src/libs/finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,13 @@ function bottomUpSearch(
}

function findUniquePath(stack: Knot[][], fallback?: () => Path | null): Path | null {
const paths = sort(combinations(stack));
if (paths.length > config.threshold) {
// Check first the total number of combinations first since generating the combinations can cause memory exhaustion
const numCombinations = stack.reduce((acc, i) => acc * i.length, 1);
if (numCombinations > config.threshold) {
return fallback ? fallback() : null;
}

const paths = sort(combinations(stack));
for (const candidate of paths) {
if (unique(candidate)) {
return candidate;
Expand Down

0 comments on commit 6fd6495

Please sign in to comment.