From 866245b46f83c16473cbd9e44eaa0f90bb2d471a Mon Sep 17 00:00:00 2001 From: CPANSec Security Scanner Bot Date: Tue, 24 Nov 2026 14:29:38 +0000 Subject: [PATCH] Net::Server: anchor the whole allow/deny regex allow_deny matched each allow and deny rule as /^$_$/. When a rule contains top-level alternation, such as 10\.0\.0\.1|10\.0\.0\.2, the ^ binds only to the first branch and the $ only to the last, so the rule matches any address that starts with the first alternative or ends with the last one. An allow rule of that form admits unlisted peers such as 10.0.0.10 or 110.0.0.2, and a deny rule of that form rejects them. Wrap the rule in a non-capturing group, /^(?:$_)$/, so every branch must match the whole peeraddr or peerhost. Document the whole-string match in the allow/deny POD and add a regression test. Notes for deciding whether to apply this patch: - Only rules containing top-level alternation (|) change behaviour. Such a deny rule also stops rejecting addresses that merely start with its first alternative or end with its last one. - Adds t/AllowDeny.t and lists it in MANIFEST. - 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. --- MANIFEST | 1 + lib/Net/Server.pm | 8 ++++---- lib/Net/Server.pod | 3 +++ t/AllowDeny.t | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 t/AllowDeny.t diff --git a/MANIFEST b/MANIFEST index 960a5a3..b1ebff8 100644 --- a/MANIFEST +++ b/MANIFEST @@ -61,6 +61,7 @@ t/26_IPv6_socketip_Only.t t/27_IPv6_socketinet6_Only.t t/28_IPvA_socketip_Only.t t/29_IPvA_socketinet6_Only.t +t/AllowDeny.t t/Options.t t/Options.t.conf t/Port_Configuration.t diff --git a/lib/Net/Server.pm b/lib/Net/Server.pm index 17353d3..3a9e71d 100644 --- a/lib/Net/Server.pm +++ b/lib/Net/Server.pm @@ -617,8 +617,8 @@ sub allow_deny { # if the addr or host matches a deny, reject it immediately foreach (@{ $prop->{'deny'} }) { return 0 if $prop->{'reverse_lookups'} - && defined($prop->{'peerhost'}) && $prop->{'peerhost'} =~ /^$_$/; - return 0 if $peeraddr =~ /^$_$/; + && defined($prop->{'peerhost'}) && $prop->{'peerhost'} =~ /^(?:$_)$/; + return 0 if $peeraddr =~ /^(?:$_)$/; } if (@{ $prop->{'cidr_deny'} }) { require Net::CIDR; @@ -628,8 +628,8 @@ sub allow_deny { # if the addr or host isn't blocked yet, allow it if it is allowed foreach (@{ $prop->{'allow'} }) { return 1 if $prop->{'reverse_lookups'} - && defined($prop->{'peerhost'}) && $prop->{'peerhost'} =~ /^$_$/; - return 1 if $peeraddr =~ /^$_$/; + && defined($prop->{'peerhost'}) && $prop->{'peerhost'} =~ /^(?:$_)$/; + return 1 if $peeraddr =~ /^(?:$_)$/; } if (@{ $prop->{'cidr_allow'} }) { require Net::CIDR; diff --git a/lib/Net/Server.pod b/lib/Net/Server.pod index 960cfb4..51f9a99 100644 --- a/lib/Net/Server.pod +++ b/lib/Net/Server.pod @@ -870,6 +870,9 @@ rules. May be specified multiple times. Contains regex to compare to incoming peeraddr or peerhost (if reverse_lookups has been enabled). +Each regex must match the whole peeraddr or peerhost, as if written +C<^(?:regex)$>, so alternation such as C<10\.0\.0\.1|10\.0\.0\.2> works +as expected. If allow or deny options are given, the incoming client must match an allow and not match a deny or the client connection will be closed. Defaults to empty array refs. diff --git a/t/AllowDeny.t b/t/AllowDeny.t new file mode 100644 index 0000000..054925b --- /dev/null +++ b/t/AllowDeny.t @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +=head1 NAME + +AllowDeny.t - Test that allow/deny rules match the whole peer address + +=cut + +package FakeSock; +sub new { bless {}, shift } +sub NS_proto { 'TCP' } + +package main; + +use strict; +use FindBin qw($Bin); +use lib $Bin; +use NetServerTest qw(prepare_test ok use_ok); +prepare_test({n_tests => 6, plan_only => 1}); + +use_ok('Net::Server'); + +sub check { + my ($addr, %rules) = @_; + my $server = bless {server => { + peeraddr => $addr, + allow => $rules{'allow'} || [], + deny => $rules{'deny'} || [], + cidr_allow => [], + cidr_deny => [], + }}, 'Net::Server'; + return $server->allow_deny(FakeSock->new) ? 1 : 0; +} + +my @allow = ('10\.0\.0\.1|10\.0\.0\.2'); +ok(check('10.0.0.1', allow => \@allow), "First alternative is allowed"); +ok(check('10.0.0.2', allow => \@allow), "Last alternative is allowed"); +ok(!check('10.0.0.10', allow => \@allow), "Suffix of first alternative is not allowed"); +ok(!check('110.0.0.2', allow => \@allow), "Prefix of last alternative is not allowed"); +ok(check('10.0.0.10', allow => ['.*'], deny => ['10\.0\.0\.1|10\.0\.0\.2']), "Deny with alternation does not reject a longer address"); -- 2.53.0