Skip to content

MONGOID-6037 Encryption schema map keeps one model per collection and drops the other models' encrypted fields - #6212

Merged
jamis merged 1 commit into
mongodb:9.0-stablefrom
jamis:6037-backport
Nov 12, 2026
Merged

jamis merged 1 commit into
mongodb:9.0-stablefrom
jamis:6037-backport

Conversation

@jamis

@jamis jamis commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

backport from master

* 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
jamis requested a review from a team as a code owner November 12, 2026 22:26
@jamis jamis added the bug Fixes a bug, with no new features or broken compatibility label Nov 12, 2026
@jamis
jamis requested review from comandeo-mongo and a balanced review from Copilot November 12, 2026 22:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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)
@jamis
jamis merged commit d1c7210 into mongodb:9.0-stable Nov 12, 2026
51 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Fixes a bug, with no new features or broken compatibility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants