Skip to content
Snippets Groups Projects
Commit 0172357d authored by Ulf Seltmann's avatar Ulf Seltmann Committed by Dorian Merz
Browse files

refs #17816, fixes HTTP_X_FORWARDED_FOR handling

parent 5c3b2368
No related merge requests found
...@@ -45,6 +45,28 @@ namespace finc\Role\PermissionProvider; ...@@ -45,6 +45,28 @@ namespace finc\Role\PermissionProvider;
*/ */
class IpRangeFoFor extends \VuFind\Role\PermissionProvider\IpRange class IpRangeFoFor extends \VuFind\Role\PermissionProvider\IpRange
{ {
/**
* returns remote address based on eventual proxy headers
*
* @return string
*/
private function getRemoteAddr() {
// a list of ips the request is forwarded for - first is latest
$HttpXForwardedForList = explode(',', $this->request->getServer()->get('HTTP_X_FORWARDED_FOR'));
if ($ip = array_shift($HttpXForwardedForList)) {
return $ip;
}
// often provided by nginx-reverse-proxies, should be used since its the nature of the value
if ($ip = $this->request->getServer()->get('HTTP_X_REAL_IP')) {
return $ip;
}
return $this->request->getServer()->get('REMOTE_ADDR');
}
/** /**
* Return an array of roles which may be granted the permission based on * Return an array of roles which may be granted the permission based on
* the options. * the options.
...@@ -57,11 +79,10 @@ class IpRangeFoFor extends \VuFind\Role\PermissionProvider\IpRange ...@@ -57,11 +79,10 @@ class IpRangeFoFor extends \VuFind\Role\PermissionProvider\IpRange
*/ */
public function getPermissions($options) public function getPermissions($options)
{ {
// Check if any regex matches.... // Check if any regex matches....
$ip = $this->request->getServer()->get('HTTP_X_FORWARDED_FOR') != null if ($this->ipAddressUtils->isInRange($this->getRemoteAddr(), (array)$options)) {
? $this->request->getServer()->get('HTTP_X_FORWARDED_FOR')
: $this->request->getServer()->get('REMOTE_ADDR');
if ($this->ipAddressUtils->isInRange($ip, (array)$options)) {
// Match? Grant to all users (guest or logged in). // Match? Grant to all users (guest or logged in).
return ['guest', 'loggedin']; return ['guest', 'loggedin'];
} }
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
* @category VuFind * @category VuFind
* @package Authorization * @package Authorization
* @author Gregor Gawol <gawol@ub.uni-leipzig.de> * @author Gregor Gawol <gawol@ub.uni-leipzig.de>
* @author Ulf Seltmann <ulf.seltmann@hmt-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page * @link https://vufind.org Main Page
*/ */
...@@ -33,11 +34,33 @@ namespace finc\Role\PermissionProvider; ...@@ -33,11 +34,33 @@ namespace finc\Role\PermissionProvider;
* @category VuFind * @category VuFind
* @package Authorization * @package Authorization
* @author Gregor Gawol <gawol@ub.uni-leipzig.de> * @author Gregor Gawol <gawol@ub.uni-leipzig.de>
* @author Ulf Seltmann <ulf.seltmann@hmt-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Page * @link https://vufind.org Main Page
*/ */
class IpRegExFoFor extends \VuFind\Role\PermissionProvider\IpRegEx class IpRegExFoFor extends \VuFind\Role\PermissionProvider\IpRegEx
{ {
/**
* returns remote address based on eventual proxy headers
*
* @return string
*/
private function getRemoteAddr() {
// a list of ips the request is forwarded for - first is latest
$HttpXForwardedForList = explode(',', $this->request->getServer()->get('HTTP_X_FORWARDED_FOR'));
if ($ip = array_shift($HttpXForwardedForList)) {
return $ip;
}
// often provided by nginx-reverse-proxies, should be used since its the nature of the value
if ($ip = $this->request->getServer()->get('HTTP_X_REAL_IP')) {
return $ip;
}
return $this->request->getServer()->get('REMOTE_ADDR');
}
/** /**
* Return an array of roles which may be granted the permission based on * Return an array of roles which may be granted the permission based on
* the options. * the options.
...@@ -51,9 +74,7 @@ class IpRegExFoFor extends \VuFind\Role\PermissionProvider\IpRegEx ...@@ -51,9 +74,7 @@ class IpRegExFoFor extends \VuFind\Role\PermissionProvider\IpRegEx
public function getPermissions($options) public function getPermissions($options)
{ {
// Check if any regex matches.... // Check if any regex matches....
$ip = $this->request->getServer()->get('HTTP_X_FORWARDED_FOR') != null $ip = $this->getRemoteAddr();
? $this->request->getServer()->get('HTTP_X_FORWARDED_FOR')
: $this->request->getServer()->get('REMOTE_ADDR');
foreach ((array)$options as $current) { foreach ((array)$options as $current) {
if (preg_match($current, $ip)) { if (preg_match($current, $ip)) {
// Match? Grant to all users (guest or logged in). // Match? Grant to all users (guest or logged in).
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment