Skip to content

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

SECURITY: XSS on category description update via API

Martin Brennan05e3da2a670c
diff --git a/app/models/category.rb b/app/models/category.rb
index f473948c51b..6f322cc1679 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -111,6 +111,7 @@ class Category < ActiveRecord::Base
after_create :delete_category_permalink
after_update :rename_category_definition, if: :saved_change_to_name?
after_update :create_category_permalink, if: :saved_change_to_slug?
+ after_update :revise_category_definition, if: :saved_change_to_description?
after_update :run_plugin_category_update_param_callbacks
after_destroy :trash_category_definition
after_destroy :clear_related_site_settings
@@ -552,6 +553,15 @@ class Category < ActiveRecord::Base
end
end
+ def revise_category_definition
+ return if skip_category_definition
+ return if self.topic.blank? || self.topic.first_post.blank?
+
+ # NOTE: Revising the first post will also update the category description,
+ # see PostRevisor#update_category_description
+ self.topic.first_post.revise(self.user, { raw: self.description }, skip_validations: true)
+ end
+
def trash_category_definition
self.topic&.trash!
end
diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb
index d792b1a01b1..c44a91c0a7b 100644
--- a/spec/models/category_spec.rb
+++ b/spec/models/category_spec.rb
@@ -417,6 +417,36 @@ RSpec.describe Category do
end
end
+ describe "description sanitization" do
+ fab!(:admin)
+
+ it "sanitizes description to prevent XSS on create" do
+ category =
+ Category.create!(
+ name: "XSS Test Category",
+ user: admin,
+ description:
+ "This has <script>alert('xss')</script> and <img src=x onerror=alert('xss')>",
+ )
+
+ expect(category.description).not_to include("<script>")
+ expect(category.description).not_to include("&lt;script&gt;")
+ expect(category.description).to include("&lt;img")
+ end
+
+ it "sanitizes description to prevent XSS on update" do
+ category = Fabricate(:category_with_definition, user: admin)
+ category.update(
+ description: "This has <script>alert('xss')</script> and <img src=x onerror=alert('xss')>",
+ )
+
+ category.reload
+ expect(category.description).not_to include("<script>")
+ expect(category.description).not_to include("&lt;script&gt;")
+ expect(category.description).to include("&lt;img")
+ end
+ end
+
describe "after create" do
fab!(:category) { Fabricate(:category_with_definition, name: "Amazing Category") }
let(:topic) { category.topic }
diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb
index 6843e7bdb0b..cc9d2c5d1a0 100644
--- a/spec/requests/categories_controller_spec.rb
+++ b/spec/requests/categories_controller_spec.rb
@@ -574,23 +574,6 @@ RSpec.describe CategoriesController do
expect(category.topic.first_post.raw).to include("[link](https://example.com)")
end
- it "sanitizes description to prevent XSS" do
- post "/categories.json",
- params: {
- name: "XSS Test Category",
- description:
- "This has <script>alert('xss')</script> and <img src=x onerror=alert('xss')>",
- }
-
- expect(response.status).to eq(200)
- cat_json = response.parsed_body["category"]
-
- category = Category.find(cat_json["id"])
- expect(category.description).not_to include("<script>")
- expect(category.description).not_to include("&lt;script&gt;")
- expect(category.description).to include("&lt;img")
- end
-
describe "when category_type is provided" do
before { SiteSetting.enable_simplified_category_creation = true }
@@ -960,6 +943,19 @@ RSpec.describe CategoriesController do
expect(category.topic_title_placeholder).to eq("test topic title placeholder")
end
+ it "updates description and revises category topic OP to stay in sync" do
+ cat = Fabricate(:category_with_definition, user: admin)
+ raw_description = "New **markdown** description here"
+
+ put "/categories/#{cat.id}.json", params: { description: raw_description }
+
+ expect(response.status).to eq(200)
+ cat.reload
+ expect(cat.description).to include("<strong>markdown</strong>")
+ expect(cat.topic.first_post.raw).to eq(raw_description)
+ expect(cat.topic.first_post.cooked).to include("<strong>markdown</strong>")
+ end
+
it "logs the changes correctly" do
category.update!(
permissions: {