Skip to content

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

DEV: Accept description param in category creation endpoint (#36852)

Osama Sayegh220a1f4e632e
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb
index 0b78e02d7a7..0f06408d43d 100644
--- a/app/controllers/categories_controller.rb
+++ b/app/controllers/categories_controller.rb
@@ -135,10 +135,25 @@ class CategoriesController < ApplicationController
render_serialized(@category, CategorySerializer)
end
+ MAX_DESCRIPTION_PARAM_LENGTH = 1000
def create
guardian.ensure_can_create!(Category)
position = category_params.delete(:position)
+ if category_params[:description].present? &&
+ category_params[:description].size > MAX_DESCRIPTION_PARAM_LENGTH
+ render json: {
+ errors: [
+ I18n.t(
+ "category.errors.description_too_long",
+ count: MAX_DESCRIPTION_PARAM_LENGTH,
+ ),
+ ],
+ },
+ status: :unprocessable_entity
+ return
+ end
+
@category =
begin
Category.new(required_create_params.merge(user: current_user))
@@ -576,6 +591,7 @@ class CategoriesController < ApplicationController
:slug,
:allow_badges,
:topic_template,
+ :description,
:sort_order,
:sort_ascending,
:topic_featured_link_allowed,
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 2a86377e42b..b4804a0bdec 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -870,6 +870,9 @@ en:
email_already_used_in_group: "'%{email}' is already used by the group '%{group_name}'."
email_already_used_in_category: "'%{email}' is already used by the category '%{category_name}'."
description_incomplete: "The category description post must have at least one paragraph."
+ description_too_long:
+ one: "Description is too long (maximum is %{count} character)."
+ other: "Description is too long (maximum is %{count} characters)."
permission_conflict: "Any group that is allowed to access a subcategory must also be allowed to access the parent category. The following groups have access to one of the subcategories, but no access to parent category: %{group_names}."
disallowed_topic_tags: "This topic has tags not allowed by this category: '%{tags}'"
disallowed_tags_generic: "This topic has disallowed tags."
diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb
index c998826ca1c..fe31f7dca0b 100644
--- a/spec/requests/categories_controller_spec.rb
+++ b/spec/requests/categories_controller_spec.rb
@@ -500,6 +500,20 @@ RSpec.describe CategoriesController do
expect(response.status).to eq(422)
expect(response.parsed_body["errors"]).to be_present
end
+
+ it "rejects too long descriptions" do
+ limit = CategoriesController::MAX_DESCRIPTION_PARAM_LENGTH
+ post "/categories.json",
+ params: {
+ name: "Long Description Category",
+ description: "a" * (limit + 1),
+ }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"].first).to eq(
+ I18n.t("category.errors.description_too_long", count: limit),
+ )
+ end
end
describe "success" do
@@ -541,6 +555,41 @@ RSpec.describe CategoriesController do
)
expect(UserHistory.count).to eq(5)
end
+
+ it "creates category with description containing markdown" do
+ post "/categories.json",
+ params: {
+ name: "Test Category",
+ description: "This is a **test** with [link](https://example.com)",
+ }
+
+ expect(response.status).to eq(200)
+ cat_json = response.parsed_body["category"]
+
+ category = Category.find(cat_json["id"])
+ expect(category.description).to include("<strong>test</strong>")
+ expect(category.description).to include('<a href="https://example.com')
+ expect(category.description).not_to include("**test**")
+ expect(category.topic.first_post.raw).to include("**test**")
+ 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
end
end
end