El RIDO 2 недель назад
Родитель
Сommit
6ae8d398c7
1 измененных файлов с 77 добавлено и 42 удалено
  1. 77 42
      js/test/AttachmentViewer.js

+ 77 - 42
js/test/AttachmentViewer.js

@@ -1,5 +1,24 @@
 'use strict';
 'use strict';
 const common = require('../common');
 const common = require('../common');
+const bodyTemplate = '<div id="attachmentPreview" class="col-md-12 text-center hidden"></div>' +
+    '<div id="attachment" class="hidden"></div>' +
+    '<div id="templates">' +
+        '<div id="attachmenttemplate" role="alert" class="attachment hidden alert alert-info">' +
+            '<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>' +
+            '<a class="alert-link">Download attachment</a>' +
+        '</div>' +
+    '</div>';
+const createMockObjectURL = function(window, includeType = true) {
+    if (typeof window.URL.createObjectURL === 'undefined') {
+        Object.defineProperty(
+            window.URL,
+            'createObjectURL',
+            {value: function(blob) {
+                return 'blob:' + (includeType ? blob.type : location.origin) + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
+            }}
+        );
+    }
+}
 
 
 describe('AttachmentViewer', function () {
 describe('AttachmentViewer', function () {
     describe('setAttachment, showAttachment, removeAttachment, hideAttachment, hideAttachmentPreview, hasAttachment, getAttachment & moveAttachmentTo', function () {
     describe('setAttachment, showAttachment, removeAttachment, hideAttachment, hideAttachmentPreview, hasAttachment, getAttachment & moveAttachmentTo', function () {
@@ -31,26 +50,8 @@ describe('AttachmentViewer', function () {
                 }
                 }
                 prefix  = prefix.replace(/%(s|d)/g, '%%');
                 prefix  = prefix.replace(/%(s|d)/g, '%%');
                 postfix = postfix.replace(/%(s|d)/g, '%%').replace(/<|>/g, '');
                 postfix = postfix.replace(/%(s|d)/g, '%%').replace(/<|>/g, '');
-                $('body').html(
-                    '<div id="attachmentPreview" class="col-md-12 text-center hidden"></div>' +
-                    '<div id="attachment" class="hidden"></div>' +
-                    '<div id="templates">' +
-                        '<div id="attachmenttemplate" role="alert" class="attachment hidden alert alert-info">' +
-                            '<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>' +
-                            '<a class="alert-link">Download attachment</a>' +
-                        '</div>' +
-                    '</div>'
-                );
-                // mock createObjectURL for jsDOM
-                if (typeof window.URL.createObjectURL === 'undefined') {
-                    Object.defineProperty(
-                        window.URL,
-                        'createObjectURL',
-                        {value: function(blob) {
-                            return 'blob:' + location.origin + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
-                        }}
-                    );
-                }
+                $('body').html(bodyTemplate);
+                createMockObjectURL(window, false);
                 $.PrivateBin.AttachmentViewer.init();
                 $.PrivateBin.AttachmentViewer.init();
                 $.PrivateBin.Model.init();
                 $.PrivateBin.Model.init();
                 results.push(
                 results.push(
@@ -66,7 +67,7 @@ describe('AttachmentViewer', function () {
                 } else {
                 } else {
                     $.PrivateBin.AttachmentViewer.setAttachment(data);
                     $.PrivateBin.AttachmentViewer.setAttachment(data);
                 }
                 }
-                // // beyond this point we will get the blob URL instead of the data
+                // beyond this point we will get the blob URL instead of the data
                 data = window.URL.createObjectURL(data);
                 data = window.URL.createObjectURL(data);
                 const attachment = $.PrivateBin.AttachmentViewer.getAttachments();
                 const attachment = $.PrivateBin.AttachmentViewer.getAttachments();
                 results.push(
                 results.push(
@@ -131,29 +132,11 @@ describe('AttachmentViewer', function () {
         );
         );
 
 
         it(
         it(
-            'sanitizes file names and MIME types in attachments',
+            'sanitizes file names',
             function() {
             function() {
                 const clean = jsdom();
                 const clean = jsdom();
-                $('body').html(
-                    '<div id="attachmentPreview" class="col-md-12 text-center hidden"></div>' +
-                    '<div id="attachment" class="hidden"></div>' +
-                    '<div id="templates">' +
-                        '<div id="attachmenttemplate" role="alert" class="attachment hidden alert alert-info">' +
-                            '<span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>' +
-                            '<a class="alert-link">Download attachment</a>' +
-                        '</div>' +
-                    '</div>'
-                );
-                // mock createObjectURL for jsDOM
-                if (typeof window.URL.createObjectURL === 'undefined') {
-                    Object.defineProperty(
-                        window.URL,
-                        'createObjectURL',
-                        {value: function(blob) {
-                            return 'blob:' + blob.type + '/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed';
-                        }}
-                    );
-                }
+                $('body').html(bodyTemplate);
+                createMockObjectURL(window);
                 $.PrivateBin.AttachmentViewer.init();
                 $.PrivateBin.AttachmentViewer.init();
                 $.PrivateBin.Model.init();
                 $.PrivateBin.Model.init();
                 global.atob = common.atob;
                 global.atob = common.atob;
@@ -167,6 +150,19 @@ describe('AttachmentViewer', function () {
                     assert.ok(!$('body').html().includes(filename), 'does not allow file name ' + filename);
                     assert.ok(!$('body').html().includes(filename), 'does not allow file name ' + filename);
                     $.PrivateBin.AttachmentViewer.removeAttachment();
                     $.PrivateBin.AttachmentViewer.removeAttachment();
                 }
                 }
+                clean();
+            }
+        );
+
+        it(
+            'sanitizes MIME types in attachments',
+            function() {
+                const clean = jsdom();
+                $('body').html(bodyTemplate);
+                createMockObjectURL(window);
+                $.PrivateBin.AttachmentViewer.init();
+                $.PrivateBin.Model.init();
+                global.atob = common.atob;
 
 
                 const maliciousMimeTypes = [
                 const maliciousMimeTypes = [
                     // PDF bypasses
                     // PDF bypasses
@@ -189,6 +185,19 @@ describe('AttachmentViewer', function () {
                     assert.ok(!$('body').html().includes('<img'), 'does not allow image MIME type: ' + mimeType);
                     assert.ok(!$('body').html().includes('<img'), 'does not allow image MIME type: ' + mimeType);
                     $.PrivateBin.AttachmentViewer.removeAttachment();
                     $.PrivateBin.AttachmentViewer.removeAttachment();
                 }
                 }
+                clean();
+            }
+        );
+
+        it(
+            'supports safe MIME types in attachments',
+            function() {
+                const clean = jsdom();
+                $('body').html(bodyTemplate);
+                createMockObjectURL(window);
+                $.PrivateBin.AttachmentViewer.init();
+                $.PrivateBin.Model.init();
+                global.atob = common.atob;
 
 
                 const supportedSafeMimeTypes = [
                 const supportedSafeMimeTypes = [
                     'text/plain',
                     'text/plain',
@@ -198,6 +207,19 @@ describe('AttachmentViewer', function () {
                 for (const mimeType of supportedSafeMimeTypes) {
                 for (const mimeType of supportedSafeMimeTypes) {
                     assert.ok($.PrivateBin.AttachmentViewer.isSafeMimeType(mimeType), 'treats as safe MIME type: '+ mimeType);
                     assert.ok($.PrivateBin.AttachmentViewer.isSafeMimeType(mimeType), 'treats as safe MIME type: '+ mimeType);
                 }
                 }
+                clean();
+            }
+        );
+
+        it(
+            'supports safe MIME type previews in attachments',
+            function() {
+                const clean = jsdom();
+                $('body').html(bodyTemplate);
+                createMockObjectURL(window);
+                $.PrivateBin.AttachmentViewer.init();
+                $.PrivateBin.Model.init();
+                global.atob = common.atob;
 
 
                 const supportedPreviewMimeTypes = [
                 const supportedPreviewMimeTypes = [
                     'application/pdf',
                     'application/pdf',
@@ -210,6 +232,19 @@ describe('AttachmentViewer', function () {
                     assert.ok($('body').html().includes(mimeType), 'allows MIME type: ' + mimeType);
                     assert.ok($('body').html().includes(mimeType), 'allows MIME type: ' + mimeType);
                     $.PrivateBin.AttachmentViewer.removeAttachment();
                     $.PrivateBin.AttachmentViewer.removeAttachment();
                 }
                 }
+                clean();
+            }
+        );
+
+        it(
+            'special case sanitizes potentially unsafe SVG previews',
+            function() {
+                const clean = jsdom();
+                $('body').html(bodyTemplate);
+                createMockObjectURL(window);
+                $.PrivateBin.AttachmentViewer.init();
+                $.PrivateBin.Model.init();
+                global.atob = common.atob;
 
 
                 // special case: not a safe type, but renders a sanitized preview
                 // special case: not a safe type, but renders a sanitized preview
                 const svgMimeTypes = [
                 const svgMimeTypes = [