Reported privately (SECBUG-6015, SECBUG-6006).
A field declared read-only can still be changed through an attribute key that contains a dot. The read-only check compares the incoming key with the declared field name as an exact string, so a dotted key is not recognised, is stored as a literal attribute, and reaches the server as a set on a nested path. The server applies it inside the protected Hash.
The whole-field update is ignored as documented. The dotted update goes through on every supported line (7.6 to 9.1) and on master.
class Account include Mongoid::Document include Mongoid::Attributes::Dynamic field :settings, type: Hash attr_readonly :settings end a = Account.create!(settings: { 'is_admin' => false }) a.update('settings' => { 'is_admin' => true }) Account.collection.find(_id: a.id).first['settings'] # => {"is_admin" => false} ignored, as documented a.update('settings.is_admin' => true) Account.collection.find(_id: a.id).first['settings'] # => {"is_admin" => true} sent as {"$set" => {"settings.is_admin" => true}}
Two more paths bypass the check, and these do not need dynamic attributes:
a['settings.is_admin'] = true; a.save # writes a.update_attribute('settings.is_admin', true) # writes silently, docs promise ReadonlyAttribute
Relevant code:
lib/mongoid/attributes/readonly.rb:23-25 # exact-match check lib/mongoid/fields.rb:260-263 # dot guard only for declared fields lib/mongoid/fields.rb:434-441 # dotted key returned unchanged lib/mongoid/changeable.rb:125-140 # emitted as a $set path
Fix: resolve the key to its first path segment before the read-only check, on every write path: mass assignment, direct attribute write, the bracket setter, and the single-attribute update. Consider rejecting dotted keys on non-dynamic Hash fields outright. Add specs for each path.