Skip to content

Commit 24b9d20

Browse files
committed
Merge remote-tracking branch 'falcon/MAGETWO-56317-2' into MAGETWO-59974
2 parents 0c6227d + b2e9ceb commit 24b9d20

File tree

12 files changed

+634
-117
lines changed

12 files changed

+634
-117
lines changed

app/code/Magento/Config/Model/Config/Backend/Baseurl.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55
*/
66
namespace Magento\Config\Model\Config\Backend;
77

8+
use Magento\Framework\Validator\Url as UrlValidator;
9+
use Magento\Framework\App\ObjectManager;
10+
811
class Baseurl extends \Magento\Framework\App\Config\Value
912
{
1013
/**
1114
* @var \Magento\Framework\View\Asset\MergeService
1215
*/
1316
protected $_mergeService;
1417

18+
/**
19+
* @var UrlValidator
20+
*/
21+
private $urlValidator;
22+
1523
/**
1624
* @param \Magento\Framework\Model\Context $context
1725
* @param \Magento\Framework\Registry $registry
@@ -193,8 +201,7 @@ private function _validateFullyQualifiedUrl($value)
193201
*/
194202
private function _isFullyQualifiedUrl($value)
195203
{
196-
$url = parse_url($value);
197-
return isset($url['scheme']) && isset($url['host']) && preg_match('/\/$/', $value);
204+
return preg_match('/\/$/', $value) && $this->getUrlValidator()->isValid($value, ['http', 'https']);
198205
}
199206

200207
/**
@@ -216,4 +223,18 @@ public function afterSave()
216223
}
217224
return parent::afterSave();
218225
}
226+
227+
/**
228+
* Get URL Validator
229+
*
230+
* @deprecated
231+
* @return UrlValidator
232+
*/
233+
private function getUrlValidator()
234+
{
235+
if (!$this->urlValidator) {
236+
$this->urlValidator = ObjectManager::getInstance()->get(UrlValidator::class);
237+
}
238+
return $this->urlValidator;
239+
}
219240
}

dev/tests/integration/testsuite/Magento/Config/Model/Config/Backend/BaseurlTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,33 +95,44 @@ public function validationExceptionDataProvider()
9595
$unsecurePlaceholder = '{{unsecure_base_url}}';
9696
$unsecureSuffix = '{{unsecure_base_url}}test/';
9797
$unsecureWrongSuffix = '{{unsecure_base_url}}test';
98+
$unsecureWrongDomainName = 'http://example.com_test/';
9899
$securePlaceholder = '{{secure_base_url}}';
99100
$secureSuffix = '{{secure_base_url}}test/';
100101
$secureWrongSuffix = '{{secure_base_url}}test';
102+
$secureWrongDomainName = 'https://example.com_test/';
101103

102104
return [
103105
['', 'not a valid URL'],
104106
['', 'example.com'],
105107
['', 'http://example.com'],
106108
['', 'http://example.com/uri'],
109+
['', $unsecureWrongDomainName],
107110
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL, ''],
108111
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL, $baseSuffix],
109112
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL, $unsecureSuffix],
110113
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL, $unsecurePlaceholder],
114+
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_URL, $unsecureWrongDomainName],
111115
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_LINK_URL, ''],
112116
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_LINK_URL, $baseSuffix],
113117
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_LINK_URL, $unsecureWrongSuffix],
118+
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_LINK_URL, $unsecureWrongDomainName],
114119
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_MEDIA_URL, $unsecureWrongSuffix],
120+
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_MEDIA_URL, $unsecureWrongDomainName],
115121
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_STATIC_URL, $unsecureWrongSuffix],
122+
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_STATIC_URL, $unsecureWrongDomainName],
116123
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL, ''],
117124
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL, $baseSuffix],
118125
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL, $secureSuffix],
119126
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL, $securePlaceholder],
127+
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL, $secureWrongDomainName],
120128
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_LINK_URL, ''],
121129
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_LINK_URL, $baseSuffix],
122130
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_LINK_URL, $secureWrongSuffix],
131+
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_LINK_URL, $secureWrongDomainName],
123132
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_MEDIA_URL, $secureWrongSuffix],
133+
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_MEDIA_URL, $secureWrongDomainName],
124134
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_STATIC_URL, $secureWrongSuffix],
135+
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_STATIC_URL, $secureWrongDomainName],
125136
];
126137
}
127138
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Setup\Controller;
7+
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
use Zend\Stdlib\RequestInterface as Request;
10+
use Zend\View\Model\JsonModel;
11+
12+
class UrlCheckTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var UrlCheck
16+
*/
17+
private $controller;
18+
19+
protected function setUp()
20+
{
21+
$this->controller = Bootstrap::getObjectManager()->create(UrlCheck::class);
22+
}
23+
24+
/**
25+
* @param array $requestContent
26+
* @param bool $successUrl
27+
* @param bool $successSecureUrl
28+
* @return void
29+
* @dataProvider indexActionDataProvider
30+
*/
31+
public function testIndexAction($requestContent, $successUrl, $successSecureUrl)
32+
{
33+
$requestMock = $this->getMockBuilder(Request::class)
34+
->getMockForAbstractClass();
35+
$requestMock->expects($this->once())
36+
->method('getContent')
37+
->willReturn(json_encode($requestContent));
38+
39+
$requestProperty = new \ReflectionProperty(get_class($this->controller), 'request');
40+
$requestProperty->setAccessible(true);
41+
$requestProperty->setValue($this->controller, $requestMock);
42+
43+
$resultModel = new JsonModel(['successUrl' => $successUrl, 'successSecureUrl' => $successSecureUrl]);
44+
45+
$this->assertEquals($resultModel, $this->controller->indexAction());
46+
}
47+
48+
/**
49+
* @return array
50+
*/
51+
public function indexActionDataProvider()
52+
{
53+
return [
54+
[
55+
'requestContent' => [
56+
'address' => [
57+
'actual_base_url' => 'http://example.com/'
58+
],
59+
'https' => [
60+
'text' => 'https://example.com/',
61+
'admin' => true,
62+
'front' => false
63+
],
64+
],
65+
'successUrl' => true,
66+
'successSecureUrl' => true
67+
],
68+
[
69+
'requestContent' => [
70+
'address' => [
71+
'actual_base_url' => 'http://example.com/folder/'
72+
],
73+
'https' => [
74+
'text' => 'https://example.com/folder_name/',
75+
'admin' => false,
76+
'front' => true
77+
],
78+
],
79+
'successUrl' => true,
80+
'successSecureUrl' => true
81+
],
82+
[
83+
'requestContent' => [
84+
'address' => [
85+
'actual_base_url' => 'ftp://example.com/'
86+
],
87+
'https' => [
88+
'text' => 'https://example.com_test/',
89+
'admin' => true,
90+
'front' => true
91+
],
92+
],
93+
'successUrl' => false,
94+
'successSecureUrl' => false
95+
],
96+
];
97+
}
98+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Validator\Test\Unit;
7+
8+
use Magento\Framework\Validator\Url as UrlValidator;
9+
10+
class UrlTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @var UrlValidator
14+
*/
15+
private $validator;
16+
17+
protected function setUp()
18+
{
19+
$this->validator = new UrlValidator();
20+
}
21+
22+
/**
23+
* @param array $allowedSchemes
24+
* @param string $url
25+
* @param bool $expectedResult
26+
* @dataProvider isValidDataProvider
27+
*/
28+
public function testIsValid(array $allowedSchemes, $url, $expectedResult)
29+
{
30+
$this->assertSame($expectedResult, $this->validator->isValid($url, $allowedSchemes));
31+
}
32+
33+
/**
34+
* @return array
35+
*/
36+
public function isValidDataProvider()
37+
{
38+
return [
39+
[
40+
'allowedSchemes' => [],
41+
'url' => 'http://example.com',
42+
'expectedResult' => true,
43+
],
44+
[
45+
'allowedSchemes' => ['http'],
46+
'url' => 'http://example.com',
47+
'expectedResult' => true,
48+
],
49+
[
50+
'allowedSchemes' => [],
51+
'url' => 'https://example.com',
52+
'expectedResult' => true,
53+
],
54+
[
55+
'allowedSchemes' => ['https'],
56+
'url' => 'https://example.com',
57+
'expectedResult' => true,
58+
],
59+
[
60+
'allowedSchemes' => [],
61+
'url' => 'http://example.com_test',
62+
'expectedResult' => false,
63+
],
64+
[
65+
'allowedSchemes' => [],
66+
'url' => 'ftp://example.com',
67+
'expectedResult' => true,
68+
],
69+
[
70+
'allowedSchemes' => ['ftp'],
71+
'url' => 'ftp://example.com',
72+
'expectedResult' => true,
73+
],
74+
];
75+
}
76+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Validator;
7+
8+
/**
9+
* Class Url validates URL and checks that it has allowed scheme
10+
*/
11+
class Url
12+
{
13+
/**
14+
* Validate URL and check that it has allowed scheme
15+
*
16+
* @param string $value
17+
* @param array $allowedSchemes
18+
* @return bool
19+
*/
20+
public function isValid($value, array $allowedSchemes = [])
21+
{
22+
$isValid = true;
23+
24+
if (!filter_var($value, FILTER_VALIDATE_URL)) {
25+
$isValid = false;
26+
}
27+
28+
if ($isValid && !empty($allowedSchemes)) {
29+
$url = parse_url($value);
30+
if (empty($url['scheme']) || !in_array($url['scheme'], $allowedSchemes)) {
31+
$isValid = false;
32+
}
33+
}
34+
35+
return $isValid;
36+
}
37+
}

setup/config/di.config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
\Magento\Setup\Controller\Environment::class,
2222
\Magento\Setup\Controller\DependencyCheck::class,
2323
\Magento\Setup\Controller\DatabaseCheck::class,
24+
\Magento\Setup\Controller\UrlCheck::class,
2425
\Magento\Setup\Controller\ValidateAdminCredentials::class,
2526
\Magento\Setup\Controller\AddDatabase::class,
2627
\Magento\Setup\Controller\WebConfiguration::class,

setup/pub/magento/setup/web-configuration.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict';
77
angular.module('web-configuration', ['ngStorage'])
8-
.controller('webConfigurationController', ['$scope', '$state', '$localStorage', function ($scope, $state, $localStorage) {
8+
.controller('webConfigurationController', ['$scope', '$state', '$localStorage', '$http', function ($scope, $state, $localStorage, $http) {
99
$scope.config = {
1010
address: {
1111
base_url: '',
@@ -119,4 +119,28 @@ angular.module('web-configuration', ['ngStorage'])
119119
$scope.webconfig.submitted = false;
120120
}
121121
});
122+
123+
// Validate URL
124+
$scope.validateUrl = function () {
125+
if (!$scope.webconfig.submitted) {
126+
$http.post('index.php/url-check', $scope.config)
127+
.success(function (data) {
128+
$scope.validateUrl.result = data;
129+
if ($scope.validateUrl.result.successUrl && $scope.validateUrl.result.successSecureUrl) {
130+
$scope.nextState();
131+
}
132+
if (!$scope.validateUrl.result.successUrl) {
133+
$scope.webconfig.submitted = true;
134+
$scope.webconfig.base_url.$setValidity('url', false);
135+
}
136+
if (!$scope.validateUrl.result.successSecureUrl) {
137+
$scope.webconfig.submitted = true;
138+
$scope.webconfig.https.$setValidity('url', false);
139+
}
140+
})
141+
.error(function (data) {
142+
$scope.validateUrl.failed = data;
143+
});
144+
}
145+
};
122146
}]);

0 commit comments

Comments
 (0)