Skip to content
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

New feature: enhance security with custom admin URL. #4264

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/code/core/Mage/Adminhtml/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Mage_Adminhtml_Helper_Data extends Mage_Adminhtml_Helper_Help_Mapping
{
public const XML_PATH_ADMINHTML_ROUTER_FRONTNAME = 'admin/routers/adminhtml/args/frontName';
public const XML_PATH_USE_CUSTOM_ADMIN_URL = 'default/admin/url/use_custom';
public const XML_PATH_CUSTOM_ADMIN_URL = 'default/admin/url/custom';
public const XML_PATH_USE_CUSTOM_ADMIN_PATH = 'default/admin/url/use_custom_path';
public const XML_PATH_CUSTOM_ADMIN_PATH = 'default/admin/url/custom_path';
public const XML_PATH_ADMINHTML_SECURITY_USE_FORM_KEY = 'admin/security/use_form_key';
Expand Down Expand Up @@ -85,6 +86,21 @@ public static function getUrl($route = '', $params = [])
return Mage::getModel('adminhtml/url')->getUrl($route, $params);
}

/**
* @return string|false
*/
public static function getCustomAdminUrl()
{
$config = Mage::getConfig();
if ($config->getNode(self::XML_PATH_USE_CUSTOM_ADMIN_URL)
&& $config->getNode(self::XML_PATH_CUSTOM_ADMIN_URL)
) {
return (string) $config->getNode(self::XML_PATH_CUSTOM_ADMIN_URL);
}

return false;
}

/**
* @return false|int
*/
Expand Down
16 changes: 16 additions & 0 deletions app/code/core/Mage/Core/Controller/Varien/Router/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,20 @@ protected function _validateControllerInstance($controllerInstance)
{
return true;
}

/**
* Check if URL host matches custom admin URL.
*
* @inheritDoc
*/
public function match(Zend_Controller_Request_Http $request)
{
if ($adminUrl = Mage_Adminhtml_Helper_Data::getCustomAdminUrl()) {
if (!str_contains($adminUrl, $request->getHttpHost())) {
return false;
}
}

return parent::match($request);
}
}
10 changes: 9 additions & 1 deletion app/code/core/Mage/Core/Model/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,15 @@ public function getBaseUrl($type = self::URL_TYPE_LINK, $secure = null)
$url = str_replace('{{base_url}}', $baseUrl, $url);
}

$this->_baseUrlCache[$cacheKey] = rtrim($url, '/') . '/';
$url = rtrim($url, '/') . '/';
$adminUrl = $this->isAdmin() ? Mage_Adminhtml_Helper_Data::getCustomAdminUrl() : false;
if ($adminUrl) {
$adminUrl = rtrim($adminUrl, '/') . '/';
$baseUrl = str_starts_with($url, 'https://') ? $this->getConfig(self::XML_PATH_SECURE_BASE_URL) : $this->getConfig(self::XML_PATH_UNSECURE_BASE_URL);
$url = str_replace($baseUrl, $adminUrl, $url);
}

$this->_baseUrlCache[$cacheKey] = $url;
}

return $this->_baseUrlCache[$cacheKey];
Expand Down
4 changes: 1 addition & 3 deletions errors/processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,7 @@ protected function _validate(): bool
*/
protected function _setSkin(string $value, ?stdClass $config = null)
{
if (preg_match('/^[a-z0-9_]+$/i', $value)
&& is_dir($this->_indexDir . self::ERROR_DIR . '/' . $value)
) {
if (preg_match('/^[a-z0-9_]+$/i', $value) && is_dir($this->_errorDir . $value)) {
if (!$config && $this->_config) {
$config = $this->_config;
}
Expand Down