@@ -709,6 +709,22 @@ const CombodoGlobalToolbox = {
709709 || oDOMElem . contains ( efp ( oRect . left , oRect . bottom ) )
710710 ) ;
711711 }
712+ } ,
713+ /**
714+ * This method should be a JS mirror of the PHP {@see utils::FilterXSS} method
715+ *
716+ * @param sInput {string} Input text to filter from XSS attacks
717+ * @returns {string } The sInput string filtered from possible XSS attacks
718+ * @constructor
719+ * @since 3.0.0
720+ */
721+ FilterXSS : function ( sInput ) {
722+ let sOutput = sInput ;
723+
724+ // Remove HTML script tags
725+ sOutput = sOutput . replace ( / < s c r i p t / g, '<script WARNING: scripts are not allowed in tooltips' ) ;
726+
727+ return sOutput ;
712728 }
713729} ;
714730
@@ -731,9 +747,7 @@ const CombodoTooltip = {
731747 * @constructor
732748 */
733749 InitTooltipFromMarkup : function ( oElem , bForce = false ) {
734- const oOptions = {
735- allowHTML : true , // Always true so line breaks can work. Don't worry content will be sanitized.
736- } ;
750+ const oOptions = { } ;
737751
738752 // First, check if the tooltip isn't already instantiated
739753 if ( ( oElem . attr ( 'data-tooltip-instantiated' ) === 'true' ) && ( bForce === false ) ) {
@@ -746,24 +760,18 @@ const CombodoTooltip = {
746760 // Content must be reworked before getting into the tooltip
747761 // - Should we enable HTML content or keep text as is
748762 const bEnableHTML = oElem . attr ( 'data-tooltip-html-enabled' ) === 'true' ;
763+ oOptions [ 'allowHTML' ] = bEnableHTML ;
749764
750765 // - Content should be sanitized unless the developer says otherwise
751766 // Note: Condition is inversed on purpose. When the developer is instantiating a tooltip,
752- // we want him/her to explicitly declare that he/she wants the sanitizer to be skipped.
767+ // we want they to explicitly declare that they want the sanitizer to be skipped.
753768 // Whereas in this code, it's easier to follow the logic with the variable oriented this way.
754769 const bSanitizeContent = oElem . attr ( 'data-tooltip-sanitizer-skipped' ) !== 'true' ;
755770
756- // - Sanitize content and make sure line breaks are kept
757- const oTmpContentElem = $ ( '<div />' ) . html ( oElem . attr ( 'data-tooltip-content' ) ) ;
758- let sContent = '' ;
759- if ( bEnableHTML ) {
760- sContent = oTmpContentElem . html ( ) ;
761- if ( bSanitizeContent ) {
762- sContent = sContent . replace ( / < s c r i p t / g, '<script WARNING: scripts are not allowed in tooltips' ) ;
763- }
764- } else {
765- sContent = oTmpContentElem . text ( ) ;
766- sContent = sContent . replace ( / ( \r \n | \n \r | \r | \n ) / g, '<br/>' ) ;
771+ let sContent = oElem . attr ( 'data-tooltip-content' ) ;
772+ // - Check if both HTML and sanitizer are enabled
773+ if ( bEnableHTML && bSanitizeContent ) {
774+ sContent = CombodoGlobalToolbox . FilterXSS ( sContent ) ;
767775 }
768776 oOptions [ 'content' ] = sContent ;
769777
0 commit comments