Skip to content

Commit f6d4f52

Browse files
committed
Fix ReDoS in Rack::Utils.get_byte_ranges
This commit fixes a ReDoS problem in `get_byte_ranges`. Thanks @ooooooo_q for the patch! [CVE-2022-44570]
1 parent 20bc90c commit f6d4f52

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lib/rack/utils.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -348,17 +348,18 @@ def get_byte_ranges(http_range, size)
348348
return nil unless http_range && http_range =~ /bytes=([^;]+)/
349349
ranges = []
350350
$1.split(/,\s*/).each do |range_spec|
351-
return nil unless range_spec =~ /(\d*)-(\d*)/
352-
r0, r1 = $1, $2
353-
if r0.empty?
354-
return nil if r1.empty?
351+
return nil unless range_spec.include?('-')
352+
range = range_spec.split('-')
353+
r0, r1 = range[0], range[1]
354+
if r0.nil? || r0.empty?
355+
return nil if r1.nil?
355356
# suffix-byte-range-spec, represents trailing suffix of file
356357
r0 = size - r1.to_i
357358
r0 = 0 if r0 < 0
358359
r1 = size - 1
359360
else
360361
r0 = r0.to_i
361-
if r1.empty?
362+
if r1.nil?
362363
r1 = size - 1
363364
else
364365
r1 = r1.to_i

0 commit comments

Comments
 (0)