AttachmentViewer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. 'use strict';
  2. const common = require('../common');
  3. const fc = require('fast-check');
  4. describe('AttachmentViewer', function () {
  5. beforeEach(() => {
  6. mockCreateObjectUrl();
  7. });
  8. afterEach(() => {
  9. globalThis.cleanup()
  10. });
  11. describe('whole run (setAttachment, showAttachment, removeAttachment, hideAttachment, hideAttachmentPreview, hasAttachment, getAttachment & moveAttachmentTo)', function () {
  12. this.timeout(30000);
  13. it('displays & hides data as requested', () => {
  14. fc.assert(fc.property(
  15. common.fcMimeTypes(),
  16. fc.string(),
  17. fc.string(),
  18. fc.string(),
  19. fc.string(),
  20. // eslint-disable-next-line complexity
  21. function (mimeType, rawdata, filename, prefix, postfix) {
  22. let data = 'data:' + mimeType + ';base64,' + common.btoa(rawdata),
  23. mimePrefix = mimeType.substring(0, 6),
  24. previewSupported = (
  25. mimePrefix === 'image/' ||
  26. mimePrefix === 'audio/' ||
  27. mimePrefix === 'video/' ||
  28. mimeType.match(/\/pdf/i)
  29. ),
  30. results = [],
  31. result = '';
  32. // text node of attachment will truncate at null byte
  33. if (filename === '\u0000') {
  34. filename = '';
  35. }
  36. prefix = prefix.replace(/%(s|d)/g, '%%');
  37. postfix = postfix.replace(/%(s|d)/g, '%%').replace(/<|>/g, '');
  38. document.body.innerHTML = (
  39. '<div id="attachmentPreview" class="col-md-12 text-center hidden"></div>' +
  40. '<div id="attachment" class="hidden"></div>' +
  41. '<div id="templates">' +
  42. '<div id="attachmenttemplate" role="alert" class="attachment hidden alert alert-info">' +
  43. '<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>' +
  44. '<a class="alert-link">Download attachment</a>' +
  45. '</div>' +
  46. '</div>'
  47. );
  48. PrivateBin.AttachmentViewer.init();
  49. PrivateBin.Model.init();
  50. results.push(
  51. !PrivateBin.AttachmentViewer.hasAttachment() &&
  52. document.getElementById('attachment').classList.contains('hidden') &&
  53. document.getElementById('attachment').children.length === 0 &&
  54. document.getElementById('attachmenttemplate').classList.contains('hidden') &&
  55. document.getElementById('attachmentPreview').classList.contains('hidden')
  56. );
  57. global.atob = common.atob;
  58. if (filename.length) {
  59. PrivateBin.AttachmentViewer.setAttachment(data, filename);
  60. } else {
  61. PrivateBin.AttachmentViewer.setAttachment(data);
  62. }
  63. // // beyond this point we will get the blob URL instead of the data
  64. data = window.URL.createObjectURL(data);
  65. const attachment = PrivateBin.AttachmentViewer.getAttachments();
  66. results.push(
  67. PrivateBin.AttachmentViewer.hasAttachment() &&
  68. document.getElementById('attachment').classList.contains('hidden') &&
  69. document.getElementById('attachment').children.length > 0 &&
  70. document.getElementById('attachmentPreview').classList.contains('hidden') &&
  71. attachment[0][0] === data &&
  72. attachment[0][1] === filename
  73. );
  74. PrivateBin.AttachmentViewer.showAttachment();
  75. results.push(
  76. !document.getElementById('attachment').classList.contains('hidden') &&
  77. document.getElementById('attachment').children.length > 0 &&
  78. (previewSupported ? !document.getElementById('attachmentPreview').classList.contains('hidden') : document.getElementById('attachmentPreview').classList.contains('hidden'))
  79. );
  80. PrivateBin.AttachmentViewer.hideAttachment();
  81. results.push(
  82. document.getElementById('attachment').classList.contains('hidden') &&
  83. (previewSupported ? !document.getElementById('attachmentPreview').classList.contains('hidden') : document.getElementById('attachmentPreview').classList.contains('hidden'))
  84. );
  85. if (previewSupported) {
  86. PrivateBin.AttachmentViewer.hideAttachmentPreview();
  87. results.push(document.getElementById('attachmentPreview').classList.contains('hidden'));
  88. }
  89. PrivateBin.AttachmentViewer.showAttachment();
  90. results.push(
  91. !document.getElementById('attachment').classList.contains('hidden') &&
  92. (previewSupported ? !document.getElementById('attachmentPreview').classList.contains('hidden') : document.getElementById('attachmentPreview').classList.contains('hidden'))
  93. );
  94. let element = document.createElement('div');
  95. PrivateBin.AttachmentViewer.moveAttachmentTo(element, attachment[0], prefix + '%s' + postfix);
  96. // messageIDs with links get a relaxed treatment
  97. if (prefix.indexOf('<a') === -1 && postfix.indexOf('<a') === -1) {
  98. const tempTA = document.createElement('textarea');
  99. tempTA.textContent = (prefix + filename + postfix);
  100. result = tempTA.textContent;
  101. } else {
  102. result = DOMPurify.sanitize(
  103. prefix + PrivateBin.Helper.htmlEntities(filename) + postfix, {
  104. ALLOWED_TAGS: ['a', 'i', 'span'],
  105. ALLOWED_ATTR: ['href', 'id']
  106. }
  107. );
  108. }
  109. if (filename.length) {
  110. results.push(
  111. element.querySelector('a').href === data &&
  112. element.querySelector('a').getAttribute('download') === filename &&
  113. element.querySelector('a').textContent === result
  114. );
  115. } else {
  116. results.push(element.querySelector('a').href === data);
  117. }
  118. PrivateBin.AttachmentViewer.removeAttachment();
  119. results.push(
  120. document.getElementById('attachment').classList.contains('hidden') &&
  121. document.getElementById('attachment').children.length === 0 &&
  122. document.getElementById('attachmentPreview').classList.contains('hidden')
  123. );
  124. return results.every(element => element);
  125. }
  126. ));
  127. });
  128. it(
  129. 'sanitizes file names in attachments',
  130. function() {
  131. document.body.innerHTML = (
  132. '<div id="attachmentPreview" class="col-md-12 text-center hidden"></div>' +
  133. '<div id="attachment" class="hidden"></div>' +
  134. '<div id="templates">' +
  135. '<div id="attachmenttemplate" role="alert" class="attachment hidden alert alert-info">' +
  136. '<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>' +
  137. '<a class="alert-link">Download attachment</a>' +
  138. '</div>' +
  139. '</div>'
  140. );
  141. PrivateBin.AttachmentViewer.init();
  142. PrivateBin.Model.init();
  143. global.atob = common.atob;
  144. const maliciousFileNames = [
  145. '<script>alert("☹️");//<a',
  146. '"><meta http-equiv="refresh" content="0;url=http://example.com/">.txt'
  147. ];
  148. for (const filename of maliciousFileNames) {
  149. PrivateBin.AttachmentViewer.setAttachment('data:;base64,', filename);
  150. assert.ok(!document.body.innerHTML.includes(filename));
  151. }
  152. }
  153. );
  154. });
  155. describe('showAttachment()', function () {
  156. it('displays attachment even when attachmentPreview element is missing',
  157. function() {
  158. document.body.innerHTML = (
  159. '<div id="attachment" class="hidden"></div>' +
  160. '<div id="templates">' +
  161. '<div id="attachmenttemplate" role="alert" class="attachment hidden alert alert-info">' +
  162. '<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>' +
  163. '<a class="alert-link">Download attachment</a>' +
  164. '</div>' +
  165. '</div>'
  166. );
  167. // Note: attachmentPreview element is intentionally NOT created
  168. PrivateBin.AttachmentViewer.init();
  169. PrivateBin.Model.init();
  170. global.atob = common.atob;
  171. // Set attachment without preview element
  172. PrivateBin.AttachmentViewer.setAttachment('data:text/plain;base64,', 'test.txt');
  173. // Show attachment should work even without attachmentPreview
  174. PrivateBin.AttachmentViewer.showAttachment();
  175. const attachment = document.getElementById('attachment');
  176. assert.ok(!attachment.classList.contains('hidden'), 'Attachment should be visible');
  177. assert.ok(attachment.children.length > 0, 'Attachment should have content');
  178. }
  179. )
  180. });
  181. function mockCreateObjectUrl() {
  182. if (typeof window.URL.createObjectURL === 'undefined') {
  183. Object.defineProperty(
  184. window.URL,
  185. 'createObjectURL',
  186. {
  187. value: function (_blob) {
  188. return 'blob:' + location.origin + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
  189. }
  190. }
  191. );
  192. }
  193. }
  194. });