From e5715ae90b2b9ac5654314cda341aa13dd004108 Mon Sep 17 00:00:00 2001 From: CPANSec Security Scanner Bot Date: Tue, 24 Nov 2026 14:31:23 +0000 Subject: [PATCH] Net::Server::HTTP: remove dot segments before app dispatch http_dispatch matches the configured app locations against PATH_INFO as it came from the request line, without removing "." and ".." segments, while the configured locations themselves are normalized. A request for /admin/../pub (or /admin/%2e%2e/pub) therefore runs the handler configured for /admin. Behind a reverse proxy that applies its access rules to the normalized path (/pub) but forwards the original request path, a client can run a handler the proxy was meant to block. When PATH_INFO contains a "." or ".." segment, including one written with percent-encoded dots, collapse repeated slashes and remove the dot segments as described in RFC 3986 section 5.2.4 before matching, so the request is dispatched on the same path a normalizing proxy sees. Paths without dot segments are matched exactly as before. Document the behaviour under the app configuration item. Notes for deciding whether to apply this patch: - Only paths that contain a dot segment are rewritten. For those, SCRIPT_NAME and PATH_INFO are built from the normalized path, and /../x becomes /x rather than being rejected. - Percent-encoded dots are decoded before matching. Other differences between proxy and server normalization, such as a proxy that decodes %2f, are not covered. - Dispatch in Net::Server::PSGI is not changed by this patch. - 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/HTTP.pm | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/Net/Server/HTTP.pm b/lib/Net/Server/HTTP.pm index d186e06..4824c8f 100644 --- a/lib/Net/Server/HTTP.pm +++ b/lib/Net/Server/HTTP.pm @@ -471,6 +471,15 @@ sub http_note { sub http_dispatch { my ($self, $dispatch_qr, $dispatch_table) = @_; + # Match against the path with "." and ".." segments removed (RFC 3986 5.2.4), + # as a front end proxy applying rules to the normalized path would see it. + my $path = defined($ENV{'PATH_INFO'}) ? $ENV{'PATH_INFO'} : ''; + $path =~ s/%2e/./gi; + if ($path =~ m{(?:^|/)\.\.?(?:/|$)}) { + $path =~ s{//+}{/}g; + $ENV{'PATH_INFO'} = _remove_dot_segments($path); + } + $ENV{'PATH_INFO'} =~ s{^($dispatch_qr)(?=/|$|(?<=/))}{} or die "Dispatch not found\n"; $ENV{'SCRIPT_NAME'} = $1; if ($ENV{'PATH_INFO'}) { @@ -482,6 +491,19 @@ sub http_dispatch { $self->exec_cgi($code); } +sub _remove_dot_segments { + my $in = shift; + my $out = ''; + while (length $in) { + if ($in =~ s{^\.\.?/}{}) { } + elsif ($in =~ s{^/\.(?:/|$)}{/}) { } + elsif ($in =~ s{^/\.\.(?:/|$)}{/}) { $out =~ s{/?[^/]*$}{} } + elsif ($in =~ m{^\.\.?$}) { $in = '' } + else { $in =~ s{^(/?[^/]*)}{}; $out .= $1 } + } + return $out; +} + sub process_http_request { my ($self, $client) = @_; @@ -1092,7 +1114,10 @@ target should be a valid executable suitable for passing to exec_cgi. The locations will be added in the order that they are configured. They will be added to a regular expression which will be applied to -the incoming PATH_INFO string. If the match is successful, the +the incoming PATH_INFO string. If PATH_INFO contains "." or ".." +segments (including percent-encoded dots), they are removed as +described in RFC 3986 section 5.2.4 before matching, so that +/admin/../pub is dispatched as /pub. If the match is successful, the $ENV{'SCRIPT_NAME'} will be set to the matched portion and the matched portion will be removed from $ENV{'PATH_INFO'}. -- 2.53.0