nwsapi: infinite recursion on invalid selector _:-ms-fullscreen
function compileSelector in nwsapi.js
source = 'n=e;while((e=e.parentElement)){' + source + '}e=n;';
source grows to infinite length, until RangeError: Invalid string length
normal source.length are around 1000 bytes
with infinite loop, source.length grows to the maximum string length of 1GB
the selector _:-ms-fullscreen comes from deepl.com/css/lmt.$d783c6.css
.lmt--web .lmt__side_container--target .lmt__ad_charLimit_container _:-ms-fullscreen
the problem is
default:
emit('\'' + selector_string + '\'' + qsInvalid);
break;
}
// end of switch symbol
which only breaks the switch (symbol) { block, but not the while (selector) { loop -> deadloop
the parser should seek to the next comma or to end of selector string
workarounds:
class CustomResourceLoader extends jsdom.ResourceLoader {
fetch(url, options) {
// ignore all styles
if (url.endsWith('.css')) {
return Promise.resolve(Buffer.from(''));
}
// patch style
if (url == 'https://example.com/path/to/style.css')) {
var str = require('fs').readFileSync('style.patched.css', 'utf8');
return Promise.resolve(Buffer.from(str));
}
return super.fetch(url, options);
}
}
const resourceLoader = new CustomResourceLoader();
const dom = new jsdom.JSDOM('', { resources: resourceLoader });
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 21 (17 by maintainers)
Commits related to this issue
- resolve the infinite recursion problem on invalid selector, fixes issue #46 (@milahu) — committed to dperini/nwsapi by dperini 5 years ago
Could you please create a release for this bugfix?
@milahu we have to distinguish reality from fantasy. I believe the tools you mention are perfect for such long selectors. I really hope nwsapi is used with a complete different scope in mind. You are talking about selector length now not by resolver “source” length. In this case I would also set a limit to selector length and return an empty Array. This will increase speed of the selector engine 10.000 time on these kind of sites. 😉 Thank you for the head up with the infinite recursion it is now fixed and I will close the issue.
Hello! I have the same issue using
testing-library-react. Currently using its methods I run into an issue where this error occurs. A string grows to millions of symbols in this block of code.case '~': match = selector.match(Patterns.relative); source = 'n=e;while((e=e.previousElementSibling)){' + source + '}e=n;'; break;still a bug in nwsapi 2.2.0
the error is
emited over and over, but the function does not returneasiest fix is to add
return '';in theswitch (symbol)default casenwsapi.js
400 lines to reproduce
can you try … ?