From 7992d7a65a39cf3bb57faa3c13c2f1414665a781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20David=20Mart=C3=ADnez=20Cubillos?= Date: Mon, 27 May 2024 08:24:55 -0500 Subject: [PATCH] DEV: Refactor to add Custom emoji sanitization (#24368) Co-authored-by: Joffrey JAFFEUX --- app/controllers/admin/emojis_controller.rb | 3 +-- app/models/emoji.rb | 4 ++++ lib/plugin/instance.rb | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/emojis_controller.rb b/app/controllers/admin/emojis_controller.rb index 774506744d8..65e5a66ba0c 100644 --- a/app/controllers/admin/emojis_controller.rb +++ b/app/controllers/admin/emojis_controller.rb @@ -16,8 +16,7 @@ class Admin::EmojisController < Admin::AdminController hijack do # fix the name name = File.basename(name, ".*") - name = name.gsub(/[^a-z0-9]+/i, "_").gsub(/_{2,}/, "_").downcase - + name = Emoji.sanitize_emoji_name(name) upload = UploadCreator.new(file.tempfile, file.original_filename, type: "custom_emoji").create_for( current_user.id, diff --git a/app/models/emoji.rb b/app/models/emoji.rb index 4d85b4eb530..867767823cb 100644 --- a/app/models/emoji.rb +++ b/app/models/emoji.rb @@ -324,4 +324,8 @@ class Emoji end end end + + def self.sanitize_emoji_name(name) + name.gsub(/[^a-z0-9]+/i, "_").gsub(/_{2,}/, "_").downcase + end end diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index 290c69d6e56..78588ee9975 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -718,7 +718,7 @@ class Plugin::Instance end def register_emoji(name, url, group = Emoji::DEFAULT_GROUP) - name = name.gsub(/[^a-z0-9]+/i, "_").gsub(/_{2,}/, "_").downcase + name = Emoji.sanitize_emoji_name(name) Plugin::CustomEmoji.register(name, url, group) Emoji.clear_cache end