Skip to content

Commit 6d5b0e5

Browse files
In admin, url params may get url encoded more than once, fix backend url model
1 parent 3e4259a commit 6d5b0e5

File tree

5 files changed

+48
-46
lines changed

5 files changed

+48
-46
lines changed

app/code/Magento/Backend/Model/Url.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66
namespace Magento\Backend\Model;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\Serialize\Serializer\Json;
910
use Magento\Framework\Url\HostChecker;
10-
use Magento\Framework\App\ObjectManager;
1111

1212
/**
1313
* Class \Magento\Backend\Model\UrlInterface
@@ -179,10 +179,10 @@ protected function _isSecure()
179179
protected function _setRouteParams(array $data, $unsetOldParams = true)
180180
{
181181
if (isset($data['_nosecret'])) {
182-
$this->setNoSecret(true);
182+
$this->turnOffSecretKey();
183183
unset($data['_nosecret']);
184184
} else {
185-
$this->setNoSecret(false);
185+
$this->turnOnSecretKey();
186186
}
187187
unset($data['_scope_to_url']);
188188
return parent::_setRouteParams($data, $unsetOldParams);
@@ -206,29 +206,33 @@ public function getUrl($routePath = null, $routeParams = null)
206206
unset($routeParams['_cache_secret_key']);
207207
$cacheSecretKey = true;
208208
}
209-
$result = parent::getUrl($routePath, $routeParams);
210-
if (!$this->useSecretKey()) {
211-
return $result;
212-
}
209+
210+
$this->_setRouteParams([]);
213211
$this->_setRoutePath($routePath);
214212
$routeName = $this->_getRouteName('*');
215213
$controllerName = $this->_getControllerName(self::DEFAULT_CONTROLLER_NAME);
216214
$actionName = $this->_getActionName(self::DEFAULT_ACTION_NAME);
217-
if ($cacheSecretKey) {
218-
$secret = [self::SECRET_KEY_PARAM_NAME => "\${$routeName}/{$controllerName}/{$actionName}\$"];
219-
} else {
220-
$secret = [
221-
self::SECRET_KEY_PARAM_NAME => $this->getSecretKey($routeName, $controllerName, $actionName),
222-
];
223-
}
224-
if (is_array($routeParams)) {
225-
$routeParams = array_merge($secret, $routeParams);
226-
} else {
227-
$routeParams = $secret;
215+
216+
if ($this->useSecretKey()) {
217+
if ($cacheSecretKey) {
218+
$secret = [self::SECRET_KEY_PARAM_NAME => "\${$routeName}/{$controllerName}/{$actionName}\$"];
219+
} else {
220+
$secret = [
221+
self::SECRET_KEY_PARAM_NAME => $this->getSecretKey($routeName, $controllerName, $actionName),
222+
];
223+
}
224+
if (is_array($routeParams)) {
225+
$routeParams = array_merge($routeParams, $secret);
226+
} else {
227+
$routeParams = $secret;
228+
}
228229
}
229-
if (is_array($this->_getRouteParams())) {
230-
$routeParams = array_merge($this->_getRouteParams(), $routeParams);
230+
231+
$routePathParams = $this->_getRouteParams();
232+
if (is_array($routePathParams)) {
233+
$routeParams = array_merge($routePathParams, $routeParams);
231234
}
235+
232236
return parent::getUrl("{$routeName}/{$controllerName}/{$actionName}", $routeParams);
233237
}
234238

dev/tests/integration/framework/Magento/TestFramework/TestCase/AbstractBackendController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
*/
1313
abstract class AbstractBackendController extends \Magento\TestFramework\TestCase\AbstractController
1414
{
15+
/**
16+
* @var \Magento\Backend\Model\UrlInterface
17+
*/
18+
protected $_urlBuilder;
19+
1520
/**
1621
* @var \Magento\Backend\Model\Auth\Session
1722
*/
@@ -40,8 +45,7 @@ protected function setUp()
4045
{
4146
parent::setUp();
4247

43-
$this->_objectManager->get(\Magento\Backend\Model\UrlInterface::class)->turnOffSecretKey();
44-
48+
$this->_urlBuilder = $this->_objectManager->get(\Magento\Backend\Model\UrlInterface::class)->turnOffSecretKey();
4549
$this->_auth = $this->_objectManager->get(\Magento\Backend\Model\Auth::class);
4650
$this->_session = $this->_auth->getAuthStorage();
4751
$credentials = $this->_getAdminCredentials();

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Action/AttributeTest.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,24 @@ class AttributeTest extends \Magento\TestFramework\TestCase\AbstractBackendContr
1717
*/
1818
public function testSaveActionRedirectsSuccessfully()
1919
{
20-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
21-
2220
/** @var $session \Magento\Backend\Model\Session */
23-
$session = $objectManager->get(\Magento\Backend\Model\Session::class);
21+
$session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
2422
$session->setProductIds([1]);
2523

2624
$this->dispatch('backend/catalog/product_action_attribute/save/store/0');
2725

2826
$this->assertEquals(302, $this->getResponse()->getHttpResponseCode());
29-
/** @var \Magento\Backend\Model\UrlInterface $urlBuilder */
30-
$urlBuilder = $objectManager->get(\Magento\Framework\UrlInterface::class);
3127

3228
/** @var \Magento\Catalog\Helper\Product\Edit\Action\Attribute $attributeHelper */
33-
$attributeHelper = $objectManager->get(\Magento\Catalog\Helper\Product\Edit\Action\Attribute::class);
34-
$expectedUrl = $urlBuilder->getUrl(
29+
$attributeHelper = $this->_objectManager->get(\Magento\Catalog\Helper\Product\Edit\Action\Attribute::class);
30+
31+
$this->_urlBuilder->turnOffSecretKey();
32+
$expectedUrl = $this->_urlBuilder->getUrl(
3533
'catalog/product/index',
3634
['store' => $attributeHelper->getSelectedStoreId()]
3735
);
38-
$isRedirectPresent = false;
39-
foreach ($this->getResponse()->getHeaders() as $header) {
40-
if ($header->getFieldName() === 'Location' && strpos($header->getFieldValue(), $expectedUrl) === 0) {
41-
$isRedirectPresent = true;
42-
}
43-
}
44-
45-
$this->assertTrue($isRedirectPresent);
36+
37+
$this->assertRedirect($this->stringStartsWith($expectedUrl));
4638
}
4739

4840
/**

dev/tests/integration/testsuite/Magento/Customer/Controller/Adminhtml/IndexTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ public function testSaveActionExistingCustomerAndExistingAddressData()
287287
];
288288
$this->getRequest()->setPostValue($post);
289289
$this->getRequest()->setParam('id', 1);
290+
$this->_urlBuilder->turnOffSecretKey();
290291
$this->dispatch('backend/customer/index/save');
291292

292293
/** Check that success message is set */
@@ -324,7 +325,8 @@ public function testSaveActionExistingCustomerAndExistingAddressData()
324325
$subscriber->loadByCustomerId($customerId);
325326
$this->assertNotEmpty($subscriber->getId());
326327
$this->assertEquals(1, $subscriber->getStatus());
327-
$this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'index/key/'));
328+
329+
$this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'index/'));
328330
}
329331

330332
/**
@@ -353,6 +355,7 @@ public function testSaveActionExistingCustomerUnsubscribeNewsletter()
353355
];
354356
$this->getRequest()->setPostValue($post);
355357
$this->getRequest()->setParam('id', 1);
358+
$this->_urlBuilder->turnOffSecretKey();
356359
$this->dispatch('backend/customer/index/save');
357360

358361
/** @var \Magento\Newsletter\Model\Subscriber $subscriber */
@@ -370,7 +373,7 @@ public function testSaveActionExistingCustomerUnsubscribeNewsletter()
370373
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
371374
);
372375

373-
$this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'index/key/'));
376+
$this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'index/'));
374377
}
375378

376379
/**
@@ -410,13 +413,14 @@ public function testSaveActionExistingCustomerChangeEmail()
410413
];
411414
$this->getRequest()->setPostValue($post);
412415
$this->getRequest()->setParam('id', 1);
416+
$this->_urlBuilder->turnOffSecretKey();
413417
$this->dispatch('backend/customer/index/save');
414418

415419
/**
416420
* Check that no errors were generated and set to session
417421
*/
418422
$this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
419-
$this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'index/key/'));
423+
$this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'index/'));
420424
}
421425

422426
/**
@@ -479,6 +483,7 @@ public function testSaveActionCoreException()
479483
],
480484
];
481485
$this->getRequest()->setPostValue($post);
486+
$this->_urlBuilder->turnOffSecretKey();
482487
$this->dispatch('backend/customer/index/save');
483488
/*
484489
* Check that error message is set
@@ -491,7 +496,7 @@ public function testSaveActionCoreException()
491496
$post,
492497
Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session::class)->getCustomerFormData()
493498
);
494-
$this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'new/key/'));
499+
$this->assertRedirect($this->stringStartsWith($this->_baseControllerUrl . 'new/'));
495500
}
496501

497502
/**

dev/tests/integration/testsuite/Magento/Newsletter/Controller/Adminhtml/NewsletterTemplateTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,8 @@ public function testSaveActionTemplateWithGetAndVerifyRedirect()
146146
/**
147147
* Check that correct redirect performed.
148148
*/
149-
$backendUrlModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
150-
\Magento\Backend\Model\UrlInterface::class
151-
);
152-
$backendUrlModel->turnOffSecretKey();
153-
$url = $backendUrlModel->getUrl('newsletter');
149+
$this->_urlBuilder->turnOffSecretKey();
150+
$url = $this->_urlBuilder->getUrl('*/template');
154151
$this->assertRedirect($this->stringStartsWith($url));
155152
}
156153
}

0 commit comments

Comments
 (0)