AttachmentViewer.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. 'use strict';
  2. const common = require('../common');
  3. const bodyTemplate = '<div id="attachmentPreview" class="col-md-12 text-center hidden"></div>' +
  4. '<div id="attachment" class="hidden"></div>' +
  5. '<div id="templates">' +
  6. '<div id="attachmenttemplate" role="alert" class="attachment hidden alert alert-info">' +
  7. '<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>' +
  8. '<a class="alert-link">Download attachment</a>' +
  9. '</div>' +
  10. '</div>';
  11. const fc = require('fast-check');
  12. describe('AttachmentViewer', function () {
  13. beforeEach(() => {
  14. mockCreateObjectUrl();
  15. });
  16. afterEach(() => {
  17. globalThis.cleanup()
  18. });
  19. describe('whole run (setAttachment, showAttachment, removeAttachment, hideAttachment, hideAttachmentPreview, hasAttachment, getAttachment & moveAttachmentTo)', function () {
  20. this.timeout(30000);
  21. it('displays & hides data as requested', () => {
  22. fc.assert(fc.property(
  23. common.fcMimeTypes(),
  24. fc.string(),
  25. fc.string(),
  26. fc.string(),
  27. fc.string(),
  28. // eslint-disable-next-line complexity
  29. function (mimeType, rawdata, filename, prefix, postfix) {
  30. let data = 'data:' + mimeType + ';base64,' + common.btoa(rawdata),
  31. mimePrefix = mimeType.substring(0, 6),
  32. previewSupported = (
  33. mimePrefix === 'image/' ||
  34. mimePrefix === 'audio/' ||
  35. mimePrefix === 'video/' ||
  36. mimeType.match(/\/pdf/i)
  37. ),
  38. results = [],
  39. result = '';
  40. // text node of attachment will truncate at null byte
  41. if (filename === '\u0000') {
  42. filename = '';
  43. }
  44. prefix = prefix.replace(/%(s|d)/g, '%%');
  45. postfix = postfix.replace(/%(s|d)/g, '%%').replace(/<|>/g, '');
  46. document.body.innerHTML = bodyTemplate;
  47. mockCreateObjectUrl(false);
  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',
  130. function() {
  131. document.body.innerHTML = bodyTemplate;
  132. PrivateBin.AttachmentViewer.init();
  133. PrivateBin.Model.init();
  134. global.atob = common.atob;
  135. const maliciousFileNames = [
  136. '<script>alert("☹️");//<a',
  137. '"><meta http-equiv="refresh" content="0;url=http://example.com/">.txt'
  138. ];
  139. for (const filename of maliciousFileNames) {
  140. PrivateBin.AttachmentViewer.setAttachment('data:;base64,', filename);
  141. assert.ok(!document.body.innerHTML.includes(filename));
  142. PrivateBin.AttachmentViewer.removeAttachment();
  143. }
  144. }
  145. );
  146. it(
  147. 'sanitizes MIME types in attachments',
  148. function() {
  149. document.body.innerHTML = bodyTemplate;
  150. PrivateBin.AttachmentViewer.init();
  151. PrivateBin.Model.init();
  152. global.atob = common.atob;
  153. const maliciousMimeTypes = [
  154. // PDF bypasses
  155. 'application/x-pdf', // legacy, we don't need to support this
  156. 'text/html /pdf', // trips up Firefox and Chromium
  157. 'text/html(/pdf', // Chromium, see: https://chromium.googlesource.com/chromium/src/+/refs/tags/152.0.7949.0/net/base/mime_util.cc#521
  158. // SVG bypass
  159. 'text/html svg',
  160. 'text/html(svg',
  161. // invalid bytes after string
  162. 'image/png\x01'
  163. ];
  164. for (const mimeType of maliciousMimeTypes) {
  165. assert.ok(!PrivateBin.AttachmentViewer.isSafeMimeType(mimeType), 'does not treat as safe MIME type: '+ mimeType);
  166. PrivateBin.AttachmentViewer.setAttachment('data:' + mimeType + ';base64,', 'example file name');
  167. assert.ok(!document.body.innerHTML.includes(mimeType), 'does not allow MIME type: ' + mimeType);
  168. assert.ok(!document.body.innerHTML.includes(mimeType.toLowerCase()), 'does not allow lower cased MIME type: ' + mimeType);
  169. assert.ok(!document.body.innerHTML.includes('<img'), 'does not allow image MIME type: ' + mimeType);
  170. PrivateBin.AttachmentViewer.removeAttachment();
  171. }
  172. }
  173. );
  174. it(
  175. 'supports safe MIME types in attachments',
  176. function() {
  177. document.body.innerHTML = bodyTemplate;
  178. PrivateBin.AttachmentViewer.init();
  179. PrivateBin.Model.init();
  180. global.atob = common.atob;
  181. const supportedSafeMimeTypes = [
  182. 'text/plain',
  183. 'image/png',
  184. 'image/jpeg'
  185. ];
  186. for (const mimeType of supportedSafeMimeTypes) {
  187. assert.ok(PrivateBin.AttachmentViewer.isSafeMimeType(mimeType), 'treats as safe MIME type: '+ mimeType);
  188. }
  189. }
  190. );
  191. it(
  192. 'supports safe MIME type previews in attachments',
  193. function() {
  194. document.body.innerHTML = bodyTemplate;
  195. PrivateBin.AttachmentViewer.init();
  196. PrivateBin.Model.init();
  197. global.atob = common.atob;
  198. const supportedPreviewMimeTypes = [
  199. 'application/pdf',
  200. 'audio/wav',
  201. 'video/avi'
  202. ];
  203. for (const mimeType of supportedPreviewMimeTypes) {
  204. assert.ok(PrivateBin.AttachmentViewer.isSafeMimeType(mimeType), 'treats as safe preview MIME type: '+ mimeType);
  205. PrivateBin.AttachmentViewer.setAttachment('data:' + mimeType + ';base64,', 'example file name');
  206. assert.ok(document.body.innerHTML.includes(mimeType), 'allows MIME type: ' + mimeType);
  207. PrivateBin.AttachmentViewer.removeAttachment();
  208. }
  209. }
  210. );
  211. it(
  212. 'special case sanitizes potentially unsafe SVG previews',
  213. function() {
  214. document.body.innerHTML = bodyTemplate;
  215. PrivateBin.AttachmentViewer.init();
  216. PrivateBin.Model.init();
  217. global.atob = common.atob;
  218. // special case: not a safe type, but renders a sanitized preview
  219. const svgMimeTypes = [
  220. 'image/svg+xml',
  221. 'image/SVG+xml',
  222. 'image/sVg'
  223. ];
  224. for (const mimeType of svgMimeTypes) {
  225. assert.ok(!PrivateBin.AttachmentViewer.isSafeMimeType(mimeType), 'treats as unsafe MIME type: '+ mimeType);
  226. PrivateBin.AttachmentViewer.setAttachment('data:' + mimeType + ';base64,', 'example file name');
  227. assert.ok(document.body.innerHTML.includes('image/svg+xml'), 'allows sanitized MIME type: ' + mimeType);
  228. PrivateBin.AttachmentViewer.removeAttachment();
  229. }
  230. }
  231. );
  232. });
  233. describe('showAttachment()', function () {
  234. it('displays attachment even when attachmentPreview element is missing',
  235. function() {
  236. document.body.innerHTML = (
  237. '<div id="attachment" class="hidden"></div>' +
  238. '<div id="templates">' +
  239. '<div id="attachmenttemplate" role="alert" class="attachment hidden alert alert-info">' +
  240. '<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>' +
  241. '<a class="alert-link">Download attachment</a>' +
  242. '</div>' +
  243. '</div>'
  244. );
  245. // Note: attachmentPreview element is intentionally NOT created
  246. PrivateBin.AttachmentViewer.init();
  247. PrivateBin.Model.init();
  248. global.atob = common.atob;
  249. // Set attachment without preview element
  250. PrivateBin.AttachmentViewer.setAttachment('data:text/plain;base64,', 'test.txt');
  251. // Show attachment should work even without attachmentPreview
  252. PrivateBin.AttachmentViewer.showAttachment();
  253. const attachment = document.getElementById('attachment');
  254. assert.ok(!attachment.classList.contains('hidden'), 'Attachment should be visible');
  255. assert.ok(attachment.children.length > 0, 'Attachment should have content');
  256. }
  257. )
  258. });
  259. function mockCreateObjectUrl(includeType = true) {
  260. if (typeof window.URL.createObjectURL === 'undefined') {
  261. Object.defineProperty(
  262. window.URL,
  263. 'createObjectURL',
  264. {
  265. value: function (blob) {
  266. return 'blob:' + (includeType ? blob.type : location.origin) + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';;
  267. }
  268. }
  269. );
  270. }
  271. }
  272. });