From c11dc8e17effd30d047b0101b19742c9bd34fc28 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Fri, 6 Mar 2020 22:18:38 +0100 Subject: [PATCH] reverting Helper.urls2links() method to old style, applied to element instead of string, allows inserting plain text as text node --- js/privatebin.js | 48 ++++++++++++++++------------------------------- js/test/Helper.js | 43 ++++++++++++++++++++++++++++++++---------- tpl/bootstrap.php | 2 +- tpl/page.php | 2 +- 4 files changed, 51 insertions(+), 44 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index d2b85448..73bdd924 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -281,7 +281,7 @@ jQuery.PrivateBin = (function($, RawDeflate) { }; /** - * convert URLs to clickable links. + * convert URLs to clickable links in the provided element. * * URLs to handle: *
@@ -292,14 +292,15 @@ jQuery.PrivateBin = (function($, RawDeflate) {
          *
          * @name   Helper.urls2links
          * @function
-         * @param  {string} html
-         * @return {string}
+         * @param  {HTMLElement} element
          */
-        me.urls2links = function(html)
+        me.urls2links = function(element)
         {
-            return html.replace(
-                /(((https?|ftp):\/\/[\w?!=&.\/-;#@~%+*-]+(?![\w\s?!&.\/;#~%"=-]*>))|((magnet):[\w?=&.\/-;#@~%+*-]+))/ig,
-                '$1'
+            element.html(
+                element.html().replace(
+                    /(((https?|ftp):\/\/[\w?!=&.\/-;#@~%+*-]+(?![\w\s?!&.\/;#~%"=-]*>))|((magnet):[\w?=&.\/-;#@~%+*-]+))/ig,
+                    '$1'
+                )
             );
         };
 
@@ -2439,11 +2440,6 @@ jQuery.PrivateBin = (function($, RawDeflate) {
                 // add table classes from bootstrap css
                 $plainText.find('table').addClass('table-condensed table-bordered');
             } else {
-                // escape HTML entities, link URLs, sanitize
-                const escapedLinkedText = Helper.urls2links(text);
-                let sanitizeLinkedText = '',
-                    sanitizerConfiguration = {};
-
                 if (format === 'syntaxhighlighting') {
                     // yes, this is really needed to initialize the environment
                     if (typeof prettyPrint === 'function')
@@ -2451,22 +2447,16 @@ jQuery.PrivateBin = (function($, RawDeflate) {
                         prettyPrint();
                     }
 
-                    sanitizeLinkedText = prettyPrintOne(
-                        escapedLinkedText, null, true
+                    $prettyPrint.html(
+                        prettyPrintOne(
+                            Helper.htmlEntities(text), null, true
+                        )
                     );
                 } else {
                     // = 'plaintext'
-                    sanitizeLinkedText = escapedLinkedText;
-                    sanitizerConfiguration = {
-                        ALLOWED_TAGS: ['a'],
-                        ALLOWED_ATTR: ['href', 'rel']
-                    };
+                    $prettyPrint.text(text);
                 }
-                $prettyPrint.html(
-                    DOMPurify.sanitize(
-                        sanitizeLinkedText, sanitizerConfiguration
-                    )
-                );
+                Helper.urls2links($prettyPrint);
                 $prettyPrint.css('white-space', 'pre-wrap');
                 $prettyPrint.css('word-break', 'normal');
                 $prettyPrint.removeClass('prettyprint');
@@ -3243,14 +3233,8 @@ jQuery.PrivateBin = (function($, RawDeflate) {
             const $commentEntryData = $commentEntry.find('div.commentdata');
 
             // set & parse text
-            $commentEntryData.html(
-                DOMPurify.sanitize(
-                    Helper.urls2links(commentText), {
-                        ALLOWED_TAGS: ['a'],
-                        ALLOWED_ATTR: ['href', 'rel']
-                    }
-                )
-            );
+            $commentEntryData.text(commentText);
+            Helper.urls2links($commentEntryData);
 
             // set nickname
             if (nickname.length > 0) {
diff --git a/js/test/Helper.js b/js/test/Helper.js
index dd38e3c4..ab7638e9 100644
--- a/js/test/Helper.js
+++ b/js/test/Helper.js
@@ -81,7 +81,15 @@ describe('Helper', function () {
             'ignores non-URL content',
             'string',
             function (content) {
-                return content === $.PrivateBin.Helper.urls2links(content);
+                content = content.replace("\r", "\n").replace("\u0000", '');
+                let clean = jsdom();
+                $('body').html('
'); + let e = $('#foo'); + e.text(content); + $.PrivateBin.Helper.urls2links(e); + let result = e.text(); + clean(); + return content === result; } ); jsc.property( @@ -95,9 +103,12 @@ describe('Helper', function () { function (prefix, schema, address, query, fragment, postfix) { query = query.join(''); fragment = fragment.join(''); - prefix = $.PrivateBin.Helper.htmlEntities(prefix); - postfix = ' ' + $.PrivateBin.Helper.htmlEntities(postfix); - let url = schema + '://' + address.join('') + '/?' + query + '#' + fragment; + prefix = prefix.replace("\r", "\n").replace("\u0000", ''); + postfix = ' ' + postfix.replace("\r", "\n").replace("\u0000", ''); + let url = schema + '://' + address.join('') + '/?' + query + '#' + fragment, + clean = jsdom(); + $('body').html('
'); + let e = $('#foo'); // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. � or &#x if ( @@ -108,8 +119,12 @@ describe('Helper', function () { url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1); postfix = ''; } - - return prefix + '' + url + '' + postfix === $.PrivateBin.Helper.urls2links(prefix + url + postfix); + e.text(prefix + url + postfix); + $.PrivateBin.Helper.urls2links(e); + let result = e.html(); + clean(); + url = $('
').text(url).html(); + return $('
').text(prefix).html() + '' + url + '' + $('
').text(postfix).html() === result; } ); jsc.property( @@ -118,10 +133,18 @@ describe('Helper', function () { jsc.array(common.jscQueryString()), 'string', function (prefix, query, postfix) { - prefix = $.PrivateBin.Helper.htmlEntities(prefix); - postfix = $.PrivateBin.Helper.htmlEntities(postfix); - let url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm,''); - return prefix + '' + url + ' ' + postfix === $.PrivateBin.Helper.urls2links(prefix + url + ' ' + postfix); + prefix = prefix.replace("\r", "\n").replace("\u0000", ''); + postfix = ' ' + postfix.replace("\r", "\n").replace("\u0000", ''); + let url = 'magnet:?' + query.join('').replace(/^&+|&+$/gm,''), + clean = jsdom(); + $('body').html('
'); + let e = $('#foo'); + e.text(prefix + url + postfix); + $.PrivateBin.Helper.urls2links(e); + let result = e.html(); + clean(); + url = $('
').text(url).html(); + return $('
').text(prefix).html() + '' + url + '' + $('
').text(postfix).html() === result; } ); }); diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index 843dc6a7..b511a48e 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -72,7 +72,7 @@ endif; ?> - + diff --git a/tpl/page.php b/tpl/page.php index c976ce2f..f9b55840 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -50,7 +50,7 @@ endif; ?> - +