CVE-2022-31081: Inconsistent Interpretation of HTTP Requests ('HTTP Request/Response Smuggling') (CWE-444)
value: HTTP::Daemon is a simple http server class written in perl. Versions prior to 6.15 are subject to a vulnerability which could potentially be exploited to gain privileged access to APIs or poison intermediate caches. It is uncertain how large the risks are, most Perl based applications are served on top of Nginx or Apache, not on the `HTTP::Daemon`. This library is commonly used for local development and tests. Users are advised to update to resolve this issue. Users unable to upgrade may add additional request handling logic as a mitigation. After calling `my $rqst = $conn->get_request()` one could inspect the returned `HTTP::Request` object. Querying the 'Content-Length' (`my $cl = $rqst->header('Content-Length')`) will show any abnormalities that should be dealt with by a `400` response. Expected strings of 'Content-Length' SHOULD consist of either a single non-negative integer, or, a comma separated repetition of that number. (that is `42` or `42, 42, 42`). Anything else MUST be rejected.
CWE description: The product acts as an intermediary HTTP agent
         (such as a proxy or firewall) in the data flow between two
         entities such as a client and server, but it does not
         interpret malformed HTTP requests or responses in ways that
         are consistent with how the messages will be processed by
         those entities that are at the ultimate destination.
CVSS v3 base score: 6.5
Published: 2022-06-27T21:15Z
Repository: https://github.com/libwww-perl/HTTP-Daemon
Language: Perl
Files changed: lib/HTTP/Daemon.pm
Commit date: 2022-06-27 10:35:42 -0400
Commit message: 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

Diff: diff --git a/lib/HTTP/Daemon.pm b/lib/HTTP/Daemon.pm
index c0cdf76..a5112b3 100644
--- a/lib/HTTP/Daemon.pm
+++ b/lib/HTTP/Daemon.pm
@@ -286,10 +286,36 @@ READ_HEADER:
         return;
 
     }
     elsif ($ct_len) {
 
+        # 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
+
+        # split and clean up Content-Length ', ' separated string
+        my @vals = map {my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
+            split ',', $ct_len;
+        # check that they are all numbers (RFC: Content-Length = 1*DIGIT)
+        my @nums = grep { /^[0-9]+$/} @vals;
+        unless (@vals == @nums) {
+            $self->send_error(400);
+            $self->reason("Content-Length value must be a unsigned integer");
+            return;
+        }
+        # check they are all the same
+        my $ct_len = shift @nums;
+        foreach (@nums) {
+            next if $_ == $ct_len;
+            $self->send_error(400);
+            $self->reason("Content-Length values are not the same");
+            return;
+        }
+        # ensure we have now a fixed header, with only 1 value
+        $r->header('Content-Length' => $ct_len);
+
         # Plain body specified by "Content-Length"
         my $missing = $ct_len - length($buf);
         while ($missing > 0) {
             print "Need $missing more bytes of content\n" if $DEBUG;
             my $n = $self->_need_more($buf, $timeout, $fdset);

Fixed code:         # 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

        # split and clean up Content-Length ', ' separated string
        my @vals = map {my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
            split ',', $ct_len;
        # check that they are all numbers (RFC: Content-Length = 1*DIGIT)
        my @nums = grep { /^[0-9]+$/} @vals;
        unless (@vals == @nums) {
            $self->send_error(400);
            $self->reason("Content-Length value must be a unsigned integer");
            return;
        }
        # check they are all the same
        my $ct_len = shift @nums;
        foreach (@nums) {
            next if $_ == $ct_len;
            $self->send_error(400);
            $self->reason("Content-Length values are not the same");
            return;
        }
        # ensure we have now a fixed header, with only 1 value
        $r->header('Content-Length' => $ct_len);