Inefficient Regular Expression Complexity in chalk/ansi-regex
The ansi-regex library contains a regular expression with inefficient complexity, allowing an attacker to cause a denial of service by supplying a crafted string of invalid ANSI escape codes. The proof of concept shows that parsing such input takes increasingly long as the input length grows.
chalk/ansi-regex
Originally disclosed on huntr
ansi-regex is vulnerable to Inefficient Regular Expression Complexity which could lead to a denial of service when parsing invalid ANSI escape codes.
Proof of Concept
import ansiRegex from 'ansi-regex';
for(var i = 1; i <= 50000; i++) {
var time = Date.now();
var attack_str = "\u001B["+";".repeat(i*10000);
ansiRegex().test(attack_str)
var time_cost = Date.now() - time;
console.log("attack_str.length: " + attack_str.length + ": " + time_cost+" ms")
}
The ReDOS is mainly due to the sub-patterns [[\\]()#;?]* and (?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*
From the GitHub Advisory Database (GHSA-93q8-gq69-wqmw, 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 contains nested quantifiers that cause catastrophic backtracking when matching invalid ANSI escape sequences.
Impact
An attacker could cause a denial of service by making the application unresponsive when it processes a malicious string.
What the attacker needed
Application uses ansi-regex to parse untrusted input
How it was found
- Import the ansi-regex library.
- Construct a string starting with an ANSI escape sequence followed by many semicolons.
- Call the regex test method on the string and measure the time taken.
- Repeat with increasing lengths to observe the exponential time growth.
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 process certain inputs, especially if they contain many repeated characters. Here, an attacker can send a specially crafted string that makes the pattern matching slow down dramatically, potentially freezing the application.
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.