3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\App;
24 use Friendica\Core\Config\Capability\IManageConfigValues;
27 * Container for the whole request
29 * @see https://www.php-fig.org/psr/psr-7/#321-psrhttpmessageserverrequestinterface
31 * @todo future container class for whole requests, currently it's not :-)
35 /** @var string the default possible headers, which could contain the client IP */
36 const ORDERED_FORWARD_FOR_HEADER = 'HTTP_X_FORWARDED_FOR';
38 /** @var string The remote IP address of the current request */
39 protected $remoteAddress;
42 * @return string The remote IP address of the current request
44 * Do always use this instead of $_SERVER['REMOTE_ADDR']
46 public function getRemoteAddress(): string
48 return $this->remoteAddress;
51 public function __construct(IManageConfigValues $config, array $server = [])
53 $this->remoteAddress = $this->determineRemoteAddress($config, $server);
57 * Checks if given $remoteAddress matches given $trustedProxy.
58 * If $trustedProxy is an IPv4 IP range given in CIDR notation, true will be returned if
59 * $remoteAddress is an IPv4 address within that IP range.
60 * Otherwise, $remoteAddress will be compared to $trustedProxy literally and the result
63 * @param string $trustedProxy The current, trusted proxy to check
64 * @param string $remoteAddress The current remote IP address
67 * @return boolean true if $remoteAddress matches $trustedProxy, false otherwise
69 protected function matchesTrustedProxy(string $trustedProxy, string $remoteAddress): bool
71 $cidrre = '/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\/([0-9]{1,2})$/';
73 if (preg_match($cidrre, $trustedProxy, $match)) {
75 $shiftbits = min(32, max(0, 32 - intval($match[2])));
76 $netnum = ip2long($net) >> $shiftbits;
77 $ipnum = ip2long($remoteAddress) >> $shiftbits;
79 return $ipnum === $netnum;
82 return $trustedProxy === $remoteAddress;
86 * Checks if given $remoteAddress matches any entry in the given array $trustedProxies.
87 * For details regarding what "match" means, refer to `matchesTrustedProxy`.
89 * @param string[] $trustedProxies A list of the trusted proxies
90 * @param string $remoteAddress The current remote IP address
92 * @return boolean true if $remoteAddress matches any entry in $trustedProxies, false otherwise
94 protected function isTrustedProxy(array $trustedProxies, string $remoteAddress): bool
96 foreach ($trustedProxies as $tp) {
97 if ($this->matchesTrustedProxy($tp, $remoteAddress)) {
106 * Determines the remote address, if the connection came from a trusted proxy
107 * and `forwarded_for_headers` has been configured then the IP address
108 * specified in this header will be returned instead.
110 * @param IManageConfigValues $config
111 * @param array $server
115 protected function determineRemoteAddress(IManageConfigValues $config, array $server): string
117 $remoteAddress = $server['REMOTE_ADDR'] ?? '0.0.0.0';
118 $trustedProxies = preg_split('/(\s*,*\s*)*,+(\s*,*\s*)*/', $config->get('proxy', 'trusted_proxies', ''));
120 if (\is_array($trustedProxies) && $this->isTrustedProxy($trustedProxies, $remoteAddress)) {
121 $forwardedForHeaders = preg_split('/(\s*,*\s*)*,+(\s*,*\s*)*/', $config->get('proxy', 'forwarded_for_headers')) ?? static::ORDERED_FORWARD_FOR_HEADER;
123 foreach ($forwardedForHeaders as $header) {
124 if (isset($server[$header])) {
125 foreach (explode(',', $server[$header]) as $IP) {
128 // remove brackets from IPv6 addresses
129 if (strpos($IP, '[') === 0 && substr($IP, -1) === ']') {
130 $IP = substr($IP, 1, -1);
133 // skip trusted proxies in the list itself
134 if ($this->isTrustedProxy($trustedProxies, $IP)) {
138 if (filter_var($IP, FILTER_VALIDATE_IP) !== false) {
146 return $remoteAddress;