ransack 5.0.1 → 5.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 488db32f12e0902a6194cc3f5d0b4e08676b8533302f00a2bd30739f0bead9de
4
- data.tar.gz: f884ce36bbb02c25a58c2293f75d1147a790075242b11fec6a24b487a1d39f19
3
+ metadata.gz: bb028d041bbb2d5f57c32e8846cd1db041e232bec4a96e3f0d9d7d271aa44235
4
+ data.tar.gz: 66193a18248b27e47ae7271acbf8fa76772f4583b1ec99c7d80f8cf88811ce9d
5
5
  SHA512:
6
- metadata.gz: eb8990b59a5d2c06eda4851c80682628f3f818cfc9d6a8e6a68c870a7e69d36e97406bc4606e0234c8a1325e8bde143930e6849eb3afbf834a707c7fa89063a6
7
- data.tar.gz: 97b4c79957abc27206b8a0de0a681a13904a0e80de942c8fd0501edc03d56c166bab15f456a6c5e1abecf2f0f2a142e854b2c01509db0a85da41b258b2b7a16d
6
+ metadata.gz: 1b0e855e1a62829faf572273b65947b36ad57452826c10ccf8dc12f2f3f009600c1eef8e3789a1acca549d09711dd09f58fa38c72636e506ac4ca135e36bf324
7
+ data.tar.gz: a8ac8fca894d3c2f842bfd29bd99e78c801453b0f692f6c47f9786fc0ac0cb404d03f6ec7c8d773ae2cbcfd4c084cdf6b203163238e0326361084517bfe64f1d
@@ -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
  ransortable_attribute?(str, klass)
@@ -238,6 +240,8 @@ module Ransack
238
240
  end
239
241
 
240
242
  def get_parent_and_attribute_name(str, parent = @base)
243
+ return [parent, nil] unless key_within_depth_limit?(str)
244
+
241
245
  attr_name = nil
242
246
 
243
247
  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,
@@ -52,6 +52,15 @@ module Ransack
52
52
  @base = @join_dependency.instance_variable_get(:@join_root)
53
53
  end
54
54
 
55
+ # False when a key has more `_`-separated segments than Ransack will
56
+ # parse. The attribute / association / predicate parsing that follows is
57
+ # superlinear in the segment count and the key is attacker-controlled, so
58
+ # an over-long key is treated as unknown rather than parsed
59
+ # (GHSA-j3f8-w227-4hh8).
60
+ def key_within_depth_limit?(key)
61
+ key.to_s.count(Constants::UNDERSCORE) <= Constants::MAX_KEY_DEPTH
62
+ end
63
+
55
64
  def bind_pair_for(key)
56
65
  @bind_pairs ||= {}
57
66
 
@@ -137,6 +146,8 @@ module Ransack
137
146
  end
138
147
 
139
148
  def association_path(str, base = @base)
149
+ return ''.freeze unless key_within_depth_limit?(str)
150
+
140
151
  base = klassify(base)
141
152
  str ||= ''.freeze
142
153
  path = []
@@ -162,6 +162,17 @@ module Ransack
162
162
  end
163
163
 
164
164
  class SortLink
165
+ # Keys url_for reads as instructions for building the link, not as
166
+ # query params. The request's params are carried over into the link,
167
+ # so a crafted query string could otherwise change its scheme, host,
168
+ # port, path prefix or route (GHSA-58rx-96gf-w92x). Userinfo needs
169
+ # :user and :password together, so dropping :password is enough to
170
+ # keep a ?user= filter.
171
+ RESERVED_URL_OPTIONS = (
172
+ ActionDispatch::Routing::RouteSet::RESERVED_OPTIONS +
173
+ %i[password use_route _recall]
174
+ ).freeze
175
+
165
176
  def initialize(search, attribute, args, params)
166
177
  @search = search
167
178
  @params = parameters_hash(params)
@@ -195,7 +206,7 @@ module Ransack
195
206
  end
196
207
 
197
208
  def url_options
198
- @params.except(:host).merge(
209
+ @params.except(*RESERVED_URL_OPTIONS).merge(
199
210
  @options.except(:class, :data, :host).merge(
200
211
  @search.context.search_key => search_and_sort_params))
201
212
  end
@@ -218,12 +229,11 @@ module Ransack
218
229
 
219
230
  private
220
231
 
232
+ # Indifferent access, so RESERVED_URL_OPTIONS also matches the string
233
+ # keys of a plain Hash.
221
234
  def parameters_hash(params)
222
- if params.respond_to?(:to_unsafe_h)
223
- params.to_unsafe_h
224
- else
225
- params
226
- end
235
+ params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
236
+ params.with_indifferent_access
227
237
  end
228
238
 
229
239
  def extract_sort_fields_and_mutate_args!(args)
@@ -126,6 +126,8 @@ module Ransack
126
126
  end
127
127
 
128
128
  def attribute_method?(name)
129
+ return false unless @context.key_within_depth_limit?(name)
130
+
129
131
  stripped_name = strip_predicate_and_index(name)
130
132
  return true if @context.attribute_method?(stripped_name) ||
131
133
  @context.attribute_method?(name)
@@ -1,3 +1,3 @@
1
1
  module Ransack
2
- VERSION = '5.0.1'
2
+ VERSION = '5.0.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: 5.0.1
4
+ version: 5.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernie Miller