From 35a79a70c3f1ddffacb5b120dbfd1e91b661e9b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 20 Oct 2016 19:53:41 +0200 Subject: [PATCH] FIX: uploading custom avatar was always hidden --- .../discourse/lib/utilities.js.es6 | 37 +++++++++---------- lib/validators/upload_validator.rb | 8 ++-- test/javascripts/lib/utilities-test.js.es6 | 31 +++++++++++++--- 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/utilities.js.es6 b/app/assets/javascripts/discourse/lib/utilities.js.es6 index a46fdb0c8c6..0385fa65550 100644 --- a/app/assets/javascripts/discourse/lib/utilities.js.es6 +++ b/app/assets/javascripts/discourse/lib/utilities.js.es6 @@ -190,10 +190,8 @@ export function validateUploadedFiles(files, bypassNewUserRestriction) { export function validateUploadedFile(file, type, bypassNewUserRestriction) { // check that the uploaded file is authorized - if (!authorizesAllExtensions() && - !isAuthorizedUpload(file)) { - var extensions = authorizedExtensions(); - bootbox.alert(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: extensions })); + if (!authorizesAllExtensions() && !isAuthorizedUpload(file)) { + bootbox.alert(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: authorizedExtensions() })); return false; } @@ -217,23 +215,24 @@ export function authorizesAllExtensions() { return Discourse.SiteSettings.authorized_extensions.indexOf("*") >= 0; } +function extensions() { + return Discourse.SiteSettings.authorized_extensions + .toLowerCase() + .replace(/[\s\.]+/g, "") + .split("|") + .filter(ext => ext.indexOf("*") === -1); +} + +function extensionsRegex() { + return new RegExp("\\.(" + extensions().join("|") + ")$", "i"); +} + export function isAuthorizedUpload(file) { - if (file && file.name) { - var extensions = _.chain(Discourse.SiteSettings.authorized_extensions.split("|")) - .reject(function(extension) { return extension.indexOf("*") >= 0; }) - .map(function(extension) { return (extension.indexOf(".") === 0 ? extension.substring(1) : extension).replace(".", "\\."); }) - .value(); - return new RegExp("\\.(" + extensions.join("|") + ")$", "i").test(file.name); - } - return false; + return file && file.name && extensionsRegex().test(file.name); } export function authorizedExtensions() { - return _.chain(Discourse.SiteSettings.authorized_extensions.split("|")) - .reject(function(extension) { return extension.indexOf("*") >= 0; }) - .map(function(extension) { return extension.toLowerCase(); }) - .value() - .join(", "); + return extensions().join(", "); } export function uploadLocation(url) { @@ -267,12 +266,12 @@ export function isAnImage(path) { export function allowsImages() { return authorizesAllExtensions() || - (/\.(png|jpe?g|gif|bmp|tiff?|svg|webp|ico)/i).test(authorizedExtensions()); + (/(png|jpe?g|gif|bmp|tiff?|svg|webp|ico)/i).test(authorizedExtensions()); } export function allowsAttachments() { return authorizesAllExtensions() || - !/^(\.(png|jpe?g|gif|bmp|tiff?|svg|webp|ico)(,\s)?)+$/i.test(authorizedExtensions()); + !/^((png|jpe?g|gif|bmp|tiff?|svg|webp|ico)(,\s)?)+$/i.test(authorizedExtensions()); } export function displayErrorForUpload(data) { diff --git a/lib/validators/upload_validator.rb b/lib/validators/upload_validator.rb index 9d871ba520b..5612662cb11 100644 --- a/lib/validators/upload_validator.rb +++ b/lib/validators/upload_validator.rb @@ -49,12 +49,10 @@ class Validators::UploadValidator < ActiveModel::Validator authorized_uploads = Set.new SiteSetting.authorized_extensions - .tr(" ", "") + .gsub(/[\s\.]+/, "") + .downcase .split("|") - .each do |extension| - next if extension.include?("*") - authorized_uploads << (extension.start_with?(".") ? extension[1..-1] : extension).downcase - end + .each { |extension| authorized_uploads << extension unless extension.include?("*") } authorized_uploads end diff --git a/test/javascripts/lib/utilities-test.js.es6 b/test/javascripts/lib/utilities-test.js.es6 index fd3583de8c0..403cb2c9719 100644 --- a/test/javascripts/lib/utilities-test.js.es6 +++ b/test/javascripts/lib/utilities-test.js.es6 @@ -5,6 +5,8 @@ import { extractDomainFromUrl, isAnImage, avatarUrl, + authorizedExtensions, + allowsImages, allowsAttachments, getRawSize, avatarImg, @@ -63,12 +65,11 @@ test("new user cannot upload attachments", function() { }); test("ensures an authorized upload", function() { - var html = { name: "unauthorized.html" }; - var extensions = Discourse.SiteSettings.authorized_extensions.replace(/\|/g, ", "); + const html = { name: "unauthorized.html" }; sandbox.stub(bootbox, "alert"); not(validUpload([html])); - ok(bootbox.alert.calledWith(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: extensions }))); + ok(bootbox.alert.calledWith(I18n.t('post.errors.upload_not_authorized', { authorized_extensions: authorizedExtensions() }))); }); var imageSize = 10 * 1024; @@ -163,15 +164,33 @@ test("avatarImg", function() { setDevicePixelRatio(oldRatio); }); +test("allowsImages", function() { + Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif"; + ok(allowsImages(), "works"); + + Discourse.SiteSettings.authorized_extensions = ".jpg|.jpeg|.gif"; + ok(allowsImages(), "works with old extensions syntax"); + + Discourse.SiteSettings.authorized_extensions = "txt|pdf|*"; + ok(allowsImages(), "images are allowed when all extensions are allowed"); + + Discourse.SiteSettings.authorized_extensions = "json|jpg|pdf|txt"; + ok(allowsImages(), "images are allowed when at least one extension is an image extension"); +}); + + test("allowsAttachments", function() { - Discourse.SiteSettings.authorized_extensions = ".jpg, .jpeg, .gif"; + Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif"; not(allowsAttachments(), "no attachments allowed by default"); - Discourse.SiteSettings.authorized_extensions = ".jpg, .jpeg, .gif, *"; + Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif|*"; ok(allowsAttachments(), "attachments are allowed when all extensions are allowed"); - Discourse.SiteSettings.authorized_extensions = ".jpg, .jpeg, .gif, .pdf"; + Discourse.SiteSettings.authorized_extensions = "jpg|jpeg|gif|pdf"; ok(allowsAttachments(), "attachments are allowed when at least one extension is not an image extension"); + + Discourse.SiteSettings.authorized_extensions = ".jpg|.jpeg|.gif|.pdf"; + ok(allowsAttachments(), "works with old extensions syntax"); }); test("defaultHomepage", function() {