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

allow specifiying safe query params for sort_link/sort_header_tag

Ivan Kuchinb811a4ebb251
diff --git a/app/components/table_component.html.erb b/app/components/table_component.html.erb
index f3162d84db3..e5b16b52ebe 100644
--- a/app/components/table_component.html.erb
+++ b/app/components/table_component.html.erb
@@ -40,7 +40,7 @@ See COPYRIGHT and LICENSE files for more details.
<tr>
<% headers.each do |name, options| %>
<% if sortable_column?(name) %>
- <%= helpers.sort_header_tag name, options %>
+ <%= helpers.sort_header_tag(name, **options) %>
<% else %>
<th>
<div class="generic-table--sort-header-outer">
diff --git a/app/helpers/sort_helper.rb b/app/helpers/sort_helper.rb
index baf255394ce..cbbda73fd75 100644
--- a/app/helpers/sort_helper.rb
+++ b/app/helpers/sort_helper.rb
@@ -272,7 +272,7 @@ module SortHelper
# - the optional caption explicitly specifies the displayed link text.
# - 2 CSS classes reflect the state of the link: sort and asc or desc
#
- def sort_link(column, caption, default_order, html_options = {})
+ def sort_link(column, caption, default_order, allowed_params: nil, **html_options)
order = order_string(column, inverted: true) || default_order
caption ||= column.to_s.humanize
@@ -283,8 +283,10 @@ module SortHelper
sort_options = { sort_key => sort_param }
+ allowed_params ||= %w[filters per_page expand columns]
+
# Don't lose other params.
- link_to_content_update(h(caption), safe_query_params(%w{filters per_page expand columns}).merge(sort_options), html_options)
+ link_to_content_update(h(caption), safe_query_params(allowed_params).merge(sort_options), html_options)
end
# Returns a table header <th> tag with a sort link for the named column
@@ -311,7 +313,7 @@ module SortHelper
# </div>
# </th>
#
- def sort_header_tag(column, options = {})
+ def sort_header_tag(column, allowed_params: nil, **options)
caption = get_caption(column, options)
default_order = options.delete(:default_order) || "asc"
@@ -321,7 +323,7 @@ module SortHelper
options[:title] = sort_header_title(column, caption, options)
within_sort_header_tag_hierarchy(options, sort_class(column)) do
- sort_link(column, caption, default_order, param:, lang:, title: options[:title])
+ sort_link(column, caption, default_order, allowed_params:, param:, lang:, title: options[:title])
end
end
diff --git a/spec/helpers/sort_helper_spec.rb b/spec/helpers/sort_helper_spec.rb
index 37478221961..768f5253ec7 100644
--- a/spec/helpers/sort_helper_spec.rb
+++ b/spec/helpers/sort_helper_spec.rb
@@ -117,9 +117,11 @@ RSpec.describe SortHelper do
end
describe "#sort_header_tag" do
- let(:output) do
- helper.sort_header_tag("id")
+ subject(:output) do
+ helper.sort_header_tag("id", **options)
end
+
+ let(:options) { {} }
let(:sort_key) { "" }
let(:sort_asc) { true }
let(:sort_criteria) do
@@ -140,7 +142,7 @@ RSpec.describe SortHelper do
end
it "renders a th with a sort link" do
- expect(output).to be_html_eql(%{
+ expect(output).to be_html_eql(<<-HTML)
<th title="Sort by &quot;Id&quot;">
<div class="generic-table--sort-header-outer">
<div class="generic-table--sort-header">
@@ -151,14 +153,14 @@ RSpec.describe SortHelper do
</div>
</div>
</th>
- })
+ HTML
end
context "when sorting by the column" do
let(:sort_key) { "id" }
it "adds the sort class" do
- expect(output).to be_html_eql(%{
+ expect(output).to be_html_eql(<<-HTML)
<th title="Ascending sorted by &quot;Id&quot;">
<div class="generic-table--sort-header-outer">
<div class="generic-table--sort-header">
@@ -169,7 +171,7 @@ RSpec.describe SortHelper do
</div>
</div>
</th>
- })
+ HTML
end
end
@@ -178,7 +180,7 @@ RSpec.describe SortHelper do
let(:sort_asc) { false }
it "adds the sort class" do
- expect(output).to be_html_eql(%{
+ expect(output).to be_html_eql(<<-HTML)
<th title="Descending sorted by &quot;Id&quot;">
<div class="generic-table--sort-header-outer">
<div class="generic-table--sort-header">
@@ -189,7 +191,57 @@ RSpec.describe SortHelper do
</div>
</div>
</th>
- })
+ HTML
+ end
+ end
+
+ describe "copying parameters" do
+ before do
+ controller.params = ActionController::Parameters.new(
+ filters: "xyz",
+ per_page: "42",
+ expand: "nope",
+ columns: "a,b,c",
+ foo: "bar",
+ bar: "baz",
+ baz: "foo"
+ )
+ end
+
+ context "when not given allowed parameters" do
+ it "copies default ones to the link" do
+ expect(output).to be_html_eql(<<-HTML)
+ <th title="Sort by &quot;Id&quot;">
+ <div class="generic-table--sort-header-outer">
+ <div class="generic-table--sort-header">
+ <span>
+ <a href="/work_packages?columns=a%2Cb%2Cc&amp;expand=nope&amp;filters=xyz&amp;per_page=42&amp;sort=sort_criteria_params"
+ title="Sort by &quot;Id&quot;">Id</a>
+ </span>
+ </div>
+ </div>
+ </th>
+ HTML
+ end
+ end
+
+ context "when given allowed parameters" do
+ let(:options) { { allowed_params: %w[foo baz lol] } }
+
+ it "copies them to the link" do
+ expect(output).to be_html_eql(<<-HTML)
+ <th title="Sort by &quot;Id&quot;">
+ <div class="generic-table--sort-header-outer">
+ <div class="generic-table--sort-header">
+ <span>
+ <a href="/work_packages?baz=foo&amp;foo=bar&amp;sort=sort_criteria_params"
+ title="Sort by &quot;Id&quot;">Id</a>
+ </span>
+ </div>
+ </div>
+ </th>
+ HTML
+ end
end
end
end