'use strict'; const common = require('../common'); const fc = require('fast-check'); describe('AttachmentViewer', function () { beforeEach(() => { mockCreateObjectUrl(); }); afterEach(() => { globalThis.cleanup() }); describe('whole run (setAttachment, showAttachment, removeAttachment, hideAttachment, hideAttachmentPreview, hasAttachment, getAttachment & moveAttachmentTo)', function () { this.timeout(30000); it('displays & hides data as requested', () => { fc.assert(fc.property( common.fcMimeTypes(), fc.string(), fc.string(), fc.string(), fc.string(), // eslint-disable-next-line complexity function (mimeType, rawdata, filename, prefix, postfix) { let data = 'data:' + mimeType + ';base64,' + common.btoa(rawdata), mimePrefix = mimeType.substring(0, 6), previewSupported = ( mimePrefix === 'image/' || mimePrefix === 'audio/' || mimePrefix === 'video/' || mimeType.match(/\/pdf/i) ), results = [], result = ''; // text node of attachment will truncate at null byte if (filename === '\u0000') { filename = ''; } prefix = prefix.replace(/%(s|d)/g, '%%'); postfix = postfix.replace(/%(s|d)/g, '%%').replace(/<|>/g, ''); document.body.innerHTML = ( '' + '' + '
' + '' + '
' ); PrivateBin.AttachmentViewer.init(); PrivateBin.Model.init(); results.push( !PrivateBin.AttachmentViewer.hasAttachment() && document.getElementById('attachment').classList.contains('hidden') && document.getElementById('attachment').children.length === 0 && document.getElementById('attachmenttemplate').classList.contains('hidden') && document.getElementById('attachmentPreview').classList.contains('hidden') ); global.atob = common.atob; if (filename.length) { PrivateBin.AttachmentViewer.setAttachment(data, filename); } else { PrivateBin.AttachmentViewer.setAttachment(data); } // // beyond this point we will get the blob URL instead of the data data = window.URL.createObjectURL(data); const attachment = PrivateBin.AttachmentViewer.getAttachments(); results.push( PrivateBin.AttachmentViewer.hasAttachment() && document.getElementById('attachment').classList.contains('hidden') && document.getElementById('attachment').children.length > 0 && document.getElementById('attachmentPreview').classList.contains('hidden') && attachment[0][0] === data && attachment[0][1] === filename ); PrivateBin.AttachmentViewer.showAttachment(); results.push( !document.getElementById('attachment').classList.contains('hidden') && document.getElementById('attachment').children.length > 0 && (previewSupported ? !document.getElementById('attachmentPreview').classList.contains('hidden') : document.getElementById('attachmentPreview').classList.contains('hidden')) ); PrivateBin.AttachmentViewer.hideAttachment(); results.push( document.getElementById('attachment').classList.contains('hidden') && (previewSupported ? !document.getElementById('attachmentPreview').classList.contains('hidden') : document.getElementById('attachmentPreview').classList.contains('hidden')) ); if (previewSupported) { PrivateBin.AttachmentViewer.hideAttachmentPreview(); results.push(document.getElementById('attachmentPreview').classList.contains('hidden')); } PrivateBin.AttachmentViewer.showAttachment(); results.push( !document.getElementById('attachment').classList.contains('hidden') && (previewSupported ? !document.getElementById('attachmentPreview').classList.contains('hidden') : document.getElementById('attachmentPreview').classList.contains('hidden')) ); let element = document.createElement('div'); PrivateBin.AttachmentViewer.moveAttachmentTo(element, attachment[0], prefix + '%s' + postfix); // messageIDs with links get a relaxed treatment if (prefix.indexOf(' element); } )); }); it( 'sanitizes file names in attachments', function() { document.body.innerHTML = ( '' + '' + '
' + '' + '
' ); PrivateBin.AttachmentViewer.init(); PrivateBin.Model.init(); global.atob = common.atob; const maliciousFileNames = [ '