From 1e65fc29edf0d4f10effb88bd77f79e718dfcb6d Mon Sep 17 00:00:00 2001 From: Kolyunya Date: Sat, 22 Apr 2017 19:42:52 +0300 Subject: [PATCH] Fixes #14033: Fixed `yii\filters\AccessRule::matchIp()` erroring in case IP is not defined under HHVM --- framework/CHANGELOG.md | 1 + framework/filters/AccessRule.php | 11 +++++++++-- tests/framework/filters/AccessRuleTest.php | 8 ++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 2d35d49b9c..03d6b0c4c4 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.12 under development -------------------------- +- Bug #14033: Fixed `yii\filters\AccessRule::matchIp()` erroring in case IP is not defined under HHVM (Kolyunya) - Bug #13848: `yii\di\Instance::ensure()` wasn't throwing an exception when `$type` is specified and `$reference` object isn't instance of `$type` (c-jonua) - Enh #13226: `yii cache` command now warns about the fact that it's not able to flush APC cache from console (samdark) - Bug #13689: Fixed handling of errors in closures (mikehaertl) diff --git a/framework/filters/AccessRule.php b/framework/filters/AccessRule.php index 4eacf57942..d827e6330c 100644 --- a/framework/filters/AccessRule.php +++ b/framework/filters/AccessRule.php @@ -165,7 +165,7 @@ class AccessRule extends Component } /** - * @param string $ip the IP address + * @param string|null $ip the IP address * @return bool whether the rule applies to the IP address */ protected function matchIP($ip) @@ -174,7 +174,14 @@ class AccessRule extends Component return true; } foreach ($this->ips as $rule) { - if ($rule === '*' || $rule === $ip || (($pos = strpos($rule, '*')) !== false && !strncmp($ip, $rule, $pos))) { + if ($rule === '*' || + $rule === $ip || + ( + $ip !== null && + ($pos = strpos($rule, '*')) !== false && + strncmp($ip, $rule, $pos) === 0 + ) + ) { return true; } } diff --git a/tests/framework/filters/AccessRuleTest.php b/tests/framework/filters/AccessRuleTest.php index 57e67ac17a..b9a56803cf 100644 --- a/tests/framework/filters/AccessRuleTest.php +++ b/tests/framework/filters/AccessRuleTest.php @@ -292,6 +292,14 @@ class AccessRuleTest extends \yiiunit\TestCase $this->assertNull($rule->allows($action, $user, $request)); $rule->allow = false; $this->assertNull($rule->allows($action, $user, $request)); + + // undefined IP + $_SERVER['REMOTE_ADDR'] = null; + $rule->ips = ['192.168.*']; + $rule->allow = true; + $this->assertNull($rule->allows($action, $user, $request)); + $rule->allow = false; + $this->assertNull($rule->allows($action, $user, $request)); } public function testMatchIPWildcard()