diff --git a/js/privatebin.js b/js/privatebin.js index 139ecc73..c6b98b9b 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -375,7 +375,7 @@ jQuery.PrivateBin = (function($, RawDeflate) { }; /** - * convert URLs to clickable links. + * convert URLs to clickable links in the provided element. * * URLs to handle: *
@@ -386,14 +386,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' + ) ); }; @@ -2504,36 +2505,24 @@ jQuery.PrivateBin = (function($, RawDeflate) { return; } - // escape HTML entities, link URLs, sanitize - const escapedLinkedText = Helper.urls2links(text), - sanitizedLinkedText = DOMPurify.sanitize( - escapedLinkedText, { - ALLOWED_TAGS: ['a'], - ALLOWED_ATTR: ['href', 'rel'] - } - ); - $plainText.html(sanitizedLinkedText); - $prettyPrint.html(sanitizedLinkedText); - - switch (format) { - case 'markdown': - const converter = new showdown.Converter({ - strikethrough: true, - tables: true, - tablesHeaderId: true, - simplifiedAutoLink: true, - excludeTrailingPunctuationFromURLs: true - }); - // let showdown convert the HTML and sanitize HTML *afterwards*! - $plainText.html( - DOMPurify.sanitize( - converter.makeHtml(text) - ) - ); - // add table classes from bootstrap css - $plainText.find('table').addClass('table-condensed table-bordered'); - break; - case 'syntaxhighlighting': + if (format === 'markdown') { + const converter = new showdown.Converter({ + strikethrough: true, + tables: true, + tablesHeaderId: true, + simplifiedAutoLink: true, + excludeTrailingPunctuationFromURLs: true + }); + // let showdown convert the HTML and sanitize HTML *afterwards*! + $plainText.html( + DOMPurify.sanitize( + converter.makeHtml(text) + ) + ); + // add table classes from bootstrap css + $plainText.find('table').addClass('table-condensed table-bordered'); + } else { + if (format === 'syntaxhighlighting') { // yes, this is really needed to initialize the environment if (typeof prettyPrint === 'function') { @@ -2541,15 +2530,18 @@ jQuery.PrivateBin = (function($, RawDeflate) { } $prettyPrint.html( - DOMPurify.sanitize( - prettyPrintOne(escapedLinkedText, null, true) + prettyPrintOne( + Helper.htmlEntities(text), null, true ) ); - // fall through, as the rest is the same - default: // = 'plaintext' - $prettyPrint.css('white-space', 'pre-wrap'); - $prettyPrint.css('word-break', 'normal'); - $prettyPrint.removeClass('prettyprint'); + } else { + // = 'plaintext' + $prettyPrint.text(text); + } + Helper.urls2links($prettyPrint); + $prettyPrint.css('white-space', 'pre-wrap'); + $prettyPrint.css('word-break', 'normal'); + $prettyPrint.removeClass('prettyprint'); } } @@ -3323,14 +3315,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..b8f6aa21 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/g, '\n').replace(/\u0000/g, ''); + 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/g, '\n').replace(/\u0000/g, ''); + postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, ''); + 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 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/g, '\n').replace(/\u0000/g, ''); + postfix = ' ' + postfix.replace(/\r/g, '\n').replace(/\u0000/g, ''); + 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 a5474bba..e9853f22 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 171f997f..f54170e1 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -50,7 +50,7 @@ endif; ?> - +