A platform for community discussion. Free, open, simple.
FIX: Apply CDN to Custom Emojis loaded from S3/R2 (#41267)
Lillycc1123ea4286
diff --git a/app/models/emoji.rb b/app/models/emoji.rbindex 5ae30220ac4..d057f40300c 100644--- a/app/models/emoji.rb+++ b/app/models/emoji.rb@@ -15,6 +15,14 @@ class Emojiattr_accessor :name, :url, :tonable, :group, :created_by+ # The cached `url` is the raw upload/asset URL. CDN conversion is applied+ # lazily here (and in EmojiSerializer) so that changing the S3/asset CDN+ # settings takes effect without having to rebuild the emoji cache.+ def cdn_url+ return nil if url.blank?+ Discourse.store.cdn_url(url)+ end+def self.global_emoji_cache@global_emoji_cache ||= DistributedCache.new("global_emoji_cache", namespace: false)end@@ -259,7 +267,6 @@ class Emojidef self.load_customresult = []-if !GlobalSetting.skip_db?CustomEmoji.includes(:upload)@@ -407,7 +414,7 @@ class Emojiresult << if code && Emoji.custom?(code)emoji = Emoji[code]- emoji_img_tag(emoji.url, code)+ emoji_img_tag(emoji.cdn_url, code)elsif code && Emoji.exists?(code)emoji_img_tag(Emoji.url_for(code), code)elsediff --git a/app/serializers/emoji_serializer.rb b/app/serializers/emoji_serializer.rbindex 25666e73401..7ddea4bf057 100644--- a/app/serializers/emoji_serializer.rb+++ b/app/serializers/emoji_serializer.rb@@ -8,7 +8,6 @@ class EmojiSerializer < ApplicationSerializerenddef url- return nil if object.url.blank?- Discourse.store.cdn_url(object.url)+ object.cdn_urlendenddiff --git a/lib/pretty_text.rb b/lib/pretty_text.rbindex b5075f0a112..1e618689247 100644--- a/lib/pretty_text.rb+++ b/lib/pretty_text.rb@@ -198,7 +198,7 @@ module PrettyTextcontext = v8custom_emoji = {}- Emoji.custom.map { |e| custom_emoji[e.name] = e.url }+ Emoji.custom.map { |e| custom_emoji[e.name] = e.cdn_url }allowed_iframes =DiscoursePluginRegistry.apply_modifier(@@ -305,7 +305,7 @@ module PrettyTextreturn title unless SiteSetting.enable_emoji? && titleset = SiteSetting.emoji_set.inspect- custom = Emoji.custom.map { |e| [e.name, e.url] }.to_h.to_json+ custom = Emoji.custom.map { |e| [e.name, e.cdn_url] }.to_h.to_jsonprotect { v8.eval(<<~JS) }__paths = #{paths_json};diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rbindex 8e679a84881..e5ff6a736f3 100644--- a/spec/lib/pretty_text_spec.rb+++ b/spec/lib/pretty_text_spec.rb@@ -1763,6 +1763,34 @@ RSpec.describe PrettyText doexpect(PrettyText.cook("hello :trout:")).to match(/<img src[^>]+trout[^>]+>/)end++ it "rewrites a custom emoji's S3 url through the configured CDN" do+ setup_s3+ SiteSetting.s3_cdn_url = "https://cdn.example.com"++ raw_url = "#{SiteSetting.Upload.absolute_base_url}/original/1X/trout.png"+ CustomEmoji.create!(name: "trout", upload: Fabricate(:upload, url: raw_url))+ Emoji.clear_cache++ cooked = PrettyText.cook("hello :trout:")+ expect(cooked).to include("https://cdn.example.com/original/1X/trout.png")+ expect(cooked).not_to include(raw_url)+ end++ it "rewrites a custom emoji's S3 url through the CDN when unescaping titles" do+ setup_s3+ SiteSetting.s3_cdn_url = "https://cdn.example.com"++ raw_url = "#{SiteSetting.Upload.absolute_base_url}/original/1X/trout.png"+ CustomEmoji.create!(name: "trout", upload: Fabricate(:upload, url: raw_url))+ Emoji.clear_cache++ unescaped = PrettyText.unescape_emoji("hello :trout:")+ expect(unescaped).to match(+ %r{<img[^>]+\bsrc=['"]https://cdn\.example\.com/original/1X/trout\.png},+ )+ expect(unescaped).not_to include(SiteSetting.Upload.absolute_base_url)+ endenddescribe "custom emoji translation" dodiff --git a/spec/models/emoji_spec.rb b/spec/models/emoji_spec.rbindex 9decb356c0e..919ee91d89d 100644--- a/spec/models/emoji_spec.rb+++ b/spec/models/emoji_spec.rb@@ -15,6 +15,56 @@ RSpec.describe Emoji doexpect(emoji.name).to eq("test")expect(emoji.url).to be_nilend++ it "caches the raw upload url, not the CDN-transformed one" do+ upload = Fabricate(:upload, url: "//my-bucket.s3.amazonaws.com/images/my-emoji.png")+ CustomEmoji.create!(name: "my_s3_emoji", upload: upload)++ Emoji.clear_cache+ emoji = Emoji.load_custom.find { |e| e.name == "my_s3_emoji" }++ # The cached object must hold the raw url so that later CDN setting+ # changes are reflected (CDN is applied lazily via #cdn_url).+ expect(emoji.url).to eq(upload.url)+ end+ end++ describe "#cdn_url" do+ it "returns nil when there is no url" do+ expect(Emoji.new.cdn_url).to be_nil+ end++ context "with a configured S3 store and CDN" do+ before do+ setup_s3+ SiteSetting.s3_cdn_url = "https://cdn.example.com"+ end++ def custom_emoji_for(raw_url)+ upload = Fabricate(:upload, url: raw_url)+ CustomEmoji.create!(name: "my_s3_emoji", upload: upload)+ Emoji.clear_cache+ Emoji.load_custom.find { |e| e.name == "my_s3_emoji" }+ end++ it "rewrites a schemaless S3 bucket url to the configured s3_cdn_url" do+ raw_url = "#{SiteSetting.Upload.absolute_base_url}/original/1X/my-emoji.png"+ emoji = custom_emoji_for(raw_url)++ # the cache still holds the raw bucket url...+ expect(emoji.url).to eq(raw_url)+ # ...and the CDN conversion happens lazily on read.+ expect(emoji.cdn_url).to eq("https://cdn.example.com/original/1X/my-emoji.png")+ end++ it "does not leak the bucket subfolder into the rewritten url" do+ SiteSetting.s3_upload_bucket = "s3-upload-bucket/emojis"+ raw_url = "#{SiteSetting.Upload.absolute_base_url}/emojis/original/1X/my-emoji.png"+ emoji = custom_emoji_for(raw_url)++ expect(emoji.cdn_url).to eq("https://cdn.example.com/original/1X/my-emoji.png")+ end+ endenddescribe ".unicode_replacements" do