Gungho/Component/BlockPrivateIP.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 85 CC 11 SCAR 30.1
CC
11
Cov
0%
address_is_private
42.0
block_private_ip_address
20.0
request_is_allowed
6.0
CRAP 20.2
1 # $Id: /mirror/gungho/lib/Gungho/Component/BlockPrivateIP.pm 31095 2007-11-26T00:05:40.329716Z lestrrat $
2 #
3 # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp>
4 # All rights reserved.
5  
6 package Gungho::Component::BlockPrivateIP;
7 use strict;
8 use warnings;
9 use base qw(Gungho::Component);
10 use Regexp::Common qw(net);
11  
12 sub request_is_allowed
13 {
14 my ($c, $request) = @_;
15  
16 # Check if we are filtering private addresses
17 return if $c->block_private_ip_address($request, $request->uri);
18 return $c->next::method($request);
19 }
20  
21 sub handle_dns_response
22 {
23 my ($c, $request, $answer, $dns_response) = @_;
24  
25 # Check if we are filtering private addresses
26 return if $c->block_private_ip_address($request, $answer->address);
27  
28 $c->next::method($request, $answer, $dns_response);
29 }
30  
31 sub block_private_ip_address
32 {
33 my ($c, $request, $address) = @_;
34  
35 if (ref $address && $address->isa('URI')) {
36 if (! $address->can('host')) {
37 # no host, no check
38 return undef;
39 }
40 $address = $address->host;
41 }
42  
43 if ($c->address_is_private($address)) {
44 $c->log->debug('Hostname ' . $request->uri->host . ' has a private ip address: ' . $address);
45 $c->handle_response($request, $c->_http_error(500, 'Access blocked for hostname with private address: ' . $request->uri->host, $request));
46 return 1;
47 }
48
49 undef;
50 }
51  
52 sub address_is_private
53 {
54 my ($self, $address) = @_;
55  
56 if ($address =~ /^$RE{net}{IPv4}{-keep}$/) {
57 my ($o1, $o2, $o3, $o4) = ($2, $3, $4, $5);
58  
59 if ($o1 eq '10') {
60 return 1;
61 } elsif ($o1 eq '127') {
62 return 1;
63 } elsif ($o1 eq '172') {
64 return $o2 >= 16 && $o2 <= 31
65 } elsif ($o1 eq '192' && $o2 eq '168') {
66 return 1;
67 }
68 }
69
70 return 0;
71 }
72  
73 1;
74  
75 __END__