Skip to content

Commit 8160c02

Browse files
committed
Secure cleanVector
1 parent d705812 commit 8160c02

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Html/HtmlBuilder.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,13 @@ public static function cleanVector(string $html): string
560560
$html = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $html);
561561
$html = html_entity_decode($html, ENT_COMPAT, 'UTF-8');
562562

563-
// Remove any attribute starting with "on" or xmlns
564-
$html = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on)[^>]*>#iUu', "$1>", $html);
563+
// Remove any attribute starting with "on" (event handlers)
564+
// Handle quoted attribute values: onload="...", onclick='...'
565+
$html = preg_replace('#\s+on\w+\s*=\s*"[^"]*"#iu', '', $html);
566+
$html = preg_replace('#\s+on\w+\s*=\s*\'[^\']*\'#iu', '', $html);
567+
568+
// Handle unquoted attribute values: onload=alert(1)
569+
$html = preg_replace('#\s+on\w+\s*=\s*[^\s>]+#iu', '', $html);
565570

566571
// Remove javascript: and vbscript: protocols
567572
$html = preg_replace('#([a-z]*)[\x00-\x20\/]*=[\x00-\x20\/]*([\`\'\"]*)[\x00-\x20\/|(&\#\d+;)]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2nojavascript...', $html);

0 commit comments

Comments
 (0)