Skip to content

Commit 50c9f36

Browse files
committed
ACP2E-3373: [Cloud] Admin Panel exposed to the public - Custom Admin URL not effective
1 parent 628163b commit 50c9f36

File tree

2 files changed

+16
-34
lines changed

2 files changed

+16
-34
lines changed

app/code/Magento/Backend/App/Area/FrontNameResolver.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,18 @@ public function isHostBackend()
137137
}
138138
}
139139
$this->uri->parse($backendUrl);
140-
if (!$this->uri->getHost()) {
140+
$configuredHost = $this->uri->getHost();
141+
if (!$configuredHost) {
141142
return false;
142143
}
143144

144-
$configuredPort = $this->uri->getPort() ?: ($this->standardPorts[$this->uri->getScheme()] ?? '80');
145-
$configuredHost = $this->uri->getHost() . ':' . $configuredPort;
146-
$host = $this->request->getServer('HTTP_HOST');
147-
if (!str_contains($host, ':')) {
148-
$host .= ':' . ($this->standardPorts[$this->request->getServer('REQUEST_SCHEME')] ?? '80');
145+
$configuredPort = $this->uri->getPort() ?: ($this->standardPorts[$this->uri->getScheme()] ?? null);
146+
$uri = ($this->request->isSecure() ? 'https' : 'http') . '://' . $this->request->getServer('HTTP_HOST');
147+
$this->uri->parse($uri);
148+
$host = $this->uri->getHost();
149+
if ($configuredPort) {
150+
$configuredHost .= ':' . $configuredPort;
151+
$host .= ':' . ($this->uri->getPort() ?: $this->standardPorts[$this->uri->getScheme()]);
149152
}
150153

151154
return strcasecmp($configuredHost, $host) === 0;

app/code/Magento/Backend/Test/Unit/App/Area/FrontNameResolverTest.php

+7-28
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function setUp(): void
6262
->method('get')
6363
->with(ConfigOptionsList::CONFIG_PATH_BACKEND_FRONTNAME)
6464
->willReturn($this->_defaultFrontName);
65-
$this->uri = $this->createMock(Uri::class);
65+
$this->uri = $this->createPartialMock(Uri::class, ['parse']);
6666
$this->request = $this->createMock(Http::class);
6767
$this->configMock = $this->createMock(Config::class);
6868
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
@@ -149,34 +149,16 @@ public function testIsHostBackend(
149149
->willReturnMap(
150150
[
151151
['HTTP_HOST', null, $host],
152-
['REQUEST_SCHEME', null, $isHttps ? 'https' : 'http'],
153152
]
154153
);
154+
$this->request->method('isSecure')
155+
->willReturn($isHttps);
155156

156-
$urlParts = [];
157157
$this->uri->method('parse')
158158
->willReturnCallback(
159-
function ($url) use (&$urlParts) {
160-
$urlParts = parse_url($url);
161-
}
162-
);
163-
$this->uri->method('getScheme')
164-
->willReturnCallback(
165-
function () use (&$urlParts) {
166-
return array_key_exists('scheme', $urlParts) ? $urlParts['scheme'] : '';
167-
}
168-
);
169-
$this->uri->method('getHost')
170-
->willReturnCallback(
171-
function () use (&$urlParts) {
172-
return array_key_exists('host', $urlParts) ? $urlParts['host'] : '';
173-
}
174-
);
175-
$this->uri->method('getPort')
176-
->willReturnCallback(
177-
function () use (&$urlParts) {
178-
return array_key_exists('port', $urlParts) ? $urlParts['port'] : '';
179-
}
159+
fn ($url) => $this->uri->setScheme(parse_url($url, PHP_URL_SCHEME))
160+
->setHost(parse_url($url, PHP_URL_HOST))
161+
->setPort(parse_url($url, PHP_URL_PORT))
180162
);
181163

182164
$this->assertEquals($expectedValue, $this->model->isHostBackend());
@@ -192,11 +174,8 @@ public function testIsHostBackendWithEmptyHost(): void
192174
$this->request->expects($this->any())
193175
->method('getServer')
194176
->willReturn('magento2.loc');
195-
$this->uri->expects($this->once())
196-
->method('getHost')
197-
->willReturn(null);
198177

199-
$this->assertEquals($this->model->isHostBackend(), false);
178+
$this->assertFalse($this->model->isHostBackend());
200179
}
201180

202181
/**

0 commit comments

Comments
 (0)