| 1 | package Mojolicious::Plugin::ClientIP::Pluggable; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Client IP header handling for Mojolicious requests | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Mojolicious::Plugin::ClientIP::Pluggable - Customizable client IP detection plugin for Mojolicious | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use Mojolicious::Lite; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # CloudFlare-waware settings | ||||||||||||||||||||||||||||
| 14 | plugin 'ClientIP::Pluggable', | ||||||||||||||||||||||||||||
| 15 | analyze_headers => [qw/cf-pseudo-ipv4 cf-connecting-ip true-client-ip/], | ||||||||||||||||||||||||||||
| 16 | restrict_family => 'ipv4', | ||||||||||||||||||||||||||||
| 17 | fallbacks => [qw/rfc-7239 x-forwarded-for remote_address/]; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | get '/' => sub { | ||||||||||||||||||||||||||||
| 21 | my $c = shift; | ||||||||||||||||||||||||||||
| 22 | $c->render(text => $c->client_ip); | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | app->start; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Mojolicious::Plugin::ClientIP::Pluggable is a Mojolicious plugin to get an IP address, which | ||||||||||||||||||||||||||||
| 30 | allows to specify different HTTP-headers (and their priorities) for client IP address | ||||||||||||||||||||||||||||
| 31 | extraction. This is needed as different cloud providers set different headers to disclose | ||||||||||||||||||||||||||||
| 32 | real IP address. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | If the address cannot be extracted from headers different fallback options are available: | ||||||||||||||||||||||||||||
| 35 | detect IP address from C<X-Forwarded-For> header, detect IP address from C<forwarded> header | ||||||||||||||||||||||||||||
| 36 | (rfc-7239), or use C<remote_address> environment. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | The plugin is inspired by L<Mojolicious::Plugin::ClientIP>. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 METHODS | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head2 client_ip | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Find a client IP address from the specified headers, with optional fallbacks. The address is | ||||||||||||||||||||||||||||
| 45 | validated that it is publicly available (aka routable) IP address. Empty string is returned | ||||||||||||||||||||||||||||
| 46 | if no valid address can be found. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head2 analyzed_headers | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | Define order and names of cloud provider injected headers with client IP address. | ||||||||||||||||||||||||||||
| 53 | For C<cloudflare> we found the following headers are suitable: | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | plugin 'ClientIP::Pluggable', | ||||||||||||||||||||||||||||
| 56 | analyzed_headers => [qw/cf-pseudo-ipv4 cf-connecting-ip true-client-ip/]. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | This option is mandatory. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | More details at L<https://support.cloudflare.com/hc/en-us/articles/202494830-Pseudo-IPv4-Supporting-IPv6-addresses-in-legacy-IPv4-applications>, | ||||||||||||||||||||||||||||
| 61 | L<https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-CloudFlare-handle-HTTP-Request-headers>, | ||||||||||||||||||||||||||||
| 62 | L<https://support.cloudflare.com/hc/en-us/articles/206776727-What-is-True-Client-IP> | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head2 restrict_family | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | plugin 'ClientIP::Pluggable', restrict_family => 'ipv4'; | ||||||||||||||||||||||||||||
| 67 | plugin 'ClientIP::Pluggable', restrict_family => 'ipv6'; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | If defined only IPv4 or IPv6 addresses are considered valid among the possible addresses. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | By default this option is not defined, allowing IPv4 and IPv6 addresses. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 fallbacks | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | plugin 'ClientIP::Pluggable', | ||||||||||||||||||||||||||||
| 76 | fallbacks => [qw/rfc-7239 x-forwarded-for remote_address/]); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Try to get valid client IP-address from fallback sources, if we fail to do that from | ||||||||||||||||||||||||||||
| 79 | cloud-provider headers. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | C<rfc-7239> uses C<forwarded> header, C<x-forwarded-for> use <x-forwarded-for> header | ||||||||||||||||||||||||||||
| 82 | (appeared before rfc-7239 and still widely used) or use remote_address environment | ||||||||||||||||||||||||||||
| 83 | (C<$c->tx->remote_address>). | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Default value is C<[remote_address]>. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 ENVIRONMENT | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 CLIENTIP_PLUGGABLE_ALLOW_LOOPBACK | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Allows non-routable loopback address (C<127.0.0.1>) to pass validation. Use it for | ||||||||||||||||||||||||||||
| 92 | test purposes. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Default value is C<0>, i.e. loopback addresses do not pass IP-address validation. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Copyright (C) 2017 binary.com | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 104 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3 | ▶ | use Data::Validate::IP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 3 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # for tests only | ||||||||||||||||||||||||||||
| 113 | 3 | ▶ | use constant ALLOW_LOOPBACK => $ENV{CLIENTIP_PLUGGABLE_ALLOW_LOOPBACK} || 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{'CLIENTIP_PLUGGABLE_ALLOW_LOOPBACK'} || 0
MC/DC100%1/1
$ENV{'CLIENTIP_PLUGGABLE_ALLOW_LOOPBACK'} || 0
$ENV{'CLIENTIP_PLUGGABLE_ALLOW_LOOPBACK'}
Truth table100%2/2
A
A= $ENV{'CLIENTIP_PLUGGABLE_ALLOW_LOOPBACK'}
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 10 | ▶ | sub _check_ipv4 { | ||||||||||||||||||||||||||
|
Subroutine
_check_ipv4: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 116 | 10 | my ($ip) = @_; | |||||||||||||||||||||||||||
| 117 | 10 | ▶ | return Data::Validate::IP::is_public_ipv4($ip) | ||||||||||||||||||||||||||
|
Condition100%3/3
Data::Validate::IP::is_public_ipv4($ip) || 0
MC/DC100%1/1
Data::Validate::IP::is_public_ipv4($ip) || 0
Data::Validate::IP::is_public_ipv4($ip)
Truth table100%2/2
A
A= Data::Validate::IP::is_public_ipv4($ip)
| |||||||||||||||||||||||||||||
| 118 | || (ALLOW_LOOPBACK && Data::Validate::IP::is_loopback_ipv4($ip)); | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 3 | ▶ | sub _check_ipv6 { | ||||||||||||||||||||||||||
|
Subroutine
_check_ipv6: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 122 | 3 | my ($ip) = @_; | |||||||||||||||||||||||||||
| 123 | 3 | ▶ | return Data::Validate::IP::is_public_ipv6($ip) | ||||||||||||||||||||||||||
|
Condition33%1/3
Data::Validate::IP::is_public_ipv6($ip) || 0
MC/DC0%0/1
Data::Validate::IP::is_public_ipv6($ip) || 0
Data::Validate::IP::is_public_ipv6($ip)
Truth table50%1/2
A
A= Data::Validate::IP::is_public_ipv6($ip)
| |||||||||||||||||||||||||||||
| 124 | || (ALLOW_LOOPBACK && Data::Validate::IP::is_loopback_ipv6($ip)); | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 17 | ▶ | sub _classify_ip { | ||||||||||||||||||||||||||
|
Subroutine
_classify_ip: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 128 | 17 | my ($ip) = @_; | |||||||||||||||||||||||||||
| 129 | return | ||||||||||||||||||||||||||||
| 130 | 17 | ▶ | Data::Validate::IP::is_ipv4($ip) ? 'ipv4' | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 131 | : Data::Validate::IP::is_ipv6($ip) ? 'ipv6' | ||||||||||||||||||||||||||||
| 132 | : undef; | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 11 | ▶ | sub _candidates_iterator { | ||||||||||||||||||||||||||
|
Subroutine
_candidates_iterator: called CC=10 SCAR=23.2
| |||||||||||||||||||||||||||||
| 136 | 11 | my ($c, $analyzed_headers, $fallback_options) = @_; | |||||||||||||||||||||||||||
| 137 | 11 | my $headers = $c->tx->req->headers; | |||||||||||||||||||||||||||
| 138 | 11 | ▶ | my @candidates = map { $headers->header($_) // () } @$analyzed_headers; | ||||||||||||||||||||||||||
|
Condition66%2/3
$headers->header($_) // ()
MC/DC0%0/2
$headers->header($_) // ()
$headers->header($_)
()
Truth table66%2/3
A // B
A= $headers->header($_)B= ()
| |||||||||||||||||||||||||||||
| 139 | 11 | my $comma_re = qr/\s*,\s*/; | |||||||||||||||||||||||||||
| 140 | 11 | for my $fallback (map { lc } @$fallback_options) { | |||||||||||||||||||||||||||
| 141 | 33 | ▶ | if ($fallback eq 'x-forwarded-for') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 142 | 11 | my $xff = $headers->header('x-forwarded-for'); | |||||||||||||||||||||||||||
| 143 | 11 | ▶ | next unless $xff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 5 | my @ips = split $comma_re, $xff; | |||||||||||||||||||||||||||
| 145 | 5 | push @candidates, @ips; | |||||||||||||||||||||||||||
| 146 | } elsif ($fallback eq 'remote_address') { | ||||||||||||||||||||||||||||
| 147 | 11 | push @candidates, $c->tx->remote_address; | |||||||||||||||||||||||||||
| 148 | } elsif ($fallback eq 'rfc-7239') { | ||||||||||||||||||||||||||||
| 149 | 11 | my $f = $headers->header('forwarded'); | |||||||||||||||||||||||||||
| 150 | 11 | ▶ | next unless $f; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 4 | my @pairs = map { split $comma_re, $_ } split ';', $f; | |||||||||||||||||||||||||||
| 152 | my @ips = map { | ||||||||||||||||||||||||||||
| 153 | 4 | my $ipv4_mask = qr/\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}/; | |||||||||||||||||||||||||||
| 154 | # it is not completely valid ipv6 mask, but enough | ||||||||||||||||||||||||||||
| 155 | # to extract address. It will be validated later | ||||||||||||||||||||||||||||
| 156 | 10 | my $ipv6_mask = qr/[\w:]+/; | |||||||||||||||||||||||||||
| 157 | 10 | ▶ | if (/for=($ipv4_mask)|(?:"?\[($ipv6_mask)\].*"?)/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 6 | ▶ | ($1 // $2); | ||||||||||||||||||||||||||
|
Condition66%2/3
$1 // $2
MC/DC0%0/2
$1 // $2
$1
$2
Truth table66%2/3
A // B
A= $1B= $2
| |||||||||||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||||||||||
| 160 | 4 | (); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | } @pairs; | ||||||||||||||||||||||||||||
| 163 | 4 | push @candidates, @ips; | |||||||||||||||||||||||||||
| 164 | } else { | ||||||||||||||||||||||||||||
| 165 | 0 | warn "Unknown fallback option $fallback, ignoring"; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 11 | my $idx = 0; | |||||||||||||||||||||||||||
| 169 | 18 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 170 | 18 | ▶ | if ($idx < @candidates) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 18 | return $candidates[$idx++]; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 0 | return (undef); | |||||||||||||||||||||||||||
| 174 | 11 | }; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 2 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 2 | my ($self, $app, $conf) = @_; | |||||||||||||||||||||||||||
| 179 | 2 | ▶ | my $analyzed_headers = $conf->{analyze_headers} // die "Please, specify 'analyzed_headers' option"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{"analyze_headers"} // die("Please, specify 'analyzed_headers' option")
MC/DC0%0/1
$conf->{"analyze_headers"} // die("Please, specify 'analyzed_headers' option")
$conf->{"analyze_headers"}
Truth table50%1/2
A
A= $conf->{"analyze_headers"}
| |||||||||||||||||||||||||||||
| 180 | 2 | my %validator_for = ( | |||||||||||||||||||||||||||
| 181 | ipv4 => \&_check_ipv4, | ||||||||||||||||||||||||||||
| 182 | ipv6 => \&_check_ipv6, | ||||||||||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||||||||||
| 184 | 2 | my $restrict_family = $conf->{restrict_family}; | |||||||||||||||||||||||||||
| 185 | 2 | ▶ | my $fallback_options = $conf->{fallbacks} // [qw/remote_address/]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{"fallbacks"} // ["remote_address"]
MC/DC0%0/1
$conf->{"fallbacks"} // ["remote_address"]
$conf->{"fallbacks"}
Truth table50%1/2
A
A= $conf->{"fallbacks"}
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | $app->helper( | ||||||||||||||||||||||||||||
| 188 | 11 | ▶ | client_ip => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||
| 189 | 11 | my ($c) = @_; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 11 | my $next_candidate = _candidates_iterator($c, $analyzed_headers, $fallback_options); | |||||||||||||||||||||||||||
| 192 | 11 | ▶ | while (my $ip = $next_candidate->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | # generic check | ||||||||||||||||||||||||||||
| 194 | 18 | ▶ | next unless Data::Validate::IP::is_ip($ip); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # classify & check | ||||||||||||||||||||||||||||
| 197 | 17 | my $address_family = _classify_ip($ip); | |||||||||||||||||||||||||||
| 198 | 17 | ▶ | next unless $address_family; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # possibly limit to acceptable address family | ||||||||||||||||||||||||||||
| 201 | 17 | ▶ | next if $restrict_family && $restrict_family ne $address_family; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$restrict_family and $restrict_family ne $address_family
MC/DC50%1/2
$restrict_family and $restrict_family ne $address_family
$restrict_family
$restrict_family ne $address_family
Truth table66%2/3
A and B
A= $restrict_familyB= $restrict_family ne $address_family
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # validate by family | ||||||||||||||||||||||||||||
| 204 | 13 | my $validator = $validator_for{$address_family}; | |||||||||||||||||||||||||||
| 205 | 13 | ▶ | next unless $validator->($ip); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # address seems valid, return its textual representation | ||||||||||||||||||||||||||||
| 208 | 11 | return $ip; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 0 | return ''; | |||||||||||||||||||||||||||
| 211 | 2 | }); | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 2 | return; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 3 | 1; | |||||||||||||||||||||||||||