A default scope that evaluates to none is silently ignored. Class-level queries then run with an empty selector and match every document in the collection.
class Note include Mongoid::Document default_scope { none } end Note.count # returns the number of documents in the collection, should be 0
The same happens for all, where, find, first, pluck, update_all and delete_all, and when none is one branch of a conditional default scope.
Criteria merge! copies the selector, options and other state from the other criteria, but not the none flag. Default scopes are applied through merge!, so the none is lost. Named scopes are not affected because they have their own check, added in MONGOID-4125.
lib/mongoid/criteria.rb Criteria#merge! does not copy @none lib/mongoid/criteria/scopable.rb apply_default_scope calls merge!(klass.default_scoping.call)
lib/mongoid/scopable.rb define_scope_method checks empty_and_chainable?
Affects all released versions, 7.x through 9.1.1.
Fix: carry the none state through merge!, or guard the default scope path the same way named scopes are guarded. MONGOID-4125 added a guard to Criteria#merge and reverted it an hour later, so check why before changing merge!. Add specs for a default scope that is none, both on its own and as one branch of a conditional.
Reported in SECBUG-6025 (Minos) and SECBUG-6020 (HackerOne).