Fixes SECBUG-6041 (Minos) and SECBUG-6037 (HackerOne).
The automatic encryption schema map emits the nested schema of an embeds_one association under the association's Ruby name, while the document is persisted under the association's store_as key. When they differ, libmongocrypt finds no matching path and every encrypt: field on that embedded document is written in plaintext. Nothing is raised or logged. Reads and deterministic queries through Mongoid keep working because the alias is rewritten on the query side too, so the literal also goes to the server in plaintext and matches the plaintext at rest.
class EmbeddedSecret include Mongoid::Document field :token, type: String, encrypt: { deterministic: true, key_id: KEY_ID } embedded_in :record end class Record include Mongoid::Document embeds_one :secret, class_name: 'EmbeddedSecret', store_as: :s end Mongoid.config.encryption_schema_map('db')['db.records']['properties'].keys # => ["secret"] Record.create!(secret: EmbeddedSecret.new(token: 'x')) # stored as { s: { token: 'x' } }, token in plaintext; without store_as it is ciphertext
Cause: in lib/mongoid/config/encryption.rb the relations walk iterates model.relations, which is keyed by the Ruby name, and emits the nested schema under that key. The stored key is available as relation.store_as (same as relation.key). Field-level aliases (field :x, as: :y) are not affected, the fields hash is keyed by the stored name.
Expected:
- Key the nested schema by relation.store_as at every nesting level.
- Specs: embeds_one with store_as at the top level and nested one level down; generated map keyed by the stored name; an integration spec that writes through an auto-encryption client and reads the raw document back with a plain client, asserting ciphertext under the stored key.
- The stricter write-time check planned in MONGOID-6037 should cover this case too.
Documents written before the fix keep their plaintext values, and deterministic queries will not match them until they are rewritten. Say so in the release notes.
Affects 9.0.0 through 9.1.1 and master. 8.x has no automatic schema generation. Backport to 9.0.
Do not mention the SECBUG keys or the vulnerability in commit messages. The fix goes through the private security-advisory fork, not a public PR, until the release.