Skip to content

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

SECURITY: Stored XSS via Shared AI Conversation Onebox

Roman Rizzic14f8f52b799
diff --git a/plugins/discourse-ai/app/models/shared_ai_conversation.rb b/plugins/discourse-ai/app/models/shared_ai_conversation.rb
index 59b15d67597..e99bdcf0e24 100644
--- a/plugins/discourse-ai/app/models/shared_ai_conversation.rb
+++ b/plugins/discourse-ai/app/models/shared_ai_conversation.rb
@@ -91,8 +91,9 @@ class SharedAiConversation < ActiveRecord::Base
html = +""
populated_context.each do |post|
text = PrettyText.excerpt(post.cooked, 400, strip_links: true, strip_details: true)
+ username = ERB::Util.html_escape(post.user.username)
- html << "<p><b>#{post.user.username}</b>: #{text}</p>"
+ html << "<p><b>#{username}</b>: #{text}</p>"
if html.length > 1000
html << "<p>...</p>"
break
@@ -103,6 +104,7 @@ class SharedAiConversation < ActiveRecord::Base
end
def onebox
+ escaped_title = ERB::Util.html_escape(title)
<<~HTML
<div>
<aside class="onebox allowlistedgeneric" data-onebox-src="#{url}">
@@ -111,7 +113,7 @@ class SharedAiConversation < ActiveRecord::Base
<a href="#{url}" target="_blank" rel="nofollow ugc noopener" tabindex="-1">#{Discourse.base_uri}</a>
</header>
<article class="onebox-body">
- <h3><a href="#{url}" rel="nofollow ugc noopener" tabindex="-1">#{title}</a></h3>
+ <h3><a href="#{url}" rel="nofollow ugc noopener" tabindex="-1">#{escaped_title}</a></h3>
#{html_excerpt}
</article>
<div style="clear: both"></div>
diff --git a/plugins/discourse-ai/spec/models/shared_ai_conversation_spec.rb b/plugins/discourse-ai/spec/models/shared_ai_conversation_spec.rb
index 08cd8b63ec0..bd31eb6afcb 100644
--- a/plugins/discourse-ai/spec/models/shared_ai_conversation_spec.rb
+++ b/plugins/discourse-ai/spec/models/shared_ai_conversation_spec.rb
@@ -117,4 +117,25 @@ RSpec.describe SharedAiConversation, type: :model do
expect(onebox).to include("AI Conversation with Claude-2")
end
end
+
+ describe "#onebox" do
+ it "escapes title and username" do
+ malicious_username = %(user"><img src=x onerror=alert(1)>)
+ malicious_title = %(title</a><script>alert("x")</script>)
+ user.update_columns(username: malicious_username, username_lower: malicious_username.downcase)
+ topic.update_column(:title, malicious_title)
+
+ Fabricate(:post, topic: topic, user: user, post_number: 3, raw: "safe post")
+
+ conversation = described_class.share_conversation(user, topic)
+
+ onebox = conversation.onebox
+
+ expect(onebox).to include("user&quot;&gt;&lt;img src=x onerror=alert(1)&gt;")
+ expect(onebox).to include("title&lt;/a&gt;&lt;script&gt;alert(&quot;x&quot;)&lt;/script&gt;")
+
+ expect(onebox).not_to include("<img src=x onerror=alert(1)>")
+ expect(onebox).not_to include(%(<script>alert("x")</script>))
+ end
+ end
end