diff --git a/app/code/Magento/Customer/Model/Plugin/UpdateCustomer.php b/app/code/Magento/Customer/Model/Plugin/UpdateCustomer.php index c6207896fc55..e8d4bdaec2d2 100644 --- a/app/code/Magento/Customer/Model/Plugin/UpdateCustomer.php +++ b/app/code/Magento/Customer/Model/Plugin/UpdateCustomer.php @@ -52,14 +52,19 @@ public function beforeSave( CustomerInterface $customer, ?string $passwordHash = null ): array { - $customerSessionId = $this->userContext->getUserType() === $this->userContext::USER_TYPE_CUSTOMER ? - (int)$this->userContext->getUserId() : 0; + $userType = $this->userContext->getUserType(); + $customerSessionId = (int)$this->userContext->getUserId(); $customerId = (int)$this->request->getParam('customerId'); $bodyParams = $this->request->getBodyParams(); - if (!isset($bodyParams['customer']['Id']) && $customerId) { - if ($customerId === $customerSessionId || $customerSessionId === 0) { - $customer = $this->getUpdatedCustomer($customerRepository->getById($customerId), $customer); - } + + if ($userType === UserContextInterface::USER_TYPE_CUSTOMER && + !isset($bodyParams['customer']['Id']) && + $customerId && + $customerId === $customerSessionId + ) { + $customer = $this->getUpdatedCustomer($customerRepository->getById($customerId), $customer); + } elseif ($userType === UserContextInterface::USER_TYPE_ADMIN && $customerId) { + $customer = $this->getUpdatedCustomer($customerRepository->getById($customerId), $customer); } return [$customer, $passwordHash]; diff --git a/app/code/Magento/Customer/Plugin/Webapi/Controller/Rest/ValidateCustomerData.php b/app/code/Magento/Customer/Plugin/Webapi/Controller/Rest/ValidateCustomerData.php deleted file mode 100644 index 63551ff5a757..000000000000 --- a/app/code/Magento/Customer/Plugin/Webapi/Controller/Rest/ValidateCustomerData.php +++ /dev/null @@ -1,56 +0,0 @@ -validateInputData($inputData[self::CUSTOMER_KEY]); - } - return [$inputData, $parameters]; - } - - /** - * Validates InputData - * - * @param array $inputData - * @return array - */ - private function validateInputData(array $inputData): array - { - $result = []; - - $data = array_filter($inputData, function ($k) use (&$result) { - $key = is_string($k) ? strtolower(str_replace('_', "", $k)) : $k; - return !isset($result[$key]) && ($result[$key] = true); - }, ARRAY_FILTER_USE_KEY); - - return array_map(function ($value) { - return is_array($value) ? $this->validateInputData($value) : $value; - }, $data); - } -} diff --git a/app/code/Magento/Customer/Test/Unit/Plugin/Webapi/Controller/Rest/ValidateCustomerDataTest.php b/app/code/Magento/Customer/Test/Unit/Plugin/Webapi/Controller/Rest/ValidateCustomerDataTest.php deleted file mode 100644 index 564601e8e448..000000000000 --- a/app/code/Magento/Customer/Test/Unit/Plugin/Webapi/Controller/Rest/ValidateCustomerDataTest.php +++ /dev/null @@ -1,132 +0,0 @@ -createMock(ValidateCustomerData::class) - ] - ]; - $objectManager->prepareObjectManager($objects); - $this->validateCustomerDataObject = ObjectManager::getInstance()->get(ValidateCustomerData::class); - $this->reflectionObject = new ReflectionClass(get_class($this->validateCustomerDataObject)); - } - - /** - * Test if the customer Info is valid - * - * @param array $customerInfo - * @param array $result - * @dataProvider dataProviderInputData - * @throws Exception - */ - public function testValidateInputData(array $customerInfo, array $result) - { - $this->assertEquals( - $result, - $this->invokeValidateInputData('validateInputData', [$customerInfo]) - ); - } - - /** - * @param string $methodName - * @param array $arguments - * @return mixed - * @throws Exception - */ - private function invokeValidateInputData(string $methodName, array $arguments = []) - { - $validateInputDataMethod = $this->reflectionObject->getMethod($methodName); - $validateInputDataMethod->setAccessible(true); - return $validateInputDataMethod->invokeArgs($this->validateCustomerDataObject, $arguments); - } - - /** - * @return array - */ - public static function dataProviderInputData(): array - { - return [ - [ - ['customer' => [ - 'id' => -1, - 'Id' => 1, - 'name' => [ - 'firstName' => 'Test', - 'LastName' => 'user' - ], - 'isHavingOwnHouse' => 1, - 'address' => [ - 'street' => '1st Street', - 'Street' => '3rd Street', - 'city' => 'London' - ], - ] - ], - ['customer' => [ - 'id' => -1, - 'name' => [ - 'firstName' => 'Test', - 'LastName' => 'user' - ], - 'isHavingOwnHouse' => 1, - 'address' => [ - 'street' => '1st Street', - 'city' => 'London' - ], - ] - ], - ['customer' => [ - 'id' => -1, - '_Id' => 1, - 'name' => [ - 'firstName' => 'Test', - 'LastName' => 'user' - ], - 'isHavingOwnHouse' => 1, - 'address' => [ - 'street' => '1st Street', - 'city' => 'London' - ], - ] - ], - ] - ]; - } -} diff --git a/app/code/Magento/Customer/etc/webapi_rest/di.xml b/app/code/Magento/Customer/etc/webapi_rest/di.xml index c5d7a28a3651..18627b68320e 100644 --- a/app/code/Magento/Customer/etc/webapi_rest/di.xml +++ b/app/code/Magento/Customer/etc/webapi_rest/di.xml @@ -31,9 +31,6 @@ - - - diff --git a/app/code/Magento/Quote/Model/BillingAddressManagement.php b/app/code/Magento/Quote/Model/BillingAddressManagement.php index 9ed4f5ecd516..066a1829625f 100644 --- a/app/code/Magento/Quote/Model/BillingAddressManagement.php +++ b/app/code/Magento/Quote/Model/BillingAddressManagement.php @@ -77,10 +77,6 @@ public function assign($cartId, AddressInterface $address, $useForShipping = fal { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); - - // validate the address - $this->addressValidator->validateWithExistingAddress($quote, $address); - $address->setCustomerId($quote->getCustomerId()); $quote->removeAddress($quote->getBillingAddress()->getId()); $quote->setBillingAddress($address); diff --git a/app/code/Magento/Quote/Model/QuoteAddressValidator.php b/app/code/Magento/Quote/Model/QuoteAddressValidator.php index 5ac830d1b78c..dded1c84c3dd 100644 --- a/app/code/Magento/Quote/Model/QuoteAddressValidator.php +++ b/app/code/Magento/Quote/Model/QuoteAddressValidator.php @@ -121,27 +121,6 @@ public function validate(AddressInterface $addressData): bool return true; } - /** - * Validate Quest Address for guest user - * - * @param AddressInterface $address - * @param CartInterface $cart - * @return void - * @throws NoSuchEntityException - */ - private function doValidateForGuestQuoteAddress(AddressInterface $address, CartInterface $cart): void - { - //validate guest cart address - if ($address->getId() !== null) { - $old = $cart->getAddressById($address->getId()); - if ($old === false) { - throw new NoSuchEntityException( - __('Invalid quote address id %1', $address->getId()) - ); - } - } - } - /** * Validate address to be used for cart. * @@ -153,9 +132,6 @@ private function doValidateForGuestQuoteAddress(AddressInterface $address, CartI */ public function validateForCart(CartInterface $cart, AddressInterface $address): void { - if ($cart->getCustomerIsGuest()) { - $this->doValidateForGuestQuoteAddress($address, $cart); - } $this->doValidate($address, !$cart->getCustomer()->getId() ? null : (int) $cart->getCustomer()->getId()); } @@ -171,8 +147,8 @@ public function validateWithExistingAddress(CartInterface $cart, AddressInterfac { // check if address belongs to quote. if ($address->getId() !== null) { - $old = $cart->getAddressesCollection()->getItemById($address->getId()); - if ($old === null) { + $old = $cart->getAddressById($address->getId()); + if (empty($old)) { throw new NoSuchEntityException( __('Invalid quote address id %1', $address->getId()) ); diff --git a/app/code/Magento/Quote/Plugin/QuoteAddress.php b/app/code/Magento/Quote/Plugin/QuoteAddress.php new file mode 100644 index 000000000000..53848b8ee9d6 --- /dev/null +++ b/app/code/Magento/Quote/Plugin/QuoteAddress.php @@ -0,0 +1,67 @@ +addressValidator = $addressValidator; + } + + /** + * Validate address before setting billing address + * + * @param Quote $subject + * @param AddressInterface|null $address + * @return array + * @throws NoSuchEntityException + */ + public function beforeSetBillingAddress(Quote $subject, AddressInterface $address = null): array + { + if ($address !== null) { + $this->addressValidator->validateWithExistingAddress($subject, $address); + } + + return [$address]; + } + + /** + * Validate address before setting shipping address + * + * @param Quote $subject + * @param AddressInterface|null $address + * @return array + * @throws NoSuchEntityException + */ + public function beforeSetShippingAddress(Quote $subject, AddressInterface $address = null): array + { + if ($address !== null) { + $this->addressValidator->validateWithExistingAddress($subject, $address); + } + + return [$address]; + } +} diff --git a/app/code/Magento/Quote/Plugin/ValidateQuoteOrigOrder.php b/app/code/Magento/Quote/Plugin/ValidateQuoteOrigOrder.php new file mode 100644 index 000000000000..00bfda5738b8 --- /dev/null +++ b/app/code/Magento/Quote/Plugin/ValidateQuoteOrigOrder.php @@ -0,0 +1,65 @@ +request = $request; + $this->orderRepository = $orderRepository; + } + + /** + * Validate the user authorization to order + * + * @param CartRepositoryInterface $cartRepository + * @param CartInterface $quote + * @return void + * @throws NoSuchEntityException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function beforeSave( + CartRepositoryInterface $cartRepository, + CartInterface $quote + ): void { + $params = $this->request->getBodyParams(); + if (!empty($params) && isset($params['quote']['orig_order_id'])) { + $orderId = $params['quote']['orig_order_id']; + $order = $this->orderRepository->get($orderId); + $orderCustomer = (int)$order->getCustomerId(); + if ($quote->getCustomerId() !== $orderCustomer) { + throw new NoSuchEntityException(__('Please check input parameters.')); + } + } + } +} diff --git a/app/code/Magento/Quote/Plugin/Webapi/Controller/Rest/ValidateQuoteData.php b/app/code/Magento/Quote/Plugin/Webapi/Controller/Rest/ValidateQuoteData.php deleted file mode 100644 index d27d49571e04..000000000000 --- a/app/code/Magento/Quote/Plugin/Webapi/Controller/Rest/ValidateQuoteData.php +++ /dev/null @@ -1,56 +0,0 @@ -validateInputData($inputData[self:: QUOTE_KEY]); - }; - return [$inputData, $parameters]; - } - - /** - * Validates InputData - * - * @param array $inputData - * @return array - */ - private function validateInputData(array $inputData): array - { - $result = []; - - $data = array_filter($inputData, function ($k) use (&$result) { - $key = is_string($k) ? strtolower($k) : $k; - return !isset($result[$key]) && ($result[$key] = true); - }, ARRAY_FILTER_USE_KEY); - - return array_map(function ($value) { - return is_array($value) ? $this->validateInputData($value) : $value; - }, $data); - } -} diff --git a/app/code/Magento/Quote/Test/Unit/Plugin/Webapi/Controller/Rest/ValidateQuoteDataTest.php b/app/code/Magento/Quote/Test/Unit/Plugin/Webapi/Controller/Rest/ValidateQuoteDataTest.php deleted file mode 100644 index 8ed3713f546b..000000000000 --- a/app/code/Magento/Quote/Test/Unit/Plugin/Webapi/Controller/Rest/ValidateQuoteDataTest.php +++ /dev/null @@ -1,123 +0,0 @@ -createMock(ValidateQuoteData::class) - ] - ]; - $objectManager->prepareObjectManager($objects); - $this->validateQuoteDataObject = ObjectManager::getInstance()->get(ValidateQuoteData::class); - $this->reflectionObject = new ReflectionClass(get_class($this->validateQuoteDataObject)); - } - /** - * Test if the quote array is valid - * - * @param array $array - * @param array $result - * @dataProvider dataProviderInputData - * @throws Exception - */ - public function testValidateInputData(array $array, array $result) - { - $this->assertEquals( - $result, - $this->invokeValidateInputData('validateInputData', [$array]) - ); - } - - /** - * @param string $methodName - * @param array $arguments - * @return mixed - * @throws Exception - */ - private function invokeValidateInputData(string $methodName, array $arguments = []) - { - $validateInputDataMethod = $this->reflectionObject->getMethod($methodName); - $validateInputDataMethod->setAccessible(true); - return $validateInputDataMethod->invokeArgs($this->validateQuoteDataObject, $arguments); - } - - /** - * @return array - */ - public static function dataProviderInputData(): array - { - return [ - [ - ['person' => - [ - 'id' => -1, - 'Id' => 1, - 'name' => - [ - 'firstName' => 'John', - 'LastName' => 'S' - ], - 'isHavingVehicle' => 1, - 'address' => - [ - 'street' => '4th Street', - 'Street' => '2nd Street', - 'city' => 'Atlanta' - ], - ] - ], - ['person' => - [ - 'id' => -1, - 'name' => - [ - 'firstName' => 'John', - 'LastName' => 'S' - ], - 'isHavingVehicle' => 1, - 'address' => - [ - 'street' => '4th Street', - 'city' => 'Atlanta' - ], - ] - ], - ] - ]; - } -} diff --git a/app/code/Magento/Quote/etc/webapi_rest/di.xml b/app/code/Magento/Quote/etc/webapi_rest/di.xml index d5893f7d16d8..a3f481bd4946 100644 --- a/app/code/Magento/Quote/etc/webapi_rest/di.xml +++ b/app/code/Magento/Quote/etc/webapi_rest/di.xml @@ -18,8 +18,9 @@ + - - + + diff --git a/app/code/Magento/Quote/i18n/en_US.csv b/app/code/Magento/Quote/i18n/en_US.csv index 1931709c1b0e..65c12c26fc6d 100644 --- a/app/code/Magento/Quote/i18n/en_US.csv +++ b/app/code/Magento/Quote/i18n/en_US.csv @@ -75,3 +75,4 @@ Carts,Carts "Please select a valid rate limit period in seconds: %1.","Please select a valid rate limit period in seconds: %1." "Identity type not found","Identity type not found" "Invalid order backpressure limit config","Invalid order backpressure limit config" +"Please check input parameters.","Please check input parameters." diff --git a/app/code/Magento/Sales/Helper/Admin.php b/app/code/Magento/Sales/Helper/Admin.php index 1e2e5dfb7966..cc6e62bc710b 100644 --- a/app/code/Magento/Sales/Helper/Admin.php +++ b/app/code/Magento/Sales/Helper/Admin.php @@ -160,84 +160,6 @@ public function applySalableProductTypesFilter($collection) */ public function escapeHtmlWithLinks($data, $allowedTags = null) { - if (!empty($data) && is_array($allowedTags) && in_array('a', $allowedTags)) { - $wrapperElementId = uniqid(); - $domDocument = $this->domDocumentFactory->create(); - - $internalErrors = libxml_use_internal_errors(true); - - $convmap = [0x80, 0x10FFFF, 0, 0x1FFFFF]; - $data = mb_encode_numericentity( - $data, - $convmap, - 'UTF-8' - ); - - $domDocument->loadHTML( - '' . $data . '' - ); - - libxml_use_internal_errors($internalErrors); - - $linkTags = $domDocument->getElementsByTagName('a'); - - foreach ($linkTags as $linkNode) { - $linkAttributes = []; - foreach ($linkNode->attributes as $attribute) { - $linkAttributes[$attribute->name] = $attribute->value; - } - - foreach ($linkAttributes as $attributeName => $attributeValue) { - if ($attributeName === 'href') { - $url = $this->filterUrl($attributeValue ?? ''); - $url = $this->escaper->escapeUrl($url); - $linkNode->setAttribute('href', $url); - } else { - $linkNode->removeAttribute($attributeName); - } - } - } - - $result = mb_decode_numericentity( - // phpcs:ignore Magento2.Functions.DiscouragedFunction - html_entity_decode( - $domDocument->saveHTML(), - ENT_QUOTES|ENT_SUBSTITUTE, - 'UTF-8' - ), - $convmap, - 'UTF-8' - ); - - preg_match('/(.+)<\/body><\/html>$/si', $result, $matches); - $data = !empty($matches) ? $matches[1] : ''; - } - return $this->escaper->escapeHtml($data, $allowedTags); } - - /** - * Filter the URL for allowed protocols. - * - * @param string $url - * @return string - */ - private function filterUrl(string $url): string - { - if ($url) { - //Revert the sprintf escaping - // phpcs:ignore Magento2.Functions.DiscouragedFunction - $urlScheme = parse_url($url, PHP_URL_SCHEME); - $urlScheme = $urlScheme ? strtolower($urlScheme) : ''; - if ($urlScheme !== 'http' && $urlScheme !== 'https') { - $url = null; - } - } - - if (!$url) { - $url = '#'; - } - - return $url; - } } diff --git a/app/code/Magento/Sales/view/adminhtml/templates/order/comments/view.phtml b/app/code/Magento/Sales/view/adminhtml/templates/order/comments/view.phtml index c3a7321a3052..a83202754a90 100644 --- a/app/code/Magento/Sales/view/adminhtml/templates/order/comments/view.phtml +++ b/app/code/Magento/Sales/view/adminhtml/templates/order/comments/view.phtml @@ -5,13 +5,14 @@ */ /** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */ +/** @var \Magento\Framework\Escaper $escaper */ ?> getEntity()): ?>
+ for="history_comment">escapeHtml(__('Comment Text')) ?>