AttachmentViewer.js 9.1 KB

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