|
11 | 11 |
|
12 | 12 | namespace CodeIgniter\HTTP; |
13 | 13 |
|
| 14 | +use CodeIgniter\Exceptions\ConfigException; |
14 | 15 | use CodeIgniter\Validation\FormatRules; |
15 | 16 |
|
16 | 17 | /** |
@@ -59,95 +60,111 @@ public function getIPAddress(): string |
59 | 60 | /** |
60 | 61 | * @deprecated $this->proxyIPs property will be removed in the future |
61 | 62 | */ |
| 63 | + // @phpstan-ignore-next-line |
62 | 64 | $proxyIPs = $this->proxyIPs ?? config('App')->proxyIPs; |
63 | | - if (! empty($proxyIPs) && ! is_array($proxyIPs)) { |
64 | | - $proxyIPs = explode(',', str_replace(' ', '', $proxyIPs)); |
| 65 | + if (! empty($proxyIPs)) { |
| 66 | + // @phpstan-ignore-next-line |
| 67 | + if (! is_array($proxyIPs) || is_int(array_keys($proxyIPs)[0])) { |
| 68 | + throw new ConfigException( |
| 69 | + 'You must set an array with Proxy IP address key and HTTP header name value in Config\App::$proxyIPs.' |
| 70 | + ); |
| 71 | + } |
65 | 72 | } |
66 | 73 |
|
67 | 74 | $this->ipAddress = $this->getServer('REMOTE_ADDR'); |
68 | 75 |
|
69 | 76 | if ($proxyIPs) { |
70 | | - foreach (['x-forwarded-for', 'client-ip', 'x-client-ip', 'x-cluster-client-ip'] as $header) { |
71 | | - $spoof = null; |
72 | | - $headerObj = $this->header($header); |
73 | | - |
74 | | - if ($headerObj !== null) { |
75 | | - $spoof = $headerObj->getValue(); |
| 77 | + foreach ($proxyIPs as $proxyIP => $header) { |
| 78 | + // Check if we have an IP address or a subnet |
| 79 | + if (strpos($proxyIP, '/') === false) { |
| 80 | + // An IP address (and not a subnet) is specified. |
| 81 | + // We can compare right away. |
| 82 | + if ($proxyIP === $this->ipAddress) { |
| 83 | + $spoof = null; |
| 84 | + $headerObj = $this->header($header); |
| 85 | + |
| 86 | + if ($headerObj !== null) { |
| 87 | + $spoof = $headerObj->getValue(); |
| 88 | + |
| 89 | + // Some proxies typically list the whole chain of IP |
| 90 | + // addresses through which the client has reached us. |
| 91 | + // e.g. client_ip, proxy_ip1, proxy_ip2, etc. |
| 92 | + sscanf($spoof, '%[^,]', $spoof); |
| 93 | + |
| 94 | + if (! $ipValidator($spoof)) { |
| 95 | + $spoof = null; |
| 96 | + } else { |
| 97 | + $this->ipAddress = $spoof; |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
76 | 102 |
|
77 | | - // Some proxies typically list the whole chain of IP |
78 | | - // addresses through which the client has reached us. |
79 | | - // e.g. client_ip, proxy_ip1, proxy_ip2, etc. |
80 | | - sscanf($spoof, '%[^,]', $spoof); |
| 103 | + continue; |
| 104 | + } |
81 | 105 |
|
82 | | - if (! $ipValidator($spoof)) { |
83 | | - $spoof = null; |
84 | | - } else { |
85 | | - break; |
86 | | - } |
| 106 | + // We have a subnet ... now the heavy lifting begins |
| 107 | + if (! isset($separator)) { |
| 108 | + $separator = $ipValidator($this->ipAddress, 'ipv6') ? ':' : '.'; |
87 | 109 | } |
88 | | - } |
89 | 110 |
|
90 | | - if ($spoof) { |
91 | | - foreach ($proxyIPs as $proxyIP) { |
92 | | - // Check if we have an IP address or a subnet |
93 | | - if (strpos($proxyIP, '/') === false) { |
94 | | - // An IP address (and not a subnet) is specified. |
95 | | - // We can compare right away. |
96 | | - if ($proxyIP === $this->ipAddress) { |
97 | | - $this->ipAddress = $spoof; |
98 | | - break; |
99 | | - } |
| 111 | + // If the proxy entry doesn't match the IP protocol - skip it |
| 112 | + if (strpos($proxyIP, $separator) === false) { |
| 113 | + continue; |
| 114 | + } |
100 | 115 |
|
101 | | - continue; |
102 | | - } |
| 116 | + // Convert the REMOTE_ADDR IP address to binary, if needed |
| 117 | + if (! isset($ip, $sprintf)) { |
| 118 | + if ($separator === ':') { |
| 119 | + // Make sure we're having the "full" IPv6 format |
| 120 | + $ip = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($this->ipAddress, ':')), $this->ipAddress)); |
103 | 121 |
|
104 | | - // We have a subnet ... now the heavy lifting begins |
105 | | - if (! isset($separator)) { |
106 | | - $separator = $ipValidator($this->ipAddress, 'ipv6') ? ':' : '.'; |
107 | | - } |
| 122 | + for ($j = 0; $j < 8; $j++) { |
| 123 | + $ip[$j] = intval($ip[$j], 16); |
| 124 | + } |
108 | 125 |
|
109 | | - // If the proxy entry doesn't match the IP protocol - skip it |
110 | | - if (strpos($proxyIP, $separator) === false) { |
111 | | - continue; |
| 126 | + $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b'; |
| 127 | + } else { |
| 128 | + $ip = explode('.', $this->ipAddress); |
| 129 | + $sprintf = '%08b%08b%08b%08b'; |
112 | 130 | } |
113 | 131 |
|
114 | | - // Convert the REMOTE_ADDR IP address to binary, if needed |
115 | | - if (! isset($ip, $sprintf)) { |
116 | | - if ($separator === ':') { |
117 | | - // Make sure we're have the "full" IPv6 format |
118 | | - $ip = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($this->ipAddress, ':')), $this->ipAddress)); |
| 132 | + $ip = vsprintf($sprintf, $ip); |
| 133 | + } |
119 | 134 |
|
120 | | - for ($j = 0; $j < 8; $j++) { |
121 | | - $ip[$j] = intval($ip[$j], 16); |
122 | | - } |
| 135 | + // Split the netmask length off the network address |
| 136 | + sscanf($proxyIP, '%[^/]/%d', $netaddr, $masklen); |
123 | 137 |
|
124 | | - $sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b'; |
125 | | - } else { |
126 | | - $ip = explode('.', $this->ipAddress); |
127 | | - $sprintf = '%08b%08b%08b%08b'; |
128 | | - } |
| 138 | + // Again, an IPv6 address is most likely in a compressed form |
| 139 | + if ($separator === ':') { |
| 140 | + $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr)); |
129 | 141 |
|
130 | | - $ip = vsprintf($sprintf, $ip); |
| 142 | + for ($i = 0; $i < 8; $i++) { |
| 143 | + $netaddr[$i] = intval($netaddr[$i], 16); |
131 | 144 | } |
| 145 | + } else { |
| 146 | + $netaddr = explode('.', $netaddr); |
| 147 | + } |
132 | 148 |
|
133 | | - // Split the netmask length off the network address |
134 | | - sscanf($proxyIP, '%[^/]/%d', $netaddr, $masklen); |
| 149 | + // Convert to binary and finally compare |
| 150 | + if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0) { |
| 151 | + $spoof = null; |
| 152 | + $headerObj = $this->header($header); |
135 | 153 |
|
136 | | - // Again, an IPv6 address is most likely in a compressed form |
137 | | - if ($separator === ':') { |
138 | | - $netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr)); |
| 154 | + if ($headerObj !== null) { |
| 155 | + $spoof = $headerObj->getValue(); |
139 | 156 |
|
140 | | - for ($i = 0; $i < 8; $i++) { |
141 | | - $netaddr[$i] = intval($netaddr[$i], 16); |
142 | | - } |
143 | | - } else { |
144 | | - $netaddr = explode('.', $netaddr); |
145 | | - } |
| 157 | + // Some proxies typically list the whole chain of IP |
| 158 | + // addresses through which the client has reached us. |
| 159 | + // e.g. client_ip, proxy_ip1, proxy_ip2, etc. |
| 160 | + sscanf($spoof, '%[^,]', $spoof); |
146 | 161 |
|
147 | | - // Convert to binary and finally compare |
148 | | - if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0) { |
149 | | - $this->ipAddress = $spoof; |
150 | | - break; |
| 162 | + if (! $ipValidator($spoof)) { |
| 163 | + $spoof = null; |
| 164 | + } else { |
| 165 | + $this->ipAddress = $spoof; |
| 166 | + break; |
| 167 | + } |
151 | 168 | } |
152 | 169 | } |
153 | 170 | } |
|
0 commit comments