ransack 4.4.1 → 4.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ransack/adapters/active_record/context.rb +4 -0
  3. data/lib/ransack/constants.rb +9 -0
  4. data/lib/ransack/context.rb +11 -0
  5. data/lib/ransack/nodes/grouping.rb +2 -0
  6. data/lib/ransack/search.rb +19 -7
  7. data/lib/ransack/version.rb +1 -1
  8. metadata +4 -60
  9. data/spec/console.rb +0 -13
  10. data/spec/factories/articles.rb +0 -7
  11. data/spec/factories/comments.rb +0 -7
  12. data/spec/factories/notes.rb +0 -13
  13. data/spec/factories/people.rb +0 -10
  14. data/spec/factories/tags.rb +0 -5
  15. data/spec/helpers/polyamorous_helper.rb +0 -13
  16. data/spec/helpers/ransack_helper.rb +0 -9
  17. data/spec/polyamorous/activerecord_compatibility_spec.rb +0 -15
  18. data/spec/polyamorous/join_association_spec.rb +0 -29
  19. data/spec/polyamorous/join_dependency_spec.rb +0 -80
  20. data/spec/polyamorous/join_spec.rb +0 -19
  21. data/spec/ransack/adapters/active_record/base_spec.rb +0 -943
  22. data/spec/ransack/adapters/active_record/context_spec.rb +0 -219
  23. data/spec/ransack/configuration_spec.rb +0 -201
  24. data/spec/ransack/helpers/form_builder_spec.rb +0 -169
  25. data/spec/ransack/helpers/form_helper_spec.rb +0 -1075
  26. data/spec/ransack/invalid_search_error_spec.rb +0 -27
  27. data/spec/ransack/nodes/condition_spec.rb +0 -333
  28. data/spec/ransack/nodes/grouping_spec.rb +0 -111
  29. data/spec/ransack/nodes/value_spec.rb +0 -126
  30. data/spec/ransack/predicate_spec.rb +0 -504
  31. data/spec/ransack/ransacker_spec.rb +0 -69
  32. data/spec/ransack/search_spec.rb +0 -821
  33. data/spec/ransack/translate_spec.rb +0 -16
  34. data/spec/spec_helper.rb +0 -52
  35. data/spec/support/en.yml +0 -18
  36. data/spec/support/schema.rb +0 -419
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9174f26f96f894e12f75427d0bc4fee92d3cda026ed899f2356b42973d1fb3bf
4
- data.tar.gz: aa87e6f2d75557369ca2cf1e7821aaf833490c2793380535d9e5514823ebd117
3
+ metadata.gz: 4de514e3bd66dcd465c8253260a52e8fa9e40476a6e1d12b09dae4ed1ec8aaaf
4
+ data.tar.gz: 2f156ce6c2bde6adca76eec24e59cf625e2e53b6922d5a0de370d7a1be75354c
5
5
  SHA512:
6
- metadata.gz: cdb319a97b23b03a24ac010136212ac53460f03b208e5dff1efae8ff32833256dff1222abbce533e704320740a5bd1cd1d56e14d35d7264a733d0bc1aa75e918
7
- data.tar.gz: 12dd3eae7f2cd9bf78d30ab4ae76e5a3ffc80f33ddb3737856cabf3cfb395adf8b9af83eb5feb4e131c8ffd5294e7bf81870da42f7cdbc734288f086902aab96
6
+ metadata.gz: f4c4983ce0a10d9afc2f538666f98eeee0eb8f9da168fcd30800ded495cff71807d456e1c462530866d7e5c7954a737fd7127f3badcdb511d48bc9eab22b5d89
7
+ data.tar.gz: 83403fc8e6cfce559597fa844e0ff79b45623dfd285382a2d0d84547263bd8c95f59998c226b7bd5b4c4745e6d70ddf45c6d5e1fd75c9b7278ae4b09592ef150
@@ -64,6 +64,8 @@ module Ransack
64
64
  end
65
65
 
66
66
  def attribute_method?(str, klass = @klass)
67
+ return false unless key_within_depth_limit?(str)
68
+
67
69
  exists = false
68
70
  if ransackable_attribute?(str, klass)
69
71
  exists = true
@@ -237,6 +239,8 @@ module Ransack
237
239
  end
238
240
 
239
241
  def get_parent_and_attribute_name(str, parent = @base)
242
+ return [parent, nil] unless key_within_depth_limit?(str)
243
+
240
244
  attr_name = nil
241
245
 
242
246
  if ransackable_attribute?(str, klassify(parent))
@@ -50,6 +50,15 @@ module Ransack
50
50
 
51
51
  DISTINCT = 'DISTINCT '.freeze
52
52
 
53
+ # The most `_`-separated segments a single condition or sort key may have
54
+ # before Ransack rejects it as invalid instead of parsing it. Working out
55
+ # whether a key is an attribute, an `_and_`/`_or_` compound or a path
56
+ # through associations is superlinear in this count, and the key comes
57
+ # straight from the query string, so an unbounded one is a CPU-exhaustion
58
+ # denial of service (GHSA-j3f8-w227-4hh8). No real search key is this
59
+ # long: OR-ing every column of a very wide table stays well under it.
60
+ MAX_KEY_DEPTH = 200
61
+
53
62
  DERIVED_PREDICATES = [
54
63
  [CONT, {
55
64
  arel_predicate: 'matches'.freeze,
@@ -47,6 +47,15 @@ module Ransack
47
47
  @base = @join_dependency.instance_variable_get(:@join_root)
48
48
  end
49
49
 
50
+ # False when a key has more `_`-separated segments than Ransack will
51
+ # parse. The attribute / association / predicate parsing that follows is
52
+ # superlinear in the segment count and the key is attacker-controlled, so
53
+ # an over-long key is treated as unknown rather than parsed
54
+ # (GHSA-j3f8-w227-4hh8).
55
+ def key_within_depth_limit?(key)
56
+ key.to_s.count(Constants::UNDERSCORE) <= Constants::MAX_KEY_DEPTH
57
+ end
58
+
50
59
  def bind_pair_for(key)
51
60
  @bind_pairs ||= {}
52
61
 
@@ -124,6 +133,8 @@ module Ransack
124
133
  end
125
134
 
126
135
  def association_path(str, base = @base)
136
+ return ''.freeze unless key_within_depth_limit?(str)
137
+
127
138
  base = klassify(base)
128
139
  str ||= ''.freeze
129
140
  path = []
@@ -122,6 +122,8 @@ module Ransack
122
122
  end
123
123
 
124
124
  def attribute_method?(name)
125
+ return false unless @context.key_within_depth_limit?(name)
126
+
125
127
  stripped_name = strip_predicate_and_index(name)
126
128
  return true if @context.attribute_method?(stripped_name) ||
127
129
  @context.attribute_method?(name)
@@ -160,19 +160,31 @@ module Ransack
160
160
  end
161
161
  end
162
162
 
163
+ # The largest position a multiparameter key (`created_at(1i)`) may carry.
164
+ # Rails' date and time selects emit at most six, year to second. The
165
+ # position indexes an array, so an unbounded one from a crafted query
166
+ # string would allocate an array that size (GHSA-vxc9-rm8f-p56j).
167
+ MULTIPARAMETER_POSITION_LIMIT = 16
168
+
169
+ # Folds `created_at(1i)`, `created_at(2i)`, ... into `created_at` as an
170
+ # array of cast values, the way Active Record does for form input. The
171
+ # keys are untrusted, so a malformed one is dropped rather than raised
172
+ # on: no position (`created_at(`), a position outside 1 to the limit,
173
+ # or a fragment for an attribute that was also given as a plain value.
163
174
  def collapse_multiparameter_attributes!(attrs)
164
175
  attrs.keys.each do |k|
165
176
  if k.include?(Constants::LEFT_PARENTHESIS)
166
177
  real_attribute, position = k.split(/\(|\)/)
167
- cast =
168
- if Constants::A_S_I.include?(position.last)
169
- position.last
170
- else
171
- nil
172
- end
173
- position = position.to_i - 1
174
178
  value = attrs.delete(k)
179
+ next if position.nil?
180
+
181
+ cast = Constants::A_S_I.include?(position.last) ? position.last : nil
182
+ position = position.to_i - 1
183
+ next if position < 0 || position >= MULTIPARAMETER_POSITION_LIMIT
184
+
175
185
  attrs[real_attribute] ||= []
186
+ next unless attrs[real_attribute].is_a?(Array)
187
+
176
188
  attrs[real_attribute][position] =
177
189
  if cast
178
190
  if value.blank? && cast == Constants::I
@@ -1,3 +1,3 @@
1
1
  module Ransack
2
- VERSION = '4.4.1'
2
+ VERSION = '4.4.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ransack
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.1
4
+ version: 4.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernie Miller
@@ -11,7 +11,7 @@ authors:
11
11
  - David Rodríguez
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2025-09-29 00:00:00.000000000 Z
14
+ date: 1980-01-02 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord
@@ -130,34 +130,6 @@ files:
130
130
  - lib/ransack/translate.rb
131
131
  - lib/ransack/version.rb
132
132
  - lib/ransack/visitor.rb
133
- - spec/console.rb
134
- - spec/factories/articles.rb
135
- - spec/factories/comments.rb
136
- - spec/factories/notes.rb
137
- - spec/factories/people.rb
138
- - spec/factories/tags.rb
139
- - spec/helpers/polyamorous_helper.rb
140
- - spec/helpers/ransack_helper.rb
141
- - spec/polyamorous/activerecord_compatibility_spec.rb
142
- - spec/polyamorous/join_association_spec.rb
143
- - spec/polyamorous/join_dependency_spec.rb
144
- - spec/polyamorous/join_spec.rb
145
- - spec/ransack/adapters/active_record/base_spec.rb
146
- - spec/ransack/adapters/active_record/context_spec.rb
147
- - spec/ransack/configuration_spec.rb
148
- - spec/ransack/helpers/form_builder_spec.rb
149
- - spec/ransack/helpers/form_helper_spec.rb
150
- - spec/ransack/invalid_search_error_spec.rb
151
- - spec/ransack/nodes/condition_spec.rb
152
- - spec/ransack/nodes/grouping_spec.rb
153
- - spec/ransack/nodes/value_spec.rb
154
- - spec/ransack/predicate_spec.rb
155
- - spec/ransack/ransacker_spec.rb
156
- - spec/ransack/search_spec.rb
157
- - spec/ransack/translate_spec.rb
158
- - spec/spec_helper.rb
159
- - spec/support/en.yml
160
- - spec/support/schema.rb
161
133
  homepage: https://github.com/activerecord-hackery/ransack
162
134
  licenses:
163
135
  - MIT
@@ -177,35 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
149
  - !ruby/object:Gem::Version
178
150
  version: '0'
179
151
  requirements: []
180
- rubygems_version: 3.6.2
152
+ rubygems_version: 3.6.9
181
153
  specification_version: 4
182
154
  summary: Object-based searching for Active Record.
183
- test_files:
184
- - spec/console.rb
185
- - spec/factories/articles.rb
186
- - spec/factories/comments.rb
187
- - spec/factories/notes.rb
188
- - spec/factories/people.rb
189
- - spec/factories/tags.rb
190
- - spec/helpers/polyamorous_helper.rb
191
- - spec/helpers/ransack_helper.rb
192
- - spec/polyamorous/activerecord_compatibility_spec.rb
193
- - spec/polyamorous/join_association_spec.rb
194
- - spec/polyamorous/join_dependency_spec.rb
195
- - spec/polyamorous/join_spec.rb
196
- - spec/ransack/adapters/active_record/base_spec.rb
197
- - spec/ransack/adapters/active_record/context_spec.rb
198
- - spec/ransack/configuration_spec.rb
199
- - spec/ransack/helpers/form_builder_spec.rb
200
- - spec/ransack/helpers/form_helper_spec.rb
201
- - spec/ransack/invalid_search_error_spec.rb
202
- - spec/ransack/nodes/condition_spec.rb
203
- - spec/ransack/nodes/grouping_spec.rb
204
- - spec/ransack/nodes/value_spec.rb
205
- - spec/ransack/predicate_spec.rb
206
- - spec/ransack/ransacker_spec.rb
207
- - spec/ransack/search_spec.rb
208
- - spec/ransack/translate_spec.rb
209
- - spec/spec_helper.rb
210
- - spec/support/en.yml
211
- - spec/support/schema.rb
155
+ test_files: []
data/spec/console.rb DELETED
@@ -1,13 +0,0 @@
1
- Bundler.setup
2
- require 'factory_bot'
3
- require 'faker'
4
- require 'ransack'
5
-
6
- Dir[File.expand_path('../../spec/{helpers,support,factories}/*.rb', __FILE__)]
7
- .each do |f|
8
- require f
9
- end
10
-
11
- Faker::Config.random = Random.new(0)
12
-
13
- Schema.create
@@ -1,7 +0,0 @@
1
- FactoryBot.define do
2
- factory :article do
3
- association :person
4
- title { Faker::Lorem.sentence }
5
- body { Faker::Lorem.paragraph }
6
- end
7
- end
@@ -1,7 +0,0 @@
1
- FactoryBot.define do
2
- factory :comment do
3
- association :article
4
- association :person
5
- body { Faker::Lorem.paragraph }
6
- end
7
- end
@@ -1,13 +0,0 @@
1
- FactoryBot.define do
2
- factory :note do
3
- note { Faker::Lorem.words(number: 7).join(' ') }
4
-
5
- trait :for_person do
6
- association :notable, factory: :person
7
- end
8
-
9
- trait :for_article do
10
- association :notable, factory: :article
11
- end
12
- end
13
- end
@@ -1,10 +0,0 @@
1
- FactoryBot.define do
2
- factory :person do
3
- name { Faker::Name.name }
4
- email { "test@example.com" }
5
- sequence(:salary) { |n| 30000 + (n * 1000) }
6
- only_sort { Faker::Lorem.words(number: 3).join(' ') }
7
- only_search { Faker::Lorem.words(number: 3).join(' ') }
8
- only_admin { Faker::Lorem.words(number: 3).join(' ') }
9
- end
10
- end
@@ -1,5 +0,0 @@
1
- FactoryBot.define do
2
- factory :tag do
3
- name { Faker::Lorem.words(number: 3).join(' ') }
4
- end
5
- end
@@ -1,13 +0,0 @@
1
- module PolyamorousHelper
2
- def new_join_association(reflection, children, klass)
3
- Polyamorous::JoinAssociation.new reflection, children, klass
4
- end
5
-
6
- def new_join_dependency(klass, associations = {})
7
- Polyamorous::JoinDependency.new klass, klass.arel_table, associations, Polyamorous::InnerJoin
8
- end
9
-
10
- def new_join(name, type = Polyamorous::InnerJoin, klass = nil)
11
- Polyamorous::Join.new name, type, klass
12
- end
13
- end
@@ -1,9 +0,0 @@
1
- module RansackHelper
2
- def quote_table_name(table)
3
- ActiveRecord::Base.connection.quote_table_name(table)
4
- end
5
-
6
- def quote_column_name(column)
7
- ActiveRecord::Base.connection.quote_column_name(column)
8
- end
9
- end
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Polyamorous
4
- describe "ActiveRecord Compatibility" do
5
- it 'works with self joins and includes' do
6
- trade_account = Account.create!
7
- Account.create!(trade_account: trade_account)
8
-
9
- accounts = Account.joins(:trade_account).includes(:trade_account, :agent_account)
10
- account = accounts.first
11
-
12
- expect(account.agent_account).to be_nil
13
- end
14
- end
15
- end
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Polyamorous
4
- describe JoinAssociation do
5
- let(:join_dependency) { new_join_dependency Note, {} }
6
- let(:reflection) { Note.reflect_on_association(:notable) }
7
- let(:parent) { join_dependency.send(:join_root) }
8
- let(:join_association) {
9
- new_join_association(reflection, parent.children, Article)
10
- }
11
-
12
- subject { new_join_association(reflection, parent.children, Person) }
13
-
14
- it 'leaves the original reflection intact for thread safety' do
15
- reflection.instance_variable_set(:@klass, Article)
16
- join_association
17
- .swapping_reflection_klass(reflection, Person) do |new_reflection|
18
- expect(new_reflection.options).not_to equal reflection.options
19
- expect(new_reflection.options).not_to have_key(:polymorphic)
20
- expect(new_reflection.klass).to eq(Person)
21
- expect(reflection.klass).to eq(Article)
22
- end
23
- end
24
-
25
- it 'sets the polymorphic option to true after initializing' do
26
- expect(join_association.reflection.options[:polymorphic]).to be true
27
- end
28
- end
29
- end
@@ -1,80 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Polyamorous
4
- describe JoinDependency do
5
- context 'with symbol joins' do
6
- subject { new_join_dependency Person, articles: :comments }
7
-
8
- specify { expect(subject.send(:join_root).drop(1).size)
9
- .to eq(2) }
10
- specify { expect(subject.send(:join_root).drop(1).map(&:join_type).uniq)
11
- .to eq [Polyamorous::InnerJoin] }
12
- end
13
-
14
- context 'with has_many :through association' do
15
- subject { new_join_dependency Person, :authored_article_comments }
16
-
17
- specify { expect(subject.send(:join_root).drop(1).size)
18
- .to eq 1 }
19
- specify { expect(subject.send(:join_root).drop(1).first.table_name)
20
- .to eq 'comments' }
21
- end
22
-
23
- context 'with outer join' do
24
- subject { new_join_dependency Person, new_join(:articles, :outer) }
25
-
26
- specify { expect(subject.send(:join_root).drop(1).size)
27
- .to eq 1 }
28
- specify { expect(subject.send(:join_root).drop(1).first.join_type)
29
- .to eq Polyamorous::OuterJoin }
30
- end
31
-
32
- context 'with nested outer joins' do
33
- subject { new_join_dependency Person,
34
- new_join(:articles, :outer) => new_join(:comments, :outer) }
35
-
36
- specify { expect(subject.send(:join_root).drop(1).size)
37
- .to eq 2 }
38
- specify { expect(subject.send(:join_root).drop(1).map(&:join_type))
39
- .to eq [Polyamorous::OuterJoin, Polyamorous::OuterJoin] }
40
- specify { expect(subject.send(:join_root).drop(1).map(&:join_type).uniq)
41
- .to eq [Polyamorous::OuterJoin] }
42
- end
43
-
44
- context 'with polymorphic belongs_to join' do
45
- subject { new_join_dependency Note, new_join(:notable, :inner, Person) }
46
-
47
- specify { expect(subject.send(:join_root).drop(1).size)
48
- .to eq 1 }
49
- specify { expect(subject.send(:join_root).drop(1).first.join_type)
50
- .to eq Polyamorous::InnerJoin }
51
- specify { expect(subject.send(:join_root).drop(1).first.table_name)
52
- .to eq 'people' }
53
- end
54
-
55
- context 'with polymorphic belongs_to join and nested symbol join' do
56
- subject { new_join_dependency Note,
57
- new_join(:notable, :inner, Person) => :comments }
58
-
59
- specify { expect(subject.send(:join_root).drop(1).size)
60
- .to eq 2 }
61
- specify { expect(subject.send(:join_root).drop(1).map(&:join_type).uniq)
62
- .to eq [Polyamorous::InnerJoin] }
63
- specify { expect(subject.send(:join_root).drop(1).first.table_name)
64
- .to eq 'people' }
65
- specify { expect(subject.send(:join_root).drop(1)[1].table_name)
66
- .to eq 'comments' }
67
- end
68
-
69
- context 'with polymorphic belongs_to join and nested join' do
70
- subject { new_join_dependency Note,
71
- new_join(:notable, :outer, Person) => :comments }
72
- specify { expect(subject.send(:join_root).drop(1).size).to eq 2 }
73
- specify { expect(subject.send(:join_root).drop(1).map(&:join_type)).to eq [Polyamorous::OuterJoin, Polyamorous::InnerJoin] }
74
- specify { expect(subject.send(:join_root).drop(1).first.table_name)
75
- .to eq 'people' }
76
- specify { expect(subject.send(:join_root).drop(1)[1].table_name)
77
- .to eq 'comments' }
78
- end
79
- end
80
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Polyamorous
4
- describe Join do
5
- it 'is a tree node' do
6
- join = new_join(:articles, :outer)
7
- expect(join).to be_kind_of(TreeNode)
8
- end
9
-
10
- it 'can be added to a tree' do
11
- join = new_join(:articles, :outer)
12
-
13
- tree_hash = {}
14
- join.add_to_tree(tree_hash)
15
-
16
- expect(tree_hash[join]).to be {}
17
- end
18
- end
19
- end