Skip to content

Commit ba4bf64

Browse files
committed
Merge remote-tracking branch 'origin/AC-12730' into spartans_pr_10122025
2 parents b319969 + 73d668b commit ba4bf64

File tree

5 files changed

+1217
-2
lines changed

5 files changed

+1217
-2
lines changed

app/code/Magento/Wishlist/Controller/Index/Send.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex implements Action\
9494
*/
9595
private $captchaStringResolver;
9696

97+
/**
98+
* @var \Magento\Wishlist\Model\Validator\MessageValidator
99+
*/
100+
private $messageValidator;
101+
97102
/**
98103
* @param Action\Context $context
99104
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
@@ -109,6 +114,7 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex implements Action\
109114
* @param CaptchaHelper|null $captchaHelper
110115
* @param CaptchaStringResolver|null $captchaStringResolver
111116
* @param Escaper|null $escaper
117+
* @param \Magento\Wishlist\Model\Validator\MessageValidator|null $messageValidator
112118
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
113119
*/
114120
public function __construct(
@@ -125,7 +131,8 @@ public function __construct(
125131
StoreManagerInterface $storeManager,
126132
?CaptchaHelper $captchaHelper = null,
127133
?CaptchaStringResolver $captchaStringResolver = null,
128-
?Escaper $escaper = null
134+
?Escaper $escaper = null,
135+
?\Magento\Wishlist\Model\Validator\MessageValidator $messageValidator = null
129136
) {
130137
$this->_formKeyValidator = $formKeyValidator;
131138
$this->_customerSession = $customerSession;
@@ -144,6 +151,9 @@ public function __construct(
144151
$this->escaper = $escaper ?? ObjectManager::getInstance()->get(
145152
Escaper::class
146153
);
154+
$this->messageValidator = $messageValidator ?? ObjectManager::getInstance()->get(
155+
\Magento\Wishlist\Model\Validator\MessageValidator::class
156+
);
147157
parent::__construct($context);
148158
}
149159

@@ -193,6 +203,11 @@ public function execute()
193203

194204
$error = false;
195205
$message = (string)$this->getRequest()->getPost('message');
206+
207+
if (!$this->messageValidator->isValid($message)) {
208+
$error = __('Invalid content detected in message. Please remove any special codes or scripts.');
209+
}
210+
196211
if (strlen($message) > $textLimit) {
197212
$error = __('Message length must not exceed %1 symbols', $textLimit);
198213
} else {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Wishlist\Model\Validator;
9+
10+
use Magento\Framework\Validator\AbstractValidator;
11+
12+
/**
13+
* Wishlist message validator with strict content rules.
14+
*/
15+
class MessageValidator extends AbstractValidator
16+
{
17+
/**
18+
* Patterns that indicate template injection or code execution attempts.
19+
*/
20+
private const FORBIDDEN_PATTERNS = [
21+
// Template directives
22+
'/\{\{.*?\}\}/s',
23+
'/\{%.*?%\}/s',
24+
25+
// Server-side code execution
26+
'/<\?/i',
27+
28+
// Template filter/processor access (Magento-specific)
29+
'/\bthis\s*\.\s*get\w+/i',
30+
'/TemplateFilter|FilterCallback/i',
31+
];
32+
33+
/**
34+
* Validates the message against allowed patterns.
35+
*
36+
* @param mixed $value
37+
* @return bool
38+
*/
39+
public function isValid($value): bool
40+
{
41+
if (!is_string($value) || trim($value) === '') {
42+
return true;
43+
}
44+
45+
// Decode URL encoding to catch obfuscation
46+
$decoded = urldecode($value);
47+
48+
// Remove newlines/carriage returns that might be used for obfuscation
49+
$normalized = preg_replace('/[\r\n\t]+/', ' ', $decoded);
50+
51+
// Check for suspicious patterns in both decoded and normalized versions
52+
foreach (self::FORBIDDEN_PATTERNS as $pattern) {
53+
if (preg_match($pattern, $decoded) || preg_match($pattern, $normalized)) {
54+
$this->_addMessages([
55+
'Invalid content detected in message. Code and system commands are not allowed.'
56+
]);
57+
return false;
58+
}
59+
}
60+
61+
return true;
62+
}
63+
}

0 commit comments

Comments
 (0)