A platform for community discussion. Free, open, simple.
FIX: Skip CSRF check for POST /categories/search (#29392)
Bianca Nenciu2f1d1cd062dc
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rbindex 558fc168da6..82a8949e641 100644--- a/app/controllers/categories_controller.rb+++ b/app/controllers/categories_controller.rb@@ -18,6 +18,7 @@ class CategoriesController < ApplicationControllerbefore_action :fetch_category, only: %i[show update destroy visible_groups]before_action :initialize_staff_action_logger, only: %i[create update destroy]skip_before_action :check_xhr, only: %i[index categories_and_latest categories_and_top redirect]+ skip_before_action :verify_authenticity_token, only: %i[search]SYMMETRICAL_CATEGORIES_TO_TOPICS_FACTOR = 1.5MIN_CATEGORIES_TOPICS = 5diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rbindex d4ebd02fcef..fb3749c5fdc 100644--- a/spec/requests/categories_controller_spec.rb+++ b/spec/requests/categories_controller_spec.rb@@ -1504,6 +1504,23 @@ RSpec.describe CategoriesController doexpect(response.status).to eq(200)expect(response.parsed_body["categories"].map { |c| c["id"] }).not_to include(category.id)end++ context "when not logged in" do+ before { ActionController::Base.allow_forgery_protection = true }+ after { ActionController::Base.allow_forgery_protection = false }++ it "works and is not CSRF protected" do+ post "/categories/search.json", params: { term: "" }++ expect(response.status).to eq(200)+ expect(response.parsed_body["categories"].map { |c| c["id"] }).to contain_exactly(+ SiteSetting.uncategorized_category_id,+ category.id,+ subcategory.id,+ category2.id,+ )+ end+ endenddescribe "#hierachical_search" do