A platform for community discussion. Free, open, simple.
SECURITY: Escape Markdown link-label delimiters in PM invitation email topic titles (#45036)
Isaac Janzen1f0c8155626b
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rbindex e490f2bb4a0..44c45982fb9 100644--- a/app/mailers/user_notifications.rb+++ b/app/mailers/user_notifications.rb@@ -851,14 +851,29 @@ class UserNotifications < ActionMailer::Baseinvite_template,username: username,group_name: group_name,- topic_title: ERB::Util.html_escape(gsub_emoji_to_unicode(title)),+ topic_title: "%{topic_title}",topic_excerpt: topic_excerpt,site_title: SiteSetting.title,site_description: SiteSetting.site_description,topic_url: topic_url,)- html = PrettyText.cook(message, sanitize: false).html_safe+ # the title is substituted after cooking so it always renders as literal+ # text and can never alter the template's Markdown structure+ topic_title_text = gsub_emoji_to_unicode(title).to_s+ html =+ PrettyText+ .cook(message, sanitize: false)+ .gsub("%{topic_title}") { ERB::Util.html_escape(topic_title_text) }+ .html_safe+ # Customized bodies cook both %{message} and %{topic_title} as Markdown.+ # Keep the subject unchanged, but use the same escaped title in both body values.+ if translation_override_exists+ body_topic_title =+ topic_title_text.gsub(/[!"\#$%&'()*+,\-.\/:;<=>?@\[\]\\^_`{|}~]/) { |char| "\\#{char}" }+ topic_title_text = ERB::Util.html_escape(body_topic_title)+ end+ message = message.gsub("%{topic_title}") { topic_title_text }elsereached_limit = SiteSetting.max_emails_per_day_per_user > 0reached_limit &&=@@ -935,6 +950,7 @@ class UserNotifications < ActionMailer::Baselocale: locale,}+ email_opts[:body_topic_title] = body_topic_title if body_topic_titleemail_opts[:recipient_user] = useremail_opts[:html_override] = html unless translation_override_existsdiff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rbindex d3d8e1d8e56..b9b92e6cf2b 100644--- a/lib/email/message_builder.rb+++ b/lib/email/message_builder.rb@@ -211,6 +211,10 @@ module Email@template_args[key] = escaped_template_arg(key) if @template_args.key?(key)end+ if @opts.key?(:body_topic_title)+ @template_args[:topic_title] = ERB::Util.html_escape(@opts[:body_topic_title])+ end+augmented_template_args =@template_args.merge(optional_re: "",diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rbindex ee26d4dc298..00274303150 100644--- a/spec/mailers/user_notifications_spec.rb+++ b/spec/mailers/user_notifications_spec.rb@@ -1909,7 +1909,7 @@ RSpec.describe UserNotifications doendend- shared_examples "notification email building" do+ shared_examples "notification email building" do |invitation: false|let(:post) { Fabricate(:post, user: user) }let(:mail_type) { "user_#{notification_type}" }let(:mail_template) { "user_notifications.#{mail_type}" }@@ -1983,6 +1983,53 @@ RSpec.describe UserNotifications doexpects_build_with(has_entry(:include_respond_instructions, false))end+ if invitation+ {+ "the default body" => nil,+ "a customized message" => "%{message}",+ "a customized title and message" => "%{topic_title}\n\n%{message}",+ }.each do |description, custom_body|+ context "with #{description}" do+ before do+ if custom_body+ TranslationOverride.upsert!(+ SiteSetting.default_locale,+ "#{mail_template}.text_body_template",+ custom_body,+ )+ TranslationOverride.upsert!(+ SiteSetting.default_locale,+ "#{mail_template}.subject_template",+ "Invitation: %{topic_title}",+ )+ end+ end++ it "preserves the literal title and invitation link without changing the subject" do+ title = 'Planning *notes* `draft` [agenda] (updated) \ & <details>'+ post.topic.update!(title: title)+ notification.data_hash[:topic_title] = title+ mail =+ UserNotifications.public_send(+ mail_type,+ user,+ notification_type: notification_type,+ notification_data_hash: notification.data_hash,+ post: post,+ )+ html = Nokogiri::HTML5.fragment(Email::Renderer.new(mail).html)++ expect(mail.subject).to include(title)+ expect(html.css("a[href='#{post.topic.url}']").map(&:text)).to include(title)+ expect(html.css("em, code, details")).to be_empty+ expect(html.text.scan(title).size).to eq(+ custom_body&.include?("%{topic_title}") ? 2 : 1,+ )+ end+ end+ end+ end+context "when customized" dolet(:custom_body) dobody = +<<~BODY@@ -2135,7 +2182,7 @@ RSpec.describe UserNotifications doenddescribe "user invited to a private message" do- include_examples "notification email building" do+ include_examples "notification email building", invitation: true dolet(:notification_type) { :invited_to_private_message }let(:post) { Fabricate(:private_message_post) }let(:user) { post.user }@@ -2148,7 +2195,7 @@ RSpec.describe UserNotifications doenddescribe "group invited to a private message" do- include_examples "notification email building" do+ include_examples "notification email building", invitation: true dolet(:notification_type) { :invited_to_private_message }let(:post) { Fabricate(:private_message_post) }let(:user) { post.user }@@ -2173,7 +2220,7 @@ RSpec.describe UserNotifications dodescribe "user invited to a topic" dolet(:notification_type) { :invited_to_topic }- include_examples "notification email building" do+ include_examples "notification email building", invitation: true doinclude_examples "respect for private_email"include_examples "no reply by email"include_examples "sets user locale"