Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: Escape topic titles in invitation emails (#44239)

Natalie Tay67bc74d0d83f
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 215773e5ef1..c33d7e0a201 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -727,7 +727,7 @@ class UserNotifications < ActionMailer::Base
invite_template,
username: username,
group_name: group_name,
- topic_title: gsub_emoji_to_unicode(title),
+ topic_title: ERB::Util.html_escape(gsub_emoji_to_unicode(title)),
topic_excerpt: topic_excerpt,
site_title: SiteSetting.title,
site_description: SiteSetting.site_description,
diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index cbe63f65d20..6cd532fcff4 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -1476,6 +1476,41 @@ RSpec.describe UserNotifications do
end
end
+ describe "invitation email titles" do
+ fab!(:inviter) { Fabricate(:user, trust_level: TrustLevel[2]) }
+ fab!(:invitee, :user)
+
+ %i[private_message topic].each do |destination|
+ it "renders HTML in a #{destination} title as text" do
+ post =
+ if destination == :private_message
+ Fabricate(:private_message_post, recipient: inviter)
+ else
+ Fabricate(:post)
+ end
+ post.topic.update!(title: %(<img src="x" onerror="alert(1)"> Meeting & planning))
+ post.topic.invite(inviter, invitee.username)
+ notification = invitee.notifications.find_by!(topic_id: post.topic_id)
+ notification_type = "invited_to_#{destination}"
+ mail =
+ UserNotifications.public_send(
+ "user_#{notification_type}",
+ invitee,
+ notification_type: notification_type,
+ notification_data_hash: notification.data_hash,
+ post: notification.post,
+ )
+ renderer = Email::Renderer.new(mail)
+ html = Nokogiri::HTML5.fragment(renderer.html)
+
+ expect(html.css("[onerror], img[src='x']")).to be_empty
+ expect(html.text).to include(post.topic.title)
+ expect(renderer.text).to include(post.topic.title)
+ expect(html.css("a").map { |link| link["href"] }).to include(post.topic.url)
+ end
+ end
+ end
+
describe "user invited to a private message" do
include_examples "notification email building" do
let(:notification_type) { :invited_to_private_message }