Skip to content

Commit d44a805

Browse files
committed
Merge branch '2.3-develop' of github.com:magento/magento2ce into MC-15824
2 parents c5fc160 + 07e57ac commit d44a805

File tree

58 files changed

+702
-301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+702
-301
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Welcome to Magento 2 installation! We're glad you chose to install Magento 2, a cutting-edge, feature-rich eCommerce solution that gets results.
77

88
## Magento System Requirements
9-
[Magento System Requirements](https://devdocs.magento.com/guides/v2.3/install-gde/system-requirements2.html).
9+
[Magento System Requirements](https://devdocs.magento.com/guides/v2.3/install-gde/system-requirements.html).
1010

1111
## Install Magento
1212

app/code/Magento/Backend/Controller/Adminhtml/Dashboard/ProductsViewed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
*/
77
namespace Magento\Backend\Controller\Adminhtml\Dashboard;
88

9-
use Magento\Framework\App\Action\HttpGetActionInterface;
9+
use Magento\Framework\App\Action\HttpPostActionInterface;
1010

1111
/**
1212
* Get most viewed products controller.
1313
*/
14-
class ProductsViewed extends AjaxBlock implements HttpGetActionInterface
14+
class ProductsViewed extends AjaxBlock implements HttpPostActionInterface
1515
{
1616
/**
1717
* Gets most viewed products list

app/code/Magento/ConfigurableProduct/Model/LinkManagement.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
@@ -11,6 +10,11 @@
1110
use Magento\Framework\Exception\NoSuchEntityException;
1211
use Magento\Framework\Exception\StateException;
1312

13+
/**
14+
* Configurable product link management.
15+
*
16+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17+
*/
1418
class LinkManagement implements \Magento\ConfigurableProduct\Api\LinkManagementInterface
1519
{
1620
/**
@@ -68,7 +72,7 @@ public function __construct(
6872
}
6973

7074
/**
71-
* {@inheritdoc}
75+
* @inheritdoc
7276
*/
7377
public function getChildren($sku)
7478
{
@@ -107,11 +111,15 @@ public function getChildren($sku)
107111
}
108112

109113
/**
110-
* {@inheritdoc}
114+
* @inheritdoc
115+
* @throws InputException
116+
* @throws NoSuchEntityException
117+
* @throws StateException
118+
* @throws \Magento\Framework\Exception\CouldNotSaveException
111119
*/
112120
public function addChild($sku, $childSku)
113121
{
114-
$product = $this->productRepository->get($sku);
122+
$product = $this->productRepository->get($sku, true);
115123
$child = $this->productRepository->get($childSku);
116124

117125
$childrenIds = array_values($this->configurableType->getChildrenIds($product->getId())[0]);
@@ -150,7 +158,11 @@ public function addChild($sku, $childSku)
150158
}
151159

152160
/**
153-
* {@inheritdoc}
161+
* @inheritdoc
162+
* @throws InputException
163+
* @throws NoSuchEntityException
164+
* @throws StateException
165+
* @throws \Magento\Framework\Exception\CouldNotSaveException
154166
*/
155167
public function removeChild($sku, $childSku)
156168
{

app/code/Magento/GraphQl/Controller/GraphQl.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\App\RequestInterface;
1313
use Magento\Framework\App\ResponseInterface;
1414
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
15+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1516
use Magento\Framework\GraphQl\Query\QueryProcessor;
1617
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1718
use Magento\Framework\GraphQl\Schema\SchemaGeneratorInterface;
@@ -47,12 +48,12 @@ class GraphQl implements FrontControllerInterface
4748
private $queryProcessor;
4849

4950
/**
50-
* @var \Magento\Framework\GraphQl\Exception\ExceptionFormatter
51+
* @var ExceptionFormatter
5152
*/
5253
private $graphQlError;
5354

5455
/**
55-
* @var \Magento\Framework\GraphQl\Query\Resolver\ContextInterface
56+
* @var ContextInterface
5657
*/
5758
private $resolverContext;
5859

@@ -71,8 +72,8 @@ class GraphQl implements FrontControllerInterface
7172
* @param SchemaGeneratorInterface $schemaGenerator
7273
* @param SerializerInterface $jsonSerializer
7374
* @param QueryProcessor $queryProcessor
74-
* @param \Magento\Framework\GraphQl\Exception\ExceptionFormatter $graphQlError
75-
* @param \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $resolverContext
75+
* @param ExceptionFormatter $graphQlError
76+
* @param ContextInterface $resolverContext
7677
* @param HttpRequestProcessor $requestProcessor
7778
* @param QueryFields $queryFields
7879
*/
@@ -107,12 +108,14 @@ public function dispatch(RequestInterface $request) : ResponseInterface
107108
$statusCode = 200;
108109
try {
109110
/** @var Http $request */
111+
$this->requestProcessor->validateRequest($request);
110112
$this->requestProcessor->processHeaders($request);
111-
$data = $this->jsonSerializer->unserialize($request->getContent());
112113

113-
$query = isset($data['query']) ? $data['query'] : '';
114-
$variables = isset($data['variables']) ? $data['variables'] : null;
115-
// We have to extract queried field names to avoid instantiation of non necessary fields in webonyx schema
114+
$data = $this->getDataFromRequest($request);
115+
$query = $data['query'] ?? '';
116+
$variables = $data['variables'] ?? null;
117+
118+
// We must extract queried field names to avoid instantiation of unnecessary fields in webonyx schema
116119
// Temporal coupling is required for performance optimization
117120
$this->queryFields->setQuery($query, $variables);
118121
$schema = $this->schemaGenerator->generate();
@@ -121,7 +124,7 @@ public function dispatch(RequestInterface $request) : ResponseInterface
121124
$schema,
122125
$query,
123126
$this->resolverContext,
124-
isset($data['variables']) ? $data['variables'] : []
127+
$data['variables'] ?? []
125128
);
126129
} catch (\Exception $error) {
127130
$result['errors'] = isset($result) && isset($result['errors']) ? $result['errors'] : [];
@@ -134,4 +137,26 @@ public function dispatch(RequestInterface $request) : ResponseInterface
134137
)->setHttpResponseCode($statusCode);
135138
return $this->response;
136139
}
140+
141+
/**
142+
* Get data from request body or query string
143+
*
144+
* @param RequestInterface $request
145+
* @return array
146+
*/
147+
private function getDataFromRequest(RequestInterface $request) : array
148+
{
149+
/** @var Http $request */
150+
if ($request->isPost()) {
151+
$data = $this->jsonSerializer->unserialize($request->getContent());
152+
} elseif ($request->isGet()) {
153+
$data = $request->getParams();
154+
$data['variables'] = isset($data['variables']) ?
155+
$this->jsonSerializer->unserialize($data['variables']) : null;
156+
} else {
157+
return [];
158+
}
159+
160+
return $data;
161+
}
137162
}

app/code/Magento/GraphQl/Controller/HttpHeaderProcessor/ContentTypeProcessor.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

app/code/Magento/GraphQl/Controller/HttpHeaderProcessor/StoreProcessor.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Magento\GraphQl\Controller\HttpHeaderProcessor;
99

10-
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Magento\Framework\App\HttpRequestInterface;
1111
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1212
use Magento\GraphQl\Controller\HttpHeaderProcessorInterface;
1313
use Magento\Store\Model\StoreManagerInterface;
@@ -35,8 +35,9 @@ public function __construct(StoreManagerInterface $storeManager)
3535
/**
3636
* Handle the value of the store and set the scope
3737
*
38-
* {@inheritDoc}
39-
* @throws NoSuchEntityException
38+
* @param string $headerValue
39+
* @return void
40+
* @throws GraphQlInputException
4041
*/
4142
public function processHeaderValue(string $headerValue) : void
4243
{

app/code/Magento/GraphQl/Controller/HttpRequestProcessor.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ class HttpRequestProcessor
1919
*/
2020
private $headerProcessors = [];
2121

22+
/**
23+
* @var HttpRequestValidatorInterface[] array
24+
*/
25+
private $requestValidators = [];
26+
2227
/**
2328
* @param HttpHeaderProcessorInterface[] $graphQlHeaders
29+
* @param HttpRequestValidatorInterface[] $requestValidators
2430
*/
25-
public function __construct(array $graphQlHeaders = [])
31+
public function __construct(array $graphQlHeaders = [], array $requestValidators = [])
2632
{
2733
$this->headerProcessors = $graphQlHeaders;
34+
$this->requestValidators = $requestValidators;
2835
}
2936

3037
/**
@@ -39,4 +46,17 @@ public function processHeaders(Http $request) : void
3946
$headerClass->processHeaderValue((string)$request->getHeader($headerName));
4047
}
4148
}
49+
50+
/**
51+
* Validate HTTP request
52+
*
53+
* @param Http $request
54+
* @return void
55+
*/
56+
public function validateRequest(Http $request) : void
57+
{
58+
foreach ($this->requestValidators as $requestValidator) {
59+
$requestValidator->validate($request);
60+
}
61+
}
4262
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Controller\HttpRequestValidator;
9+
10+
use Magento\Framework\App\HttpRequestInterface;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\GraphQl\Controller\HttpRequestValidatorInterface;
13+
14+
/**
15+
* Processes the "Content-Type" header entry
16+
*/
17+
class ContentTypeValidator implements HttpRequestValidatorInterface
18+
{
19+
/**
20+
* Handle the mandatory application/json header
21+
*
22+
* @param HttpRequestInterface $request
23+
* @return void
24+
* @throws GraphQlInputException
25+
*/
26+
public function validate(HttpRequestInterface $request) : void
27+
{
28+
$headerName = 'Content-Type';
29+
$requiredHeaderValue = 'application/json';
30+
31+
$headerValue = (string)$request->getHeader($headerName);
32+
if ($request->isPost()
33+
&& strpos($headerValue, $requiredHeaderValue) === false
34+
) {
35+
throw new GraphQlInputException(
36+
new \Magento\Framework\Phrase('Request content type must be application/json')
37+
);
38+
}
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Controller\HttpRequestValidator;
9+
10+
use Magento\Framework\App\HttpRequestInterface;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\App\Request\Http;
13+
use Magento\GraphQl\Controller\HttpRequestValidatorInterface;
14+
15+
/**
16+
* Validator to check HTTP verb for Graphql requests
17+
*/
18+
class HttpVerbValidator implements HttpRequestValidatorInterface
19+
{
20+
/**
21+
* Check if request is using correct verb for query or mutation
22+
*
23+
* @param HttpRequestInterface $request
24+
* @return void
25+
* @throws GraphQlInputException
26+
*/
27+
public function validate(HttpRequestInterface $request) : void
28+
{
29+
/** @var Http $request */
30+
if (false === $request->isPost()) {
31+
$query = $request->getParam('query', '');
32+
// The easiest way to determine mutations without additional parsing
33+
if (strpos(trim($query), 'mutation') === 0) {
34+
throw new GraphQlInputException(
35+
new \Magento\Framework\Phrase('Mutation requests allowed only for POST requests')
36+
);
37+
}
38+
}
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Controller;
9+
10+
use Magento\Framework\App\HttpRequestInterface;
11+
12+
/**
13+
* Use this interface to implement a validator for a Graphql HTTP requests
14+
*/
15+
interface HttpRequestValidatorInterface
16+
{
17+
/**
18+
* Perform validation of request
19+
*
20+
* @param HttpRequestInterface $request
21+
* @return void
22+
*/
23+
public function validate(HttpRequestInterface $request) : void;
24+
}

0 commit comments

Comments
 (0)