mongoid 8.1.12 → 8.1.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 (38) 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/config.rb +62 -0
  8. data/lib/mongoid/contextual/aggregable/memory.rb +5 -2
  9. data/lib/mongoid/contextual/memory.rb +18 -7
  10. data/lib/mongoid/criteria/queryable/mergeable.rb +12 -0
  11. data/lib/mongoid/criteria/queryable/selectable.rb +109 -0
  12. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
  13. data/lib/mongoid/errors.rb +1 -0
  14. data/lib/mongoid/field_readable.rb +70 -0
  15. data/lib/mongoid/matchable.rb +6 -1
  16. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +3 -5
  17. data/lib/mongoid/matcher/regex.rb +8 -9
  18. data/lib/mongoid/matcher/regexp_budget.rb +389 -0
  19. data/lib/mongoid/matcher.rb +1 -0
  20. data/lib/mongoid/threaded.rb +3 -0
  21. data/lib/mongoid/version.rb +1 -1
  22. data/lib/mongoid/warnings.rb +1 -0
  23. data/spec/integration/app_spec.rb +8 -0
  24. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
  25. data/spec/integration/dots_and_dollars_spec.rb +12 -2
  26. data/spec/integration/matcher_operator_data/regex.yml +21 -0
  27. data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
  28. data/spec/integration/query_operator_guard_spec.rb +89 -0
  29. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
  30. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +145 -0
  31. data/spec/mongoid/attributes/nested_spec.rb +204 -10
  32. data/spec/mongoid/contextual/aggregable/memory_spec.rb +91 -0
  33. data/spec/mongoid/contextual/memory_spec.rb +177 -0
  34. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2 -0
  35. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +235 -0
  36. data/spec/mongoid/criteria_spec.rb +2 -0
  37. data/spec/mongoid/matcher/regexp_budget_spec.rb +570 -0
  38. metadata +11 -2
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:todo all
3
+
4
+ module Mongoid
5
+ # Reads the value of a field name from a document without dispatching the
6
+ # name as an arbitrary method.
7
+ #
8
+ # Field names may come from application input, where a name like +destroy+
9
+ # or +attributes+ would delete the document or disclose its contents.
10
+ # Declared field and association names are validated when they are declared
11
+ # (see Mongoid.destructive_fields), so a name that resolves to one of them
12
+ # is safe to send to a document. Any other name is read from the attributes
13
+ # hash, which is what the database-backed query contexts do.
14
+ #
15
+ # @api private
16
+ module FieldReadable
17
+ private
18
+
19
+ # Read the value of the given field name from the given document.
20
+ #
21
+ # @param [ Document ] document The document to read from.
22
+ # @param [ String | Symbol ] name The name of the field.
23
+ #
24
+ # @return [ Object | nil ] The value of the field, or nil when the name
25
+ # is neither a declared field nor present in the attributes.
26
+ def read_field_value(document, name)
27
+ name = name.to_s
28
+ # A blank name cannot name a field. Return nil, which is what reading
29
+ # one has always done.
30
+ return nil if name.blank?
31
+
32
+ if (meth = readable_method_for(document.class, name))
33
+ document.public_send(meth)
34
+ else
35
+ document.attributes[document.class.database_field_name(name)]
36
+ end
37
+ end
38
+
39
+ # Resolve the given name to a method that is declared by the given class,
40
+ # and therefore safe to send to one of its instances.
41
+ #
42
+ # @param [ Class ] klass The document class.
43
+ # @param [ String ] name The name of the field.
44
+ #
45
+ # @return [ String | nil ] The method to send, or nil when the name is
46
+ # not declared by the class.
47
+ def readable_method_for(klass, name)
48
+ # Fields, associations, and field aliases each define a reader of their
49
+ # own name. Note that associations must be resolved before aliases: a
50
+ # belongs_to aliases its own name to its foreign key, and reading
51
+ # `band` must give the document, not the id.
52
+ return name if klass.relations.key?(name) ||
53
+ klass.fields.key?(name) ||
54
+ klass.aliased_fields.key?(name)
55
+
56
+ # An association may also be named by its `store_as`, which has no
57
+ # reader of its own, or by its ids accessor, which does.
58
+ if (assoc = klass.relations[klass.aliased_associations[name]])
59
+ return (assoc.store_as == name) ? assoc.name.to_s : name
60
+ end
61
+
62
+ # Localized fields also get a _translations reader, which does not
63
+ # appear in the fields hash.
64
+ base = name.delete_suffix(Fields::TRANSLATIONS_SFX)
65
+ return nil if base == name
66
+
67
+ name if klass.fields[klass.database_field_name(base)]&.localized?
68
+ end
69
+ end
70
+ end
@@ -17,7 +17,12 @@ module Mongoid
17
17
  #
18
18
  # @return [ true | false ] True if matches, false if not.
19
19
  def _matches?(selector)
20
- Matcher::Expression.matches?(self, selector)
20
+ # Opens a regexp budget for this document only. Callers that match many
21
+ # documents against one selector open a budget of their own first, and
22
+ # this one joins it rather than giving every document a fresh limit.
23
+ Matcher::RegexpBudget.open(selector) do
24
+ Matcher::Expression.matches?(self, selector)
25
+ end
21
26
  end
22
27
  end
23
28
  end
@@ -6,12 +6,10 @@ module Mongoid
6
6
  #
7
7
  # @api private
8
8
  module EqImplWithRegexp
9
- module_function def matches?(original_operator, value, condition)
9
+ module_function def matches?(_original_operator, value, condition)
10
10
  case condition
11
- when Regexp
12
- value =~ condition
13
- when ::BSON::Regexp::Raw
14
- value =~ condition.compile
11
+ when Regexp, ::BSON::Regexp::Raw
12
+ value.respond_to?(:=~) && RegexpBudget.match?(value, condition)
15
13
  else
16
14
  if Mongoid.compare_time_by_ms &&
17
15
  value.kind_of?(Time) && condition.kind_of?(Time)
@@ -3,25 +3,24 @@ module Mongoid
3
3
 
4
4
  # @api private
5
5
  module Regex
6
- module_function def matches?(exists, value, condition)
7
- condition = case condition
8
- when Regexp
9
- condition
10
- when BSON::Regexp::Raw
11
- condition.compile
12
- else
6
+ module_function def matches?(_exists, value, condition)
7
+ unless condition.is_a?(Regexp) || condition.is_a?(BSON::Regexp::Raw)
13
8
  # Note that strings must have been converted to a regular expression
14
9
  # instance already (with $options taken into account, if provided).
15
10
  raise Errors::InvalidQuery, "$regex requires a regular expression argument: #{Errors::InvalidQuery.truncate_expr(condition)}"
16
11
  end
17
12
 
13
+ # The condition is compiled by RegexpBudget rather than here, so that
14
+ # the budget's timeout can be baked into the pattern.
18
15
  case value
19
16
  when Array
17
+ # Object#=~ is gone as of Ruby 3.2, so an element that cannot be
18
+ # matched against has to be rejected rather than passed to =~.
20
19
  value.any? do |v|
21
- v =~ condition
20
+ v.respond_to?(:=~) && RegexpBudget.match?(v, condition)
22
21
  end
23
22
  when String
24
- value =~ condition
23
+ RegexpBudget.match?(value, condition)
25
24
  else
26
25
  false
27
26
  end
@@ -0,0 +1,389 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'timeout'
4
+
5
+ module Mongoid
6
+ module Matcher
7
+ # Bounds the time spent executing regular expressions while evaluating a
8
+ # single in-memory match operation.
9
+ #
10
+ # A query condition can carry an application-supplied pattern, and the
11
+ # in-memory matcher compiles and runs that pattern in the caller's thread.
12
+ # Both the cost of one match and the number of matches performed are under
13
+ # the control of whoever supplied the condition, so the limit is cumulative
14
+ # over an entire operation rather than per match.
15
+ #
16
+ # The budget is held in thread- or fiber-local storage, so concurrent
17
+ # queries are accounted for independently.
18
+ #
19
+ # @api private
20
+ module RegexpBudget
21
+ # Whether a per-Regexp timeout can be relied on to reach Regexp.new.
22
+ #
23
+ # MRI added them in 3.2. JRuby 10.0.6 defines Regexp::TimeoutError,
24
+ # reports Ruby 3.4, and does honour a timeout that reaches it, but its
25
+ # Regexp.new accepts the keyword only for the first couple of calls
26
+ # through a given call site and raises ArgumentError from then on.
27
+ # Because that breakage is per call site, no load-time probe can predict
28
+ # it: a probe at its own call site reports a capability that the call in
29
+ # Budget#compile does not have. So non-MRI engines are excluded outright
30
+ # and use the Timeout fallback, which does interrupt a Joni match already
31
+ # under way. Worth revisiting if JRuby fixes the keyword handling.
32
+ PER_REGEXP_TIMEOUT =
33
+ if RUBY_ENGINE == 'ruby' && defined?(::Regexp::TimeoutError)
34
+ true
35
+ else
36
+ false
37
+ end
38
+
39
+ # The exception raised by a per-Regexp timeout. Tied to the constant
40
+ # rather than to the probe, so that a timeout set some other way (an
41
+ # application assigning Regexp.timeout, say) is still translated. On
42
+ # Rubies with no such constant, a class that is never raised stands in.
43
+ TIMEOUT_ERROR = defined?(::Regexp::TimeoutError) ? ::Regexp::TimeoutError : Class.new(StandardError)
44
+
45
+ # Raised by Timeout on Rubies without per-Regexp timeouts, and converted
46
+ # immediately. It is private to this module so that an application's own
47
+ # Timeout, firing inside our block, is never mistaken for ours.
48
+ class TimedOut < StandardError; end
49
+
50
+ # The marker stored in thread-local storage when a scope is open but has
51
+ # no budget to enforce. A nil-valued thread variable reads as absent
52
+ # (Thread.current[:key] returns nil for a missing key), so an
53
+ # open-but-unbounded scope has to store a real marker; a nested call
54
+ # checks for the key and joins the enclosing scope rather than deciding
55
+ # again for itself.
56
+ NO_BUDGET = Object.new
57
+
58
+ # The state of one open budget: what is left of the limit, and the
59
+ # patterns compiled under it.
60
+ #
61
+ # @api private
62
+ class Budget
63
+ # @return [ Float ] The limit this budget started with.
64
+ attr_reader :limit
65
+
66
+ # @return [ Float ] The seconds left before the budget is spent.
67
+ attr_reader :remaining
68
+
69
+ # @param [ Float ] limit The seconds this budget may spend.
70
+ def initialize(limit)
71
+ @limit = limit
72
+ @remaining = limit
73
+ @cache = {}
74
+ # A per-Regexp timeout bounds one match; the cumulative budget bounds
75
+ # the operation. So the timeout is fixed for the life of the scope
76
+ # rather than following the drawdown, which is what lets a pattern be
77
+ # compiled once instead of once per match. It means a match that
78
+ # starts with almost nothing left can still run for a whole limit, so
79
+ # an operation can overshoot by at most one limit -- bounded, which is
80
+ # the point, and far cheaper than recompiling.
81
+ #
82
+ # An application that has set a stricter global Regexp.timeout keeps
83
+ # it: baking in a larger value would leave it less protected than it
84
+ # asked to be. A timeout set on one individual pattern is a different
85
+ # matter, and is not preserved -- Budget#compile rebuilds the pattern
86
+ # from its source and flags, neither of which carries one, so this
87
+ # value takes its place. Only trusted code can supply such a pattern:
88
+ # a condition decoded from JSON or BSON arrives as a string or a
89
+ # BSON::Regexp::Raw, with no timeout of its own.
90
+ @timeout = [ limit, ::Regexp.timeout ].compact.min if PER_REGEXP_TIMEOUT
91
+ end
92
+
93
+ # Draws the elapsed time down from the budget.
94
+ #
95
+ # @param [ Float ] elapsed The seconds to charge.
96
+ def charge(elapsed)
97
+ @remaining -= elapsed
98
+ end
99
+
100
+ # @return [ true | false ] Whether the budget is spent.
101
+ def exhausted?
102
+ @remaining <= 0
103
+ end
104
+
105
+ # Returns the condition as a Regexp which, where the Ruby in use
106
+ # supports it, gives up once its timeout is spent.
107
+ #
108
+ # The condition is taken uncompiled so that the cache can answer before
109
+ # any compiling happens. A BSON::Regexp::Raw memoizes its own compile,
110
+ # but FieldExpression builds a fresh one for every $regex it evaluates,
111
+ # so that memo is worth nothing across documents and the source would
112
+ # otherwise be compiled once per document.
113
+ #
114
+ # @param [ Regexp | BSON::Regexp::Raw ] condition The condition.
115
+ #
116
+ # @return [ Regexp ] The compiled pattern.
117
+ def compile(condition)
118
+ @cache[cache_key(condition)] ||= bake(RegexpBudget.coerce(condition))
119
+ end
120
+
121
+ private
122
+
123
+ # BSON::Regexp::Raw aliases eql? to == but leaves hash alone, so two
124
+ # equal instances hash differently and cannot key the cache. What they
125
+ # are equal by can. Anything else keys on itself and is left to coerce
126
+ # to reject.
127
+ def cache_key(condition)
128
+ case condition
129
+ when BSON::Regexp::Raw then [ condition.pattern, condition.options ]
130
+ else condition
131
+ end
132
+ end
133
+
134
+ # Rebuilds the pattern with the budget's timeout, where the Ruby in use
135
+ # has them.
136
+ def bake(regexp)
137
+ return regexp unless PER_REGEXP_TIMEOUT
138
+
139
+ ::Regexp.new(regexp.source, regexp.options, timeout: @timeout)
140
+ end
141
+ end
142
+
143
+ class << self
144
+ # Opens a budget scope for the duration of the block.
145
+ #
146
+ # Nested calls join the enclosing budget instead of starting a new one,
147
+ # which is what lets a scan over many documents share a single limit.
148
+ # It also keeps the recursion in Expression.matches? (through
149
+ # $elemMatch, $and, $or and $nor) from resetting the budget.
150
+ #
151
+ # No budget is opened for a selector that carries no regular
152
+ # expression. There would be nothing for it to bound, and on the
153
+ # Timeout path it would put a deadline on in-memory work that has
154
+ # nothing to do with regular expressions.
155
+ #
156
+ # The scope covers everything nested inside the block, a selector other
157
+ # than this one included: a nested call joins the scope rather than
158
+ # deciding for itself, which is what keeps a scan from walking the
159
+ # selector once per document. Where the scope has nothing to bound, that
160
+ # means nested selectors are not bounded either -- so do not open one
161
+ # around work that can run application code. Loading documents runs find
162
+ # callbacks, and a query in one of those brings its own selector.
163
+ #
164
+ # Where a selector does carry one, the Timeout path still measures the
165
+ # whole scope rather than the matching alone, so a long scan can trip
166
+ # the limit with a cheap pattern. That imprecision is accepted: the
167
+ # alternative is a Timeout around each individual match, which was
168
+ # measured at about nine seconds per million matches, and the limit
169
+ # exists to bound a scan of exactly that size. The error message is
170
+ # worded to hold either way, and Rubies with per-Regexp timeouts --
171
+ # every supported MRI from 3.2 on -- do not take this path at all.
172
+ #
173
+ # Code inside the block that mutates state should be wrapped in
174
+ # .protect, since on Rubies without a per-Regexp timeout the budget is
175
+ # enforced with an asynchronous exception that can land anywhere.
176
+ #
177
+ # @param [ Hash ] selector The selector about to be evaluated.
178
+ #
179
+ # @return [ Object ] The value of the block.
180
+ def open(selector, &block)
181
+ # The key is present (holding NO_BUDGET) where an enclosing scope
182
+ # found nothing to bound, so that a nested call does not scan the
183
+ # selector again. Deciding before this check, in a default argument
184
+ # say, would walk the selector once per document on a scan. The
185
+ # storage is Thread.current directly, matching this branch's Threaded
186
+ # conventions; only the key is shared with Threaded.
187
+ return yield if Thread.current[Threaded::REGEXP_BUDGET_KEY]
188
+
189
+ open_with(limit_for(selector), &block)
190
+ end
191
+
192
+ # Opens a budget scope for a limit the caller has already decided on.
193
+ #
194
+ # A caller that rearranges its work around the decision -- loading
195
+ # documents up front so that nothing is mutated before the scan
196
+ # finishes, say -- has to make it before it can act on it, and must not
197
+ # then make it a second time. Asking .limit_for and letting .open ask
198
+ # again reads the configured limit twice, and the two reads can differ:
199
+ # a limit that becomes positive in between would establish a budget in
200
+ # the branch that was chosen for not needing one, and on the Timeout
201
+ # path that arms a deadline over work the branch never made
202
+ # interruptible.
203
+ #
204
+ # See .open for what the scope does and does not bound, and for the
205
+ # note about mutating state inside it.
206
+ #
207
+ # @param [ Float | nil ] limit The seconds the scope may spend, or nil
208
+ # for a scope with nothing to bound.
209
+ #
210
+ # @return [ Object ] The value of the block.
211
+ def open_with(limit, &block)
212
+ return yield if Thread.current[Threaded::REGEXP_BUDGET_KEY]
213
+
214
+ budget = Budget.new(limit) if limit&.positive?
215
+
216
+ begin
217
+ # Set inside the begin so that an asynchronous exception from an
218
+ # enclosing timeout cannot leave the key behind on a pooled thread.
219
+ Thread.current[Threaded::REGEXP_BUDGET_KEY] = budget || NO_BUDGET
220
+
221
+ if budget.nil? || PER_REGEXP_TIMEOUT
222
+ yield
223
+ else
224
+ begin
225
+ Timeout.timeout(budget.limit, TimedOut, &block)
226
+ rescue TimedOut
227
+ raise timeout_error(budget)
228
+ end
229
+ end
230
+ ensure
231
+ # Setting nil on the way out removes the key on MRI, and reading a
232
+ # nil value back is treated as absent regardless, so a lingering
233
+ # nil on JRuby is harmless too.
234
+ Thread.current[Threaded::REGEXP_BUDGET_KEY] = nil
235
+ end
236
+ end
237
+
238
+ # The limit a scope evaluating this selector would be bounded by.
239
+ #
240
+ # A caller that has to rearrange its work to make the scan
241
+ # interruptible -- loading documents up front so that nothing is
242
+ # mutated before the scan finishes, say -- can ask this first and skip
243
+ # the rearrangement, and whatever it costs, when there is no pattern to
244
+ # bound. It then passes what it got to .open_with, so that the decision
245
+ # it acted on is the one the scope is opened with. Callers with nothing
246
+ # to rearrange should just call .open, which asks this itself.
247
+ #
248
+ # @param [ Hash ] selector The selector about to be evaluated.
249
+ #
250
+ # @return [ Float | nil ] The limit, or nil where there is nothing to
251
+ # bound.
252
+ def limit_for(selector)
253
+ # nil.to_f is 0.0, so an unset limit and a limit of zero or less are
254
+ # the same thing here: no limit. Zero is a common way to spell
255
+ # "disabled", and taking it literally would mean a budget that is
256
+ # spent before the first match and a query that can never run.
257
+ limit = Mongoid::Config.in_memory_regexp_time_limit.to_f
258
+ return nil unless limit.positive?
259
+
260
+ limit if contains_regexp?(selector)
261
+ end
262
+
263
+ # Matches a value against a regular expression condition, charging the
264
+ # time it takes against the open budget.
265
+ #
266
+ # @param [ Object ] value The value to match.
267
+ # @param [ Regexp | BSON::Regexp::Raw ] condition The condition.
268
+ #
269
+ # @raise [ Errors::InMemoryRegexpTimeout ] if the budget is exhausted.
270
+ #
271
+ # @return [ Integer | nil ] The offset of the match, or nil.
272
+ def match?(value, condition)
273
+ budget = current
274
+ return value =~ coerce(condition) unless budget
275
+
276
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
277
+ pattern = nil
278
+ begin
279
+ raise timeout_error(budget) if budget.exhausted?
280
+
281
+ # Compiling is charged too. It is not free, and for a pattern with
282
+ # very many branches it costs far more than running the pattern
283
+ # does, so leaving it out would leave a way to spend unbounded time
284
+ # without the budget ever noticing.
285
+ pattern = budget.compile(condition)
286
+ value =~ pattern
287
+ rescue TIMEOUT_ERROR
288
+ # Name the limit that actually fired, which is not always the
289
+ # budget's. A baked pattern carries it: the smaller of the budget's
290
+ # limit and any global Regexp.timeout the application has set.
291
+ # Where nothing was baked -- an engine that raises this error but
292
+ # will not take a per-Regexp timeout, which is JRuby -- the global
293
+ # is the only thing that can have fired, and the pattern reports
294
+ # nil. Naming the budget's limit in either case would state a time
295
+ # that was never spent and send the reader after a setting that is
296
+ # not the one in the way.
297
+ #
298
+ # Both readers arrived together with Regexp::TimeoutError, so every
299
+ # engine that can reach this rescue at all has them.
300
+ raise timeout_error(budget, pattern&.timeout || ::Regexp.timeout || budget.limit)
301
+ ensure
302
+ budget.charge(Process.clock_gettime(Process::CLOCK_MONOTONIC) - started)
303
+ end
304
+ end
305
+
306
+ # Runs the block without letting a scope timeout tear it in half.
307
+ #
308
+ # Where the budget is enforced with Timeout, the exception is raised
309
+ # asynchronously and can arrive at any point. Wrapping a mutation in
310
+ # this holds the exception back until the block has finished, so the
311
+ # interruption is deferred rather than given up.
312
+ #
313
+ # @return [ Object ] The value of the block.
314
+ def protect(&block)
315
+ Thread.handle_interrupt(TimedOut => :never, &block)
316
+ end
317
+
318
+ # The time left in the open budget, or nil when no budget is open.
319
+ #
320
+ # @return [ Float | nil ] The remaining seconds.
321
+ def remaining
322
+ current&.remaining
323
+ end
324
+
325
+ # Returns the condition as a Regexp, without a timeout.
326
+ #
327
+ # @param [ Regexp | BSON::Regexp::Raw ] condition The condition.
328
+ #
329
+ # @return [ Regexp ] The pattern.
330
+ def coerce(condition)
331
+ case condition
332
+ when ::Regexp then condition
333
+ when BSON::Regexp::Raw then condition.compile
334
+ else raise ArgumentError, "Not a regular expression: #{condition.inspect}"
335
+ end
336
+ end
337
+
338
+ private
339
+
340
+ # The budget for the open scope, if there is one.
341
+ #
342
+ # A scope with nothing to bound leaves NO_BUDGET in storage, which reads
343
+ # as no budget.
344
+ #
345
+ # @return [ Budget | nil ] The open budget.
346
+ def current
347
+ budget = Thread.current[Threaded::REGEXP_BUDGET_KEY]
348
+ budget unless budget.equal?(NO_BUDGET)
349
+ end
350
+
351
+ # Whether evaluating the selector could run a regular expression.
352
+ #
353
+ # A string under $regex counts: FieldExpression turns it into a pattern
354
+ # at match time.
355
+ def contains_regexp?(object)
356
+ case object
357
+ when ::Regexp, BSON::Regexp::Raw
358
+ true
359
+ when Hash
360
+ object.any? do |k, v|
361
+ k.to_s == '$regex' || contains_regexp?(v)
362
+ end
363
+ when Array
364
+ object.any? { |v| contains_regexp?(v) }
365
+ else
366
+ false
367
+ end
368
+ end
369
+
370
+ # Builds the error with the scope timeout held back.
371
+ #
372
+ # Composing the message goes through I18n, which reads locale files the
373
+ # first time it runs. An asynchronous TimedOut landing in the middle of
374
+ # that is caught by I18n and reraised as a locale-loading failure, so
375
+ # the real error never surfaces.
376
+ #
377
+ # @param [ Budget ] budget The open budget.
378
+ # @param [ Float ] limit The limit that was exceeded. Defaults to the
379
+ # budget's own, which is the right one to name everywhere the budget
380
+ # itself ran out.
381
+ def timeout_error(budget, limit = budget.limit)
382
+ protect do
383
+ Errors::InMemoryRegexpTimeout.new(limit)
384
+ end
385
+ end
386
+ end
387
+ end
388
+ end
389
+ end
@@ -138,6 +138,7 @@ require 'mongoid/matcher/nor'
138
138
  require 'mongoid/matcher/not'
139
139
  require 'mongoid/matcher/or'
140
140
  require 'mongoid/matcher/regex'
141
+ require 'mongoid/matcher/regexp_budget'
141
142
  require 'mongoid/matcher/size'
142
143
  require 'mongoid/matcher/type'
143
144
  require 'mongoid/matcher/expression_operator'
@@ -30,6 +30,9 @@ module Mongoid
30
30
  # executed on documents.
31
31
  EXECUTE_CALLBACKS = '[mongoid]:execute-callbacks'
32
32
 
33
+ # The key for the time left in the current in-memory regexp budget.
34
+ REGEXP_BUDGET_KEY = 'regexp-budget'
35
+
33
36
  extend self
34
37
 
35
38
  # Begin entry into a named thread local stack.
@@ -5,5 +5,5 @@ module Mongoid
5
5
  #
6
6
  # Note that this file is automatically updated via `rake candidate:create`.
7
7
  # Manual changes to this file will be overwritten by that rake task.
8
- VERSION = '8.1.12'
8
+ VERSION = '8.1.13'
9
9
  end
@@ -38,6 +38,7 @@ module Mongoid
38
38
  warning :object_id_as_json_oid_deprecated, 'Config option :object_id_as_json_oid is deprecated. It will always be false beginning in Mongoid 9.0. Please use load_defaults for Mongoid 8.0 or later, then remove it from your config.'
39
39
  warning :overwrite_chained_operators_deprecated, 'Config option :overwrite_chained_operators is deprecated. It will always be false beginning in Mongoid 9.0. Please use load_defaults for Mongoid 8.0 or later, then remove it from your config.'
40
40
  warning :mutable_ids, 'In Mongoid 9.0 the _id field will be immutable. In earlier versions of 8.x, mutating the _id field was supported inconsistently. Prepare your code for 9.0 by setting Mongoid::Config.immutable_ids to true.'
41
+ warning :reparenting_via_nested_attributes, 'Reparenting documents via nested attributes is insecure and is deprecated. Set Mongoid.allow_reparenting_via_nested_attributes to false and update your code to avoid reparenting documents via nested attributes.'
41
42
  warning :mongoid_query_cache, 'In Mongoid 9.0, Mongoid::QueryCache will be removed. Please replace it with Mongo::QueryCache.'
42
43
  warning :mongoid_query_cache_clear, 'In Mongoid 9.0, Mongoid::QueryCache#clear_cache should be replaced it with Mongo::QueryCache#clear.'
43
44
  end
@@ -284,6 +284,14 @@ describe 'Mongoid application tests' do
284
284
  line =~ /mongoid/
285
285
  end
286
286
  gemfile_lines << "gem 'mongoid', path: '#{File.expand_path(BASE)}'\n"
287
+
288
+ # json 3.0 removed the quirks_mode and max_nesting keywords that
289
+ # ActiveSupport::JSON (every 7.x) still passes to JSON.generate and
290
+ # JSON.parse, so a freshly resolved bundle picks a 3.x json and the app
291
+ # fails to encode any response with ArgumentError: unknown keyword:
292
+ # quirks_mode. Cap json at 2.x, which still accepts those keywords.
293
+ gemfile_lines << "gem 'json', '< 3'\n"
294
+
287
295
  if rails_version
288
296
  gemfile_lines.delete_if do |line|
289
297
  line =~ /rails/
@@ -54,8 +54,23 @@ describe 'has_and_belongs_to_many associations' do
54
54
  })
55
55
  end
56
56
 
57
- it 'does not raise on save' do
58
- expect { image_block.save! }.not_to raise_error
57
+ # The nested attributes are processed before the attachment_ids
58
+ # assignment is applied, so at that point the id is not yet in the
59
+ # association and resolving it requires a collection-wide lookup.
60
+ context 'when allow_reparenting_via_nested_attributes is false' do
61
+ config_override :allow_reparenting_via_nested_attributes, false
62
+
63
+ it 'raises a document not found error' do
64
+ expect { image_block.save! }.to raise_error(Mongoid::Errors::DocumentNotFound)
65
+ end
66
+ end
67
+
68
+ context 'when allow_reparenting_via_nested_attributes is true' do
69
+ config_override :allow_reparenting_via_nested_attributes, true
70
+
71
+ it 'does not raise on save' do
72
+ expect { image_block.save! }.not_to raise_error
73
+ end
59
74
  end
60
75
  end
61
76
  end
@@ -267,10 +267,20 @@ describe "Dots and Dollars" do
267
267
  DADMUser.where("$_amount": 0).first
268
268
  end
269
269
 
270
- it "raise an error" do
270
+ it 'raises an error' do
271
271
  expect do
272
272
  queried
273
- end.to raise_error(Mongo::Error::OperationFailure)
273
+ end.to raise_error(Mongoid::Errors::InvalidQuery)
274
+ end
275
+
276
+ context 'when allow_unsafe_query_operators is true' do
277
+ config_override :allow_unsafe_query_operators, true
278
+
279
+ it 'raises an error from the server' do
280
+ expect do
281
+ queried
282
+ end.to raise_error(Mongo::Error::OperationFailure)
283
+ end
274
284
  end
275
285
  end
276
286
  end
@@ -135,6 +135,27 @@
135
135
  $regex: bar
136
136
  matches: false
137
137
 
138
+ # Object#=~ is gone as of Ruby 3.2, so an element that cannot be matched
139
+ # against has to be skipped rather than passed to =~.
140
+ - name: array field value - element that cannot be matched against
141
+ document:
142
+ title:
143
+ - 42
144
+ - foo
145
+ query:
146
+ title:
147
+ $regex: foo
148
+ matches: true
149
+
150
+ - name: array field value - no element can be matched against
151
+ document:
152
+ title:
153
+ - 42
154
+ query:
155
+ title:
156
+ $regex: foo
157
+ matches: false
158
+
138
159
  - name: true field value
139
160
  document:
140
161
  title: true