A field declared with the encrypt option loses it when a later belongs-to or HABTM association uses it as its foreign key. The association re-declares the field without the option, so the generated encryption schema leaves it out and the value is written in plaintext. Nothing is raised or logged.
class Ref include Mongoid::Document encrypt_with key_id: KEY_ID field :owner_id, type: String, encrypt: { deterministic: true } belongs_to :owner end Ref.fields['owner_id'].class # => Mongoid::Fields::ForeignKey Mongoid.config.encryption_schema_map('db')['db.refs'].key?('properties') # => false
lib/mongoid/association/referenced/belongs_to.rb:235 lib/mongoid/association/referenced/has_and_belongs_to_many.rb:240
Expected:
- Belongs-to: the foreign key keeps its encryption. Both algorithms should be allowed.
- HABTM: raise when the model is defined if the foreign key is encrypted. CSFLE cannot support an encrypted array of ids with either algorithm.
Documents written before the fix keep plaintext foreign keys, and queries won't match them until they are rewritten. The release notes should say so.
Affects 9.0.0 through 9.1.1 and master. Fixes SECBUG-6275.