Bug 24538: Handle Net::Netmask parser errors
authorDavid Cook <dcook@prosentient.com.au>
Wed, 19 Feb 2020 02:07:30 +0000 (02:07 +0000)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 20 Feb 2020 08:40:52 +0000 (08:40 +0000)
This patch switches from the new() to new2() constructor,
which will return an undef value when it fails to parse a value.

This patch warns on parser failures, but otherwise silently drops
the invalid value, and returns objects for any valid input it can parse.
This way one mistake won't disable the whole feature.

To test:
1. Run the unit test t/Koha/Middlware/RealIP.t

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Koha/Middleware/RealIP.pm

index 22f40db..88a843d 100644 (file)
@@ -106,7 +106,16 @@ sub get_trusted_proxies {
     my $proxies_conf = C4::Context->config('koha_trusted_proxies');
     return unless $proxies_conf;
     my @trusted_proxies_ip = split( / /, $proxies_conf );
-    my @trusted_proxies = map { Net::Netmask->new($_) } @trusted_proxies_ip;
+    my @trusted_proxies = ();
+    foreach my $ip (@trusted_proxies_ip){
+        my $mask = Net::Netmask->new2($ip);
+        if ($mask){
+            push(@trusted_proxies,$mask);
+        }
+        else {
+            warn "$Net::Netmask::error";
+        }
+    }
     return \@trusted_proxies;
 }