PasteViewer.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. var common = require('../common');
  3. describe('PasteViewer', function () {
  4. describe('run, hide, getText, setText, getFormat, setFormat & isPrettyPrinted', function () {
  5. this.timeout(30000);
  6. beforeEach(() => {
  7. document.body.innerHTML = (
  8. '<div id="placeholder" class="hidden">+++ no document text +++</div>' +
  9. '<div id="prettymessage" class="hidden"><pre id="prettyprint" class="prettyprint linenums:1"></pre></div>' +
  10. '<div id="plaintext" class="hidden"></div>'
  11. );
  12. });
  13. afterEach(() => {
  14. globalThis.cleanup();
  15. });
  16. it('basic plaintext display works', function () {
  17. PrivateBin.PasteViewer.init();
  18. PrivateBin.PasteViewer.setFormat('plaintext');
  19. PrivateBin.PasteViewer.setText('hello');
  20. PrivateBin.PasteViewer.run();
  21. assert.strictEqual(PrivateBin.PasteViewer.getText(), 'hello');
  22. assert.ok(!document.getElementById('prettymessage').classList.contains('hidden'));
  23. });
  24. it('basic markdown display works', function () {
  25. PrivateBin.PasteViewer.init();
  26. PrivateBin.PasteViewer.setFormat('markdown');
  27. PrivateBin.PasteViewer.setText('hello **bold**');
  28. PrivateBin.PasteViewer.run();
  29. assert.strictEqual(PrivateBin.PasteViewer.getText(), 'hello **bold**');
  30. assert.ok(!document.getElementById('plaintext').classList.contains('hidden'));
  31. });
  32. jsc.property(
  33. 'initializes with empty text and shows nothing',
  34. common.jscFormats(),
  35. function (format) {
  36. var results = [];
  37. PrivateBin.PasteViewer.init();
  38. PrivateBin.PasteViewer.setFormat(format);
  39. PrivateBin.PasteViewer.setText('');
  40. results.push(
  41. document.getElementById('placeholder').classList.contains('hidden') &&
  42. document.getElementById('prettymessage').classList.contains('hidden') &&
  43. document.getElementById('plaintext').classList.contains('hidden') &&
  44. PrivateBin.PasteViewer.getFormat() === format &&
  45. PrivateBin.PasteViewer.getText() === ''
  46. );
  47. return results.every(element => element);
  48. }
  49. );
  50. jsc.property(
  51. 'when no text is given and view is rendered, it shows placeholder',
  52. common.jscFormats(),
  53. function (format) {
  54. var results = [];
  55. PrivateBin.PasteViewer.init();
  56. PrivateBin.PasteViewer.setFormat(format);
  57. PrivateBin.PasteViewer.setText('');
  58. PrivateBin.PasteViewer.run();
  59. results.push(
  60. !document.getElementById('placeholder').classList.contains('hidden') &&
  61. document.getElementById('prettymessage').classList.contains('hidden') &&
  62. document.getElementById('plaintext').classList.contains('hidden')
  63. );
  64. return results.every(element => element);
  65. }
  66. );
  67. jsc.property(
  68. 'displays text according to format',
  69. common.jscFormats(),
  70. 'nestring',
  71. function (format, text) {
  72. var results = [];
  73. PrivateBin.PasteViewer.init();
  74. PrivateBin.PasteViewer.setFormat(format);
  75. PrivateBin.PasteViewer.setText(text);
  76. PrivateBin.PasteViewer.run();
  77. results.push(
  78. document.getElementById('placeholder').classList.contains('hidden') &&
  79. !PrivateBin.PasteViewer.isPrettyPrinted() &&
  80. PrivateBin.PasteViewer.getText() === text
  81. );
  82. if (format === 'markdown') {
  83. results.push(
  84. document.getElementById('prettymessage').classList.contains('hidden') &&
  85. !document.getElementById('plaintext').classList.contains('hidden')
  86. );
  87. console.log(document.getElementById('prettymessage').classList.contains('hidden'),
  88. !document.getElementById('plaintext').classList.contains('hidden'));
  89. } else {
  90. results.push(
  91. !document.getElementById('prettymessage').classList.contains('hidden') &&
  92. document.getElementById('plaintext').classList.contains('hidden')
  93. );
  94. }
  95. return results.every(element => element);
  96. }
  97. );
  98. jsc.property(
  99. 'sanitizes XSS',
  100. common.jscFormats(),
  101. 'string',
  102. // @see {@link https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet}
  103. jsc.elements([
  104. '<PLAINTEXT>',
  105. '></SCRIPT>">\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>',
  106. '\'\';!--"<XSS>=&{()}',
  107. '<SCRIPT SRC=http://example.com/xss.js></SCRIPT>',
  108. '\'">><marquee><img src=x onerror=confirm(1)></marquee>">' +
  109. '</plaintext\\></|\\><plaintext/onmouseover=prompt(1)>' +
  110. '<script>prompt(1)</script>@gmail.com<isindex formaction=java' +
  111. // obfuscate script URL from eslint rule: no-script-url
  112. 'script:alert(/XSS/) type=submit>\'-->"></script>' +
  113. '<script>alert(document.cookie)</script>"><img/id="confirm' +
  114. '&lpar;1)"/alt="/"src="/"onerror=eval(id)>\'">',
  115. '<IMG SRC="javascript:alert(\'XSS\');">',
  116. '<IMG SRC=javascript:alert(\'XSS\')>',
  117. '<IMG SRC=JaVaScRiPt:alert(\'XSS\')>',
  118. '<IMG SRC=javascript:alert(&quot;XSS&quot;)>',
  119. '<IMG SRC=`javascript:alert("RSnake says, \'XSS\'")`>',
  120. '<a onmouseover="alert(document.cookie)">xxs link</a>',
  121. '<a onmouseover=alert(document.cookie)>xxs link</a>',
  122. '<IMG """><SCRIPT>alert("XSS")</SCRIPT>">',
  123. '<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>',
  124. '<IMG STYLE="xss:expr/*XSS*/ession(alert(\'XSS\'))">',
  125. '<FRAMESET><FRAME SRC="javascript:alert(\'XSS\');"></FRAMESET>',
  126. '<TABLE BACKGROUND="javascript:alert(\'XSS\')">',
  127. '<TABLE><TD BACKGROUND="javascript:alert(\'XSS\')">',
  128. '<SCRIPT>document.write("<SCRI");</SCRIPT>PT SRC="httx://xss.rocks/xss.js"></SCRIPT>'
  129. ]),
  130. 'string',
  131. function (format, prefix, xss, suffix) {
  132. var text = prefix + xss + suffix;
  133. document.body.innerHTML = (
  134. '<div id="placeholder" class="hidden">+++ no document text ' +
  135. '+++</div><div id="prettymessage" class="hidden"><pre ' +
  136. 'id="prettyprint" class="prettyprint linenums:1"></pre>' +
  137. '</div><div id="plaintext" class="hidden"></div>'
  138. );
  139. PrivateBin.PasteViewer.init();
  140. PrivateBin.PasteViewer.setFormat(format);
  141. PrivateBin.PasteViewer.setText(text);
  142. PrivateBin.PasteViewer.run();
  143. var result = document.body.innerHTML.indexOf(xss) === -1;
  144. cleanup();
  145. return result;
  146. }
  147. );
  148. });
  149. });