URI parser's RFC3986 regular expression has poor performance when there are two # characters, leading to ReDoS
The URI parser's regular expression for RFC3986 validation performs poorly on URLs containing two '#' characters, causing execution time to grow much faster than input length. An attacker could send a long malicious URL in a request body to cause high resource consumption and denial of service.
- Triaged in 4 days
- Fixed in 15 months
- Disclosed after 23 months
Summary
Hello team, I hope you're doing well! The URI parser mishandles invalid URLs that have two # characters. It does correctly identify that they're invalid, but the regex performs very poorly and execution time grows much faster than the string length.
I found this somewhat accidentally when fuzzing for something else. I'm not sure if you care about such issues but I figured I'd report it anyway. The length of the strings required to actually cause the process to hang are very long, but it's not really an issue when the user-controlled input is sent in a request body.
Steps to reproduce
Run the following script
require 'benchmark'
require 'uri'
def parse(n)
URI('https://example.com/dir/' + 'a' * n + '/##.jpg')
rescue URI::InvalidURIError
# Invalid URI because of the two #
end
n = 50000
Benchmark.bm(7) do |x|
x.report('Base length') { parse(n) }
x.report('Length x2 ') { parse(n * 2) }
x.report('Length x4 ') { parse(n * 4) }
x.report('Length x8 ') { parse(n * 8) }
end
Here's the output on my machine
user system total real
Base length 1.086961 0.003059 1.090020 ( 1.090500)
Length x2 4.415046 0.000000 4.415046 ( 4.416986)
Length x4 22.021462 0.003294 22.024756 ( 22.042507)
Length x8 122.695223 0.006653 122.701876 (122.853669)
We can see the execution time is roughly quintuples when the string length only doubles.
Here's my Ruby version
$ ruby -v
ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-linux]
Impact
High resource consumption, reduced performance, denial of service
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 used for URI validation has catastrophic backtracking when encountering two '#' characters.
Impact
High resource consumption, reduced performance, denial of service.
What the attacker needed
Attacker can send a long URL in a request body
How it was found
- Run the provided Ruby script that parses URLs with increasing lengths of 'a' characters followed by '/##.jpg'.
- Observe the benchmark output showing execution time roughly quintuples when string length doubles.
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 check text. Some patterns can take a very long time on certain inputs, which is called a regular expression denial of service (ReDoS). Here, a URL with two hash symbols makes the parser slow down dramatically as the URL gets longer, so an attacker could send a very long URL to tie up the server.
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.