mongoid 8.0.11 → 8.0.13

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 (47) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +26 -0
  3. data/lib/mongoid/association/depending.rb +8 -4
  4. data/lib/mongoid/association/nested/many.rb +38 -3
  5. data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
  6. data/lib/mongoid/association/referenced/has_many/proxy.rb +100 -1
  7. data/lib/mongoid/clients/factory.rb +4 -0
  8. data/lib/mongoid/config.rb +62 -0
  9. data/lib/mongoid/contextual/aggregable/memory.rb +5 -2
  10. data/lib/mongoid/contextual/memory.rb +18 -7
  11. data/lib/mongoid/criteria/queryable/mergeable.rb +12 -0
  12. data/lib/mongoid/criteria/queryable/selectable.rb +109 -0
  13. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
  14. data/lib/mongoid/errors.rb +1 -0
  15. data/lib/mongoid/extensions/hash.rb +27 -1
  16. data/lib/mongoid/field_readable.rb +70 -0
  17. data/lib/mongoid/matchable.rb +6 -1
  18. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +3 -5
  19. data/lib/mongoid/matcher/regex.rb +8 -9
  20. data/lib/mongoid/matcher/regexp_budget.rb +389 -0
  21. data/lib/mongoid/matcher.rb +1 -0
  22. data/lib/mongoid/threaded.rb +3 -0
  23. data/lib/mongoid/version.rb +1 -1
  24. data/lib/mongoid/warnings.rb +1 -0
  25. data/spec/integration/app_spec.rb +8 -0
  26. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
  27. data/spec/integration/dots_and_dollars_spec.rb +12 -2
  28. data/spec/integration/matcher_operator_data/regex.yml +21 -0
  29. data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
  30. data/spec/integration/query_operator_guard_spec.rb +89 -0
  31. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
  32. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +145 -0
  33. data/spec/mongoid/attributes/nested_spec.rb +204 -10
  34. data/spec/mongoid/clients/factory_spec.rb +30 -0
  35. data/spec/mongoid/contextual/aggregable/memory_spec.rb +91 -0
  36. data/spec/mongoid/contextual/memory_spec.rb +177 -0
  37. data/spec/mongoid/criteria/queryable/extensions/bignum_spec.rb +2 -1
  38. data/spec/mongoid/criteria/queryable/extensions/fixnum_spec.rb +2 -1
  39. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2 -0
  40. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +235 -0
  41. data/spec/mongoid/criteria_spec.rb +2 -0
  42. data/spec/mongoid/extensions/hash_spec.rb +236 -0
  43. data/spec/mongoid/matcher/regexp_budget_spec.rb +570 -0
  44. data/spec/shared/CANDIDATE.md +28 -0
  45. data/spec/shared/lib/mrss/spec_organizer.rb +32 -3
  46. data/spec/shared/shlib/server.sh +1 -1
  47. metadata +13 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78040726b40c1709f9218bc28850b802cf5aedfbdd178c2ed90322ffb16ba0ee
4
- data.tar.gz: 438d1fee26567d71150518c5df2f764d6671c49ea6cc02c0582a7cd15e80fd7a
3
+ metadata.gz: a9945b53ebb2236e5b44ef4cfead09b790b3b00881f62dba5c7007c1d99fdcba
4
+ data.tar.gz: f092a211395767e35a20bd2492e9c9be56c3bc53721bb6e8b437c0ef4d4c3111
5
5
  SHA512:
6
- metadata.gz: 8420a185b4baf6c1183ff188bc16f5d79fc75f44f1784c37df163836b70939250efbefceaeb53269a9d97f0b7bc9ae997a6586306223bea424db8738a3f2513e
7
- data.tar.gz: 1f8dc137d3de2c5bc6ff676f8330471e4a792fe259839e6edec6107e145fd3b81bd4013df64e4b2410a62793ce2d35855069672a4a8784cf2de076f3ac4fda61
6
+ metadata.gz: 7fa47f5a3a012f20238d7813bc173c3a22b0f96e493bb391fc5d5d4cb5ffc37d50bd27959f2fa871f11b402ef739d43ab3ff044a7cd256ef4531b340bf1f882d
7
+ data.tar.gz: 8bf1abb2f757cb18b92ccfae1214a7d19c6d6331e2ed30a4a39068eab365e8cf4cd0fc017810029106a4823ddb8a8a416a63de4faa334332e012f403167b7351
@@ -105,6 +105,32 @@ en:
105
105
  A collation option is only supported if the query is executed on a MongoDB server
106
106
  with version >= 3.4."
107
107
  resolution: "Remove the collation option from the query."
108
+ in_memory_regexp_timeout:
109
+ message: "Evaluating this query in memory exceeded the %{limit} second
110
+ limit that applies to conditions containing a regular expression."
111
+ summary: "Mongoid evaluates some conditions in the calling thread
112
+ rather than sending them to the server: queries against an embedded
113
+ association, removing documents from an association by condition,
114
+ and Document#_matches?. A regular expression in such a condition
115
+ runs locally. Mongoid limits the total time one in-memory
116
+ evaluation may spend matching, because both the cost of a single
117
+ match and the number of matches performed grow with the condition,
118
+ and a pattern built from end-user input can be made to consume an
119
+ unbounded amount of CPU. The limit is cumulative over the whole
120
+ evaluation rather than per match. Where the Ruby in use offers no
121
+ per-Regexp timeout the limit is enforced as wall clock over the
122
+ whole in-memory evaluation, so there it can be exceeded by a large
123
+ scan even when the pattern itself is cheap."
124
+ resolution: "Check whether the pattern in this query comes from user
125
+ input; if it does, validate it before querying. If the query is
126
+ legitimate and simply large, raise
127
+ Mongoid::Config.in_memory_regexp_time_limit, or set it to nil to
128
+ remove the limit entirely. The limit in force is the smaller of
129
+ that setting and any global Regexp.timeout the application has set,
130
+ so check both. On JRuby, and on MRI before 3.2, there is no
131
+ per-Regexp timeout, so what the limit bounds there is the elapsed
132
+ time of the whole in-memory evaluation rather than the time spent
133
+ matching."
108
134
  invalid_config_file:
109
135
  message: "Invalid configuration file: %{path}."
110
136
  summary: "Your mongoid.yml configuration file does not contain the
@@ -11,11 +11,15 @@ module Mongoid
11
11
  included do
12
12
  class_attribute :dependents
13
13
 
14
+ # Note the leading underscore: without it, ActiveSupport's
15
+ # class_attribute would generate a helper method for this attribute
16
+ # that collides with one it generates for :dependents.
17
+ #
14
18
  # @api private
15
- class_attribute :dependents_owner
19
+ class_attribute :_dependents_owner
16
20
 
17
21
  self.dependents = []
18
- self.dependents_owner = self
22
+ self._dependents_owner = self
19
23
  end
20
24
 
21
25
  class_methods do
@@ -50,9 +54,9 @@ module Mongoid
50
54
  def self.define_dependency!(association)
51
55
  validate!(association)
52
56
  association.inverse_class.tap do |klass|
53
- if klass.dependents_owner != klass
57
+ if klass._dependents_owner != klass
54
58
  klass.dependents = []
55
- klass.dependents_owner = klass
59
+ klass._dependents_owner = klass
56
60
  end
57
61
 
58
62
  if association.dependent && !klass.dependents.include?(association)
@@ -169,7 +169,7 @@ module Mongoid
169
169
  # @param [ Hash ] attrs The single document attributes to process.
170
170
  def update_nested_relation(parent, id, attrs)
171
171
  first = existing.first
172
- converted = first ? convert_id(first.class, id) : id
172
+ converted = convert_id(first ? first.class : association.klass, id)
173
173
 
174
174
  if existing.where(_id: converted).exists?
175
175
  # document exists in association
@@ -179,11 +179,46 @@ module Mongoid
179
179
  else
180
180
  update_document(doc, attrs)
181
181
  end
182
- else
182
+ elsif association.embedded?
183
+ raise Errors::DocumentNotFound.new(association.klass, id)
184
+ elsif destroyable?(attrs)
185
+ # A destroy of a document that is not in the association is
186
+ # ignored, rather than reaching for it outside the association.
187
+ nil
188
+ elsif Mongoid.allow_reparenting_via_nested_attributes?
189
+ Mongoid::Warnings.warn_reparenting_via_nested_attributes
190
+
183
191
  # push existing document to association
184
192
  doc = association.klass.unscoped.find(converted)
193
+ # find returns nil instead of raising when
194
+ # Mongoid.raise_not_found_error is false; a missing document
195
+ # must still be an error here, consistent with the other
196
+ # not-found branches.
197
+ raise Errors::DocumentNotFound.new(association.klass, id) if doc.nil?
198
+
185
199
  update_document(doc, attrs)
186
- existing.push(doc) unless destroyable?(attrs)
200
+ existing.push(doc)
201
+ else
202
+ raise Errors::DocumentNotFound.new(association.klass, not_found_params(parent, id))
203
+ end
204
+ end
205
+
206
+ # The params to report when an id in the nested attributes could not
207
+ # be resolved within the association.
208
+ #
209
+ # @api private
210
+ #
211
+ # @param [ Document ] parent The parent document.
212
+ # @param [ String | BSON::ObjectId ] id of the related document.
213
+ #
214
+ # @return [ Hash | Object ] The params for the not found error.
215
+ def not_found_params(parent, id)
216
+ if association.is_a?(Association::Referenced::HasAndBelongsToMany)
217
+ # A many-to-many association has no foreign key on the child, so
218
+ # the id is only ever resolved within the association itself.
219
+ id
220
+ else
221
+ { _id: id, association.foreign_key => parent.id }
187
222
  end
188
223
  end
189
224
  end
@@ -53,12 +53,26 @@ module Mongoid
53
53
  # @example Convert the id.
54
54
  # builder.convert_id(Person, "4d371b444835d98b8b000010")
55
55
  #
56
+ # Ids arriving from a form are always scalars. A Hash or an Array
57
+ # here means the parameters were crafted, and letting one through
58
+ # would turn the id into a query operator, so they are rejected.
59
+ #
56
60
  # @param [ Class ] klass The class we're trying to convert for.
57
61
  # @param [ String ] id The id, usually coming from the form.
58
62
  #
59
63
  # @return [ BSON::ObjectId | String | Object ] The converted id.
64
+ #
65
+ # @raise [ Errors::DocumentNotFound ] if the id is not a scalar, or
66
+ # cannot be converted to the type the class uses for its ids.
67
+ # The BSON::Error rescue is defensive: a value that reaches
68
+ # BSON::ObjectId.mongoize and raises there must not surface as an
69
+ # unhandled BSON error in the caller.
60
70
  def convert_id(klass, id)
71
+ raise Errors::DocumentNotFound.new(klass, id) if id.is_a?(::Hash) || id.is_a?(::Array)
72
+
61
73
  klass.using_object_ids? ? BSON::ObjectId.mongoize(id) : id
74
+ rescue BSON::Error
75
+ raise Errors::DocumentNotFound.new(klass, id)
62
76
  end
63
77
  end
64
78
  end
@@ -477,12 +477,111 @@ module Mongoid
477
477
  # @return [ Integer ] The number of documents deleted.
478
478
  def remove_all(conditions = nil, method = :delete_all)
479
479
  selector = conditions || {}
480
- removed = klass.send(method, selector.merge!(criteria.selector))
480
+ selector.merge!(criteria.selector)
481
+
482
+ # Scanning before the delete means scanning only what is already in
483
+ # memory, so it is only done where there is a pattern whose cost has
484
+ # to be bounded. Everywhere else the delete comes first and the scan
485
+ # sees the survivors, which is what this association has always
486
+ # done.
487
+ #
488
+ # The limit is read once and carried into whichever branch is taken.
489
+ # Letting the branch below ask again would read it a second time,
490
+ # across a server round trip, and a limit that had become positive
491
+ # in the meantime would put a deadline on the delete_if -- a query,
492
+ # and an unbind of every match -- which is the arrangement this
493
+ # branch exists to avoid.
494
+ limit = Matcher::RegexpBudget.limit_for(selector)
495
+ return remove_all_bounded(selector, method, limit) if limit
496
+
497
+ removed = klass.send(method, selector)
498
+
499
+ # No scope around this. The scan runs after the delete, so the
500
+ # association this loads comes back holding only what the delete did
501
+ # not match, and there is no pattern here to bound in any case.
502
+ # Opening a scope would save _matches? from deciding once per
503
+ # document -- 0.48us against 1.74us for the match itself -- but a
504
+ # scope with nothing to bound suppresses that decision for
505
+ # everything nested inside it, and loading the association runs
506
+ # find callbacks. A pattern in a selector one of those evaluates
507
+ # would go unguarded, which costs more than the saving is worth on a
508
+ # path that has just made two round trips.
481
509
  _target.delete_if do |doc|
482
510
  doc._matches?(selector).tap do |b|
483
511
  unbind_one(doc) if b
484
512
  end
485
513
  end
514
+
515
+ removed
516
+ end
517
+
518
+ # Deletes all related documents matching a selector that carries a
519
+ # regular expression, with the whole scan under one regexp budget.
520
+ #
521
+ # The scan runs before anything is deleted, so a budget that runs out
522
+ # leaves both the database and the association untouched rather than
523
+ # reporting a failure for a delete that has already happened. It also
524
+ # means the budget is closed by the time the association is mutated,
525
+ # so where the budget is enforced with Timeout there is no window for
526
+ # the exception to land in the middle of an unbind.
527
+ #
528
+ # @param [ Hash ] selector The selector to delete with.
529
+ # @param [ Symbol ] method The deletion method to call.
530
+ # @param [ Float ] limit The seconds the scan may spend, as read by
531
+ # the caller when it chose this path.
532
+ #
533
+ # @return [ Integer ] The number of documents deleted.
534
+ def remove_all_bounded(selector, method, limit)
535
+ # Only what the association already holds. Iterating it instead
536
+ # would load the whole association before the delete, and for a
537
+ # broad pattern that is every document the association has: /.*/ is
538
+ # both the cheapest pattern an attacker can supply and the one that
539
+ # matches everything, so bounding the cost of the matching would
540
+ # have been paid for with an unbounded amount of memory. Nothing is
541
+ # missed by not loading, because the server evaluates the same
542
+ # selector for the delete itself.
543
+ #
544
+ # It also means the scan runs no query, so no deadline of ours can
545
+ # land on one. Where the budget is enforced with Timeout, covering
546
+ # a query would report a slow network as a regexp timeout and could
547
+ # deliver the asynchronous exception inside the driver's socket
548
+ # read.
549
+ documents = _target.in_memory
550
+
551
+ matching = Matcher::RegexpBudget.open_with(limit) do
552
+ documents.select { |doc| doc._matches?(selector) }
553
+ end
554
+
555
+ removed = klass.send(method, selector)
556
+
557
+ # The ids are already known to be in memory, so the loaded and added
558
+ # documents can be dropped directly. Enumerable#delete would look
559
+ # each one up by id and stop at the first hash that has it, which
560
+ # removes the wrong instance when both hashes hold one for the same
561
+ # id. Enumerable#delete_if drops it from both, as this does, but
562
+ # only after load_all!, which is the load this scan exists to avoid.
563
+ #
564
+ # Which documents get unbound has always depended on which ones
565
+ # happened to be in memory, and scanning only what is in memory
566
+ # keeps that. It matters for has_and_belongs_to_many, whose
567
+ # unbind_one pulls the id out of the foreign key array on _base and
568
+ # marks it dirty; the scan used to run after the delete, against an
569
+ # association that a cold proxy had just reloaded as holding only
570
+ # the survivors, so nothing matched and nothing was unbound.
571
+ #
572
+ # TODO: unbinding should not depend on what was in memory. Every
573
+ # matched document leaves the association, so every one of them
574
+ # should be unbound, which would also stop _base's foreign key
575
+ # array from keeping ids that no longer resolve. That is a
576
+ # behaviour change for has_and_belongs_to_many and belongs in a
577
+ # ticket of its own, with a release note; it should not ride along
578
+ # on this one.
579
+ matching.each do |doc|
580
+ unbind_one(doc)
581
+ _target._loaded.delete(doc._id)
582
+ _target._added.delete(doc._id)
583
+ end
584
+
486
585
  removed
487
586
  end
488
587
 
@@ -95,6 +95,10 @@ module Mongoid
95
95
  [MONGOID_WRAPPING_LIBRARY] + options[:wrapping_libraries]
96
96
  else
97
97
  [MONGOID_WRAPPING_LIBRARY]
98
+ end.tap do |wrap|
99
+ if defined?(::Rails) && ::Rails.respond_to?(:version)
100
+ wrap << { name: 'Rails', version: ::Rails.version }
101
+ end
98
102
  end
99
103
  options[:wrapping_libraries] = wrap_lib
100
104
  end
@@ -165,6 +165,68 @@ module Mongoid
165
165
  # See https://jira.mongodb.org/browse/MONGOID-5658 for more details.
166
166
  option :around_callbacks_for_embeds, default: true
167
167
 
168
+ # The maximum number of seconds that evaluating a single query in memory
169
+ # may spend executing regular expressions. Queries against an embedded
170
+ # association are evaluated in the calling thread, so a pattern built from
171
+ # user input runs locally and can otherwise consume unbounded CPU. The
172
+ # limit is cumulative over the whole query, since cost grows with the
173
+ # number of documents and conditions as well as with the pattern.
174
+ #
175
+ # Set to nil to remove the limit. On Ruby 3.2 and later the remaining
176
+ # budget is compiled into the pattern, so the limit counts only the time
177
+ # spent matching. Earlier Rubies have no per-Regexp timeout, so the query
178
+ # is bounded with Timeout instead and the limit is wall clock over the
179
+ # whole in-memory evaluation.
180
+ #
181
+ # See https://jira.mongodb.org/browse/MONGOID-5981 for details.
182
+ option :in_memory_regexp_time_limit, default: 5.0
183
+
184
+ # When false (default), query operators are restricted when building a
185
+ # selector, so that user-supplied input reaching the query builder cannot
186
+ # make MongoDB execute arbitrary JavaScript. Two rules are enforced:
187
+ #
188
+ # - An operator at the top level of an expression must appear in
189
+ # +Criteria::Queryable::Selectable::ALLOWED_QUERY_OPERATORS+.
190
+ # - +$where+, +$function+, and +$accumulator+ are rejected at any depth,
191
+ # including inside +$expr+ and the logical operators.
192
+ #
193
+ # This applies to every query method that accepts an expression, including
194
+ # +where+, +find_by+, +and+, +or+, +nor+, +not+, +any_of+, and +none_of+.
195
+ # It also governs the string form of +where+, e.g.
196
+ # +where("this.name == 'admin'")+, which the server evaluates as +$where+.
197
+ # Applications relying on that form must set this option to true.
198
+ #
199
+ # The APIs that request JavaScript explicitly, +Criteria#for_js+ and
200
+ # +js_query+, are unaffected: there the developer has asked for it.
201
+ #
202
+ # Set to true to restore the unrestricted pass-through behavior.
203
+ #
204
+ # Note that this option is deliberately not tied to +load_defaults+: an
205
+ # application that has opted into older defaults still gets the guard, and
206
+ # must set this option explicitly to turn it off.
207
+ #
208
+ # See https://jira.mongodb.org/browse/MONGOID-5939,
209
+ # https://jira.mongodb.org/browse/MONGOID-5993,
210
+ # https://jira.mongodb.org/browse/MONGOID-5994 for details.
211
+ option :allow_unsafe_query_operators, default: false
212
+
213
+ # When this flag is true, it will be possible to add a record to a
214
+ # "has_many" or "has_and_belongs_to_many" association by passing that
215
+ # record's id in the nested attributes for another parent record, even
216
+ # when the record does not already belong to that association. For a
217
+ # "has_many" association this moves the record to the new parent.
218
+ #
219
+ # When this flag is false, an id in nested attributes is only resolved
220
+ # within the association itself, and anything else raises an error.
221
+ #
222
+ # The default is `false`. Note that allowing reparenting via nested attributes
223
+ # is a potential security risk, since it could allow a malicious user to move
224
+ # records that they do not own to a parent record that they do own.
225
+ #
226
+ # This option will be removed in Mongoid 10, and the only behavior will be
227
+ # as if this option were set to false.
228
+ option :allow_reparenting_via_nested_attributes, default: false
229
+
168
230
  # Has Mongoid been configured? This is checking that at least a valid
169
231
  # client config exists.
170
232
  #
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'mongoid/field_readable'
4
+
3
5
  module Mongoid
4
6
  module Contextual
5
7
  module Aggregable
6
8
  # Contains behavior for aggregating values in memory.
7
9
  module Memory
10
+ include FieldReadable
8
11
 
9
12
  # Get all the aggregate values for the provided field.
10
13
  # Provided for interface consistency with Aggregable::Mongo.
@@ -29,7 +32,7 @@ module Mongoid
29
32
  #
30
33
  # @return [ Numeric ] The average.
31
34
  def avg(field)
32
- total = count { |doc| !doc.send(field).nil? }
35
+ total = count { |doc| !read_field_value(doc, field).nil? }
33
36
  return nil unless total > 0
34
37
 
35
38
  total = total.to_f if total.is_a?(Integer)
@@ -114,7 +117,7 @@ module Mongoid
114
117
  def aggregate_by(field, method)
115
118
  return nil unless any?
116
119
 
117
- map { |doc| doc.public_send(field) }.compact.public_send(method)
120
+ map { |doc| read_field_value(doc, field) }.compact.public_send(method)
118
121
  end
119
122
  end
120
123
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "mongoid/contextual/aggregable/memory"
4
4
  require "mongoid/association/eager_loadable"
5
+ require "mongoid/field_readable"
5
6
 
6
7
  module Mongoid
7
8
  module Contextual
@@ -11,6 +12,7 @@ module Mongoid
11
12
  include Association::EagerLoadable
12
13
  include Queryable
13
14
  include Positional
15
+ include FieldReadable
14
16
 
15
17
  # @attribute [r] root The root document.
16
18
  # @attribute [r] path The atomic path.
@@ -141,10 +143,20 @@ module Mongoid
141
143
  # @param [ Criteria ] criteria The criteria.
142
144
  def initialize(criteria)
143
145
  @criteria, @klass = criteria, criteria.klass
144
- @documents = criteria.documents.select do |doc|
145
- @root ||= doc._root
146
- @collection ||= root.collection
147
- doc._matches?(criteria.selector)
146
+
147
+ # Resolving the collection can build a Mongo::Client the first time it
148
+ # runs, so it happens before the budget opens. Where the budget is
149
+ # enforced with Timeout the exception is asynchronous, and landing in
150
+ # the middle of that would leave a half-built client behind.
151
+ if (first = criteria.documents.first)
152
+ @root = first._root
153
+ @collection = @root.collection
154
+ end
155
+
156
+ # One regexp budget covers the whole scan, so that the limit bounds the
157
+ # query rather than each document individually.
158
+ @documents = Matcher::RegexpBudget.open(criteria.selector) do
159
+ criteria.documents.select { |doc| doc._matches?(criteria.selector) }
148
160
  end
149
161
  apply_sorting
150
162
  apply_options
@@ -534,11 +546,10 @@ module Mongoid
534
546
  # _translations hash so that we can get the specified translation in
535
547
  # the remaining
536
548
  if field&.localized?
537
- document.send("#{segment}_translations")
549
+ document.public_send("#{segment}_translations")
538
550
  end
539
551
  end
540
- meth = klass.aliased_associations[segment] || segment
541
- res.nil? ? document.try(meth) : res
552
+ res.nil? ? read_field_value(document, segment) : res
542
553
  elsif document.is_a?(Hash)
543
554
  # TODO: Remove the indifferent access when implementing MONGOID-5410.
544
555
  document.key?(segment.to_s) ?
@@ -325,6 +325,10 @@ module Mongoid
325
325
  end
326
326
  end
327
327
  end
328
+ # Every query method that takes a user-supplied expression normalizes
329
+ # it here, so this is where the operator guard is enforced. See
330
+ # Selectable#_mongoid_validate_operators! for what it does not cover.
331
+ _mongoid_validate_operators!(result)
328
332
  result
329
333
  end
330
334
 
@@ -343,6 +347,14 @@ module Mongoid
343
347
  if criterion.is_a?(Selectable)
344
348
  criterion = criterion.selector
345
349
  end
350
+
351
+ # The __override__ path writes to the selector directly, so it
352
+ # bypasses _mongoid_expand_keys where the operator guard normally
353
+ # runs. Enforce the guard here so chained override methods
354
+ # (elem_match, exists, eq, gt, in, etc.) cannot smuggle $where or
355
+ # other JavaScript operators under strict mode.
356
+ _mongoid_validate_operators!(criterion)
357
+
346
358
  selection(criterion) do |selector, field, value|
347
359
  expression = prepare(field, operator, value)
348
360
  existing = selector[field]
@@ -791,6 +791,12 @@ module Mongoid
791
791
  # only ever specify one criterion to #where.
792
792
  @criterion = criterion
793
793
  if criterion.is_a?(String)
794
+ unless Mongoid.allow_unsafe_query_operators?
795
+ raise Errors::InvalidQuery,
796
+ "String criteria are not allowed because they compile to the '$where' operator, " \
797
+ 'which is not allowed in a query expression. Set Mongoid.allow_unsafe_query_operators = true ' \
798
+ 'to permit all operators.'
799
+ end
794
800
  js_query(criterion)
795
801
  else
796
802
  expr_query(criterion)
@@ -800,6 +806,19 @@ module Mongoid
800
806
 
801
807
  private
802
808
 
809
+ # Operators permitted at the top level of a query expression without
810
+ # opt-in. Excludes $where (JS execution) and other operators not needed
811
+ # for ordinary application queries.
812
+ ALLOWED_QUERY_OPERATORS = %w[
813
+ $and $or $nor $not $text $comment $expr $jsonSchema $alwaysFalse $alwaysTrue
814
+ ].freeze
815
+
816
+ # Operators that execute server-side JavaScript. These are rejected at
817
+ # any depth, not just at the top level: the allowlist above permits
818
+ # $expr and the logical operators, and their values are arbitrary
819
+ # nested expressions that can carry $function or $where.
820
+ JAVASCRIPT_QUERY_OPERATORS = %w[$where $function $accumulator].freeze
821
+
803
822
  # Adds the specified expression to the query.
804
823
  #
805
824
  # Criterion must be a hash in one of the following forms:
@@ -825,6 +844,8 @@ module Mongoid
825
844
  raise Errors::InvalidQuery, "Expression must be a Hash: #{Errors::InvalidQuery.truncate_expr(criterion)}"
826
845
  end
827
846
 
847
+ # The operator guard is applied by _mongoid_expand_keys, which every
848
+ # query method that accepts a user-supplied expression passes through.
828
849
  normalized = _mongoid_expand_keys(criterion)
829
850
  clone.tap do |query|
830
851
  normalized.each do |field, value|
@@ -840,6 +861,94 @@ module Mongoid
840
861
  end
841
862
  end
842
863
 
864
+ # Enforces the operator rules governed by the
865
+ # +allow_unsafe_query_operators+ configuration option against a
866
+ # normalized query expression.
867
+ #
868
+ # Two rules apply, and both are skipped when the option is true:
869
+ #
870
+ # - An operator at the top level of the expression must appear in
871
+ # ALLOWED_QUERY_OPERATORS.
872
+ # - An operator in JAVASCRIPT_QUERY_OPERATORS is rejected at any depth.
873
+ #
874
+ # This is called from #_mongoid_expand_keys rather than from the
875
+ # individual query methods, because that is the one point every query
876
+ # method taking a user-supplied expression passes through on its way to
877
+ # the selector.
878
+ #
879
+ # It deliberately does not cover the APIs that ask for JavaScript
880
+ # outright, such as #js_query and Criteria#for_js: there the developer
881
+ # has chosen server-side JavaScript, so there is nothing to guard
882
+ # against. The same goes for the low-level Storable methods
883
+ # (#add_field_expression, #add_operator_expression), which write to the
884
+ # selector directly.
885
+ #
886
+ # @param [ Hash ] expr A normalized query expression.
887
+ #
888
+ # @raise [ Errors::InvalidQuery ] If a disallowed operator is present.
889
+ #
890
+ # @api private
891
+ def _mongoid_validate_operators!(expr)
892
+ return if Mongoid.allow_unsafe_query_operators?
893
+
894
+ expr.each_key do |field|
895
+ field_s = field.to_s
896
+ next unless field_s.start_with?('$')
897
+ next if ALLOWED_QUERY_OPERATORS.include?(field_s)
898
+
899
+ raise Errors::InvalidQuery,
900
+ "Operator '#{field_s}' is not allowed in a query expression. " \
901
+ 'Set Mongoid.allow_unsafe_query_operators = true to permit all operators.'
902
+ end
903
+
904
+ _mongoid_validate_no_javascript!(expr)
905
+ end
906
+
907
+ # Walks a query expression looking for operators that execute
908
+ # server-side JavaScript, descending through both hashes and arrays so
909
+ # that nested forms such as {'$expr' => {'$function' => ...}} and
910
+ # {'$or' => [ {'$where' => ...} ]} are caught.
911
+ #
912
+ # The walk does not distinguish operator position from value position,
913
+ # so it also rejects queries where a JavaScript operator name appears as
914
+ # data rather than as an operator. Server 5.0+ permits $-prefixed field
915
+ # names in stored documents, which makes this reachable:
916
+ #
917
+ # Doc.where(payload: { '$eq' => { '$function' => 'abc' } })
918
+ #
919
+ # Here the $eq marks its argument as a literal value to compare, so the
920
+ # server never evaluates it, but the guard raises anyway. The only
921
+ # workaround today is the global allow_unsafe_query_operators flag.
922
+ #
923
+ # TODO: discuss whether to track operator position (skipping the subtree
924
+ # under $eq, $ne, $in, $nin, and $elemMatch values) in a future
925
+ # iteration. It removes the false positive but adds exactly the kind of
926
+ # state that a real bypass could hide in, so it was left out for now.
927
+ #
928
+ # @param [ Object ] object A fragment of a query expression.
929
+ #
930
+ # @raise [ Errors::InvalidQuery ] If a JavaScript operator is present.
931
+ #
932
+ # @api private
933
+ def _mongoid_validate_no_javascript!(object)
934
+ case object
935
+ when Hash
936
+ object.each do |key, value|
937
+ key_s = key.to_s
938
+ if JAVASCRIPT_QUERY_OPERATORS.include?(key_s)
939
+ raise Errors::InvalidQuery,
940
+ "Operator '#{key_s}' executes server-side JavaScript and is not allowed " \
941
+ 'anywhere in a query expression. Set Mongoid.allow_unsafe_query_operators = true ' \
942
+ 'to permit all operators.'
943
+ end
944
+
945
+ _mongoid_validate_no_javascript!(value)
946
+ end
947
+ when Array
948
+ object.each { |value| _mongoid_validate_no_javascript!(value) }
949
+ end
950
+ end
951
+
843
952
  # Force the values of the criterion to be evolved.
844
953
  #
845
954
  # @api private
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Errors
5
+ # This error is raised when evaluating a query in memory exceeds
6
+ # Mongoid::Config.in_memory_regexp_time_limit.
7
+ #
8
+ # What the limit bounds depends on the Ruby in use. Where per-Regexp
9
+ # timeouts are available it is the time spent executing regular
10
+ # expressions; elsewhere it is the elapsed time of the whole in-memory
11
+ # evaluation. The message is worded to hold either way.
12
+ class InMemoryRegexpTimeout < MongoidError
13
+ # Create the new error.
14
+ #
15
+ # @example Create the new in-memory regexp timeout error.
16
+ # InMemoryRegexpTimeout.new(5.0)
17
+ #
18
+ # @param [ Float ] limit The limit that was exceeded, in seconds. Not
19
+ # always the configured one: a global Regexp.timeout stricter than the
20
+ # configuration takes its place.
21
+ def initialize(limit)
22
+ super(compose_message('in_memory_regexp_timeout', limit: limit))
23
+ end
24
+ end
25
+ end
26
+ end
@@ -8,6 +8,7 @@ require "mongoid/errors/document_not_destroyed"
8
8
  require "mongoid/errors/document_not_found"
9
9
  require "mongoid/errors/empty_config_file"
10
10
  require "mongoid/errors/in_memory_collation_not_supported"
11
+ require "mongoid/errors/in_memory_regexp_timeout"
11
12
  require "mongoid/errors/invalid_collection"
12
13
  require "mongoid/errors/invalid_config_file"
13
14
  require "mongoid/errors/invalid_config_option"