Skip to content

Commit e84475d

Browse files
Theo van Hoeseloalders
authored andcommitted
Fix Content-Length ', '-separated string issues
After a security issue, we ensure we comply to RFC-7230 -- HTTP/1.1 Message Syntax and Routing - section 3.3.2 -- Content-Length - section 3.3.3 -- Message Body Length
1 parent 331d5c1 commit e84475d

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

lib/HTTP/Daemon.pm

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,32 @@ READ_HEADER:
288288
}
289289
elsif ($ct_len) {
290290

291+
# After a security issue, we ensure we comply to
292+
# RFC-7230 -- HTTP/1.1 Message Syntax and Routing
293+
# section 3.3.2 -- Content-Length
294+
# section 3.3.3 -- Message Body Length
295+
296+
# split and clean up Content-Length ', ' separated string
297+
my @vals = map {my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
298+
split ',', $ct_len;
299+
# check that they are all numbers (RFC: Content-Length = 1*DIGIT)
300+
my @nums = grep { /^[0-9]+$/} @vals;
301+
unless (@vals == @nums) {
302+
$self->send_error(400);
303+
$self->reason("Content-Length value must be a unsigned integer");
304+
return;
305+
}
306+
# check they are all the same
307+
my $ct_len = shift @nums;
308+
foreach (@nums) {
309+
next if $_ == $ct_len;
310+
$self->send_error(400);
311+
$self->reason("Content-Length values are not the same");
312+
return;
313+
}
314+
# ensure we have now a fixed header, with only 1 value
315+
$r->header('Content-Length' => $ct_len);
316+
291317
# Plain body specified by "Content-Length"
292318
my $missing = $ct_len - length($buf);
293319
while ($missing > 0) {

0 commit comments

Comments
 (0)