-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Cache ipCheck (2.8) #22819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache ipCheck (2.8) #22819
Conversation
@@ -213,6 +213,11 @@ class Request | |||
protected $defaultLocale = 'en'; | |||
|
|||
/** | |||
* @var boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be bool
, see fabbot.io patch
return false; | ||
} | ||
|
||
if (!isset($this->validTrustedProxyIP)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible that self::$trustedProxies
is changed between method calls. Make sure to reset the cached result in setTrustedProxies
and that this is tested.
shouldnt this fix be submitted against the 2.8 branch? |
I stumbled upon the same issue. Shouldn't the cache be stored statically in WDYT? |
Indeed, a cache in |
@gonzalovilaseca do you want to do it? I can if you don't have time right now. |
$request = new Request(array(), array(), array(), array(), array(), array('REMOTE_ADDR' => '8.8.8.8')); | ||
Request::setTrustedProxies(array('8.8.8.8'), Request::HEADER_FORWARDED); | ||
|
||
$this->assertEquals(true, $request->isFromTrustedProxy()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->assertTrue($this->isFromTrustedProxy());
Also seen below in testIsFromTrustedProxyCacheIsInValidatedOnSetTrustedProxies()
.
@tgalopin Sure, I'll do it, will be ready by tuesday |
|
||
Request::setTrustedProxies(array('5.5.5.5'), Request::HEADER_FORWARDED); | ||
|
||
$this->assertEquals(false, $request->isFromTrustedProxy()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertFalse()
public function testIsFromTrustedProxyReturnsFalseIfNoTrustedProxies() | ||
{ | ||
$request = new Request(); | ||
$this->assertEquals(false, $request->isFromTrustedProxy()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertFalse()
} | ||
|
||
$remoteAddr = $this->server->get('REMOTE_ADDR'); | ||
if (!array_key_exists($remoteAddr, self::$validTrustedProxyIP) || null === self::$validTrustedProxyIP[$remoteAddr]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be simply: !isset(self::$validTrustedProxyIP[$remoteAddr])
cause you do check for null
anyway.
I've updated the PR moving the cache to Feel free to improve the naming of the new methods, naming is not my strength, even less with jet lag :-). |
@@ -19,6 +19,11 @@ | |||
class IpUtils | |||
{ | |||
/** | |||
* @var array | |||
*/ | |||
protected static $checkedIps = array(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be private
@@ -61,6 +66,60 @@ public static function checkIp($requestIp, $ips) | |||
*/ | |||
public static function checkIp4($requestIp, $ip) | |||
{ | |||
return self::checkIpInternal($requestIp, $ip, 'self::checkIp4Internal'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not delegate to another method but just copy/paste the cache logic in the methods directly.
@@ -61,6 +66,50 @@ public static function checkIp($requestIp, $ips) | |||
*/ | |||
public static function checkIp4($requestIp, $ip) | |||
{ | |||
$cacheKey = $requestIp.'-'.$ip; | |||
if (!array_key_exists($cacheKey, self::$checkedIps)) { | |||
self::$checkedIps[$cacheKey] = self::checkIp4Internal($requestIp, $ip); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would inline the checkIp4Internal method as well.
@@ -19,6 +19,11 @@ | |||
class IpUtils | |||
{ | |||
/** | |||
* @var array | |||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
phpdoc should be removed
return false; | ||
} | ||
$cacheKey = $requestIp.'-'.$ip; | ||
if (!array_key_exists($cacheKey, self::$checkedIps)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would return early:
if (array_key_exists($cacheKey, self::$checkedIps)) {
return self::$checkedIps[$cacheKey];
}
I would even change arra_key_exists()
to isset()
@@ -61,26 +63,31 @@ public static function checkIp($requestIp, $ips) | |||
*/ | |||
public static function checkIp4($requestIp, $ip) | |||
{ | |||
$cacheKey = $requestIp.'-'.$ip; | |||
if (isset($cacheKey, self::$checkedIps)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks wrong to me
@gonzalovilaseca Can you change the branch of the PR to 2.7? |
@fabpot 2.7 or 2.8? (nicolas mentioned 2.8) |
I would say 2.7. |
@fabpot |
That would be wonderful, thanks. |
2.7 version here: #23098 |
In our app we use trusted proxies. Using Blackfire we found
IpUtils::checkIp
was being called 454 times taking 3.15ms.Caching the result saves those 3ms.