Skip to content

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

SECURITY: Prevent RSS CDATA termination via user-controlled post content [pt-f01cfe0b3534fc49352d] (#44709)

Bannon Tanner821e9e2f9f94
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 0eafeca13ee..eb1cca3e605 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1077,6 +1077,10 @@ module ApplicationHelper
user&.display_name
end
+ def rss_cdata(value)
+ value.to_s.gsub("]]>", "]]]]><![CDATA[>").html_safe
+ end
+
def anonymous_top_menu_items
Discourse.anonymous_top_menu_items.map(&:to_s)
end
diff --git a/app/views/badges/show.rss.erb b/app/views/badges/show.rss.erb
index 23428f6fccd..3b2b5aa241a 100644
--- a/app/views/badges/show.rss.erb
+++ b/app/views/badges/show.rss.erb
@@ -5,14 +5,14 @@
<% site_email = SiteSetting.find_by_name('contact_email').try(:value) %>
<title><%= @rss_title %></title>
<link><%= @rss_link %></link>
- <description><![CDATA[<%= @rss_description %>]]></description>
+ <description><![CDATA[<%= rss_cdata(@rss_description) %>]]></description>
<% if @badge.user_badges.first.present? %>
<atom:link href="<%= "#{@rss_link}.rss" %>" rel="self" type="application/rss+xml" />
<% @badge.user_badges.joins(:user).last(50).reverse_each do |user_badge| %>
<item>
- <title><![CDATA[<%= "@#{user_badge.user.username}#{" #{user_badge.user.name}" if (user_badge.user.name.present? && SiteSetting.enable_names?)}" -%>]]></title>
+ <title><![CDATA[<%= rss_cdata("@#{user_badge.user.username}#{" #{user_badge.user.name}" if (user_badge.user.name.present? && SiteSetting.enable_names?)}") -%>]]></title>
<discourse:grantedAt><%= user_badge.granted_at.rfc2822 %></discourse:grantedAt>
- <discourse:grantedBy><![CDATA[<%= "@#{user_badge.granted_by.username}#{" #{user_badge.granted_by.name}" if (user_badge.granted_by.name.present? && SiteSetting.enable_names?)}" -%>]]></discourse:grantedBy>
+ <discourse:grantedBy><![CDATA[<%= rss_cdata("@#{user_badge.granted_by.username}#{" #{user_badge.granted_by.name}" if (user_badge.granted_by.name.present? && SiteSetting.enable_names?)}") -%>]]></discourse:grantedBy>
<guid><%= "#{@rss_link}?username=#{user_badge.user.username}" %></guid>
</item>
<% end %>
diff --git a/app/views/list/list.rss.erb b/app/views/list/list.rss.erb
index c29c90afd4a..b6289e330b1 100644
--- a/app/views/list/list.rss.erb
+++ b/app/views/list/list.rss.erb
@@ -15,11 +15,11 @@
<% topic_url = topic.url -%>
<item>
<title><%= topic.title %></title>
- <dc:creator><![CDATA[<%= rss_creator(topic.user) -%>]]></dc:creator>
+ <dc:creator><![CDATA[<%= rss_cdata(rss_creator(topic.user)) -%>]]></dc:creator>
<category><%= topic.category.name %></category>
<description><![CDATA[
<%- if first_post = topic.ordered_posts.first %>
- <%= PrettyText.format_for_email(first_post.cooked, first_post).html_safe %>
+ <%= rss_cdata(PrettyText.format_for_email(first_post.cooked, first_post)) %>
<%- end %>
<p><small><%= t 'rss_num_posts', count: topic.posts_count %> - <%= t 'rss_num_participants', count: topic.participant_count %></small></p>
<p><%= link_to t('read_full_topic'), topic_url %></p>
diff --git a/app/views/posts/latest.rss.erb b/app/views/posts/latest.rss.erb
index 12bf699ca0c..b7fa7847a2d 100644
--- a/app/views/posts/latest.rss.erb
+++ b/app/views/posts/latest.rss.erb
@@ -10,8 +10,8 @@
<% next unless post.user %>
<item>
<title><%= post.topic.title %></title>
- <dc:creator><![CDATA[<%= "@#{post.user.username}#{" #{post.user.name}" if (post.user.name.present? && SiteSetting.enable_names?)}" -%>]]></dc:creator>
- <description><![CDATA[ <%= PrettyText.format_for_email(post.cooked, post).html_safe %> ]]></description>
+ <dc:creator><![CDATA[<%= rss_cdata("@#{post.user.username}#{" #{post.user.name}" if (post.user.name.present? && SiteSetting.enable_names?)}") -%>]]></dc:creator>
+ <description><![CDATA[ <%= rss_cdata(PrettyText.format_for_email(post.cooked, post)) %> ]]></description>
<link><%= cached_post_url(post, use_canonical: @use_canonical) %></link>
<pubDate><%= post.created_at.rfc2822 %></pubDate>
<guid isPermaLink="false"><%= Discourse.current_hostname %>-post-<%= post.id %></guid>
diff --git a/app/views/topics/show.rss.erb b/app/views/topics/show.rss.erb
index 1170d8dffc3..63e56c1f2cb 100644
--- a/app/views/topics/show.rss.erb
+++ b/app/views/topics/show.rss.erb
@@ -16,13 +16,13 @@
<% next unless post.user %>
<item>
<title><%= @topic_view.title %></title>
- <dc:creator><![CDATA[<%= rss_creator(post.user) -%>]]></dc:creator>
+ <dc:creator><![CDATA[<%= rss_cdata(rss_creator(post.user)) -%>]]></dc:creator>
<description><![CDATA[
<% post_url = Discourse.base_url + post.url %>
<% if post.hidden %>
- <%= t('flagging.user_must_edit').html_safe %>
+ <%= rss_cdata(t('flagging.user_must_edit')) %>
<% else %>
- <%= PrettyText.format_for_email(post.cooked, post).html_safe %>
+ <%= rss_cdata(PrettyText.format_for_email(post.cooked, post)) %>
<% end %>
<p><%= link_to t('read_full_topic'), post_url %></p>
]]></description>
diff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rb
index 257d1d8e046..098b68b6721 100644
--- a/spec/requests/list_controller_spec.rb
+++ b/spec/requests/list_controller_spec.rb
@@ -799,6 +799,32 @@ RSpec.describe ListController do
expect(response.headers["X-Robots-Tag"]).to eq("noindex")
end
+ it "does not allow a topic author to forge RSS items" do
+ attacker = Fabricate(:user, refresh_auto_groups: true)
+ forged_title = "Forged RSS item"
+
+ sign_in(attacker)
+ post "/posts.json",
+ params: {
+ title: "Actual RSS topic title",
+ raw:
+ "[link](https://example.com \"]]></description></item><item><title>#{forged_title}</title><description><![CDATA[\")",
+ category: topic.category_id,
+ }
+
+ expect(response.status).to eq(200)
+
+ sign_out
+ get "/latest.rss"
+
+ document = Nokogiri.XML(response.body) { |config| config.strict }
+ titles = document.xpath("/rss/channel/item/title").map(&:text)
+
+ expect(response.status).to eq(200)
+ expect(titles).to include("Actual RSS topic title")
+ expect(titles).not_to include(forged_title)
+ end
+
it "renders latest RSS with query params" do
get "/latest.rss?status=closed"
expect(response.status).to eq(200)