Skip to content

Commit eee9050

Browse files
author
Joan He
committed
Merge remote-tracking branch 'arcticfoxes/MAGETWO-98345' into 2.1.18-develop-pr
2 parents c98e421 + 5c3f269 commit eee9050

File tree

13 files changed

+637
-119
lines changed

13 files changed

+637
-119
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/functional/tests/app/Magento/Install/Test/Block/WebConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class WebConfiguration extends Form
2121
*
2222
* @var string
2323
*/
24-
protected $next = "[ng-click*='next']";
24+
protected $next = "[ng-click*='validateUrl']";
2525

2626
/**
2727
* 'Advanced Options' locator.

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
@@ -98,30 +98,41 @@ public function validationExceptionDataProvider()
9898
$securePlaceholder = '{{secure_base_url}}';
9999
$secureSuffix = '{{secure_base_url}}test/';
100100
$secureWrongSuffix = '{{secure_base_url}}test';
101+
$unsecureWrongDomainName = 'http://example.com_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],
114118
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_MEDIA_URL, $unsecureWrongSuffix],
115119
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_STATIC_URL, $unsecureWrongSuffix],
120+
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_LINK_URL, $unsecureWrongDomainName],
121+
[\Magento\Store\Model\Store::XML_PATH_UNSECURE_BASE_MEDIA_URL, $unsecureWrongDomainName],
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],
123131
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_MEDIA_URL, $secureWrongSuffix],
124132
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_STATIC_URL, $secureWrongSuffix],
133+
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_LINK_URL, $secureWrongDomainName],
134+
[\Magento\Store\Model\Store::XML_PATH_SECURE_BASE_MEDIA_URL, $secureWrongDomainName],
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 © Magento, Inc. 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 $requestContent
26+
* @param $successUrl
27+
* @param $successSecureUrl
28+
* @dataProvider indexActionDataProvider
29+
* @throws \ReflectionException
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 © Magento, Inc. 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 © Magento, Inc. 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',
2222
'Magento\Setup\Controller\DependencyCheck',
2323
'Magento\Setup\Controller\DatabaseCheck',
24+
'Magento\Setup\Controller\UrlCheck',
2425
'Magento\Setup\Controller\ValidateAdminCredentials',
2526
'Magento\Setup\Controller\AddDatabase',
2627
'Magento\Setup\Controller\WebConfiguration',

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

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

66
'use strict';
77
angular.module('web-configuration', ['ngStorage'])
8-
.controller('webConfigurationController', ['$scope', '$state', '$localStorage', function ($scope, $state, $localStorage) {
8+
.controller(
9+
'webConfigurationController',
10+
['$scope', '$state', '$localStorage', '$http', function ($scope, $state, $localStorage, $http) {
911
$scope.config = {
1012
address: {
1113
base_url: '',
@@ -119,4 +121,30 @@ angular.module('web-configuration', ['ngStorage'])
119121
$scope.webconfig.submitted = false;
120122
}
121123
});
124+
125+
// Validate URL
126+
$scope.validateUrl = function () {
127+
if (!$scope.webconfig.submitted) {
128+
$http.post('index.php/url-check', $scope.config)
129+
.then(function successCallback(resp) {
130+
$scope.validateUrl.result = resp.data;
131+
132+
if ($scope.validateUrl.result.successUrl && $scope.validateUrl.result.successSecureUrl) {
133+
$scope.nextState();
134+
}
135+
136+
if (!$scope.validateUrl.result.successUrl) {
137+
$scope.webconfig.submitted = true;
138+
$scope.webconfig.base_url.$setValidity('url', false);
139+
}
140+
141+
if (!$scope.validateUrl.result.successSecureUrl) {
142+
$scope.webconfig.submitted = true;
143+
$scope.webconfig.https.$setValidity('url', false);
144+
}
145+
}, function errorCallback(resp) {
146+
$scope.validateUrl.failed = resp.data;
147+
});
148+
}
149+
};
122150
}]);

0 commit comments

Comments
 (0)