Repository navigation
MONGOID-6037 Encryption schema map keeps one model per collection and drops the other models' encrypted fields - #6212
Merged
Conversation
* MONGOID-6037 Add specs for models sharing an encryption namespace * MONGOID-6037 Merge encryption schemas for models sharing a namespace * MONGOID-6037 Consider subclasses of embedded models when checking encryption * MONGOID-6037 Check that the schema entry covers every encrypted field * MONGOID-6037 Address review feedback * MONGOID-6037 Drop redundant document check
jamis
requested review from
comandeo-mongo
and
a balanced review from Copilot
November 12, 2026 22:26
Jibola
approved these changes
Nov 12, 2026
Jibola
approved these changes
Nov 12, 2026
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Encryption caches can miss later-loaded fields and permit plaintext writes, while schema merging can silently apply incorrect inherited encryption settings.
4 open issues
What changed in this PR
Backports the MONGOID-6037 fix so Mongoid’s encryption schema map includes encrypted fields from all models sharing a collection, including inherited and embedded models.
Changes:
- Merge schema entries across models and subclasses, detecting conflicting encryption settings.
- Check field-level schema coverage before persistence and add descriptive errors.
- Add unit and integration coverage for shared collections, inheritance, embedded subclasses, and incomplete schemas.
| File | Description |
|---|---|
spec/support/crypt/models.rb |
Add shared-collection and inheritance fixtures. |
spec/mongoid/encryptable_spec.rb |
Cover encryption detection in embedded subclasses. |
spec/mongoid/config/encryption_spec.rb |
Test schema merging and conflict detection. |
spec/integration/encryption_spec.rb |
Check stored ciphertext and incomplete-schema rejection. |
lib/mongoid/persistence_context.rb |
Validate encrypted-field coverage before persistence. |
lib/mongoid/errors/no_encryption_schema.rb |
Support errors for missing field coverage. |
lib/mongoid/errors/invalid_encryption_schema.rb |
Define the schema-conflict error. |
lib/mongoid/errors.rb |
Load the new error class. |
lib/mongoid/encryptable.rb |
Include embedded subclasses in encryption detection. |
lib/mongoid/config/encryption.rb |
Merge model schemas and collect encrypted paths. |
lib/config/locales/en.yml |
Add schema-conflict and missing-field messages. |
Review effort: Balanced
Comment on lines
+62
to
+66
| return model.instance_variable_get(:@encrypted_paths_for) if | ||
| model.instance_variable_defined?(:@encrypted_paths_for) | ||
|
|
||
| properties = properties_for(model, [ model ])['properties'] || {} | ||
| model.instance_variable_set(:@encrypted_paths_for, collect_encrypted_paths(properties, [])) |
| # | ||
| # @return [ Hash ] The merged schema. | ||
| def merge_schema_entries(left, right, namespace = nil) | ||
| left.merge(right) do |key, left_value, right_value| |
|
|
||
| found, nested_complete = klass.embedded_encryption_scan(path + [ klass ]) | ||
| return [ true, true ] if found | ||
| ([ klass ] + klass.descendants).uniq.each do |candidate| |
Comment on lines
+258
to
+259
| properties = schema_value(node, 'properties') | ||
| return false unless properties.is_a?(Hash) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


backport from master