A platform for community discussion. Free, open, simple.
FIX: security hardening form `codes_to_img` (#39776)
Sam3216ebcf7d77
diff --git a/app/models/category.rb b/app/models/category.rbindex ba682b5d7c7..fb4445cc39b 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -96,6 +96,7 @@ class Category < ActiveRecord::Basevalidates :num_featured_topics, numericality: { only_integer: true, greater_than: 0 }validates :search_priority, inclusion: { in: Searchable::PRIORITIES.values }+ validate :emoji_existsvalidate :parent_category_validatorvalidate :email_in_validatorvalidate :ensure_slug@@ -349,6 +350,10 @@ class Category < ActiveRecord::Base.uniqend+ def emoji_exists+ errors.add(:emoji, :invalid) if emoji.present? && !Emoji.exists?(emoji)+ end+# Accepts an array of slugs with each item in the array# Returns the category ids of the last slug in the array. The slugs array has to follow the proper category# nesting hierarchy. If any of the slug in the array is invalid or if the slugs array does not follow the properdiff --git a/app/models/emoji.rb b/app/models/emoji.rbindex c44ddd83ff3..2e57be3fe90 100644--- a/app/models/emoji.rb+++ b/app/models/emoji.rb@@ -396,20 +396,38 @@ class Emojidef self.codes_to_img(str)return if str.blank?- str =- str.gsub(EMOJI_CODE_REGEXP) do |name|- code = $1-- if code && Emoji.custom?(code)- emoji = Emoji[code]- "<img src=\"#{emoji.url}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\" loading=\"lazy\" width=\"20\" height=\"20\">"- elsif code && Emoji.exists?(code)- "<img src=\"#{Emoji.url_for(code)}\" title=\"#{code}\" class=\"emoji\" alt=\"#{code}\" loading=\"lazy\" width=\"20\" height=\"20\">"- else- name- end+ result = +""+ last_index = 0++ str.scan(EMOJI_CODE_REGEXP) do+ match = Regexp.last_match+ code = match[1]++ result << ERB::Util.html_escape(str[last_index...match.begin(0)])++ result << if code && Emoji.custom?(code)+ emoji = Emoji[code]+ emoji_img_tag(emoji.url, code)+ elsif code && Emoji.exists?(code)+ emoji_img_tag(Emoji.url_for(code), code)+ else+ ERB::Util.html_escape(match[0])end++ last_index = match.end(0)+ end++ result << ERB::Util.html_escape(str[last_index..])+ result+ end++ def self.emoji_img_tag(url, code)+ escaped_url = ERB::Util.html_escape(url)+ escaped_code = ERB::Util.html_escape(code)++ "<img src=\"#{escaped_url}\" title=\"#{escaped_code}\" class=\"emoji\" alt=\"#{escaped_code}\" loading=\"lazy\" width=\"20\" height=\"20\">"end+ private_class_method :emoji_img_tagdef self.sanitize_emoji_name(name)name.gsub(/[^a-z0-9\+\-]+/i, "_").gsub(/_{2,}/, "_").downcasediff --git a/spec/models/emoji_spec.rb b/spec/models/emoji_spec.rbindex 124fd768c4f..9decb356c0e 100644--- a/spec/models/emoji_spec.rb+++ b/spec/models/emoji_spec.rb@@ -163,6 +163,24 @@ RSpec.describe Emoji doHTMLend+ it "escapes non-emoji text" do+ replaced_str = described_class.codes_to_img(%(This <img src=x onerror="alert('xss')"> :foo:))++ expect(replaced_str).to eq(+ "This <img src=x onerror="alert('xss')"> :foo:",+ )+ end++ it "escapes generated image attribute values" do+ Plugin::CustomEmoji.register("xssxx", %q|" onerror="alert('xss')|)++ replaced_str = described_class.codes_to_img(":xssxx:")++ expect(replaced_str).to eq(+ %(<img src="" onerror="alert('xss')" title="xssxx" class="emoji" alt="xssxx" loading="lazy" width="20" height="20">),+ )+ end+it "doesn't replace non-existing codes" doreplaced_str =described_class.codes_to_img("This is a good day :woman: :foo: :bar:t4: :man:t8:")diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rbindex cb9ff6f2812..b68e73a7f8e 100644--- a/spec/requests/categories_controller_spec.rb+++ b/spec/requests/categories_controller_spec.rb@@ -483,6 +483,21 @@ RSpec.describe CategoriesController doexpect(response.status).to eq(422)end+ it "rejects invalid emoji names" do+ post "/categories.json",+ params: {+ name: "Emoji Category",+ color: "ff0",+ text_color: "fff",+ style_type: "emoji",+ emoji: %(<img src=x onerror="alert('xss')">),+ }++ expect(response.status).to eq(422)+ expect(response.parsed_body["errors"]).to include("Emoji is invalid")+ expect(Category.find_by(name: "Emoji Category")).to be_nil+ end+it "returns errors with invalid group" docategory = Fabricate(:category, user: admin)readonly = CategoryGroup.permission_types[:readonly]