MONGOID-5939 Guard against NoSQL operator injection in where/find_by#6161
Draft
comandeo-mongo wants to merge 1 commit into
Draft
MONGOID-5939 Guard against NoSQL operator injection in where/find_by#6161comandeo-mongo wants to merge 1 commit into
comandeo-mongo wants to merge 1 commit into
Conversation
Add an allowlist of permitted top-level query operators in expr_query. By default, operators not in the list ($where, $function, etc.) raise Errors::InvalidQuery. Set Mongoid.allow_unsafe_query_operators = true to restore the previous pass-through behavior. Also add gem 'ostruct' for Ruby 4.0+ compatibility.
1ec9d21 to
c4cf5c8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a guard in
expr_querythat rejects top-level$operators not in an explicit allowlist whenallow_unsafe_query_operatorsis set tofalse. The default istrue(existing behavior preserved — no BC break).To opt into the strict mode:
With strict mode on, operators like
$whereand$functionraiseErrors::InvalidQuery. Allowed operators:$and,$or,$nor,$not,$text,$comment,$expr,$jsonSchema,$alwaysFalse,$alwaysTrue.Why
When user-supplied params are passed directly to
where/find_by(e.g. viato_unsafe_h), an attacker can inject top-level operators like$whereto execute arbitrary JavaScript on the MongoDB server.Files changed
lib/mongoid/config.rb— newallow_unsafe_query_operatorsoption (default:true)lib/mongoid/criteria/queryable/selectable.rb—ALLOWED_QUERY_OPERATORSconstant and guard inexpr_queryspec/mongoid/criteria/queryable/selectable_where_spec.rb— 14 new examples covering the guardGemfile— addostructfor Ruby 4.0+ compatibilityTest plan
selectable_where_spec.rbcovering blocked operators, allowlisted operators, and opt-in strict modeJira: https://jira.mongodb.org/browse/MONGOID-5939