Reported as SECBUG-4352 (Aegis finding bdc2377185c2), validated by reproduction.
Mongoid builds the CSFLE schema map once at client construction and keys it by namespace. Any model whose database is not a constant gets a key that never matches the namespace its writes go to. The driver finds no schema entry, encrypts nothing, and raises no error, so fields declared for encryption are persisted in plaintext.
Two ways in
# Path 1 # Mongoid::Config::Encryption.encryption_schema_map, at # lib/mongoid/config/encryption.rb:31-32, reads # model.storage_options.fetch(:database) raw and interpolates it into # the namespace key. It skips the callable resolution that # lib/mongoid/persistence_context.rb:199 applies everywhere else, so a # lambda interpolates as Proc#to_s and the key can never match. # Path 2 # A runtime override, Model.with(database: ...) or # Mongoid.override_database, targets a namespace that was never in # the map to begin with.
The lambda form is documented on the Mongoid persistence configuration manual page, and recommended there for multi-tenant isolation. The encryption page documents no caveat.
Reproduction
Verified against MongoDB 8.2 enterprise with the crypt shared library, on master (8c976ba1c). The client is configured for auto encryption as in the encryption integration spec. The reader below is a second client with no auto encryption, used to see what actually landed on disk.
class StaticCar include Mongoid::Document store_in database: 'vehicles', collection: 'cars' encrypt_with key_id: '...', deterministic: true field :vin, type: String, encrypt: true end class ProcCar include Mongoid::Document store_in database: -> { 'vehicles_proc' }, collection: 'proc_cars' encrypt_with key_id: '...', deterministic: true field :vin, type: String, encrypt: true end car = StaticCar.create!(vin: 'STATIC-1') plain_client.use('vehicles')['cars'].find(_id: car.id).first[:vin] # control: BSON::Binary, type=ciphertext, as expected car = ProcCar.create!(vin: 'PROC-SECRET-1') plain_client.use('vehicles_proc')['proc_cars'].find(_id: car.id).first[:vin] # bug: "PROC-SECRET-1", stored in the clear, no error, no warning Mongoid.config.encryption_schema_map('default_db', [ProcCar]).keys # the interpolated Proc, which can never match vehicles_proc.proc_cars car = StaticCar.with(database: 'vehicles_tenant_b') { |k| k.create!(vin: 'RUNTIME-SECRET') } plain_client.use('vehicles_tenant_b')['cars'].find(_id: car.id).first[:vin] # same outcome for a runtime override: "RUNTIME-SECRET"
Notes on the fix
Resolving the callable when the map is built is not sufficient. The documented idiom is:
store_in database: -> { Thread.current[:tenant_database] }
which has no correct value at client construction time, so calling it there would pin one tenant's database and leave every other tenant unencrypted just as silently.
The fix needs to fail closed rather than produce an unmatched entry: detect that an encrypted model's namespace cannot be determined statically, and raise or warn loudly. Worth deciding whether the same check belongs on the write path, where the target namespace is known.
Affected versions
9.0.0 through 9.0.11, 9.1.0, and master. The file lib/mongoid/config/encryption.rb was added in 9.0.0 (MONGOID-5585) and the line is unchanged since. 8.x has no automatic encryption configuration.
Data already written by an affected application stays in plaintext after the code fix, so the fix should ship with guidance to re-encrypt affected collections.