mongoid 9.0.11 → 9.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.
- checksums.yaml +4 -4
- data/lib/config/locales/en.yml +65 -0
- data/lib/mongoid/association/depending.rb +8 -4
- data/lib/mongoid/association/embedded/batchable.rb +8 -0
- data/lib/mongoid/association/nested/many.rb +34 -5
- data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +100 -1
- data/lib/mongoid/association/relatable.rb +17 -0
- data/lib/mongoid/clients/factory.rb +2 -0
- data/lib/mongoid/collection_configurable.rb +8 -0
- data/lib/mongoid/config/encryption.rb +171 -22
- data/lib/mongoid/config.rb +56 -9
- data/lib/mongoid/contextual/aggregable/memory.rb +5 -2
- data/lib/mongoid/contextual/memory.rb +18 -7
- data/lib/mongoid/criteria/queryable/mergeable.rb +4 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +109 -0
- data/lib/mongoid/encryptable.rb +73 -0
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors/invalid_encryption_schema.rb +27 -0
- data/lib/mongoid/errors/no_encryption_schema.rb +30 -0
- data/lib/mongoid/errors.rb +3 -0
- data/lib/mongoid/field_readable.rb +70 -0
- data/lib/mongoid/indexable.rb +39 -27
- data/lib/mongoid/matchable.rb +6 -1
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +4 -9
- data/lib/mongoid/matcher/regex.rb +9 -13
- data/lib/mongoid/matcher/regexp_budget.rb +383 -0
- data/lib/mongoid/matcher.rb +1 -0
- data/lib/mongoid/persistence_context.rb +77 -0
- data/lib/mongoid/search_indexable.rb +19 -8
- data/lib/mongoid/tasks/database.rb +33 -16
- data/lib/mongoid/threaded.rb +37 -0
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +4 -0
- data/spec/integration/app_spec.rb +7 -0
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
- data/spec/integration/atomic/modifiers_spec.rb +57 -0
- data/spec/integration/dots_and_dollars_spec.rb +12 -2
- data/spec/integration/encryption_spec.rb +298 -0
- data/spec/integration/matcher_operator_data/regex.yml +21 -0
- data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
- data/spec/integration/query_operator_guard_spec.rb +89 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +145 -0
- data/spec/mongoid/attributes/nested_spec.rb +204 -10
- data/spec/mongoid/clients/factory_spec.rb +36 -0
- data/spec/mongoid/config/defaults_spec.rb +18 -0
- data/spec/mongoid/config/encryption_spec.rb +451 -0
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +91 -0
- data/spec/mongoid/contextual/memory_spec.rb +177 -0
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2 -0
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +200 -0
- data/spec/mongoid/criteria_spec.rb +2 -0
- data/spec/mongoid/encryptable_spec.rb +115 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +576 -0
- data/spec/mongoid/search_indexable_spec.rb +34 -1
- data/spec/mongoid/tasks/database_spec.rb +31 -0
- data/spec/support/crypt/models.rb +253 -0
- data/spec/support/models/modifier_models.rb +32 -0
- metadata +17 -2
data/lib/mongoid/matchable.rb
CHANGED
|
@@ -18,7 +18,12 @@ module Mongoid
|
|
|
18
18
|
#
|
|
19
19
|
# @return [ true | false ] True if matches, false if not.
|
|
20
20
|
def _matches?(selector)
|
|
21
|
-
|
|
21
|
+
# Opens a regexp budget for this document only. Callers that match many
|
|
22
|
+
# documents against one selector open a budget of their own first, and
|
|
23
|
+
# this one joins it rather than giving every document a fresh limit.
|
|
24
|
+
Matcher::RegexpBudget.open(selector) do
|
|
25
|
+
Matcher::Expression.matches?(self, selector)
|
|
26
|
+
end
|
|
22
27
|
end
|
|
23
28
|
end
|
|
24
29
|
end
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
# rubocop:todo all
|
|
2
1
|
module Mongoid
|
|
3
2
|
module Matcher
|
|
4
|
-
|
|
5
3
|
# This is an internal equality implementation that performs exact
|
|
6
4
|
# comparisons and regular expression matches.
|
|
7
5
|
#
|
|
8
6
|
# @api private
|
|
9
7
|
module EqImplWithRegexp
|
|
10
|
-
|
|
11
8
|
# Returns whether a value satisfies an $eq (or similar) expression,
|
|
12
9
|
# performing a regular expression match if the condition is a regular
|
|
13
10
|
# expression.
|
|
@@ -19,14 +16,12 @@ module Mongoid
|
|
|
19
16
|
# @return [ true | false ] Whether the value matches.
|
|
20
17
|
#
|
|
21
18
|
# @api private
|
|
22
|
-
module_function def matches?(
|
|
19
|
+
module_function def matches?(_original_operator, value, condition)
|
|
23
20
|
case condition
|
|
24
|
-
when Regexp
|
|
25
|
-
value
|
|
26
|
-
when ::BSON::Regexp::Raw
|
|
27
|
-
value =~ condition.compile
|
|
21
|
+
when Regexp, ::BSON::Regexp::Raw
|
|
22
|
+
value.respond_to?(:=~) && RegexpBudget.match?(value, condition)
|
|
28
23
|
else
|
|
29
|
-
if value.
|
|
24
|
+
if value.is_a?(Time) && condition.is_a?(Time)
|
|
30
25
|
EqImpl.time_eq?(value, condition)
|
|
31
26
|
else
|
|
32
27
|
value == condition
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
# rubocop:todo all
|
|
2
1
|
module Mongoid
|
|
3
2
|
module Matcher
|
|
4
|
-
|
|
5
3
|
# In-memory matcher for $regex expression.
|
|
6
4
|
#
|
|
7
5
|
# @see https://www.mongodb.com/docs/manual/reference/operator/query/regex/
|
|
8
6
|
#
|
|
9
7
|
# @api private
|
|
10
8
|
module Regex
|
|
11
|
-
|
|
12
9
|
# Returns whether a value satisfies a $regex expression.
|
|
13
10
|
#
|
|
14
11
|
# @param [ true | false ] exists Not used.
|
|
@@ -18,25 +15,24 @@ module Mongoid
|
|
|
18
15
|
# @return [ true | false ] Whether the value matches.
|
|
19
16
|
#
|
|
20
17
|
# @api private
|
|
21
|
-
module_function def matches?(
|
|
22
|
-
condition
|
|
23
|
-
when Regexp
|
|
24
|
-
condition
|
|
25
|
-
when BSON::Regexp::Raw
|
|
26
|
-
condition.compile
|
|
27
|
-
else
|
|
18
|
+
module_function def matches?(_exists, value, condition)
|
|
19
|
+
unless condition.is_a?(Regexp) || condition.is_a?(BSON::Regexp::Raw)
|
|
28
20
|
# Note that strings must have been converted to a regular expression
|
|
29
21
|
# instance already (with $options taken into account, if provided).
|
|
30
22
|
raise Errors::InvalidQuery, "$regex requires a regular expression argument: #{Errors::InvalidQuery.truncate_expr(condition)}"
|
|
31
23
|
end
|
|
32
24
|
|
|
25
|
+
# The condition is compiled by RegexpBudget rather than here, so that
|
|
26
|
+
# the budget's timeout can be baked into the pattern.
|
|
33
27
|
case value
|
|
34
28
|
when Array
|
|
29
|
+
# Object#=~ is gone as of Ruby 3.2, so an element that cannot be
|
|
30
|
+
# matched against has to be rejected rather than passed to =~.
|
|
35
31
|
value.any? do |v|
|
|
36
|
-
v
|
|
32
|
+
v.respond_to?(:=~) && RegexpBudget.match?(v, condition)
|
|
37
33
|
end
|
|
38
34
|
when String
|
|
39
|
-
value
|
|
35
|
+
RegexpBudget.match?(value, condition)
|
|
40
36
|
else
|
|
41
37
|
false
|
|
42
38
|
end
|
|
@@ -52,7 +48,7 @@ module Mongoid
|
|
|
52
48
|
#
|
|
53
49
|
# @api private
|
|
54
50
|
module_function def matches_array_or_scalar?(value, condition)
|
|
55
|
-
if Array
|
|
51
|
+
if value.is_a?(Array)
|
|
56
52
|
value.any? do |v|
|
|
57
53
|
matches?(true, v, condition)
|
|
58
54
|
end
|
|
@@ -0,0 +1,383 @@
|
|
|
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. Threaded.has? reports a nil-valued variable as
|
|
52
|
+
# absent, so an open-but-unbounded scope has to store a real marker; a
|
|
53
|
+
# nested call checks Threaded.has? and joins the enclosing scope rather
|
|
54
|
+
# than deciding again for itself.
|
|
55
|
+
NO_BUDGET = Object.new
|
|
56
|
+
|
|
57
|
+
# The state of one open budget: what is left of the limit, and the
|
|
58
|
+
# patterns compiled under it.
|
|
59
|
+
#
|
|
60
|
+
# @api private
|
|
61
|
+
class Budget
|
|
62
|
+
# @return [ Float ] The limit this budget started with.
|
|
63
|
+
attr_reader :limit
|
|
64
|
+
|
|
65
|
+
# @return [ Float ] The seconds left before the budget is spent.
|
|
66
|
+
attr_reader :remaining
|
|
67
|
+
|
|
68
|
+
# @param [ Float ] limit The seconds this budget may spend.
|
|
69
|
+
def initialize(limit)
|
|
70
|
+
@limit = limit
|
|
71
|
+
@remaining = limit
|
|
72
|
+
@cache = {}
|
|
73
|
+
# A per-Regexp timeout bounds one match; the cumulative budget bounds
|
|
74
|
+
# the operation. So the timeout is fixed for the life of the scope
|
|
75
|
+
# rather than following the drawdown, which is what lets a pattern be
|
|
76
|
+
# compiled once instead of once per match. It means a match that
|
|
77
|
+
# starts with almost nothing left can still run for a whole limit, so
|
|
78
|
+
# an operation can overshoot by at most one limit -- bounded, which is
|
|
79
|
+
# the point, and far cheaper than recompiling.
|
|
80
|
+
#
|
|
81
|
+
# An application that has set a stricter global Regexp.timeout keeps
|
|
82
|
+
# it: baking in a larger value would leave it less protected than it
|
|
83
|
+
# asked to be. A timeout set on one individual pattern is a different
|
|
84
|
+
# matter, and is not preserved -- Budget#compile rebuilds the pattern
|
|
85
|
+
# from its source and flags, neither of which carries one, so this
|
|
86
|
+
# value takes its place. Only trusted code can supply such a pattern:
|
|
87
|
+
# a condition decoded from JSON or BSON arrives as a string or a
|
|
88
|
+
# BSON::Regexp::Raw, with no timeout of its own.
|
|
89
|
+
@timeout = [ limit, ::Regexp.timeout ].compact.min if PER_REGEXP_TIMEOUT
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Draws the elapsed time down from the budget.
|
|
93
|
+
#
|
|
94
|
+
# @param [ Float ] elapsed The seconds to charge.
|
|
95
|
+
def charge(elapsed)
|
|
96
|
+
@remaining -= elapsed
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# @return [ true | false ] Whether the budget is spent.
|
|
100
|
+
def exhausted?
|
|
101
|
+
@remaining <= 0
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Returns the condition as a Regexp which, where the Ruby in use
|
|
105
|
+
# supports it, gives up once its timeout is spent.
|
|
106
|
+
#
|
|
107
|
+
# The condition is taken uncompiled so that the cache can answer before
|
|
108
|
+
# any compiling happens. A BSON::Regexp::Raw memoizes its own compile,
|
|
109
|
+
# but FieldExpression builds a fresh one for every $regex it evaluates,
|
|
110
|
+
# so that memo is worth nothing across documents and the source would
|
|
111
|
+
# otherwise be compiled once per document.
|
|
112
|
+
#
|
|
113
|
+
# @param [ Regexp | BSON::Regexp::Raw ] condition The condition.
|
|
114
|
+
#
|
|
115
|
+
# @return [ Regexp ] The compiled pattern.
|
|
116
|
+
def compile(condition)
|
|
117
|
+
@cache[cache_key(condition)] ||= bake(RegexpBudget.coerce(condition))
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
private
|
|
121
|
+
|
|
122
|
+
# BSON::Regexp::Raw aliases eql? to == but leaves hash alone, so two
|
|
123
|
+
# equal instances hash differently and cannot key the cache. What they
|
|
124
|
+
# are equal by can. Anything else keys on itself and is left to coerce
|
|
125
|
+
# to reject.
|
|
126
|
+
def cache_key(condition)
|
|
127
|
+
case condition
|
|
128
|
+
when BSON::Regexp::Raw then [ condition.pattern, condition.options ]
|
|
129
|
+
else condition
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Rebuilds the pattern with the budget's timeout, where the Ruby in use
|
|
134
|
+
# has them.
|
|
135
|
+
def bake(regexp)
|
|
136
|
+
return regexp unless PER_REGEXP_TIMEOUT
|
|
137
|
+
|
|
138
|
+
::Regexp.new(regexp.source, regexp.options, timeout: @timeout)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
class << self
|
|
143
|
+
# Opens a budget scope for the duration of the block.
|
|
144
|
+
#
|
|
145
|
+
# Nested calls join the enclosing budget instead of starting a new one,
|
|
146
|
+
# which is what lets a scan over many documents share a single limit.
|
|
147
|
+
# It also keeps the recursion in Expression.matches? (through
|
|
148
|
+
# $elemMatch, $and, $or and $nor) from resetting the budget.
|
|
149
|
+
#
|
|
150
|
+
# No budget is opened for a selector that carries no regular
|
|
151
|
+
# expression. There would be nothing for it to bound, and on the
|
|
152
|
+
# Timeout path it would put a deadline on in-memory work that has
|
|
153
|
+
# nothing to do with regular expressions.
|
|
154
|
+
#
|
|
155
|
+
# The scope covers everything nested inside the block, a selector other
|
|
156
|
+
# than this one included: a nested call joins the scope rather than
|
|
157
|
+
# deciding for itself, which is what keeps a scan from walking the
|
|
158
|
+
# selector once per document. Where the scope has nothing to bound, that
|
|
159
|
+
# means nested selectors are not bounded either -- so do not open one
|
|
160
|
+
# around work that can run application code. Loading documents runs find
|
|
161
|
+
# callbacks, and a query in one of those brings its own selector.
|
|
162
|
+
#
|
|
163
|
+
# Where a selector does carry one, the Timeout path still measures the
|
|
164
|
+
# whole scope rather than the matching alone, so a long scan can trip
|
|
165
|
+
# the limit with a cheap pattern. That imprecision is accepted: the
|
|
166
|
+
# alternative is a Timeout around each individual match, which was
|
|
167
|
+
# measured at about nine seconds per million matches, and the limit
|
|
168
|
+
# exists to bound a scan of exactly that size. The error message is
|
|
169
|
+
# worded to hold either way, and Rubies with per-Regexp timeouts --
|
|
170
|
+
# every supported MRI from 3.2 on -- do not take this path at all.
|
|
171
|
+
#
|
|
172
|
+
# Code inside the block that mutates state should be wrapped in
|
|
173
|
+
# .protect, since on Rubies without a per-Regexp timeout the budget is
|
|
174
|
+
# enforced with an asynchronous exception that can land anywhere.
|
|
175
|
+
#
|
|
176
|
+
# @param [ Hash ] selector The selector about to be evaluated.
|
|
177
|
+
#
|
|
178
|
+
# @return [ Object ] The value of the block.
|
|
179
|
+
def open(selector, &block)
|
|
180
|
+
# The key is present (holding NO_BUDGET) where an enclosing scope
|
|
181
|
+
# found nothing to bound, so that a nested call does not scan the
|
|
182
|
+
# selector again. Deciding before this check, in a default argument
|
|
183
|
+
# say, would walk the selector once per document on a scan.
|
|
184
|
+
return yield if Threaded.has?(Threaded::REGEXP_BUDGET_KEY)
|
|
185
|
+
|
|
186
|
+
open_with(limit_for(selector), &block)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Opens a budget scope for a limit the caller has already decided on.
|
|
190
|
+
#
|
|
191
|
+
# A caller that rearranges its work around the decision -- loading
|
|
192
|
+
# documents up front so that nothing is mutated before the scan
|
|
193
|
+
# finishes, say -- has to make it before it can act on it, and must not
|
|
194
|
+
# then make it a second time. Asking .limit_for and letting .open ask
|
|
195
|
+
# again reads the configured limit twice, and the two reads can differ:
|
|
196
|
+
# a limit that becomes positive in between would establish a budget in
|
|
197
|
+
# the branch that was chosen for not needing one, and on the Timeout
|
|
198
|
+
# path that arms a deadline over work the branch never made
|
|
199
|
+
# interruptible.
|
|
200
|
+
#
|
|
201
|
+
# See .open for what the scope does and does not bound, and for the
|
|
202
|
+
# note about mutating state inside it.
|
|
203
|
+
#
|
|
204
|
+
# @param [ Float | nil ] limit The seconds the scope may spend, or nil
|
|
205
|
+
# for a scope with nothing to bound.
|
|
206
|
+
#
|
|
207
|
+
# @return [ Object ] The value of the block.
|
|
208
|
+
def open_with(limit, &block)
|
|
209
|
+
return yield if Threaded.has?(Threaded::REGEXP_BUDGET_KEY)
|
|
210
|
+
|
|
211
|
+
budget = Budget.new(limit) if limit&.positive?
|
|
212
|
+
|
|
213
|
+
begin
|
|
214
|
+
# Set inside the begin so that an asynchronous exception from an
|
|
215
|
+
# enclosing timeout cannot leave the key behind on a pooled thread.
|
|
216
|
+
Threaded.set(Threaded::REGEXP_BUDGET_KEY, budget || NO_BUDGET)
|
|
217
|
+
|
|
218
|
+
if budget.nil? || PER_REGEXP_TIMEOUT
|
|
219
|
+
yield
|
|
220
|
+
else
|
|
221
|
+
begin
|
|
222
|
+
Timeout.timeout(budget.limit, TimedOut, &block)
|
|
223
|
+
rescue TimedOut
|
|
224
|
+
raise timeout_error(budget)
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
ensure
|
|
228
|
+
Threaded.delete(Threaded::REGEXP_BUDGET_KEY)
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# The limit a scope evaluating this selector would be bounded by.
|
|
233
|
+
#
|
|
234
|
+
# A caller that has to rearrange its work to make the scan
|
|
235
|
+
# interruptible -- loading documents up front so that nothing is
|
|
236
|
+
# mutated before the scan finishes, say -- can ask this first and skip
|
|
237
|
+
# the rearrangement, and whatever it costs, when there is no pattern to
|
|
238
|
+
# bound. It then passes what it got to .open_with, so that the decision
|
|
239
|
+
# it acted on is the one the scope is opened with. Callers with nothing
|
|
240
|
+
# to rearrange should just call .open, which asks this itself.
|
|
241
|
+
#
|
|
242
|
+
# @param [ Hash ] selector The selector about to be evaluated.
|
|
243
|
+
#
|
|
244
|
+
# @return [ Float | nil ] The limit, or nil where there is nothing to
|
|
245
|
+
# bound.
|
|
246
|
+
def limit_for(selector)
|
|
247
|
+
# nil.to_f is 0.0, so an unset limit and a limit of zero or less are
|
|
248
|
+
# the same thing here: no limit. Zero is a common way to spell
|
|
249
|
+
# "disabled", and taking it literally would mean a budget that is
|
|
250
|
+
# spent before the first match and a query that can never run.
|
|
251
|
+
limit = Mongoid::Config.in_memory_regexp_time_limit.to_f
|
|
252
|
+
return nil unless limit.positive?
|
|
253
|
+
|
|
254
|
+
limit if contains_regexp?(selector)
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Matches a value against a regular expression condition, charging the
|
|
258
|
+
# time it takes against the open budget.
|
|
259
|
+
#
|
|
260
|
+
# @param [ Object ] value The value to match.
|
|
261
|
+
# @param [ Regexp | BSON::Regexp::Raw ] condition The condition.
|
|
262
|
+
#
|
|
263
|
+
# @raise [ Errors::InMemoryRegexpTimeout ] if the budget is exhausted.
|
|
264
|
+
#
|
|
265
|
+
# @return [ Integer | nil ] The offset of the match, or nil.
|
|
266
|
+
def match?(value, condition)
|
|
267
|
+
budget = current
|
|
268
|
+
return value =~ coerce(condition) unless budget
|
|
269
|
+
|
|
270
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
271
|
+
pattern = nil
|
|
272
|
+
begin
|
|
273
|
+
raise timeout_error(budget) if budget.exhausted?
|
|
274
|
+
|
|
275
|
+
# Compiling is charged too. It is not free, and for a pattern with
|
|
276
|
+
# very many branches it costs far more than running the pattern
|
|
277
|
+
# does, so leaving it out would leave a way to spend unbounded time
|
|
278
|
+
# without the budget ever noticing.
|
|
279
|
+
pattern = budget.compile(condition)
|
|
280
|
+
value =~ pattern
|
|
281
|
+
rescue TIMEOUT_ERROR
|
|
282
|
+
# Name the limit that actually fired, which is not always the
|
|
283
|
+
# budget's. A baked pattern carries it: the smaller of the budget's
|
|
284
|
+
# limit and any global Regexp.timeout the application has set.
|
|
285
|
+
# Where nothing was baked -- an engine that raises this error but
|
|
286
|
+
# will not take a per-Regexp timeout, which is JRuby -- the global
|
|
287
|
+
# is the only thing that can have fired, and the pattern reports
|
|
288
|
+
# nil. Naming the budget's limit in either case would state a time
|
|
289
|
+
# that was never spent and send the reader after a setting that is
|
|
290
|
+
# not the one in the way.
|
|
291
|
+
#
|
|
292
|
+
# Both readers arrived together with Regexp::TimeoutError, so every
|
|
293
|
+
# engine that can reach this rescue at all has them.
|
|
294
|
+
raise timeout_error(budget, pattern&.timeout || ::Regexp.timeout || budget.limit)
|
|
295
|
+
ensure
|
|
296
|
+
budget.charge(Process.clock_gettime(Process::CLOCK_MONOTONIC) - started)
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
# Runs the block without letting a scope timeout tear it in half.
|
|
301
|
+
#
|
|
302
|
+
# Where the budget is enforced with Timeout, the exception is raised
|
|
303
|
+
# asynchronously and can arrive at any point. Wrapping a mutation in
|
|
304
|
+
# this holds the exception back until the block has finished, so the
|
|
305
|
+
# interruption is deferred rather than given up.
|
|
306
|
+
#
|
|
307
|
+
# @return [ Object ] The value of the block.
|
|
308
|
+
def protect(&block)
|
|
309
|
+
Thread.handle_interrupt(TimedOut => :never, &block)
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
# The time left in the open budget, or nil when no budget is open.
|
|
313
|
+
#
|
|
314
|
+
# @return [ Float | nil ] The remaining seconds.
|
|
315
|
+
def remaining
|
|
316
|
+
current&.remaining
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# Returns the condition as a Regexp, without a timeout.
|
|
320
|
+
#
|
|
321
|
+
# @param [ Regexp | BSON::Regexp::Raw ] condition The condition.
|
|
322
|
+
#
|
|
323
|
+
# @return [ Regexp ] The pattern.
|
|
324
|
+
def coerce(condition)
|
|
325
|
+
case condition
|
|
326
|
+
when ::Regexp then condition
|
|
327
|
+
when BSON::Regexp::Raw then condition.compile
|
|
328
|
+
else raise ArgumentError, "Not a regular expression: #{condition.inspect}"
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
private
|
|
333
|
+
|
|
334
|
+
# The budget for the open scope, if there is one.
|
|
335
|
+
#
|
|
336
|
+
# A scope with nothing to bound leaves NO_BUDGET in storage, which reads
|
|
337
|
+
# as no budget.
|
|
338
|
+
#
|
|
339
|
+
# @return [ Budget | nil ] The open budget.
|
|
340
|
+
def current
|
|
341
|
+
budget = Threaded.get(Threaded::REGEXP_BUDGET_KEY)
|
|
342
|
+
budget unless budget.equal?(NO_BUDGET)
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
# Whether evaluating the selector could run a regular expression.
|
|
346
|
+
#
|
|
347
|
+
# A string under $regex counts: FieldExpression turns it into a pattern
|
|
348
|
+
# at match time.
|
|
349
|
+
def contains_regexp?(object)
|
|
350
|
+
case object
|
|
351
|
+
when ::Regexp, BSON::Regexp::Raw
|
|
352
|
+
true
|
|
353
|
+
when Hash
|
|
354
|
+
object.any? do |k, v|
|
|
355
|
+
k.to_s == '$regex' || contains_regexp?(v)
|
|
356
|
+
end
|
|
357
|
+
when Array
|
|
358
|
+
object.any? { |v| contains_regexp?(v) }
|
|
359
|
+
else
|
|
360
|
+
false
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
# Builds the error with the scope timeout held back.
|
|
365
|
+
#
|
|
366
|
+
# Composing the message goes through I18n, which reads locale files the
|
|
367
|
+
# first time it runs. An asynchronous TimedOut landing in the middle of
|
|
368
|
+
# that is caught by I18n and reraised as a locale-loading failure, so
|
|
369
|
+
# the real error never surfaces.
|
|
370
|
+
#
|
|
371
|
+
# @param [ Budget ] budget The open budget.
|
|
372
|
+
# @param [ Float ] limit The limit that was exceeded. Defaults to the
|
|
373
|
+
# budget's own, which is the right one to name everywhere the budget
|
|
374
|
+
# itself ran out.
|
|
375
|
+
def timeout_error(budget, limit = budget.limit)
|
|
376
|
+
protect do
|
|
377
|
+
Errors::InMemoryRegexpTimeout.new(limit)
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
end
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
end
|
data/lib/mongoid/matcher.rb
CHANGED
|
@@ -142,6 +142,7 @@ require 'mongoid/matcher/nor'
|
|
|
142
142
|
require 'mongoid/matcher/not'
|
|
143
143
|
require 'mongoid/matcher/or'
|
|
144
144
|
require 'mongoid/matcher/regex'
|
|
145
|
+
require 'mongoid/matcher/regexp_budget'
|
|
145
146
|
require 'mongoid/matcher/size'
|
|
146
147
|
require 'mongoid/matcher/type'
|
|
147
148
|
require 'mongoid/matcher/expression_operator'
|
|
@@ -126,6 +126,8 @@ module Mongoid
|
|
|
126
126
|
|
|
127
127
|
client = client.with(options) unless options.empty?
|
|
128
128
|
|
|
129
|
+
verify_encryption_schema!(client)
|
|
130
|
+
|
|
129
131
|
client
|
|
130
132
|
end
|
|
131
133
|
end
|
|
@@ -199,6 +201,81 @@ module Mongoid
|
|
|
199
201
|
name.respond_to?(:call) ? name.call.to_sym : name.to_sym
|
|
200
202
|
end
|
|
201
203
|
|
|
204
|
+
# Refuse to use a client that would store a model's encrypted fields in
|
|
205
|
+
# plaintext.
|
|
206
|
+
#
|
|
207
|
+
# The automatic encryption schema is keyed by namespace and is built once,
|
|
208
|
+
# when the client is created. The driver looks the target namespace up in
|
|
209
|
+
# that schema, and when it is absent it asks the server for a schema
|
|
210
|
+
# instead; a collection without a validator then yields no encryption at
|
|
211
|
+
# all, and no error. Checking the namespace here is what turns that silent
|
|
212
|
+
# downgrade into a failure. When the namespace is present, its entry is
|
|
213
|
+
# checked against the fields the model declares as encrypted too: a
|
|
214
|
+
# hand-written schema can cover the namespace and still leave out a field.
|
|
215
|
+
#
|
|
216
|
+
# @param [ Mongo::Client ] client The client this context resolved to.
|
|
217
|
+
#
|
|
218
|
+
# @raise [ Errors::NoEncryptionSchema ] if the model needs an encryption
|
|
219
|
+
# schema and the client's schema does not cover the target namespace, or
|
|
220
|
+
# the namespace entry does not cover every encrypted field of the model.
|
|
221
|
+
def verify_encryption_schema!(client)
|
|
222
|
+
klass = @object.is_a?(Class) ? @object : @object.class
|
|
223
|
+
# A model whose encrypted fields all live on embedded models declares no
|
|
224
|
+
# encryption of its own, and still needs a schema for its collection.
|
|
225
|
+
return unless klass.respond_to?(:requires_encryption_schema?) && klass.requires_encryption_schema?
|
|
226
|
+
# An embedded document is persisted as part of its parent, so the
|
|
227
|
+
# parent's context is what governs the namespace.
|
|
228
|
+
return if klass.embedded?
|
|
229
|
+
# Collection and index management sends no document data, so it does not
|
|
230
|
+
# need an encryption-capable client.
|
|
231
|
+
return if Threaded.managing_collection?
|
|
232
|
+
|
|
233
|
+
schema_map = client.options.dig(:auto_encryption_options, :schema_map)
|
|
234
|
+
namespace = "#{client.database.name}.#{collection_name}"
|
|
235
|
+
entry = schema_map && schema_map[namespace]
|
|
236
|
+
raise Errors::NoEncryptionSchema.new(klass, namespace, client_name) unless entry
|
|
237
|
+
|
|
238
|
+
# The schema map may have been written by hand, with symbol keys rather
|
|
239
|
+
# than strings; both are tolerated at every level.
|
|
240
|
+
Mongoid.config.encrypted_paths_for(klass).each do |path|
|
|
241
|
+
next if encryption_schema_has_path?(entry, path)
|
|
242
|
+
|
|
243
|
+
raise Errors::NoEncryptionSchema.new(
|
|
244
|
+
klass, namespace, client_name, path.join('.')
|
|
245
|
+
)
|
|
246
|
+
end
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# Whether a schema entry encrypts the property at the given path.
|
|
250
|
+
#
|
|
251
|
+
# @param [ Hash ] entry The namespace schema entry.
|
|
252
|
+
# @param [ Array<String> ] path The property path, outermost first.
|
|
253
|
+
#
|
|
254
|
+
# @return [ true | false ] Whether the leaf is marked as encrypted.
|
|
255
|
+
def encryption_schema_has_path?(entry, path)
|
|
256
|
+
node = entry
|
|
257
|
+
path.each do |name|
|
|
258
|
+
properties = schema_value(node, 'properties')
|
|
259
|
+
return false unless properties.is_a?(Hash)
|
|
260
|
+
|
|
261
|
+
node = schema_value(properties, name)
|
|
262
|
+
return false unless node.is_a?(Hash)
|
|
263
|
+
end
|
|
264
|
+
!schema_value(node, 'encrypt').nil?
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# Read a schema key that may be written as a string or a symbol.
|
|
268
|
+
#
|
|
269
|
+
# @param [ Hash ] hash The schema object.
|
|
270
|
+
# @param [ String ] key The key to read.
|
|
271
|
+
#
|
|
272
|
+
# @return [ Object ] The value, or nil when the key is absent.
|
|
273
|
+
def schema_value(hash, key)
|
|
274
|
+
return hash[key] if hash.key?(key)
|
|
275
|
+
|
|
276
|
+
hash[key.to_sym]
|
|
277
|
+
end
|
|
278
|
+
|
|
202
279
|
def client_options
|
|
203
280
|
@client_options ||= begin
|
|
204
281
|
opts = options.select do |k, v|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
+
# rubocop:todo all
|
|
2
3
|
|
|
3
4
|
module Mongoid
|
|
4
5
|
# Encapsulates behavior around managing search indexes. This feature
|
|
@@ -65,7 +66,9 @@ module Mongoid
|
|
|
65
66
|
def create_search_indexes
|
|
66
67
|
return if search_index_specs.empty?
|
|
67
68
|
|
|
68
|
-
|
|
69
|
+
Threaded.with_collection_management do
|
|
70
|
+
collection.search_indexes.create_many(search_index_specs)
|
|
71
|
+
end
|
|
69
72
|
end
|
|
70
73
|
|
|
71
74
|
# Waits for the named search indexes to be created.
|
|
@@ -96,7 +99,9 @@ module Mongoid
|
|
|
96
99
|
# @option options [ Hash ] :aggregate The options hash to pass to the
|
|
97
100
|
# aggregate command (optional)
|
|
98
101
|
def search_indexes(options = {})
|
|
99
|
-
|
|
102
|
+
Threaded.with_collection_management do
|
|
103
|
+
collection.search_indexes(options)
|
|
104
|
+
end
|
|
100
105
|
end
|
|
101
106
|
|
|
102
107
|
# Removes the search index specified by the given name or id. Either
|
|
@@ -105,12 +110,14 @@ module Mongoid
|
|
|
105
110
|
# @param [ String | nil ] name the name of the index to remove
|
|
106
111
|
# @param [ String | nil ] id the id of the index to remove
|
|
107
112
|
def remove_search_index(name: nil, id: nil)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
113
|
+
Threaded.with_collection_management do
|
|
114
|
+
logger.info(
|
|
115
|
+
"MONGOID: Removing search index '#{name || id}' " \
|
|
116
|
+
"on collection '#{collection.name}'."
|
|
117
|
+
)
|
|
112
118
|
|
|
113
|
-
|
|
119
|
+
collection.search_indexes.drop_one(name: name, id: id)
|
|
120
|
+
end
|
|
114
121
|
end
|
|
115
122
|
|
|
116
123
|
# Request the removal of all registered search indexes. Note
|
|
@@ -160,7 +167,11 @@ module Mongoid
|
|
|
160
167
|
#
|
|
161
168
|
# @return [ Array<Hash> ] the raw index documents
|
|
162
169
|
def get_indexes(names)
|
|
163
|
-
|
|
170
|
+
# Reading index metadata sends no document data, so it is exempt from
|
|
171
|
+
# the encryption schema check, like the other index operations here.
|
|
172
|
+
Threaded.with_collection_management do
|
|
173
|
+
collection.search_indexes.select { |i| names.include?(i['name']) }
|
|
174
|
+
end
|
|
164
175
|
end
|
|
165
176
|
end
|
|
166
177
|
end
|