| 1 | package PAGI::Middleware::ReverseProxy; | ||||||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::Middleware::ReverseProxy::VERSION = '0.002002'; | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use parent 'PAGI::Middleware'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | PAGI::Middleware::ReverseProxy - Handle X-Forwarded-* headers from reverse proxies | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | my $app = builder { | ||||||||||||||||||||||||||||||||
| 17 | enable 'ReverseProxy', | ||||||||||||||||||||||||||||||||
| 18 | trusted_proxies => ['127.0.0.1', '10.0.0.0/8']; | ||||||||||||||||||||||||||||||||
| 19 | $my_app; | ||||||||||||||||||||||||||||||||
| 20 | }; | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | PAGI::Middleware::ReverseProxy processes X-Forwarded-* headers from trusted | ||||||||||||||||||||||||||||||||
| 25 | reverse proxies and updates the scope with the original client information. | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | =over 4 | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | =item * trusted_proxies (default: ['127.0.0.1', '::1']) | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | Arrayref of trusted proxy IP addresses or CIDR ranges. | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | =item * trust_all (default: 0) | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | If true, trust X-Forwarded headers from any source. Use with caution! | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | =back | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | =head1 HEADERS PROCESSED | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | =over 4 | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | =item * X-Forwarded-For - Original client IP | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | =item * X-Forwarded-Proto - Original protocol (http/https) | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | =item * X-Forwarded-Host - Original Host header | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | =item * X-Forwarded-Port - Original port | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | =item * X-Real-IP - Alternative to X-Forwarded-For (nginx) | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | =back | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | sub _init { | ||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||
| 60 | 3 | my ($self, $config) = @_; | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | $self->{trusted_proxies} = $config->{trusted_proxies} // ['127.0.0.1', '::1']; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"trusted_proxies"} // ["127.0.0.1", "::1"]
MC/DC0%0/1
$config->{"trusted_proxies"} // ["127.0.0.1", "::1"]
$config->{"trusted_proxies"}
Truth table50%1/2
A
A= $config->{"trusted_proxies"}
| |||||||||||||||||||||||||||||||||
| 63 | 3 | ▶ | $self->{trust_all} = $config->{trust_all} // 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"trust_all"} // 0
MC/DC0%0/1
$config->{"trust_all"} // 0
$config->{"trust_all"}
Truth table50%1/2
A
A= $config->{"trust_all"}
| |||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | sub wrap { | ||||||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 67 | 3 | my ($self, $app) = @_; | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 3 | ▶ | return async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=18 SCAR=37.7
| |||||||||||||||||||||||||||||||||
| 70 | 3 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 73 | 0 | return; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # Check if request is from trusted proxy | ||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | my $client_ip = exists $scope->{client} ? ($scope->{client}[0] // '') : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$scope->{"client"}[0] // ""
MC/DC0%0/1
$scope->{"client"}[0] // ""
$scope->{"client"}[0]
Truth table50%1/2
A
A= $scope->{"client"}[0]
| |||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | unless ($self->{trust_all} || $self->_is_trusted($client_ip)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"trust_all"} or $self->_is_trusted($client_ip)
MC/DC50%1/2
$self->{"trust_all"} or $self->_is_trusted($client_ip)
$self->{"trust_all"}
$self->_is_trusted($client_ip)
Truth table66%2/3
A or B
A= $self->{"trust_all"}B= $self->_is_trusted($client_ip)
| |||||||||||||||||||||||||||||||||
| 79 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 80 | 1 | return; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | # Build modified scope | ||||||||||||||||||||||||||||||||
| 84 | 2 | my %new_scope = %$scope; | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | # X-Forwarded-For or X-Real-IP | ||||||||||||||||||||||||||||||||
| 87 | 2 | my $forwarded_for = $self->_get_header($scope, 'x-forwarded-for'); | |||||||||||||||||||||||||||||||
| 88 | 2 | my $real_ip = $self->_get_header($scope, 'x-real-ip'); | |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | if ($forwarded_for) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 91 | # Take the leftmost IP (original client) | ||||||||||||||||||||||||||||||||
| 92 | 1 | my ($original_ip) = split /\s*,\s*/, $forwarded_for; | |||||||||||||||||||||||||||||||
| 93 | 1 | $original_ip =~ s/^\s+//; | |||||||||||||||||||||||||||||||
| 94 | 1 | $original_ip =~ s/\s+$//; | |||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | if (exists $scope->{client}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 1 | $new_scope{client} = [$original_ip, $scope->{client}[1]]; | |||||||||||||||||||||||||||||||
| 97 | 1 | $new_scope{original_client} = $scope->{client}; | |||||||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||||||
| 99 | 0 | $new_scope{client} = [$original_ip, undef]; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | } elsif ($real_ip) { | ||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if (exists $scope->{client}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | 0 | $new_scope{client} = [$real_ip, $scope->{client}[1]]; | |||||||||||||||||||||||||||||||
| 104 | 0 | $new_scope{original_client} = $scope->{client}; | |||||||||||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||||||||||
| 106 | 0 | $new_scope{client} = [$real_ip, undef]; | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | # X-Forwarded-Proto | ||||||||||||||||||||||||||||||||
| 111 | 2 | my $forwarded_proto = $self->_get_header($scope, 'x-forwarded-proto'); | |||||||||||||||||||||||||||||||
| 112 | 2 | ▶ | if ($forwarded_proto) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | 1 | $forwarded_proto = lc($forwarded_proto); | |||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | $new_scope{scheme} = $forwarded_proto if $forwarded_proto =~ /^https?$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | # X-Forwarded-Host | ||||||||||||||||||||||||||||||||
| 118 | 2 | my $forwarded_host = $self->_get_header($scope, 'x-forwarded-host'); | |||||||||||||||||||||||||||||||
| 119 | 2 | ▶ | if ($forwarded_host) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | # Update headers with new Host | ||||||||||||||||||||||||||||||||
| 121 | 0 | my @new_headers; | |||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | for my $h (@{$scope->{headers} // []}) { | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$scope->{"headers"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table0%0/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if (lc($h->[0]) eq 'host') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | 0 | push @new_headers, ['host', $forwarded_host]; | |||||||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||||||
| 126 | 0 | push @new_headers, $h; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | 0 | $new_scope{headers} = \@new_headers; | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | # X-Forwarded-Port | ||||||||||||||||||||||||||||||||
| 133 | 2 | my $forwarded_port = $self->_get_header($scope, 'x-forwarded-port'); | |||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | if ($forwarded_port && $forwarded_port =~ /^\d+$/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$forwarded_port and $forwarded_port =~ /^\d+$/
MC/DC0%0/2
$forwarded_port and $forwarded_port =~ /^\d+$/
$forwarded_port
$forwarded_port =~ /^\d+$/
Truth table33%1/3
A and B
A= $forwarded_portB= $forwarded_port =~ /^\d+$/
| |||||||||||||||||||||||||||||||||
| 135 | 0 | $new_scope{server} = [$scope->{server}[0], int($forwarded_port)]; | |||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 2 | await $app->(\%new_scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 139 | 3 | }; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 3 | ▶ | sub _is_trusted { | ||||||||||||||||||||||||||||||
|
Subroutine
_is_trusted: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||||||
| 143 | 3 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 3 | for my $trusted (@{$self->{trusted_proxies}}) { | |||||||||||||||||||||||||||||||
| 146 | 3 | ▶ | if ($trusted =~ m{/}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | # CIDR notation | ||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | return 1 if $self->_ip_in_cidr($ip, $trusted); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||||||||||||||
| 150 | # Exact match | ||||||||||||||||||||||||||||||||
| 151 | 3 | ▶ | return 1 if $ip eq $trusted; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | 1 | return 0; | |||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub _ip_in_cidr { | ||||||||||||||||||||||||||||||
|
Subroutine
_ip_in_cidr: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 158 | 0 | my ($self, $ip, $cidr) = @_; | |||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | 0 | my ($network, $bits) = split m{/}, $cidr; | |||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | # Simple IPv4 check | ||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | return 0 unless $ip =~ /^[\d.]+$/ && $network =~ /^[\d.]+$/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ip =~ /^[\d.]+$/ and $network =~ /^[\d.]+$/
MC/DC0%0/2
$ip =~ /^[\d.]+$/ and $network =~ /^[\d.]+$/
$ip =~ /^[\d.]+$/
$network =~ /^[\d.]+$/
Truth table0%0/3
A and B
A= $ip =~ /^[\d.]+$/B= $network =~ /^[\d.]+$/
| |||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 0 | my $ip_num = $self->_ip_to_num($ip); | |||||||||||||||||||||||||||||||
| 166 | 0 | my $net_num = $self->_ip_to_num($network); | |||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | return 0 unless defined $ip_num && defined $net_num; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $ip_num and defined $net_num
MC/DC0%0/2
defined $ip_num and defined $net_num
defined $ip_num
defined $net_num
Truth table0%0/3
A and B
A= defined $ip_numB= defined $net_num
| |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 0 | my $mask = ~((1 << (32 - $bits)) - 1) & 0xFFFFFFFF; | |||||||||||||||||||||||||||||||
| 171 | 0 | return ($ip_num & $mask) == ($net_num & $mask); | |||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | sub _ip_to_num { | ||||||||||||||||||||||||||||||
|
Subroutine
_ip_to_num: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 175 | 0 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | 0 | my @octets = split /\./, $ip; | |||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | return unless @octets == 4; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | return unless _all(sub { /^\d+$/ && $_ >= 0 && $_ <= 255 }, @octets); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
Condition0%0/3
/^\d+$/ and $_ >= 0
MC/DC0%0/2
/^\d+$/ and $_ >= 0
/^\d+$/
$_ >= 0
Truth table0%0/3
A and B
A= /^\d+$/B= $_ >= 0
| |||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 0 | return ($octets[0] << 24) + ($octets[1] << 16) + ($octets[2] << 8) + $octets[3]; | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | sub _all { | ||||||||||||||||||||||||||||||
|
Subroutine
_all: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 185 | 0 | my ($code, @list) = @_; | |||||||||||||||||||||||||||||||
| 186 | 0 | for (@list) { | |||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | return 0 unless $code->(); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||
| 192 | 10 | ▶ | sub _get_header { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_header: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 193 | 10 | my ($self, $scope, $name) = @_; | |||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 10 | $name = lc($name); | |||||||||||||||||||||||||||||||
| 196 | 10 | ▶ | for my $h (@{$scope->{headers} // []}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"headers"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||||||
| 197 | 10 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | 8 | return; | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | 1 | 1; | |||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | __END__ | ||||||||||||||||||||||||||||||||