mongoid 9.0.11 → 9.0.12
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 +45 -0
- data/lib/mongoid/association/depending.rb +8 -4
- 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/collection_configurable.rb +8 -0
- data/lib/mongoid/config/encryption.rb +36 -18
- 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 +46 -0
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors/no_encryption_schema.rb +28 -0
- data/lib/mongoid/errors.rb +2 -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 +35 -0
- data/lib/mongoid/search_indexable.rb +14 -7
- data/lib/mongoid/tasks/database.rb +23 -9
- data/lib/mongoid/threaded.rb +37 -0
- data/lib/mongoid/version.rb +1 -1
- data/spec/integration/app_spec.rb +7 -0
- data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
- data/spec/integration/dots_and_dollars_spec.rb +12 -2
- data/spec/integration/encryption_spec.rb +149 -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/config/defaults_spec.rb +18 -0
- data/spec/mongoid/config/encryption_spec.rb +228 -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 +61 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +570 -0
- data/spec/mongoid/tasks/database_spec.rb +18 -0
- data/spec/support/crypt/models.rb +157 -0
- metadata +14 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 066d78f8b736bad08756e737ff7c0e1d0af9b97149366fc0b5b0a5997c3f1a63
|
|
4
|
+
data.tar.gz: b55ea0b166bd8d9a0f465e88507ee8db2c4cccc7166477aec4b07a94d98c0a43
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9e6529d35d0fba7786a830bec9ffc87937580c414d6925c5c14f3c799ea03599592ad943783690a29e24ae09f706b6462e42ec1058bd316a023c51d7467c457e
|
|
7
|
+
data.tar.gz: 6625aaf9412d9ef3e5141b06a019bc83c33446612c3c1b16361473fb5a3ac1a006e2f4d3c6cdf0daa36c0b2b4ee2a9702b135b380011226f219517a578e203ac
|
data/lib/config/locales/en.yml
CHANGED
|
@@ -140,6 +140,32 @@ en:
|
|
|
140
140
|
A collation option is only supported if the query is executed on a MongoDB server
|
|
141
141
|
with version >= 3.4."
|
|
142
142
|
resolution: "Remove the collation option from the query."
|
|
143
|
+
in_memory_regexp_timeout:
|
|
144
|
+
message: "Evaluating this query in memory exceeded the %{limit} second
|
|
145
|
+
limit that applies to conditions containing a regular expression."
|
|
146
|
+
summary: "Mongoid evaluates some conditions in the calling thread
|
|
147
|
+
rather than sending them to the server: queries against an embedded
|
|
148
|
+
association, removing documents from an association by condition,
|
|
149
|
+
and Document#_matches?. A regular expression in such a condition
|
|
150
|
+
runs locally. Mongoid limits the total time one in-memory
|
|
151
|
+
evaluation may spend matching, because both the cost of a single
|
|
152
|
+
match and the number of matches performed grow with the condition,
|
|
153
|
+
and a pattern built from end-user input can be made to consume an
|
|
154
|
+
unbounded amount of CPU. The limit is cumulative over the whole
|
|
155
|
+
evaluation rather than per match. Where the Ruby in use offers no
|
|
156
|
+
per-Regexp timeout the limit is enforced as wall clock over the
|
|
157
|
+
whole in-memory evaluation, so there it can be exceeded by a large
|
|
158
|
+
scan even when the pattern itself is cheap."
|
|
159
|
+
resolution: "Check whether the pattern in this query comes from user
|
|
160
|
+
input; if it does, validate it before querying. If the query is
|
|
161
|
+
legitimate and simply large, raise
|
|
162
|
+
Mongoid::Config.in_memory_regexp_time_limit, or set it to nil to
|
|
163
|
+
remove the limit entirely. The limit in force is the smaller of
|
|
164
|
+
that setting and any global Regexp.timeout the application has set,
|
|
165
|
+
so check both. On JRuby, and on MRI before 3.2, there is no
|
|
166
|
+
per-Regexp timeout, so what the limit bounds there is the elapsed
|
|
167
|
+
time of the whole in-memory evaluation rather than the time spent
|
|
168
|
+
matching."
|
|
143
169
|
invalid_around_callback:
|
|
144
170
|
message: "An around callback must contain a yield in its definition."
|
|
145
171
|
summary: "The block needs to be yielded to for around callbacks to function as intended."
|
|
@@ -515,6 +541,25 @@ en:
|
|
|
515
541
|
environment, Mongoid cannot load its configuration."
|
|
516
542
|
resolution: "Please ensure an environment is set in one of the
|
|
517
543
|
listed locations. The environment must be explicitly set."
|
|
544
|
+
no_encryption_schema:
|
|
545
|
+
message: "The encryption schema of client %{client} does not cover
|
|
546
|
+
%{namespace}."
|
|
547
|
+
summary: "%{klass} declares encrypted fields, but the namespace it
|
|
548
|
+
resolved to, %{namespace}, is not part of the automatic encryption
|
|
549
|
+
schema of client %{client}. The encryption schema is built once,
|
|
550
|
+
when the client is created, and is keyed by namespace, so a
|
|
551
|
+
database name that is only known later - a callable :database
|
|
552
|
+
option, Model.with(database:), Mongoid.override_database - is not
|
|
553
|
+
in it. Routing the model to a client that has no
|
|
554
|
+
auto_encryption_options has the same effect. Mongoid refuses the
|
|
555
|
+
operation because the driver would store the declared fields in
|
|
556
|
+
plaintext without reporting an error."
|
|
557
|
+
resolution: "Give the model a namespace that is known when the
|
|
558
|
+
client is created, and a client configured with
|
|
559
|
+
auto_encryption_options. If the model has to live in a database
|
|
560
|
+
chosen at runtime, configure one client per database, each with
|
|
561
|
+
its own auto_encryption_options, and select between them with
|
|
562
|
+
Model.with(client:) instead of switching the database."
|
|
518
563
|
no_map_reduce_output:
|
|
519
564
|
message: "No output location was specified for the map/reduce
|
|
520
565
|
operation."
|
|
@@ -12,11 +12,15 @@ module Mongoid
|
|
|
12
12
|
included do
|
|
13
13
|
class_attribute :dependents
|
|
14
14
|
|
|
15
|
+
# Note the leading underscore: without it, ActiveSupport's
|
|
16
|
+
# class_attribute would generate a helper method for this attribute
|
|
17
|
+
# that collides with one it generates for :dependents.
|
|
18
|
+
#
|
|
15
19
|
# @api private
|
|
16
|
-
class_attribute :
|
|
20
|
+
class_attribute :_dependents_owner
|
|
17
21
|
|
|
18
22
|
self.dependents = []
|
|
19
|
-
self.
|
|
23
|
+
self._dependents_owner = self
|
|
20
24
|
end
|
|
21
25
|
|
|
22
26
|
class_methods do
|
|
@@ -57,9 +61,9 @@ module Mongoid
|
|
|
57
61
|
def self.define_dependency!(association)
|
|
58
62
|
validate!(association)
|
|
59
63
|
association.inverse_class.tap do |klass|
|
|
60
|
-
if klass.
|
|
64
|
+
if klass._dependents_owner != klass
|
|
61
65
|
klass.dependents = []
|
|
62
|
-
klass.
|
|
66
|
+
klass._dependents_owner = klass
|
|
63
67
|
end
|
|
64
68
|
|
|
65
69
|
if association.dependent && !klass.dependents.include?(association)
|
|
@@ -174,7 +174,7 @@ module Mongoid
|
|
|
174
174
|
# @param [ Hash ] attrs The single document attributes to process.
|
|
175
175
|
def update_nested_relation(parent, id, attrs)
|
|
176
176
|
first = existing.first
|
|
177
|
-
converted = first ?
|
|
177
|
+
converted = convert_id(first ? first.class : association.klass, id)
|
|
178
178
|
|
|
179
179
|
if existing.where(_id: converted).exists?
|
|
180
180
|
# document exists in association
|
|
@@ -186,19 +186,48 @@ module Mongoid
|
|
|
186
186
|
end
|
|
187
187
|
elsif association.embedded?
|
|
188
188
|
raise Errors::DocumentNotFound.new(association.klass, id)
|
|
189
|
-
elsif
|
|
190
|
-
|
|
189
|
+
elsif destroyable?(attrs)
|
|
190
|
+
# A destroy of a document that is not in the association is
|
|
191
|
+
# ignored, rather than reaching for it outside the association.
|
|
192
|
+
nil
|
|
193
|
+
elsif Mongoid.allow_reparenting_via_nested_attributes?
|
|
194
|
+
Mongoid::Warnings.warn_reparenting_via_nested_attributes
|
|
191
195
|
|
|
192
196
|
# push existing document to association
|
|
193
197
|
doc = association.klass.unscoped.find(converted)
|
|
198
|
+
# find returns nil instead of raising when
|
|
199
|
+
# Mongoid.raise_not_found_error is false; a missing document
|
|
200
|
+
# must still be an error here, consistent with the other
|
|
201
|
+
# not-found branches.
|
|
202
|
+
raise Errors::DocumentNotFound.new(association.klass, id) if doc.nil?
|
|
203
|
+
|
|
194
204
|
update_document(doc, attrs)
|
|
195
|
-
existing.push(doc)
|
|
205
|
+
existing.push(doc)
|
|
196
206
|
else
|
|
197
|
-
raise Errors::DocumentNotFound.new(association.klass,
|
|
207
|
+
raise Errors::DocumentNotFound.new(association.klass, not_found_params(parent, id))
|
|
198
208
|
end
|
|
199
209
|
|
|
200
210
|
parent.children_may_have_changed!
|
|
201
211
|
end
|
|
212
|
+
|
|
213
|
+
# The params to report when an id in the nested attributes could not
|
|
214
|
+
# be resolved within the association.
|
|
215
|
+
#
|
|
216
|
+
# @api private
|
|
217
|
+
#
|
|
218
|
+
# @param [ Document ] parent The parent document.
|
|
219
|
+
# @param [ String | BSON::ObjectId ] id of the related document.
|
|
220
|
+
#
|
|
221
|
+
# @return [ Hash | Object ] The params for the not found error.
|
|
222
|
+
def not_found_params(parent, id)
|
|
223
|
+
if association.is_a?(Association::Referenced::HasAndBelongsToMany)
|
|
224
|
+
# A many-to-many association has no foreign key on the child, so
|
|
225
|
+
# the id is only ever resolved within the association itself.
|
|
226
|
+
id
|
|
227
|
+
else
|
|
228
|
+
{ _id: id, association.foreign_key => parent.id }
|
|
229
|
+
end
|
|
230
|
+
end
|
|
202
231
|
end
|
|
203
232
|
end
|
|
204
233
|
end
|
|
@@ -58,12 +58,26 @@ module Mongoid
|
|
|
58
58
|
# @example Convert the id.
|
|
59
59
|
# builder.convert_id(Person, "4d371b444835d98b8b000010")
|
|
60
60
|
#
|
|
61
|
+
# Ids arriving from a form are always scalars. A Hash or an Array
|
|
62
|
+
# here means the parameters were crafted, and letting one through
|
|
63
|
+
# would turn the id into a query operator, so they are rejected.
|
|
64
|
+
#
|
|
61
65
|
# @param [ Class ] klass The class we're trying to convert for.
|
|
62
66
|
# @param [ String ] id The id, usually coming from the form.
|
|
63
67
|
#
|
|
64
68
|
# @return [ BSON::ObjectId | String | Object ] The converted id.
|
|
69
|
+
#
|
|
70
|
+
# @raise [ Errors::DocumentNotFound ] if the id is not a scalar, or
|
|
71
|
+
# cannot be converted to the type the class uses for its ids.
|
|
72
|
+
# The BSON::Error rescue is defensive: a value that reaches
|
|
73
|
+
# BSON::ObjectId.mongoize and raises there must not surface as an
|
|
74
|
+
# unhandled BSON error in the caller.
|
|
65
75
|
def convert_id(klass, id)
|
|
76
|
+
raise Errors::DocumentNotFound.new(klass, id) if id.is_a?(::Hash) || id.is_a?(::Array)
|
|
77
|
+
|
|
66
78
|
klass.using_object_ids? ? BSON::ObjectId.mongoize(id) : id
|
|
79
|
+
rescue BSON::Error
|
|
80
|
+
raise Errors::DocumentNotFound.new(klass, id)
|
|
67
81
|
end
|
|
68
82
|
|
|
69
83
|
private
|
|
@@ -534,12 +534,111 @@ module Mongoid
|
|
|
534
534
|
# @return [ Integer ] The number of documents deleted.
|
|
535
535
|
def remove_all(conditions = nil, method = :delete_all)
|
|
536
536
|
selector = conditions || {}
|
|
537
|
-
|
|
537
|
+
selector.merge!(criteria.selector)
|
|
538
|
+
|
|
539
|
+
# Scanning before the delete means scanning only what is already in
|
|
540
|
+
# memory, so it is only done where there is a pattern whose cost has
|
|
541
|
+
# to be bounded. Everywhere else the delete comes first and the scan
|
|
542
|
+
# sees the survivors, which is what this association has always
|
|
543
|
+
# done.
|
|
544
|
+
#
|
|
545
|
+
# The limit is read once and carried into whichever branch is taken.
|
|
546
|
+
# Letting the branch below ask again would read it a second time,
|
|
547
|
+
# across a server round trip, and a limit that had become positive
|
|
548
|
+
# in the meantime would put a deadline on the delete_if -- a query,
|
|
549
|
+
# and an unbind of every match -- which is the arrangement this
|
|
550
|
+
# branch exists to avoid.
|
|
551
|
+
limit = Matcher::RegexpBudget.limit_for(selector)
|
|
552
|
+
return remove_all_bounded(selector, method, limit) if limit
|
|
553
|
+
|
|
554
|
+
removed = klass.send(method, selector)
|
|
555
|
+
|
|
556
|
+
# No scope around this. The scan runs after the delete, so the
|
|
557
|
+
# association this loads comes back holding only what the delete did
|
|
558
|
+
# not match, and there is no pattern here to bound in any case.
|
|
559
|
+
# Opening a scope would save _matches? from deciding once per
|
|
560
|
+
# document -- 0.48us against 1.74us for the match itself -- but a
|
|
561
|
+
# scope with nothing to bound suppresses that decision for
|
|
562
|
+
# everything nested inside it, and loading the association runs
|
|
563
|
+
# find callbacks. A pattern in a selector one of those evaluates
|
|
564
|
+
# would go unguarded, which costs more than the saving is worth on a
|
|
565
|
+
# path that has just made two round trips.
|
|
538
566
|
_target.delete_if do |doc|
|
|
539
567
|
doc._matches?(selector).tap do |b|
|
|
540
568
|
unbind_one(doc) if b
|
|
541
569
|
end
|
|
542
570
|
end
|
|
571
|
+
|
|
572
|
+
removed
|
|
573
|
+
end
|
|
574
|
+
|
|
575
|
+
# Deletes all related documents matching a selector that carries a
|
|
576
|
+
# regular expression, with the whole scan under one regexp budget.
|
|
577
|
+
#
|
|
578
|
+
# The scan runs before anything is deleted, so a budget that runs out
|
|
579
|
+
# leaves both the database and the association untouched rather than
|
|
580
|
+
# reporting a failure for a delete that has already happened. It also
|
|
581
|
+
# means the budget is closed by the time the association is mutated,
|
|
582
|
+
# so where the budget is enforced with Timeout there is no window for
|
|
583
|
+
# the exception to land in the middle of an unbind.
|
|
584
|
+
#
|
|
585
|
+
# @param [ Hash ] selector The selector to delete with.
|
|
586
|
+
# @param [ Symbol ] method The deletion method to call.
|
|
587
|
+
# @param [ Float ] limit The seconds the scan may spend, as read by
|
|
588
|
+
# the caller when it chose this path.
|
|
589
|
+
#
|
|
590
|
+
# @return [ Integer ] The number of documents deleted.
|
|
591
|
+
def remove_all_bounded(selector, method, limit)
|
|
592
|
+
# Only what the association already holds. Iterating it instead
|
|
593
|
+
# would load the whole association before the delete, and for a
|
|
594
|
+
# broad pattern that is every document the association has: /.*/ is
|
|
595
|
+
# both the cheapest pattern an attacker can supply and the one that
|
|
596
|
+
# matches everything, so bounding the cost of the matching would
|
|
597
|
+
# have been paid for with an unbounded amount of memory. Nothing is
|
|
598
|
+
# missed by not loading, because the server evaluates the same
|
|
599
|
+
# selector for the delete itself.
|
|
600
|
+
#
|
|
601
|
+
# It also means the scan runs no query, so no deadline of ours can
|
|
602
|
+
# land on one. Where the budget is enforced with Timeout, covering
|
|
603
|
+
# a query would report a slow network as a regexp timeout and could
|
|
604
|
+
# deliver the asynchronous exception inside the driver's socket
|
|
605
|
+
# read.
|
|
606
|
+
documents = _target.in_memory
|
|
607
|
+
|
|
608
|
+
matching = Matcher::RegexpBudget.open_with(limit) do
|
|
609
|
+
documents.select { |doc| doc._matches?(selector) }
|
|
610
|
+
end
|
|
611
|
+
|
|
612
|
+
removed = klass.send(method, selector)
|
|
613
|
+
|
|
614
|
+
# The ids are already known to be in memory, so the loaded and added
|
|
615
|
+
# documents can be dropped directly. Enumerable#delete would look
|
|
616
|
+
# each one up by id and stop at the first hash that has it, which
|
|
617
|
+
# removes the wrong instance when both hashes hold one for the same
|
|
618
|
+
# id. Enumerable#delete_if drops it from both, as this does, but
|
|
619
|
+
# only after load_all!, which is the load this scan exists to avoid.
|
|
620
|
+
#
|
|
621
|
+
# Which documents get unbound has always depended on which ones
|
|
622
|
+
# happened to be in memory, and scanning only what is in memory
|
|
623
|
+
# keeps that. It matters for has_and_belongs_to_many, whose
|
|
624
|
+
# unbind_one pulls the id out of the foreign key array on _base and
|
|
625
|
+
# marks it dirty; the scan used to run after the delete, against an
|
|
626
|
+
# association that a cold proxy had just reloaded as holding only
|
|
627
|
+
# the survivors, so nothing matched and nothing was unbound.
|
|
628
|
+
#
|
|
629
|
+
# TODO: unbinding should not depend on what was in memory. Every
|
|
630
|
+
# matched document leaves the association, so every one of them
|
|
631
|
+
# should be unbound, which would also stop _base's foreign key
|
|
632
|
+
# array from keeping ids that no longer resolve. That is a
|
|
633
|
+
# behaviour change for has_and_belongs_to_many and belongs in a
|
|
634
|
+
# ticket of its own, with a release note; it should not ride along
|
|
635
|
+
# on this one.
|
|
636
|
+
matching.each do |doc|
|
|
637
|
+
unbind_one(doc)
|
|
638
|
+
_target._loaded.delete(doc._id)
|
|
639
|
+
_target._added.delete(doc._id)
|
|
640
|
+
end
|
|
641
|
+
|
|
543
642
|
removed
|
|
544
643
|
end
|
|
545
644
|
|
|
@@ -172,6 +172,23 @@ module Mongoid
|
|
|
172
172
|
end
|
|
173
173
|
alias :klass :relation_class
|
|
174
174
|
|
|
175
|
+
# The class of the association target, or nil when the named class is
|
|
176
|
+
# not defined.
|
|
177
|
+
#
|
|
178
|
+
# An association may name a class that never gets defined. The
|
|
179
|
+
# association is then unusable, but its owner still has to be. Callers
|
|
180
|
+
# that walk every association of every model, rather than following the
|
|
181
|
+
# one the application asked for, use this instead of relation_class.
|
|
182
|
+
#
|
|
183
|
+
# @return [ Class | nil ] The association objects' class.
|
|
184
|
+
#
|
|
185
|
+
# @api private
|
|
186
|
+
def try_relation_class
|
|
187
|
+
relation_class
|
|
188
|
+
rescue NameError
|
|
189
|
+
nil
|
|
190
|
+
end
|
|
191
|
+
|
|
175
192
|
# The class name of the object owning this association.
|
|
176
193
|
#
|
|
177
194
|
# @return [ String ] The owning objects' class name.
|
|
@@ -22,6 +22,14 @@ module Mongoid
|
|
|
22
22
|
# @raise [ Errors::CreateCollectionFailure ] If collection creation failed.
|
|
23
23
|
# @raise [ Errors::DropCollectionFailure ] If an attempt to drop collection failed.
|
|
24
24
|
def create_collection(force: false)
|
|
25
|
+
Threaded.with_collection_management do
|
|
26
|
+
perform_create_collection(force: force)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def perform_create_collection(force:)
|
|
25
33
|
if collection_name.empty?
|
|
26
34
|
# This is most probably an anonymous class, we ignore them.
|
|
27
35
|
return
|
|
@@ -21,17 +21,25 @@ module Mongoid
|
|
|
21
21
|
#
|
|
22
22
|
# @return [ Hash ] The encryption schema map.
|
|
23
23
|
def encryption_schema_map(default_database, models = ::Mongoid.models)
|
|
24
|
-
visited = Set.new
|
|
25
24
|
models.each_with_object({}) do |model, map|
|
|
26
|
-
next if visited.include?(model)
|
|
27
|
-
visited << model
|
|
28
25
|
next if model.embedded?
|
|
29
|
-
next unless model.
|
|
26
|
+
next unless model.requires_encryption_schema?
|
|
30
27
|
|
|
31
28
|
database = model.storage_options.fetch(:database) { default_database }
|
|
29
|
+
# A callable database name cannot be resolved here: the documented
|
|
30
|
+
# multi-tenant idiom has no correct value while the client is being
|
|
31
|
+
# built. Interpolating the callable would produce a key that never
|
|
32
|
+
# matches any namespace, so leave the model out of the map. Writes
|
|
33
|
+
# are refused later, by PersistenceContext, rather than silently
|
|
34
|
+
# going out unencrypted.
|
|
35
|
+
next if database.respond_to?(:call)
|
|
36
|
+
|
|
32
37
|
key = "#{database}.#{model.collection_name}"
|
|
33
|
-
props = metadata_for(model).merge(properties_for(model,
|
|
34
|
-
|
|
38
|
+
props = metadata_for(model).merge(properties_for(model, [ model ]))
|
|
39
|
+
# The root of a collection schema describes the document, so it is
|
|
40
|
+
# always an object. Saying so matters when a nested schema carries
|
|
41
|
+
# encryptMetadata: mongocryptd rejects the schema otherwise.
|
|
42
|
+
map[key] = { 'bsonType' => 'object' }.merge(props) unless props.empty?
|
|
35
43
|
end
|
|
36
44
|
end
|
|
37
45
|
|
|
@@ -100,11 +108,12 @@ module Mongoid
|
|
|
100
108
|
# are marked as encrypted.
|
|
101
109
|
#
|
|
102
110
|
# @param [ Mongoid::Document ] model The model to generate the properties for.
|
|
103
|
-
# @param [
|
|
111
|
+
# @param [ Array<Mongoid::Document> ] path The models the walk is already
|
|
112
|
+
# inside of, outermost first.
|
|
104
113
|
#
|
|
105
114
|
# @return [ Hash ] The encryption properties.
|
|
106
|
-
def properties_for(model,
|
|
107
|
-
result = properties_for_fields(model).merge(properties_for_relations(model,
|
|
115
|
+
def properties_for(model, path)
|
|
116
|
+
result = properties_for_fields(model).merge(properties_for_relations(model, path))
|
|
108
117
|
if result.empty?
|
|
109
118
|
{}
|
|
110
119
|
else
|
|
@@ -141,20 +150,29 @@ module Mongoid
|
|
|
141
150
|
# are configured to be encrypted.
|
|
142
151
|
#
|
|
143
152
|
# @param [ Mongoid::Document ] model The model to generate the properties for.
|
|
144
|
-
# @param [
|
|
153
|
+
# @param [ Array<Mongoid::Document> ] path The models the walk is already
|
|
154
|
+
# inside of, outermost first.
|
|
145
155
|
#
|
|
146
156
|
# @return [ Hash ] The encryption properties.
|
|
147
|
-
def properties_for_relations(model,
|
|
157
|
+
def properties_for_relations(model, path)
|
|
148
158
|
model.relations.each_with_object({}) do |(name, relation), props|
|
|
149
|
-
|
|
159
|
+
# relation_class constantizes, and a polymorphic embedded_in has no
|
|
160
|
+
# class to resolve, so the relation type has to be checked first.
|
|
150
161
|
next unless relation.is_a?(Association::Embedded::EmbedsOne)
|
|
151
|
-
next unless relation.relation_class.encrypted?
|
|
152
162
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
163
|
+
klass = relation.try_relation_class
|
|
164
|
+
# An association target does not have to be a Mongoid document, and
|
|
165
|
+
# the class it names does not have to exist.
|
|
166
|
+
next unless klass.respond_to?(:requires_encryption_schema?)
|
|
167
|
+
# Stop at a model the walk is already inside of, or a self-embedding
|
|
168
|
+
# model never terminates. The path covers the current branch only:
|
|
169
|
+
# a model embedded by two parents, or twice by one parent, has to be
|
|
170
|
+
# emitted at every place it appears.
|
|
171
|
+
next if path.include?(klass)
|
|
172
|
+
next unless klass.requires_encryption_schema?
|
|
173
|
+
|
|
174
|
+
metadata_for(klass).merge(
|
|
175
|
+
properties_for(klass, path + [ klass ])
|
|
158
176
|
).tap do |properties|
|
|
159
177
|
props[name] = { 'bsonType' => 'object' }.merge(properties) unless properties.empty?
|
|
160
178
|
end
|
data/lib/mongoid/config.rb
CHANGED
|
@@ -110,20 +110,22 @@ module Mongoid
|
|
|
110
110
|
# to `:global_thread_pool`.
|
|
111
111
|
option :global_executor_concurrency, default: nil
|
|
112
112
|
|
|
113
|
-
# When this flag is true, it will be possible to
|
|
114
|
-
#
|
|
115
|
-
# nested attributes for another parent record
|
|
113
|
+
# When this flag is true, it will be possible to add a record to a
|
|
114
|
+
# "has_many" or "has_and_belongs_to_many" association by passing that
|
|
115
|
+
# record's id in the nested attributes for another parent record, even
|
|
116
|
+
# when the record does not already belong to that association. For a
|
|
117
|
+
# "has_many" association this moves the record to the new parent.
|
|
116
118
|
#
|
|
117
|
-
# When this flag is false,
|
|
118
|
-
#
|
|
119
|
+
# When this flag is false, an id in nested attributes is only resolved
|
|
120
|
+
# within the association itself, and anything else raises an error.
|
|
119
121
|
#
|
|
120
|
-
# The default is `
|
|
122
|
+
# The default is `false`. Note that allowing reparenting via nested attributes
|
|
121
123
|
# is a potential security risk, since it could allow a malicious user to move
|
|
122
124
|
# records that they do not own to a parent record that they do own.
|
|
123
125
|
#
|
|
124
|
-
# This option will
|
|
125
|
-
#
|
|
126
|
-
option :allow_reparenting_via_nested_attributes, default:
|
|
126
|
+
# This option will be removed in Mongoid 10, and the only behavior will be
|
|
127
|
+
# as if this option were set to false.
|
|
128
|
+
option :allow_reparenting_via_nested_attributes, default: false
|
|
127
129
|
|
|
128
130
|
# When this flag is true, any documents in associations with `autosave: true`
|
|
129
131
|
# will be saved even if they have not been changed. When this flag is false,
|
|
@@ -205,6 +207,51 @@ module Mongoid
|
|
|
205
207
|
# See https://jira.mongodb.org/browse/MONGOID-5785 for more details.
|
|
206
208
|
option :allow_scopes_to_unset_default_scope, default: false
|
|
207
209
|
|
|
210
|
+
# The maximum number of seconds that evaluating a single query in memory
|
|
211
|
+
# may spend executing regular expressions. Queries against an embedded
|
|
212
|
+
# association are evaluated in the calling thread, so a pattern built from
|
|
213
|
+
# user input runs locally and can otherwise consume unbounded CPU. The
|
|
214
|
+
# limit is cumulative over the whole query, since cost grows with the
|
|
215
|
+
# number of documents and conditions as well as with the pattern.
|
|
216
|
+
#
|
|
217
|
+
# Set to nil to remove the limit. On Ruby 3.2 and later the remaining
|
|
218
|
+
# budget is compiled into the pattern, so the limit counts only the time
|
|
219
|
+
# spent matching. Earlier Rubies have no per-Regexp timeout, so the query
|
|
220
|
+
# is bounded with Timeout instead and the limit is wall clock over the
|
|
221
|
+
# whole in-memory evaluation.
|
|
222
|
+
#
|
|
223
|
+
# See https://jira.mongodb.org/browse/MONGOID-5981 for details.
|
|
224
|
+
option :in_memory_regexp_time_limit, default: 5.0
|
|
225
|
+
|
|
226
|
+
# When false (default), query operators are restricted when building a
|
|
227
|
+
# selector, so that user-supplied input reaching the query builder cannot
|
|
228
|
+
# make MongoDB execute arbitrary JavaScript. Two rules are enforced:
|
|
229
|
+
#
|
|
230
|
+
# - An operator at the top level of an expression must appear in
|
|
231
|
+
# +Criteria::Queryable::Selectable::ALLOWED_QUERY_OPERATORS+.
|
|
232
|
+
# - +$where+, +$function+, and +$accumulator+ are rejected at any depth,
|
|
233
|
+
# including inside +$expr+ and the logical operators.
|
|
234
|
+
#
|
|
235
|
+
# This applies to every query method that accepts an expression, including
|
|
236
|
+
# +where+, +find_by+, +and+, +or+, +nor+, +not+, +any_of+, and +none_of+.
|
|
237
|
+
# It also governs the string form of +where+, e.g.
|
|
238
|
+
# +where("this.name == 'admin'")+, which the server evaluates as +$where+.
|
|
239
|
+
# Applications relying on that form must set this option to true.
|
|
240
|
+
#
|
|
241
|
+
# The APIs that request JavaScript explicitly, +Criteria#for_js+ and
|
|
242
|
+
# +js_query+, are unaffected: there the developer has asked for it.
|
|
243
|
+
#
|
|
244
|
+
# Set to true to restore the unrestricted pass-through behavior.
|
|
245
|
+
#
|
|
246
|
+
# Note that this option is deliberately not tied to +load_defaults+: an
|
|
247
|
+
# application that has opted into older defaults still gets the guard, and
|
|
248
|
+
# must set this option explicitly to turn it off.
|
|
249
|
+
#
|
|
250
|
+
# See https://jira.mongodb.org/browse/MONGOID-5939,
|
|
251
|
+
# https://jira.mongodb.org/browse/MONGOID-5993,
|
|
252
|
+
# https://jira.mongodb.org/browse/MONGOID-5994 for details.
|
|
253
|
+
option :allow_unsafe_query_operators, default: false
|
|
254
|
+
|
|
208
255
|
# Returns the Config singleton, for use in the configure DSL.
|
|
209
256
|
#
|
|
210
257
|
# @return [ self ] The Config singleton.
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
# rubocop:todo all
|
|
3
3
|
|
|
4
|
+
require 'mongoid/field_readable'
|
|
5
|
+
|
|
4
6
|
module Mongoid
|
|
5
7
|
module Contextual
|
|
6
8
|
module Aggregable
|
|
7
9
|
# Contains behavior for aggregating values in memory.
|
|
8
10
|
module Memory
|
|
11
|
+
include FieldReadable
|
|
9
12
|
|
|
10
13
|
# Get all the aggregate values for the provided field.
|
|
11
14
|
# Provided for interface consistency with Aggregable::Mongo.
|
|
@@ -30,7 +33,7 @@ module Mongoid
|
|
|
30
33
|
#
|
|
31
34
|
# @return [ Numeric ] The average.
|
|
32
35
|
def avg(field)
|
|
33
|
-
total = count { |doc| !doc
|
|
36
|
+
total = count { |doc| !read_field_value(doc, field).nil? }
|
|
34
37
|
return nil unless total > 0
|
|
35
38
|
|
|
36
39
|
total = total.to_f if total.is_a?(Integer)
|
|
@@ -116,7 +119,7 @@ module Mongoid
|
|
|
116
119
|
def aggregate_by(field, method)
|
|
117
120
|
return nil unless any?
|
|
118
121
|
|
|
119
|
-
map { |doc| doc
|
|
122
|
+
map { |doc| read_field_value(doc, field) }.compact.public_send(method)
|
|
120
123
|
end
|
|
121
124
|
end
|
|
122
125
|
end
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
require "mongoid/contextual/aggregable/memory"
|
|
5
5
|
require "mongoid/association/eager_loadable"
|
|
6
|
+
require "mongoid/field_readable"
|
|
6
7
|
|
|
7
8
|
module Mongoid
|
|
8
9
|
module Contextual
|
|
@@ -17,6 +18,7 @@ module Mongoid
|
|
|
17
18
|
include Association::EagerLoadable
|
|
18
19
|
include Queryable
|
|
19
20
|
include Positional
|
|
21
|
+
include FieldReadable
|
|
20
22
|
|
|
21
23
|
# @attribute [r] root The root document.
|
|
22
24
|
# @attribute [r] path The atomic path.
|
|
@@ -172,10 +174,20 @@ module Mongoid
|
|
|
172
174
|
# @param [ Criteria ] criteria The criteria.
|
|
173
175
|
def initialize(criteria)
|
|
174
176
|
@criteria, @klass = criteria, criteria.klass
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
177
|
+
|
|
178
|
+
# Resolving the collection can build a Mongo::Client the first time it
|
|
179
|
+
# runs, so it happens before the budget opens. Where the budget is
|
|
180
|
+
# enforced with Timeout the exception is asynchronous, and landing in
|
|
181
|
+
# the middle of that would leave a half-built client behind.
|
|
182
|
+
if (first = criteria.documents.first)
|
|
183
|
+
@root = first._root
|
|
184
|
+
@collection = @root.collection
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# One regexp budget covers the whole scan, so that the limit bounds the
|
|
188
|
+
# query rather than each document individually.
|
|
189
|
+
@documents = Matcher::RegexpBudget.open(criteria.selector) do
|
|
190
|
+
criteria.documents.select { |doc| doc._matches?(criteria.selector) }
|
|
179
191
|
end
|
|
180
192
|
apply_sorting
|
|
181
193
|
apply_options
|
|
@@ -733,11 +745,10 @@ module Mongoid
|
|
|
733
745
|
# _translations hash so that we can get the specified translation in
|
|
734
746
|
# the remaining
|
|
735
747
|
if field&.localized?
|
|
736
|
-
document.
|
|
748
|
+
document.public_send("#{segment}_translations")
|
|
737
749
|
end
|
|
738
750
|
end
|
|
739
|
-
|
|
740
|
-
res.nil? ? document.try(meth) : res
|
|
751
|
+
res.nil? ? read_field_value(document, segment) : res
|
|
741
752
|
elsif document.is_a?(Hash)
|
|
742
753
|
# TODO: Remove the indifferent access when implementing MONGOID-5410.
|
|
743
754
|
document.key?(segment.to_s) ?
|
|
@@ -326,6 +326,10 @@ module Mongoid
|
|
|
326
326
|
end
|
|
327
327
|
end
|
|
328
328
|
end
|
|
329
|
+
# Every query method that takes a user-supplied expression normalizes
|
|
330
|
+
# it here, so this is where the operator guard is enforced. See
|
|
331
|
+
# Selectable#_mongoid_validate_operators! for what it does not cover.
|
|
332
|
+
_mongoid_validate_operators!(result)
|
|
329
333
|
result
|
|
330
334
|
end
|
|
331
335
|
|