From 37e8547094dd6fd535b45e76562de20f19c1a5bc Mon Sep 17 00:00:00 2001
From: CPANSec Security Scanner Bot <cpan-security@security.metacpan.org>
Date: Tue, 24 Nov 2026 14:29:20 +0000
Subject: [PATCH] Net::Server: stop read_until at end of file

read_until reads one byte per iteration and only leaves the loop once
it has the requested number of bytes or a match for the end pattern.
The return value of read() was never checked, so once the peer had
closed the connection every read returned 0 and the loop spun at full
CPU. Net::Server::HTTP reads every request header through read_until,
so a client that connected and closed before ending its headers held a
worker at full CPU until timeout_header fired, and other callers
without their own alarm spun forever.

Leave the loop when read() returns 0 (end of file) or undef (error),
as the SSLEAY read_until already does. $ok stays 0 in that case, so
callers see the same failure as for any other incomplete read, and
Net::Server::HTTP rejects the request as before.

The same loop is fixed in Net::Server::Proto::TCP,
Net::Server::Proto::UNIX, Net::Server::Proto::SSL and Net::Server::INET.

Notes for deciding whether to apply this patch:

- At end of file read_until now returns with a failure status and
  whatever partial data was read, instead of looping. Callers that check
  the status, such as Net::Server::HTTP, reject the request as before.

- The SSLEAY copy of read_until already stopped at end of file and is
  not changed.

- Not tested on a running server. It was checked by review and by
  applying it to Net-Server 2.018.

- Applies on its own to Net-Server 2.018, and together with the other
  CPANSec patches for Net-Server 2.018 when they are applied in CVE-ID
  order.
---
 lib/Net/Server/INET.pm       | 2 +-
 lib/Net/Server/Proto/SSL.pm  | 2 +-
 lib/Net/Server/Proto/TCP.pm  | 2 +-
 lib/Net/Server/Proto/UNIX.pm | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/Net/Server/INET.pm b/lib/Net/Server/INET.pm
index b0ee8ec..8336de9 100644
--- a/lib/Net/Server/INET.pm
+++ b/lib/Net/Server/INET.pm
@@ -177,7 +177,7 @@ sub read_until { # only sips the data - but it allows for compatibility with SSL
     my $content = '';
     my $ok = 0;
     while (1) {
-        $client->read($content, 1, length($content));
+        $client->read($content, 1, length($content)) or last; # 0 at end of file, undef on error
         if (defined($bytes) && length($content) >= $bytes) {
             $ok = 2;
             last;
diff --git a/lib/Net/Server/Proto/SSL.pm b/lib/Net/Server/Proto/SSL.pm
index d15105a..ac4382b 100644
--- a/lib/Net/Server/Proto/SSL.pm
+++ b/lib/Net/Server/Proto/SSL.pm
@@ -225,7 +225,7 @@ sub read_until { # allow for an interface that can be tied to STDOUT
     my $content = '';
     my $ok = 0;
     while (1) {
-        $client->read($content, 1, length($content));
+        $client->read($content, 1, length($content)) or last; # 0 at end of file, undef on error
         if (defined($bytes) && length($content) >= $bytes) {
             $ok = 2;
             last;
diff --git a/lib/Net/Server/Proto/TCP.pm b/lib/Net/Server/Proto/TCP.pm
index 7480a7f..47f79f1 100644
--- a/lib/Net/Server/Proto/TCP.pm
+++ b/lib/Net/Server/Proto/TCP.pm
@@ -120,7 +120,7 @@ sub read_until { # only sips the data - but it allows for compatibility with SSL
     my $content = '';
     my $ok = 0;
     while (1) {
-        $client->read($content, 1, length($content));
+        $client->read($content, 1, length($content)) or last; # 0 at end of file, undef on error
         if (defined($bytes) && length($content) >= $bytes) {
             $ok = 2;
             last;
diff --git a/lib/Net/Server/Proto/UNIX.pm b/lib/Net/Server/Proto/UNIX.pm
index d3c3dce..d7878dd 100644
--- a/lib/Net/Server/Proto/UNIX.pm
+++ b/lib/Net/Server/Proto/UNIX.pm
@@ -117,7 +117,7 @@ sub read_until { # only sips the data - but it allows for compatibility with SSL
     my $content = '';
     my $ok = 0;
     while (1) {
-        $client->read($content, 1, length($content));
+        $client->read($content, 1, length($content)) or last; # 0 at end of file, undef on error
         if (defined($bytes) && length($content) >= $bytes) {
             $ok = 2;
             last;
-- 
2.53.0

