diff --git a/src/N98/Magento/Command/System/Check/Settings/CookieDomainCheckAbstract.php b/src/N98/Magento/Command/System/Check/Settings/CookieDomainCheckAbstract.php new file mode 100644 index 000000000..d1a0e73cc --- /dev/null +++ b/src/N98/Magento/Command/System/Check/Settings/CookieDomainCheckAbstract.php @@ -0,0 +1,95 @@ + + */ + +namespace N98\Magento\Command\System\Check\Settings; + +use N98\Magento\Command\System\Check\StoreCheck; +use N98\Magento\Command\System\Check\Result; +use N98\Magento\Command\System\Check\ResultCollection; + +abstract class CookieDomainCheckAbstract implements StoreCheck +{ + protected $class = 'abstract'; + + final public function __construct() + { + $whitelist = array('secure' => 1, 'unsecure' => 2); + + if (!isset($whitelist[$this->class])) { + throw new \LogicException( + sprintf('cookie class "%s" (%s::$class) is invalid.', $this->class, get_class($this)) + ); + } + } + + /** + * @param ResultCollection $results + * @param \Mage_Core_Model_Store $store + */ + final public function check(ResultCollection $results, \Mage_Core_Model_Store $store) + { + $result = $results->createResult(); + $errorMessage = 'Cookie Domain and ' . ucfirst($this->class) . ' BaseURL (http) does not match'; + + $baseUrl = \Mage::getStoreConfig('web/' . $this->class . '/base_url', $store); + $cookieDomain = \Mage::getStoreConfig('web/cookie/cookie_domain', $store); + + if (strlen($cookieDomain)) { + $isValid = $this->validateCookieDomainAgainstUrl($cookieDomain, $baseUrl); + + $result->setStatus($isValid ? Result::STATUS_OK : Result::STATUS_ERROR); + + if ($result->isValid()) { + $result->setMessage('Cookie Domain (' . $this->class . ') of Store: ' . $store->getCode() . ' OK'); + } else { + $result->setMessage('Cookie Domain (' . $this->class . ') of Store: ' . $store->getCode() . ' ' . $errorMessage . ''); + } + } else { + $result->setMessage('Cookie Domain (' . $this->class . ') of Store: ' . $store->getCode() . ' OK - No domain set'); + } + } + + /** + * quite rough cookie domain against base-URL validation + * + * follows RFC6265 Domain Matching + * + * @param string $cookieDomain + * @param string $url + * + * @return bool + */ + private function validateCookieDomainAgainstUrl($cookieDomain, $url) + { + $host = strtolower(parse_url($url, PHP_URL_HOST)); + + $hostLen = strlen($host); + if (!$hostLen) { + return false; + } + + $domain = strtolower($cookieDomain); + + // Let cookie-domain be the attribute-value without the leading %x2E (".") character + // see 5.2.3. The Domain Attribute + strlen($domain) && ($domain[0] === '.') && ($domain = substr($domain, 1)); + + $domainLen = strlen($domain); + + if (!$domainLen) { + return false; + } + + return ( + ($host === $domain) + || ( + ($hostLen > $domainLen) + && (substr($host, -$domainLen) === $domain) + && (substr($host, -$domainLen - 1, 1) === '.') + && (ip2long($host) === false) + ) + ); + } +} diff --git a/src/N98/Magento/Command/System/Check/Settings/SecureCookieDomainCheck.php b/src/N98/Magento/Command/System/Check/Settings/SecureCookieDomainCheck.php index d49f0126b..034292e66 100644 --- a/src/N98/Magento/Command/System/Check/Settings/SecureCookieDomainCheck.php +++ b/src/N98/Magento/Command/System/Check/Settings/SecureCookieDomainCheck.php @@ -2,34 +2,7 @@ namespace N98\Magento\Command\System\Check\Settings; -use N98\Magento\Command\System\Check\StoreCheck; -use N98\Magento\Command\System\Check\Result; -use N98\Magento\Command\System\Check\ResultCollection; - -class SecureCookieDomainCheck implements StoreCheck +class SecureCookieDomainCheck extends CookieDomainCheckAbstract { - /** - * @param ResultCollection $results - * @param \Mage_Core_Model_Store $store - */ - public function check(ResultCollection $results, \Mage_Core_Model_Store $store) - { - $result = $results->createResult(); - $errorMessage = 'Cookie Domain and Secure BaseURL (http) does not match'; - - $cookieDomain = \Mage::getStoreConfig('web/cookie/cookie_domain', $store); - - if (!empty($cookieDomain)) { - $isValid = strpos(parse_url($cookieDomain, PHP_URL_HOST), $cookieDomain); - $result->setStatus($isValid ? Result::STATUS_OK : Result::STATUS_ERROR); - - if ($result->isValid()) { - $result->setMessage('Cookie Domain (secure) of Store: ' . $store->getCode() . ' OK'); - } else { - $result->setMessage('Cookie Domain (secure) Store: ' . $store->getCode() . ' ' . $errorMessage . ''); - } - } else { - $result->setMessage('Cookie Domain (secure) of Store: ' . $store->getCode() . ' OK - No domain set'); - } - } + protected $class = 'secure'; } diff --git a/src/N98/Magento/Command/System/Check/Settings/UnsecureCookieDomainCheck.php b/src/N98/Magento/Command/System/Check/Settings/UnsecureCookieDomainCheck.php index 84e57a699..1409343f7 100644 --- a/src/N98/Magento/Command/System/Check/Settings/UnsecureCookieDomainCheck.php +++ b/src/N98/Magento/Command/System/Check/Settings/UnsecureCookieDomainCheck.php @@ -2,34 +2,7 @@ namespace N98\Magento\Command\System\Check\Settings; -use N98\Magento\Command\System\Check\StoreCheck; -use N98\Magento\Command\System\Check\Result; -use N98\Magento\Command\System\Check\ResultCollection; - -class UnsecureCookieDomainCheck implements StoreCheck +class UnsecureCookieDomainCheck extends CookieDomainCheckAbstract { - /** - * @param ResultCollection $results - * @param \Mage_Core_Model_Store $store - */ - public function check(ResultCollection $results, \Mage_Core_Model_Store $store) - { - $result = $results->createResult(); - $errorMessage = 'Cookie Domain and Unsecure BaseURL (http) does not match'; - - $cookieDomain = \Mage::getStoreConfig('web/cookie/cookie_domain', $store); - - if (!empty($cookieDomain)) { - $isValid = strpos(parse_url($cookieDomain, PHP_URL_HOST), $cookieDomain); - $result->setStatus($isValid ? Result::STATUS_OK : Result::STATUS_ERROR); - - if ($result->isValid()) { - $result->setMessage('Cookie Domain (unsecure) of Store: ' . $store->getCode() . ' OK'); - } else { - $result->setMessage('Cookie Domain (unsecure) Store: ' . $store->getCode() . ' ' . $errorMessage . ''); - } - } else { - $result->setMessage('Cookie Domain (unsecure) of Store: ' . $store->getCode() . ' OK - No domain set'); - } - } + protected $class = 'unsecure'; }