Skip to content

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

SECURITY: XSS in review queue via `highlightWatchedWords`

Keegan Georgeab402b7aeef4
diff --git a/app/controllers/reviewables_controller.rb b/app/controllers/reviewables_controller.rb
index e6d741c939e..bee12fde0a1 100644
--- a/app/controllers/reviewables_controller.rb
+++ b/app/controllers/reviewables_controller.rb
@@ -241,6 +241,12 @@ class ReviewablesController < ApplicationController
begin
if reviewable.update_fields(edit_params, current_user, version: params[:version].to_i)
result = edit_params.merge(version: reviewable.version)
+ if reviewable.is_a?(ReviewableQueuedPost) && reviewable.payload.present?
+ result[:cooked] = PrettyText.cook(reviewable.payload["raw"]) if reviewable.payload["raw"]
+ if reviewable.payload["title"]
+ result[:fancy_title] = ERB::Util.html_escape(reviewable.payload["title"])
+ end
+ end
render json: result
else
render_json_error(reviewable.errors)
diff --git a/app/serializers/reviewable_queued_post_serializer.rb b/app/serializers/reviewable_queued_post_serializer.rb
index 94a7918a238..8b5ba194dce 100644
--- a/app/serializers/reviewable_queued_post_serializer.rb
+++ b/app/serializers/reviewable_queued_post_serializer.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class ReviewableQueuedPostSerializer < ReviewableSerializer
- attributes :reply_to_post_number
+ attributes :reply_to_post_number, :fancy_title, :cooked
payload_attributes(
:raw,
@@ -20,6 +20,14 @@ class ReviewableQueuedPostSerializer < ReviewableSerializer
:raw_email,
)
+ def fancy_title
+ ERB::Util.html_escape(object.payload["title"]) if object.payload&.[]("title")
+ end
+
+ def cooked
+ PrettyText.cook(object.payload["raw"]) if object.payload&.[]("raw")
+ end
+
def reply_to_post_number
object.payload["reply_to_post_number"].to_i
end
diff --git a/frontend/discourse/app/components/reviewable/queued-post.gjs b/frontend/discourse/app/components/reviewable/queued-post.gjs
index 4441d4ffea8..8c6439f8691 100644
--- a/frontend/discourse/app/components/reviewable/queued-post.gjs
+++ b/frontend/discourse/app/components/reviewable/queued-post.gjs
@@ -1,9 +1,7 @@
import Component from "@glimmer/component";
-import { hash } from "@ember/helper";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { service } from "@ember/service";
-import CookText from "discourse/components/cook-text";
import RawEmailModal from "discourse/components/modal/raw-email";
import PluginOutlet from "discourse/components/plugin-outlet";
import ReviewableCreatedBy from "discourse/components/reviewable/created-by";
@@ -35,7 +33,7 @@ export default class ReviewableQueuedPost extends Component {
<div class="review-item__meta-topic-title">
<ReviewableTopicLink @reviewable={{@reviewable}}>
<div class="title-text">
- {{highlightWatchedWords @reviewable.payload.title @reviewable}}
+ {{highlightWatchedWords @reviewable.fancy_title @reviewable}}
</div>
{{categoryBadge @reviewable.category}}
<ReviewableTags @tags={{@reviewable.payload.tags}} />
@@ -56,14 +54,9 @@ export default class ReviewableQueuedPost extends Component {
<div class="review-item__post">
<div class="review-item__post-content">
- <CookText
- class="post-body"
- @rawText={{highlightWatchedWords @reviewable.payload.raw @reviewable}}
- @categoryId={{@reviewable.category_id}}
- @topicId={{@reviewable.topic_id}}
- @paintOneboxes={{false}}
- @opts={{hash removeMissing=true}}
- />
+ <div class="post-body">
+ {{highlightWatchedWords @reviewable.cooked @reviewable}}
+ </div>
<span>
<PluginOutlet
diff --git a/frontend/discourse/app/lib/highlight-watched-words.js b/frontend/discourse/app/lib/highlight-watched-words.js
index 6a5c3e7f8b5..a95731f6ef5 100644
--- a/frontend/discourse/app/lib/highlight-watched-words.js
+++ b/frontend/discourse/app/lib/highlight-watched-words.js
@@ -1,5 +1,4 @@
import { trustHTML } from "@ember/template";
-import { decode } from "html-entities";
import highlightHTML from "discourse/lib/highlight-html";
export default function (text, reviewable) {
@@ -31,5 +30,5 @@ export default function (text, reviewable) {
});
});
- return trustHTML(decode(elem.innerHTML));
+ return trustHTML(elem.innerHTML);
}
diff --git a/frontend/discourse/tests/helpers/review-pretender.js b/frontend/discourse/tests/helpers/review-pretender.js
index 5a276f94b5d..fe7b211f098 100644
--- a/frontend/discourse/tests/helpers/review-pretender.js
+++ b/frontend/discourse/tests/helpers/review-pretender.js
@@ -41,6 +41,8 @@ export default function (helpers) {
raw: "existing body\n\nhttp://somegoodurl.com",
tags: ["hello", "world"],
},
+ fancy_title: "<p>existing body</p>",
+ cooked: "<p>existing body</p>\n\n<p>http://somegoodurl.com</p>",
version: 1,
can_edit: true,
editable_fields: [
@@ -277,6 +279,9 @@ export default function (helpers) {
Object.entries(JSON.parse(request.requestBody).reviewable).forEach((t) => {
set(result, t[0], t[1]);
});
+ if (result.payload?.raw) {
+ result.cooked = `<p>${result.payload.raw}</p>`;
+ }
return response(200, result);
});
}
diff --git a/frontend/discourse/tests/unit/lib/highlight-watched-words-test.js b/frontend/discourse/tests/unit/lib/highlight-watched-words-test.js
index c6e48416dc1..d84d5840391 100644
--- a/frontend/discourse/tests/unit/lib/highlight-watched-words-test.js
+++ b/frontend/discourse/tests/unit/lib/highlight-watched-words-test.js
@@ -97,8 +97,28 @@ module("Unit | Utility | highlight-watched-words", function (hooks) {
assert.strictEqual(
highlighted.toString(),
- '<mark class="watched-word-highlight">This</mark> is some <mark class="watched-word-highlight">தமிழ் & русский</mark> text to <mark class="watched-word-highlight">highlight</mark>',
+ '<mark class="watched-word-highlight">This</mark> is some <mark class="watched-word-highlight">தமிழ் &amp; русский</mark> text to <mark class="watched-word-highlight">highlight</mark>',
"it should highlight the term correctly"
);
});
+
+ test("highlights watched words in cooked content", function (assert) {
+ const text = "<b>cooked content</b>";
+ const reviewable = {
+ reviewable_scores: [
+ {
+ reason_type: "watched_word",
+ reason_data: ["cooked"],
+ },
+ ],
+ };
+
+ const highlighted = highlightWatchedWords(text, reviewable);
+
+ assert.strictEqual(
+ highlighted.toString(),
+ '<b><mark class="watched-word-highlight">cooked</mark> content</b>',
+ "it should preserve HTML in cooked content and highlight watched words"
+ );
+ });
});
diff --git a/spec/system/reviewables_spec.rb b/spec/system/reviewables_spec.rb
index 8dbe0d28e3f..8293819c76f 100644
--- a/spec/system/reviewables_spec.rb
+++ b/spec/system/reviewables_spec.rb
@@ -309,4 +309,82 @@ describe "Reviewables", type: :system do
)
end
end
+
+ describe "XSS prevention in queued post titles via server-side cooking" do
+ fab!(:untrusted_user) { Fabricate(:user, trust_level: 0) }
+
+ before do
+ SiteSetting.approve_post_count = 1
+ sign_in(admin)
+ end
+
+ it "prevents stored XSS in topic title when viewing review queue" do
+ xss_payload = '<img src=x onerror="alert(\'XSS\')">'
+ reviewable =
+ ReviewableQueuedPost.needs_review!(
+ target_created_by: untrusted_user,
+ created_by: untrusted_user,
+ payload: {
+ raw: "This is the post body",
+ title: xss_payload,
+ },
+ )
+
+ visit("/review")
+
+ # The title should be visible as text but not execute
+ expect(page).to have_no_css("img[src='x']")
+ expect(page).to have_no_css("img[onerror]")
+
+ # Verify the XSS payload is escaped in the HTML
+ title_element = page.find(".title-text", match: :first)
+ title_html = title_element.native.inner_html
+ expect(title_html).to include("&lt;img")
+ expect(title_html).to include("&gt;")
+ expect(title_html).not_to include("<img src=x onerror")
+ end
+
+ it "prevents stored XSS with script tags in topic title" do
+ xss_payload = '<script>alert("XSS")</script>Malicious Title'
+ reviewable =
+ ReviewableQueuedPost.needs_review!(
+ target_created_by: untrusted_user,
+ created_by: untrusted_user,
+ payload: {
+ raw: "This is the post body",
+ title: xss_payload,
+ },
+ )
+
+ visit("/review")
+
+ expect(page).to have_no_css("script")
+ title_element = page.find(".title-text", match: :first)
+ title_html = title_element.native.inner_html
+ expect(title_html).to include("&lt;script&gt;")
+ expect(title_html).not_to include("<script>alert")
+ end
+
+ it "escapes special characters in title" do
+ special_chars_title = "Test & <b>Bold</b> & \"Quotes\" & 'Apostrophes'"
+ reviewable =
+ ReviewableQueuedPost.needs_review!(
+ target_created_by: untrusted_user,
+ created_by: untrusted_user,
+ payload: {
+ raw: "This is the post body",
+ title: special_chars_title,
+ },
+ )
+
+ visit("/review")
+
+ # The <b> tag should not render as bold
+ expect(page).to have_no_css(".title-text b")
+ title_element = page.find(".title-text", match: :first)
+ title_html = title_element.native.inner_html
+ expect(title_html).to include("&amp;")
+ expect(title_html).to include("&lt;b&gt;")
+ end
+ end
end