55namespace PhpList \Core \Domain \Messaging \Service \Manager ;
66
77use Doctrine \ORM \EntityManagerInterface ;
8+ use HTMLPurifier ;
89use PhpList \Core \Domain \Messaging \Model \Message ;
910use PhpList \Core \Domain \Messaging \Model \MessageData ;
1011use PhpList \Core \Domain \Messaging \Repository \MessageDataRepository ;
@@ -14,6 +15,7 @@ class MessageDataManager
1415 public function __construct (
1516 private readonly MessageDataRepository $ messageDataRepository ,
1617 private readonly EntityManagerInterface $ entityManager ,
18+ private readonly HTMLPurifier $ purifier ,
1719 ) {
1820 }
1921
@@ -68,30 +70,11 @@ public function setMessageData(Message $campaign, string $name, mixed $value): v
6870 $ entity ->setData ($ value !== null ? (string ) $ value : null );
6971 }
7072
71- /**
72- * Remove potentially harmful JavaScript from HTML content.
73- *
74- * This is a conservative cleaner: removes <script> blocks, javascript: URLs,
75- * and inline event handlers (on*) attributes.
76- */
77- private function disableJavascript (string $ html ): string
78- {
79- // Remove script tags and their content
80- $ clean = preg_replace ('#<script\b[^>]*>.*?</script>#is ' , '' , $ html ) ?? $ html ;
81-
82- // Remove on*="..." event handler attributes
83- $ clean = preg_replace ('/\s+on[a-zA-Z]+\s*=\s*("[^"]*"| \'[^ \']* \'|[^\s>]+)/i ' , '' , $ clean ) ?? $ clean ;
84-
85- // Neutralize javascript: and data: URIs in href/src/style
86- $ clean = preg_replace ('/\b(href|src)\s*=\s*("| \')\s*(javascript:|data:)[^\2]*\2/i ' , '$1="#" ' , $ clean ) ?? $ clean ;
87- return preg_replace ('/\bstyle\s*=\s*("| \')[^\1]*\1/i ' , '' , $ clean ) ?? $ clean ;
88- }
89-
9073 private function normalizeValueByName (string $ name , mixed $ value )
9174 {
9275 return match ($ name ) {
9376 'subject ' , 'campaigntitle ' => is_string ($ value ) ? strip_tags ($ value ) : $ value ,
94- 'message ' => is_string ($ value ) ? $ this ->disableJavascript ($ value ) : $ value ,
77+ 'message ' => is_string ($ value ) ? $ this ->purifier -> purify ($ value ) : $ value ,
9578 'excludelist ' => is_array ($ value ) ? array_filter ($ value , fn ($ val ) => is_numeric ($ val )) : $ value ,
9679 'footer ' => is_string ($ value ) ? preg_replace ('/<!--.*?-->/ ' , '' , $ value ) : $ value ,
9780 default => $ value ,
0 commit comments