Security.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdminNotification\Model\System\Message;
  7. use Magento\Store\Model\Store;
  8. /**
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class Security implements \Magento\Framework\Notification\MessageInterface
  13. {
  14. /**
  15. * Cache key for saving verification result
  16. */
  17. const VERIFICATION_RESULT_CACHE_KEY = 'configuration_files_access_level_verification';
  18. /**
  19. * File path for verification
  20. *
  21. * @var string
  22. */
  23. private $_filePath = 'app/etc/config.php';
  24. /**
  25. * Time out for HTTP verification request
  26. *
  27. * @var int
  28. */
  29. private $_verificationTimeOut = 2;
  30. /**
  31. * @var \Magento\Framework\App\CacheInterface
  32. */
  33. protected $_cache;
  34. /**
  35. * @var \Magento\Backend\App\ConfigInterface
  36. */
  37. protected $_backendConfig;
  38. /**
  39. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  40. */
  41. protected $_config;
  42. /**
  43. * @var \Magento\Framework\HTTP\Adapter\CurlFactory
  44. */
  45. protected $_curlFactory;
  46. /**
  47. * @param \Magento\Framework\App\CacheInterface $cache
  48. * @param \Magento\Backend\App\ConfigInterface $backendConfig
  49. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  50. * @param \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory
  51. */
  52. public function __construct(
  53. \Magento\Framework\App\CacheInterface $cache,
  54. \Magento\Backend\App\ConfigInterface $backendConfig,
  55. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  56. \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory
  57. ) {
  58. $this->_cache = $cache;
  59. $this->_backendConfig = $backendConfig;
  60. $this->_config = $config;
  61. $this->_curlFactory = $curlFactory;
  62. }
  63. /**
  64. * Check verification result and return true if system must to show notification message
  65. *
  66. * @return bool
  67. */
  68. private function _canShowNotification()
  69. {
  70. if ($this->_cache->load(self::VERIFICATION_RESULT_CACHE_KEY)) {
  71. return false;
  72. }
  73. if ($this->_isFileAccessible()) {
  74. return true;
  75. }
  76. $adminSessionLifetime = (int)$this->_backendConfig->getValue('admin/security/session_lifetime');
  77. $this->_cache->save(true, self::VERIFICATION_RESULT_CACHE_KEY, [], $adminSessionLifetime);
  78. return false;
  79. }
  80. /**
  81. * If file is accessible return true or false
  82. *
  83. * @return bool
  84. */
  85. private function _isFileAccessible()
  86. {
  87. $unsecureBaseURL = $this->_config->getValue(Store::XML_PATH_UNSECURE_BASE_URL, 'default');
  88. /** @var $http \Magento\Framework\HTTP\Adapter\Curl */
  89. $http = $this->_curlFactory->create();
  90. $http->setConfig(['timeout' => $this->_verificationTimeOut]);
  91. $http->write(\Zend_Http_Client::POST, $unsecureBaseURL . $this->_filePath);
  92. $responseBody = $http->read();
  93. $responseCode = \Zend_Http_Response::extractCode($responseBody);
  94. $http->close();
  95. return $responseCode == 200;
  96. }
  97. /**
  98. * Retrieve unique message identity
  99. *
  100. * @return string
  101. */
  102. public function getIdentity()
  103. {
  104. return 'security';
  105. }
  106. /**
  107. * Check whether
  108. *
  109. * @return bool
  110. */
  111. public function isDisplayed()
  112. {
  113. return $this->_canShowNotification();
  114. }
  115. /**
  116. * Retrieve message text
  117. *
  118. * @return \Magento\Framework\Phrase
  119. */
  120. public function getText()
  121. {
  122. return __(
  123. 'Your web server is set up incorrectly and allows unauthorized access to sensitive files. '
  124. . 'Please contact your hosting provider.'
  125. );
  126. }
  127. /**
  128. * Retrieve message severity
  129. *
  130. * @return int
  131. */
  132. public function getSeverity()
  133. {
  134. return \Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL;
  135. }
  136. }