Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Escape project custom field list values in the search results and refactor search specs.

Dombi Attila9dfe80c0985c
diff --git a/lib_static/plugins/acts_as_searchable/lib/acts_as_searchable.rb b/lib_static/plugins/acts_as_searchable/lib/acts_as_searchable.rb
index 5ab1db7c347..884f5c38c17 100644
--- a/lib_static/plugins/acts_as_searchable/lib/acts_as_searchable.rb
+++ b/lib_static/plugins/acts_as_searchable/lib/acts_as_searchable.rb
@@ -106,7 +106,7 @@ module Redmine
end
named_tokens = tokens.each_with_object({}).with_index do |(token, acc), index|
- acc[:"token_#{index}"] = "%#{token.downcase}%"
+ acc[:"token_#{index}"] = "%#{sanitize_sql_like(token.downcase)}%"
end
find_conditions = [sql, named_tokens]
@@ -187,7 +187,7 @@ module Redmine
ON custom_options.custom_field_id = custom_values.custom_field_id
AND custom_options.id::VARCHAR = custom_values.value
SQL
- .where(customized_id: arel_table[:id])
+ .where("customized_id=#{table_name}.id")
.where(custom_field_id: searchable_custom_field_ids)
.where("(custom_values.value ILIKE ?) OR (custom_options.value ILIKE ?)")
diff --git a/spec/features/search/search_spec.rb b/spec/features/search/search_spec.rb
index c2cbb4f9f08..19f8af98caa 100644
--- a/spec/features/search/search_spec.rb
+++ b/spec/features/search/search_spec.rb
@@ -478,81 +478,85 @@ RSpec.describe "Search", :js, with_settings: { per_page_options: "5" } do
let!(:searched_for_project) { create(:project, name: "Searched for project") }
let!(:other_project) { create(:project, name: "Other project") }
- context "when globally" do
+ subject do
+ select_autocomplete(page.find(".top-menu-search--input"),
+ query:,
+ select_text: "In all projects ↵",
+ wait_dropdown_open: false)
+
+ within ".global-search--tabs" do
+ click_on "Projects"
+ end
+ end
+
+ shared_examples "finds the project" do
it "finds the project" do
- select_autocomplete(page.find(".top-menu-search--input"),
- query: "Searched",
- select_text: "In all projects ↵",
- wait_dropdown_open: false)
+ subject
+ expect(page)
+ .to have_link(searched_for_project.name)
- within ".global-search--tabs" do
- click_on "Projects"
- end
+ expect(page)
+ .to have_no_link(other_project.name)
+ end
+ end
+ shared_examples "does not find the project" do
+ it "does not find the project" do
+ subject
expect(page)
- .to have_link(searched_for_project.name)
+ .to have_no_link(searched_for_project.name)
expect(page)
.to have_no_link(other_project.name)
end
+ end
+
+ context "when globally" do
+ let(:query) { "Searched" }
+
+ it_behaves_like "finds the project"
describe "searching for list project custom field" do
- let(:project_list_cf) do
+ let(:possible_values) { %w[Value1 Value2 Value3] }
+ let!(:project_list_cf) do
create(:list_project_custom_field,
multi_value: true,
projects: [searched_for_project],
- possible_values: %w[Value1 Value2 Value3],
+ possible_values:,
searchable:).tap do |cf|
searched_for_project.update(
custom_field_values: { cf.id => cf.possible_values.pluck(:id).first(2) }
)
end
end
- let(:first_selected_value) { project_list_cf.possible_values.pick(:value) }
+ let(:query) { project_list_cf.possible_values.pick(:value) }
- before do
- select_autocomplete(page.find(".top-menu-search--input"),
- query: first_selected_value,
- select_text: "In all projects ↵",
- wait_dropdown_open: false)
-
- within ".global-search--tabs" do
- click_on "Projects"
- end
- end
-
- it "finds the project" do
- expect(page)
- .to have_link(searched_for_project.name)
-
- expect(page)
- .to have_no_link(other_project.name)
- end
+ it_behaves_like "finds the project"
context "when searchable is false" do
let(:searchable) { false }
- it "does not find the project" do
- expect(page)
- .to have_no_link(searched_for_project.name)
-
- expect(page)
- .to have_no_link(other_project.name)
- end
+ it_behaves_like "does not find the project"
end
context "when not enabled for project" do
before do
- searched_for_project.project_custom_field_project_mappings.destroy_all
+ ProjectCustomFieldProjectMapping.destroy_all
end
- it "does not find the project" do
- expect(page)
- .to have_no_link(searched_for_project.name)
+ it_behaves_like "does not find the project"
+ end
- expect(page)
- .to have_no_link(other_project.name)
- end
+ context "when using % in the query string the escaping works correcly and" do
+ let(:query) { "%#{project_list_cf.possible_values.pick(:value)}" }
+
+ it_behaves_like "does not find the project"
+ end
+
+ context "when the value contains a % character" do
+ let(:possible_values) { %w[%Value1 Value2 Value3] }
+
+ it_behaves_like "finds the project"
end
end
end