ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Critical - P2
    • 9.1.1, 9.0.12
    • Affects Version/s: 9.0.0, 9.1.0, 9.0.1, 9.0.2, 9.0.3, 9.0.4, 9.0.5, 9.0.6, 9.0.7, 9.0.8, 9.0.9, 9.0.10, 9.0.11
    • Component/s: None
    • None
    • None
    • 1
    • Ruby Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Remediation ticket for SECBUG-4354 (Mongo Internal). Do not reference this ticket in a public PR description or discuss it in public channels before the fix is released.

      Mongoid::Config::Encryption.encryption_schema_map generates the CSFLE schema map that is passed to the driver verbatim in Mongoid::Clients::Factory#build_auto_encryption_options (lib/mongoid/clients/factory.rb:99). The driver encrypts only the fields present in that map, so a field omitted from it is written to MongoDB in plaintext, with no exception raised and nothing logged.

      Two independent defects in lib/mongoid/config/encryption.rb cause encrypted fields on embedded models to be omitted.

      1. Shared visited Set

      A single visited Set is created once for all models (line 23) and serves two purposes that need opposite lifetimes: a permanent "this collection is already in the map" ledger (line 25), and a per-branch cycle guard inside properties_for_relations (line 149) that ought to reset as the walk backs out.

      Every model is added to visited (line 27) before the "next if model.embedded?" check (line 28). An embedded encrypted class appearing earlier in Mongoid.models is therefore recorded as visited with nothing emitted for it, and its parent then skips that relation at line 149.

      The relation class is added to visited at line 153 after the first parent is processed, so a second parent, or a second relation on the same parent, embedding the same class is skipped too.

      Mongoid.models is populated in pure class-load order (lib/mongoid/document.rb:34 into lib/mongoid/config.rb:390). Zeitwerk sorts directory entries before eager loading, so Rails eager-loads app/models alphabetically. A child whose file name sorts before its parent's triggers this with no unusual configuration.

      1. Top-level gate ignores embedded encrypted fields

      Line 29 skips any model whose encrypted? is false. Encryptable#encrypted? (lib/mongoid/encryptable.rb:36) inspects only the model's own fields and its own encrypt_metadata, never its embedded relations. A parent whose encrypted data lives solely in an embedded child is therefore dropped entirely and no schema is emitted for that collection at all. This variant has no load-order and no shared-parent precondition.

      Confirmed behaviour

      Verified against master at 8c976ba1c and mongoid 9.1.0. No server is required: encryption_schema_map is pure computation over model metadata and accepts an explicit models array, so load order can be exercised directly.

      • Parent embeds_one child, parent listed first: the embedded encrypted field is present. This is the only shape that works.
      • Same models, child listed first: the embedded encrypted field is absent. The parent's own encrypted fields are still present, which is what makes this easy to miss.
      • Two parents embedding the same encrypted class: only the first parent gets the directives.
      • Three-level nesting with the middle level listed first: the entire subtree below it is lost, not just that level.
      • Parent with no encrypted field of its own and no encrypt_with: nothing is emitted for the collection, in any order.
      • No exception and nothing on stderr in any of the failing cases.

      The documented example is affected. The Mongoid encryption documentation shows Patient, declaring encrypt_with, embeds_one Insurance with an encrypted policy_number. Under Rails eager-load order insurance.rb loads before patient.rb, and insurance.policy_number is then absent from the map. That same pair exists as a spec fixture in spec/support/crypt/models.rb, and spec/mongoid/config/encryption_spec.rb:66 passes it as [Crypt::Patient, Crypt::Insurance], parent first. That ordering is why the existing specs pass while the documented usage is broken.

      Affected versions

      9.0.0 through 9.0.11, and 9.1.0. The code was introduced in b968ade6a (MONGOID-5585) and first released in 9.0.0. No 8.x release contains it, confirmed with git tag --contains.

      Fix constraints

      The cycle guard is load-bearing and must not simply be removed. properties_for recurses through embedded relations at line 157, so a self-embedding model (embeds_one :reply, class_name: 'Comment') terminates only because of the check at line 149. The guard has to keep stopping true cycles while no longer stopping the cases above, which means a per-branch path that unwinds as the walk backs out rather than one Set shared across the whole run.

      Both defects need fixing. Separating the permanent map-level dedup from the per-branch cycle guard addresses the first. The second additionally requires the top-level gate to admit models that have an encrypted embedded descendant.

      The dedup at line 25 is close to redundant, since Mongoid.models is already uniqued on push at lib/mongoid/config.rb:390.

      Suggested test coverage: child-before-parent ordering, two parents sharing an embedded class, one parent with two embeds_one relations of the same class, nesting deeper than two levels, a parent with no encrypted field of its own, and a self-embedding model to prove the cycle guard survives.

      Out of scope

      Encrypted fields inside embeds_many are never mapped, in any order, because line 150 matches only Association::Embedded::EmbedsOne. This is documented behaviour ("Mongoid does not support encryption of embeds_many associations") and libmongocrypt cannot express per-field encryption under array items, so the omission itself is correct. The separate hardening item is that encrypt: on such a field is silently accepted at declaration time and then ignored; it should warn or raise. Track that separately, it is not a security issue.

            Assignee:
            Dmitry Rybakov
            Reporter:
            Dmitry Rybakov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: