From 8493b59c2b8d7afabd3aa598bf79fc8930f6ad53 Mon Sep 17 00:00:00 2001 From: Gabriel Jenik Date: Thu, 12 Nov 2020 05:01:18 -0300 Subject: [PATCH] New feature #12560: enable video in spite of active xss filtering (#1589) Dev Added new configuration to the HtmlPurifier. Dev Extended the standard HtmlPurifier so it exposes the config in a public method. --- application/core/LSYii_HtmlPurifier.php | 94 +++++++++++++++++++++++++ application/core/LSYii_Validators.php | 20 +----- 2 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 application/core/LSYii_HtmlPurifier.php diff --git a/application/core/LSYii_HtmlPurifier.php b/application/core/LSYii_HtmlPurifier.php new file mode 100644 index 00000000000..794d0a52bfe --- /dev/null +++ b/application/core/LSYii_HtmlPurifier.php @@ -0,0 +1,94 @@ +getPurifier(); + if($purifier!==null) return $purifier->config; + } + + /** + * Get an instance of LSYii_HtmlPurifier configured for XSS filtering + */ + public static function getXssPurifier() { + $instance = new self(); + $instance->options = array( + 'AutoFormat.RemoveEmpty'=>false, + 'Core.NormalizeNewlines'=>false, + 'CSS.AllowTricky'=>true, // Allow display:none; (and other) + 'HTML.SafeObject'=>true, // To allow including youtube + 'Output.FlashCompat'=>true, + 'Attr.EnableID'=>true, // Allow to set id + 'Attr.AllowedFrameTargets'=>array('_blank', '_self'), + 'URI.AllowedSchemes'=>array( + 'http' => true, + 'https' => true, + 'mailto' => true, + 'ftp' => true, + 'nntp' => true, + 'news' => true, + ) + ); + // To allow script BUT purify : HTML.Trusted=true (plugin idea for admin or without XSS filtering ?) + + // Enable video + $config = $instance->getConfig(); + if (!empty($config)) { + $config->set('HTML.DefinitionID', 'html5-definitions'); + $def = $config->maybeGetRawHTMLDefinition(); + $max = $config->get('HTML.MaxImgLength'); + if ($def) { + $def->addElement( + 'video', // name + 'Inline', // content set + 'Flow', // allowed children + 'Common', // attribute collection + array( // attributes + 'src' => 'URI', + 'id' => 'Text', + 'poster' => 'Text', + 'width' => 'Pixels#' . $max, + 'height' => 'Pixels#' . $max, + 'controls' => 'Bool#controls', + 'autobuffer' => 'Bool#autobuffer', + 'autoplay' => 'Bool#autoplay', + 'loop' => 'Bool#loop', + 'muted' => 'Bool#muted' + ) + ); + $def->addElement( + 'source', // name + 'Inline', // content set + 'Empty', // allowed children + null, // attribute collection + array( // attributes + 'src*' => 'URI', + 'type' => 'Enum#video/mp4,video/webm', + ) + ); + } + } + + return $instance; + } +} diff --git a/application/core/LSYii_Validators.php b/application/core/LSYii_Validators.php index cd96a5d6c55..436de4eaa76 100644 --- a/application/core/LSYii_Validators.php +++ b/application/core/LSYii_Validators.php @@ -108,25 +108,7 @@ public function fixCKeditor($value) */ public function xssFilter($value) { - $filter = new CHtmlPurifier(); - $filter->options = array( - 'AutoFormat.RemoveEmpty'=>false, - 'Core.NormalizeNewlines'=>false, - 'CSS.AllowTricky'=>true, // Allow display:none; (and other) - 'HTML.SafeObject'=>true, // To allow including youtube - 'Output.FlashCompat'=>true, - 'Attr.EnableID'=>true, // Allow to set id - 'Attr.AllowedFrameTargets'=>array('_blank', '_self'), - 'URI.AllowedSchemes'=>array( - 'http' => true, - 'https' => true, - 'mailto' => true, - 'ftp' => true, - 'nntp' => true, - 'news' => true, - ) - ); - // To allow script BUT purify : HTML.Trusted=true (plugin idea for admin or without XSS filtering ?) + $filter = LSYii_HtmlPurifier::getXssPurifier(); /** Start to get complete filtered value with url decode {QCODE} (bug #09300). This allow only question number in url, seems OK with XSS protection **/ $sFiltered = preg_replace('#%7B([a-zA-Z0-9\.]*)%7D#', '{$1}', $filter->purify($value));