Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ AllCops:
Exclude:
- "docs/**/*"

Layout/DefEndAlignment:
Enabled: true

Layout/EmptyLineAfterMagicComment:
Enabled: true

Expand Down
3 changes: 0 additions & 3 deletions lib/ransack/invalid_search_error.rb

This file was deleted.

6 changes: 3 additions & 3 deletions lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def attributes=(args)
end
when Hash
args.each do |index, attrs|
attrs = indifferent_params(attrs, 'attribute')
attrs = indifferent_params(attrs, 'an attribute')
build_attributes(attrs[:name], attrs[:ransacker_args])
end
else
Expand All @@ -162,7 +162,7 @@ def values=(args)
end
when Hash
args.each do |index, attrs|
val = Value.new(@context, indifferent_params(attrs, 'value')[:value])
val = Value.new(@context, indifferent_params(attrs, 'a value')[:value])
self.values << val
end
else
Expand Down Expand Up @@ -274,7 +274,7 @@ def cast_unless_null_sentinel(v)
}.freeze

def build(params)
indifferent_params(params, 'condition').each do |key, value|
indifferent_params(params, 'a condition').each do |key, value|
if key.match(/\A(a|v|p|m|attributes|values|predicate|predicate_name|combinator)\z/)
self.send("#{LONG_KEYS.fetch(key, key)}=", value)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/nodes/grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def new_grouping(params = {})
end

def build(params)
indifferent_params(params, 'grouping').each do |key, value|
indifferent_params(params, 'a grouping').each do |key, value|
case key
when /\A(g|c|m|groupings|conditions|combinator)\z/
self.send("#{key}=", value)
Expand Down
7 changes: 5 additions & 2 deletions lib/ransack/nodes/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ def combinator=(val)
# Search params usually come from the request, so a grouping, condition
# or sort given as something other than a hash (`q[g]=x`) is a malformed
# search, not a programming error, whether or not the search is strict.
# Strong parameters are unwrapped as `Search#initialize` unwraps them, so
# `build_grouping(params[:g])` takes what `ransack(params[:q])` does. That
# does not make the search permitted: only `Search#initialize` reads it.
def indifferent_params(params, node_name)
params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
return params.with_indifferent_access if params.respond_to?(:with_indifferent_access)

raise InvalidSearchError,
"Invalid argument (#{params.class}) supplied for a #{node_name}"
"Invalid argument (#{params.class}) supplied for #{node_name}"
end

# A name, predicate, direction or combinator is a single value. Given an
Expand All @@ -63,7 +67,6 @@ def scalar_param(value, field)

raise InvalidSearchError, "Invalid argument (#{value.class}) supplied for #{field}"
end

end
end
end
2 changes: 1 addition & 1 deletion lib/ransack/nodes/sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def extract(context, str)
end

def build(params)
indifferent_params(params, 'sort').each do |key, value|
indifferent_params(params, 'a sort').each do |key, value|
if key.match(/\A(name|dir|ransacker_args)\z/)
self.send("#{key}=", value)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/ransack/malformed_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def params(query)
.to raise_error(InvalidSearchError, 'Invalid argument (String) supplied to groupings=')
expect { Person.ransack(params('q[c][0]=x')) }
.to raise_error(InvalidSearchError, 'Invalid argument (String) supplied for a condition')
expect { Person.ransack(params('q[c][0][a][0]=x')) }
.to raise_error(InvalidSearchError, 'Invalid argument (String) supplied for an attribute')
end

it 'is still an ArgumentError, for code that rescues that' do
Expand Down
42 changes: 42 additions & 0 deletions spec/ransack/strong_parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,47 @@ def unpermitted(hash)
expect(Listed.ransack(permitted(s: 'only_sort asc')).result.to_sql).not_to include 'only_sort'
end
end

# `Search#initialize` unwraps strong parameters. The builders that take
# params once the search exists did not, and turned them away as malformed.
context 'given to a builder once the search exists' do
let(:name) { "#{quote_table_name("people")}.#{quote_column_name("name")}" }

it 'builds a grouping from them' do
search = Person.ransack
search.build_grouping(unpermitted(name_eq: 'x'))
expect(search.result.to_sql).to match(/#{name} = 'x'/)
end

it 'builds a grouping from nested params' do
search = Person.ransack
search.build_grouping(
unpermitted(c: { '0' => { a: { '0' => { name: 'name' } }, p: 'eq', v: { '0' => { value: 'x' } } } })
)
expect(search.result.to_sql).to match(/#{name} = 'x'/)
end

it 'builds a sort from them' do
search = Person.ransack
search.build_sort(unpermitted(name: 'name', dir: 'desc'))
expect(search.result.to_sql).to match(/ORDER BY #{name} DESC/)
end

it 'still applies the allowlist of the model' do
search = Person.ransack
search.build_grouping(permitted(only_sort_eq: 'x'))
expect(search.result.to_sql).not_to include 'only_sort'
end

# Only the params the search was built from decide whether it is
# permitted; permitted params handed to a builder later do not.
it 'does not make a search of a model with no allowlist permitted' do
search = Unlisted.ransack
expect {
search.build_grouping(permitted(only_sort_eq: 'x'))
search.result
}.to raise_error(RuntimeError, /explicitly allowlisted/)
end
end
end
end
Loading