fczbkk / css-selector-generator

JavaScript object that creates unique CSS selector for given element.
MIT License
538 stars 90 forks source link

[Bug/Request] very long src attribute causes generation to hang indefinitely #787

Closed jeff-an closed 1 month ago

jeff-an commented 1 month ago

I tried to use this library to generate a selector for an image element with an extremely long src attr that has inlined base64 data. This caused the browser to hang indefinitely.

The problem seems to occur only if the blacklist includes a regex pattern. Even a matching regex pattern like /.*\[.*src.*data:.*\].*/ will cause the library to hang, presumably due to regex matching hogging the event loop. Switching to a more simple blacklist like exact string matches seems to fix the issue.

So my feature request would be to maybe support a timeout parameter, add docs warning about regex usage, and adding an option to refuse to use attributes above a certain length. The medv selector generator has a similar option that just lets you pass a predicate for attributes.

If you point me to some relevant code, I'm happy to make a pull request as well! Thanks!

fczbkk commented 1 month ago

Hey @jeff-an, thanks for the bug report. I have just published v3.6.9 with the fix. The base64 encoded src attributes are now ignored.

jeff-an commented 1 month ago

Awesome, thanks for the fast fix! It might be good to also have a generic predicate configurable so that people can decide to ignore attributes programmatically, e.g.:

{
  attrributeFilter: (attrName: string, attrVal: string | null) => boolean
}