MONGOID-5939 Guard against NoSQL operator injection in where/find_by - #6161
Merged
comandeo-mongo merged 3 commits intoJul 2, 2026
Merged
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.
comandeo-mongo
force-pushed
the
MONGOID-5939-operator-injection-guard
branch
from
June 26, 2026 13:38
1ec9d21 to
c4cf5c8
Compare
jamis
requested changes
Jun 29, 2026
jamis
left a comment
Contributor
There was a problem hiding this comment.
There are several rubocop complaints from the new code, but otherwise it looks good 👍
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds an opt-in security hardening mode to Mongoid’s query builder to mitigate NoSQL operator injection via where/find_by by rejecting non-allowlisted top-level $ operators when configured to do so.
Changes:
- Introduces
Mongoid.allow_unsafe_query_operatorsconfig option (defaulttrue) to preserve existing behavior unless explicitly disabled. - Adds a top-level operator allowlist and enforcement in
Selectable#expr_queryfor Hash-based criteria. - Adds RSpec coverage for both default (permissive) and strict (allowlist) modes, including allowlisted operators.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| lib/mongoid/config.rb | Adds allow_unsafe_query_operators configuration option and documentation. |
| lib/mongoid/criteria/queryable/selectable.rb | Implements allowlist constant and guard in expr_query for top-level $ operators. |
| spec/mongoid/criteria/queryable/selectable_where_spec.rb | Adds examples verifying permissive vs strict behavior and allowlisted operators. |
| spec/mongoid/criteria/queryable/selectable_spec.rb | Whitespace-only change. |
| Gemfile | Adds ostruct dependency conditionally for Ruby 4.0+. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+297
to
+301
| # When true (default), all top-level query operators are passed through | ||
| # to MongoDB without restriction when using +where+/+find_by+. Set to | ||
| # false to enable a strict allowlist that rejects operators like +$where+ | ||
| # and +$function+, which can execute arbitrary JavaScript when user-supplied | ||
| # input reaches the query builder. |
jamis
approved these changes
Jul 1, 2026
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