diff --git a/decidim-accountability/spec/requests/result_search_spec.rb b/decidim-accountability/spec/requests/result_search_spec.rb index 053024d84b9c3..eb567c9db2a07 100644 --- a/decidim-accountability/spec/requests/result_search_spec.rb +++ b/decidim-accountability/spec/requests/result_search_spec.rb @@ -4,6 +4,7 @@ RSpec.describe "Result search", type: :request do include Decidim::ComponentPathHelper + include Decidim::SanitizeHelper let(:component) { create :accountability_component } let(:participatory_space) { component.participatory_space } @@ -59,8 +60,8 @@ subject { response.body } it "displays all categories that have top-level results" do - expect(subject).to include(translated(result1.category.name)) - expect(subject).to include(translated(result4.category.name)) + expect(subject).to include(decidim_html_escape(translated(result1.category.name))) + expect(subject).to include(decidim_html_escape(translated(result4.category.name))) end it "does not display the categories that only have sub-results" do diff --git a/decidim-core/app/cells/decidim/tags_cell.rb b/decidim-core/app/cells/decidim/tags_cell.rb index a68d4b0eb3bf3..f9049e60a1a31 100644 --- a/decidim-core/app/cells/decidim/tags_cell.rb +++ b/decidim-core/app/cells/decidim/tags_cell.rb @@ -9,6 +9,8 @@ module Decidim # <%= cell("decidim/category", model.category, context: {resource: model}) %> # class TagsCell < Decidim::ViewModel + include Decidim::SanitizeHelper + def show render if category? || scope? end @@ -51,7 +53,7 @@ def link_to_category end def category_name - model.category.translated_name + decidim_html_escape model.category.translated_name end def category_path diff --git a/decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb b/decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb index 520c842c2f47b..4ea756005b67e 100644 --- a/decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb +++ b/decidim-core/app/helpers/decidim/check_boxes_tree_helper.rb @@ -50,20 +50,20 @@ def filter_categories_values organization = current_participatory_space.organization sorted_main_categories = current_participatory_space.categories.first_class.includes(:subcategories).sort_by do |category| - [category.weight, translated_attribute(category.name, organization)] + [category.weight, decidim_html_escape(translated_attribute(category.name, organization))] end categories_values = sorted_main_categories.flat_map do |category| sorted_descendant_categories = category.descendants.includes(:subcategories).sort_by do |subcategory| - [subcategory.weight, translated_attribute(subcategory.name, organization)] + [subcategory.weight, decidim_html_escape(translated_attribute(subcategory.name, organization))] end subcategories = sorted_descendant_categories.flat_map do |subcategory| - TreePoint.new(subcategory.id.to_s, translated_attribute(subcategory.name, organization)) + TreePoint.new(subcategory.id.to_s, decidim_html_escape(translated_attribute(subcategory.name, organization))) end TreeNode.new( - TreePoint.new(category.id.to_s, translated_attribute(category.name, organization)), + TreePoint.new(category.id.to_s, decidim_html_escape(translated_attribute(category.name, organization))), subcategories ) end diff --git a/decidim-core/lib/decidim/core/test/factories.rb b/decidim-core/lib/decidim/core/test/factories.rb index 3d0bed839635d..f039022a14929 100644 --- a/decidim-core/lib/decidim/core/test/factories.rb +++ b/decidim-core/lib/decidim/core/test/factories.rb @@ -54,7 +54,17 @@ def generate_localized_title end factory :category, class: "Decidim::Category" do - name { generate_localized_title } + transient do + skip_injection { false } + end + + name do + if skip_injection + Decidim::Faker::Localized.localized { generate(:title) } + else + Decidim::Faker::Localized.localized { " #{generate(:title)}" } + end + end description { Decidim::Faker::Localized.wrapped("
", "
") { generate_localized_title } } weight { 0 } diff --git a/decidim-core/spec/cells/decidim/tags_cell_spec.rb b/decidim-core/spec/cells/decidim/tags_cell_spec.rb index 5986275209218..23a950c6d377b 100644 --- a/decidim-core/spec/cells/decidim/tags_cell_spec.rb +++ b/decidim-core/spec/cells/decidim/tags_cell_spec.rb @@ -87,6 +87,15 @@ expect(html).to have_content(translated(category.name)) end + it "sanitizes the category" do + name = %(Category a"a) + custom_category = create(:category, participatory_space: participatory_space, name: { "en" => name }) + proposal_categorized.category = custom_category + html = cell("decidim/tags", proposal_categorized, context: { extra_classes: ["tags--proposal"] }).call + expect(html).to have_css(".tags.tags--proposal") + expect(html).to have_content(name) + end + it "renders the correct filtering link" do html = cell("decidim/tags", proposal_categorized, context: { extra_classes: ["tags--proposal"] }).call path = Decidim::ResourceLocatorPresenter.new(proposal_categorized).index diff --git a/decidim-proposals/app/cells/decidim/proposals/proposal_tags/show.erb b/decidim-proposals/app/cells/decidim/proposals/proposal_tags/show.erb index 77745258288e9..8c1f4fa4fd8d3 100644 --- a/decidim-proposals/app/cells/decidim/proposals/proposal_tags/show.erb +++ b/decidim-proposals/app/cells/decidim/proposals/proposal_tags/show.erb @@ -3,8 +3,8 @@ <% if category.present? %>