-
-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vulnerable Regular Expression #167
Vulnerable Regular Expression #167
Comments
The regular expression listed is used in a function genstr(len, chr) {
var result = "";
for (i=0; i<=len; i++) {
result = result + chr;
}
return result;
}
var mime = require('mime'); // npm i mime@1
var str = "x." + genstr(50000, ' ') + "json";
var start = process.hrtime();
var ext = mime.lookup(str);
var end = process.hrtime(start);
console.info("Execution time (hr): %ds %dms", end[0], end[1] / 1000000);
This was the worst case I was able to figure out for the stated ~50k, and it was just 1ms. @evilpacket do you know if this is a real issue, and if so, can you contact me / @broofa with the details or can this be closed? |
I believe I'm able to repro the issue with the code below. Looks like complexity is ~O(N^2).
Yields the following output
|
Looks like anchoring the RE (as suggested by @cristianstaicu) fixes the problem. If I change the RE to
I'll patch this in both the 1.x and 2.x branches. |
|
Thanks @broofa ! |
If this change looks OK, I'll make sure to submit patch to 0.20.x and master Auditors: @evq, @darkdh, @diracdeltas
The following regular expression used in the mime lookup is vulnerable to ReDoS:
/.*[\.\/\\]/
The slowdown is moderately low: for 50.000 characters around 2 seconds matching time. However, I would still suggest one of the following:
If needed, I can provide an actual example showing the slowdown.
The text was updated successfully, but these errors were encountered: