ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Major - P3
    • 9.1.3
    • Affects Version/s: 9.1.0, 9.1.1, 9.0.12, 8.1.13, 8.0.13, 7.6.2
    • Component/s: None
    • None
    • None
    • Fully Compatible
    • 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

      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.

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

              Created:
              Updated:
              Resolved: