Skip to content

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

SECURITY: Escape upload filenames in chat message excerpts

Isaac Janzen24cd7cc4c868
diff --git a/plugins/chat/app/models/chat/message.rb b/plugins/chat/app/models/chat/message.rb
index 00bd5ff3421..10ce10cf319 100644
--- a/plugins/chat/app/models/chat/message.rb
+++ b/plugins/chat/app/models/chat/message.rb
@@ -179,14 +179,20 @@ module Chat
end
# upload-only messages are better represented as the filename
- return uploads.first.original_filename if cooked.blank? && uploads.present?
+ return upload_filename_excerpt if cooked.blank? && uploads.present?
# this may return blank for some complex things like quotes, that is acceptable
PrettyText.excerpt(cooked, EXCERPT_LENGTH, strip_links:, keep_mentions: true)
end
+ def excerpt_for_display
+ return upload_filename_excerpt if only_uploads?
+
+ excerpt || build_excerpt
+ end
+
def cooked_for_excerpt
- (cooked.blank? && uploads.present?) ? "<p>#{uploads.first.original_filename}</p>" : cooked
+ (cooked.blank? && uploads.present?) ? "<p>#{upload_filename_excerpt}</p>" : cooked
end
def push_notification_excerpt
@@ -415,6 +421,10 @@ module Chat
private
+ def upload_filename_excerpt
+ ERB::Util.html_escape(uploads.first.original_filename.truncate(EXCERPT_LENGTH, omission: ""))
+ end
+
def urls_as_links(urls)
html =
urls.map { |url| "<a href=\"#{CGI.escapeHTML(url)}\">#{CGI.escapeHTML(url)}</a>" }.join(" ")
diff --git a/plugins/chat/app/models/chat/thread.rb b/plugins/chat/app/models/chat/thread.rb
index edb06582e4b..47255125523 100644
--- a/plugins/chat/app/models/chat/thread.rb
+++ b/plugins/chat/app/models/chat/thread.rb
@@ -109,7 +109,7 @@ module Chat
end
def excerpt
- original_message.excerpt
+ original_message.excerpt_for_display
end
def update_last_message_id!
diff --git a/plugins/chat/app/serializers/chat/in_reply_to_serializer.rb b/plugins/chat/app/serializers/chat/in_reply_to_serializer.rb
index 1a447350af8..ce7ff2b3dec 100644
--- a/plugins/chat/app/serializers/chat/in_reply_to_serializer.rb
+++ b/plugins/chat/app/serializers/chat/in_reply_to_serializer.rb
@@ -12,7 +12,7 @@ module Chat
end
def excerpt
- object.excerpt || object.build_excerpt
+ object.excerpt_for_display
end
end
end
diff --git a/plugins/chat/app/serializers/chat/last_message_serializer.rb b/plugins/chat/app/serializers/chat/last_message_serializer.rb
index 625d365054f..e906d56499f 100644
--- a/plugins/chat/app/serializers/chat/last_message_serializer.rb
+++ b/plugins/chat/app/serializers/chat/last_message_serializer.rb
@@ -3,13 +3,17 @@
module Chat
class LastMessageSerializer < ::ApplicationSerializer
# NOTE: The channel last message does not need to serialize relations
- # etc. at this point in time, since the only thing we are using is
- # created_at. In future we may want to serialize more for this, at which
+ # etc. at this point in time, since the only things we are using are
+ # created_at and excerpt. In future we may want to serialize more, at which
# point we need to check existing code so we don't introduce N1s.
attributes *Chat::MessageSerializer::BASIC_ATTRIBUTES
def created_at
object.created_at.iso8601
end
+
+ def excerpt
+ object.excerpt_for_display
+ end
end
end
diff --git a/plugins/chat/app/serializers/chat/message_serializer.rb b/plugins/chat/app/serializers/chat/message_serializer.rb
index 449be50f917..5409d64ab17 100644
--- a/plugins/chat/app/serializers/chat/message_serializer.rb
+++ b/plugins/chat/app/serializers/chat/message_serializer.rb
@@ -60,7 +60,7 @@ module Chat
end
def excerpt
- object.excerpt || object.build_excerpt
+ object.excerpt_for_display
end
def is_action
diff --git a/plugins/chat/app/serializers/chat/thread_original_message_serializer.rb b/plugins/chat/app/serializers/chat/thread_original_message_serializer.rb
index 953e9418dfe..8909fd0a4b5 100644
--- a/plugins/chat/app/serializers/chat/thread_original_message_serializer.rb
+++ b/plugins/chat/app/serializers/chat/thread_original_message_serializer.rb
@@ -13,7 +13,7 @@ module Chat
:user
def excerpt
- object.excerpt || object.build_excerpt
+ object.excerpt_for_display
end
def mentioned_users
diff --git a/plugins/chat/app/serializers/chat/thread_preview_serializer.rb b/plugins/chat/app/serializers/chat/thread_preview_serializer.rb
index 3a5c5db7f52..6a2384ce7be 100644
--- a/plugins/chat/app/serializers/chat/thread_preview_serializer.rb
+++ b/plugins/chat/app/serializers/chat/thread_preview_serializer.rb
@@ -28,7 +28,7 @@ module Chat
end
def last_reply_excerpt
- object.last_message.excerpt || object.last_message.build_excerpt
+ object.last_message.excerpt_for_display
end
def last_reply_user
diff --git a/plugins/chat/lib/chat/channel_fetcher.rb b/plugins/chat/lib/chat/channel_fetcher.rb
index d4b133c3ab0..ff9286509e3 100644
--- a/plugins/chat/lib/chat/channel_fetcher.rb
+++ b/plugins/chat/lib/chat/channel_fetcher.rb
@@ -104,7 +104,7 @@ module Chat
channels =
Chat::Channel.includes(
- :last_message,
+ last_message: [:uploads],
chatable: %i[
topic_only_relative_url
uploaded_background
diff --git a/plugins/chat/lib/discourse_workflows/nodes/chat_message_created/v1.rb b/plugins/chat/lib/discourse_workflows/nodes/chat_message_created/v1.rb
index ca1cd0ea76e..83b3e5f2f81 100644
--- a/plugins/chat/lib/discourse_workflows/nodes/chat_message_created/v1.rb
+++ b/plugins/chat/lib/discourse_workflows/nodes/chat_message_created/v1.rb
@@ -122,7 +122,7 @@ if defined?(DiscourseWorkflows)
id: @message.id,
message: @message.message,
cooked: @message.cooked,
- excerpt: @message.excerpt || @message.build_excerpt,
+ excerpt: @message.excerpt_for_display,
created_at: @message.created_at.iso8601,
thread_id: @message.thread_id,
chat_channel_id: @message.chat_channel_id,
diff --git a/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb b/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb
index c060ddf3a25..b46c8b32496 100644
--- a/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb
+++ b/plugins/chat/spec/lib/chat/channel_fetcher_spec.rb
@@ -200,6 +200,20 @@ describe Chat::ChannelFetcher do
).to match_array([category_channel.id])
end
+ it "preloads last-message uploads" do
+ another_channel = Fabricate(:category_channel)
+ first_message = Fabricate(:chat_message, chat_channel: category_channel, user: user2)
+ second_message = Fabricate(:chat_message, chat_channel: another_channel, user: user2)
+ category_channel.update!(last_message: first_message)
+ another_channel.update!(last_message: second_message)
+
+ channels = described_class.secured_public_channels(guardian, following: following)
+
+ expect(channels.map { |channel| channel.last_message.association(:uploads).loaded? }).to all(
+ eq(true),
+ )
+ end
+
it "returns an empty array when public channels are disabled" do
SiteSetting.enable_public_channels = false
diff --git a/plugins/chat/spec/mailers/user_notifications_spec.rb b/plugins/chat/spec/mailers/user_notifications_spec.rb
index e4ce4c731ef..831719afe00 100644
--- a/plugins/chat/spec/mailers/user_notifications_spec.rb
+++ b/plugins/chat/spec/mailers/user_notifications_spec.rb
@@ -91,6 +91,21 @@ describe UserNotifications do
expect(html).to include(I18n.t("user_notifications.chat_summary.view_messages", count: 1))
end
+ it "renders upload filenames as text in chat summary emails" do
+ upload =
+ Fabricate(
+ :upload,
+ original_filename: "<svg data-chat-summary-filename-xss='true'></svg>.png",
+ )
+ chat_mention.update!(message: "", cooked: "", uploads: [upload])
+
+ html = chat_summary_email.html_part.body.to_s
+ document = Nokogiri::HTML5.fragment(html)
+
+ expect(document.text).to include(upload.original_filename)
+ expect(document.css("svg[data-chat-summary-filename-xss='true']")).to be_empty
+ end
+
it "sends a chat summary email with view more link" do
create_message(followed_channel, "how are you...")
create_message(followed_channel, "doing...")
diff --git a/plugins/chat/spec/models/chat/message_spec.rb b/plugins/chat/spec/models/chat/message_spec.rb
index 659e88ca492..ba0b2a65c73 100644
--- a/plugins/chat/spec/models/chat/message_spec.rb
+++ b/plugins/chat/spec/models/chat/message_spec.rb
@@ -683,6 +683,33 @@ describe Chat::Message do
expect(message.build_excerpt).to eq "cat.gif"
end
+ it "escapes upload filenames in excerpts" do
+ upload = Fabricate(:upload, original_filename: "<svg onload=alert(1)>.png")
+ message = Fabricate(:chat_message, message: "", uploads: [upload])
+
+ expect(message.build_excerpt).to eq "&lt;svg onload=alert(1)&gt;.png"
+ end
+
+ it "bounds escaped upload filenames before saving excerpts" do
+ upload = Fabricate(:upload, original_filename: "#{"&" * 250}.png")
+ message = Fabricate(:chat_message, message: "", cooked: "", uploads: [upload])
+ excerpt = message.build_excerpt
+
+ message.update!(excerpt:)
+
+ expect(excerpt.length).to be <= 1000
+ expect(excerpt).to eq("&amp;" * described_class::EXCERPT_LENGTH)
+ expect(message.reload.excerpt).to eq(excerpt)
+ end
+
+ it "escapes persisted upload filenames for display" do
+ upload = Fabricate(:upload, original_filename: "<svg onload=alert(1)>.png")
+ message = Fabricate(:chat_message, message: "", cooked: "", uploads: [upload])
+ message.update!(excerpt: upload.original_filename)
+
+ expect(message.excerpt_for_display).to eq "&lt;svg onload=alert(1)&gt;.png"
+ end
+
it "supports autolink with <>" do
cooked = described_class.cook("<https://github.com/discourse/discourse-chat/pull/468>")
diff --git a/plugins/chat/spec/requests/chat/api/current_user_channels_spec.rb b/plugins/chat/spec/requests/chat/api/current_user_channels_spec.rb
index ab2f5cd7272..3c30df12f9e 100644
--- a/plugins/chat/spec/requests/chat/api/current_user_channels_spec.rb
+++ b/plugins/chat/spec/requests/chat/api/current_user_channels_spec.rb
@@ -86,6 +86,27 @@ describe Chat::Api::CurrentUserChannelsController do
expect(response.parsed_body["public_channels"][0]["id"]).to eq(channel.id)
end
+ it "escapes legacy upload filenames in last-message excerpts" do
+ channel = Fabricate(:category_channel)
+ channel.add(current_user)
+ upload =
+ Fabricate(
+ :upload,
+ original_filename: "<svg data-chat-channel-list-filename-xss='true'></svg>.png",
+ )
+ message = Fabricate(:chat_message, message: "", cooked: "", uploads: [upload])
+ message.update!(excerpt: upload.original_filename)
+ channel.update!(last_message: message)
+
+ get "/chat/api/me/channels"
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["public_channels"][0].dig("last_message", "excerpt")).to eq(
+ "&lt;svg data-chat-channel-list-filename-xss=&#39;true&#39;&gt;&lt;/svg&gt;.png",
+ )
+ expect(response.body).not_to include(upload.original_filename)
+ end
+
context "with multiple direct messages" do
fab!(:user_1, :user)
fab!(:user_2, :user)
diff --git a/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb b/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb
index b31669700e7..f8b8e4d432a 100644
--- a/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb
+++ b/plugins/chat/spec/serializer/chat/chat_message_serializer_spec.rb
@@ -86,6 +86,15 @@ describe Chat::MessageSerializer do
expect(serializer.as_json[:excerpt]).to eq("ok ■■■■■")
end
+
+ it "escapes persisted upload filenames" do
+ upload = Fabricate(:upload, original_filename: "<svg onload=alert(1)>.png")
+ message = Fabricate(:chat_message, message: "", cooked: "", uploads: [upload])
+ message.update!(excerpt: upload.original_filename)
+ serializer = described_class.new(message, scope: guardian, root: nil)
+
+ expect(serializer.as_json[:excerpt]).to eq("&lt;svg onload=alert(1)&gt;.png")
+ end
end
describe "#is_action" do
diff --git a/plugins/chat/spec/system/pinned_messages_spec.rb b/plugins/chat/spec/system/pinned_messages_spec.rb
index 5019da2384e..bc24c041414 100644
--- a/plugins/chat/spec/system/pinned_messages_spec.rb
+++ b/plugins/chat/spec/system/pinned_messages_spec.rb
@@ -51,6 +51,36 @@ RSpec.describe "Chat pinned messages" do
expect(page).to have_no_css(".chat-pinned-bar")
end
+ it "escapes upload filenames in the pinned message bar" do
+ SiteSetting.content_security_policy = false
+ attacker = Fabricate(:user)
+ victim = Fabricate(:user)
+ upload =
+ Fabricate(
+ :upload,
+ user: attacker,
+ original_filename: %q(<svg onload="this.dataset.pinnedFilenameXss='true'"></svg>.png),
+ )
+ message.update!(
+ user: attacker,
+ message: "",
+ cooked: "",
+ excerpt: upload.original_filename,
+ uploads: [upload],
+ )
+ channel.add(attacker)
+ channel.add(victim)
+ Chat::PinnedMessage.create!(chat_message: message, chat_channel: channel, user: admin)
+
+ sign_in(victim)
+ chat_page.visit_channel(channel)
+
+ expect(page).to have_css(".chat-pinned-bar__excerpt")
+ expect(page).to have_no_css(".chat-pinned-bar__excerpt svg[data-pinned-filename-xss='true']")
+ expect(page).to have_no_css(".chat-pinned-bar__excerpt svg[onload]")
+ expect(find(".chat-pinned-bar__excerpt").native.inner_html).to include("&lt;svg")
+ end
+
it "shows unread indicator for unseen pins" do
pin_another_message # a self-pin is never unseen, and it brings out the list icon