Skip to content

MONGOID-5939 Guard against NoSQL operator injection in where/find_by - #6161

Merged
comandeo-mongo merged 3 commits into
mongodb:masterfrom
comandeo-mongo:MONGOID-5939-operator-injection-guard
Jul 2, 2026
Merged

MONGOID-5939 Guard against NoSQL operator injection in where/find_by#6161
comandeo-mongo merged 3 commits into
mongodb:masterfrom
comandeo-mongo:MONGOID-5939-operator-injection-guard

Conversation

@comandeo-mongo

@comandeo-mongo comandeo-mongo commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

What

Adds a guard in expr_query that rejects top-level $ operators not in an explicit allowlist when allow_unsafe_query_operators is set to false. The default is true (existing behavior preserved — no BC break).

To opt into the strict mode:

Mongoid.configure do |config|
  config.allow_unsafe_query_operators = false
end

With strict mode on, operators like $where and $function raise Errors::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. via to_unsafe_h), an attacker can inject top-level operators like $where to execute arbitrary JavaScript on the MongoDB server.

Files changed

  • lib/mongoid/config.rb — new allow_unsafe_query_operators option (default: true)
  • lib/mongoid/criteria/queryable/selectable.rbALLOWED_QUERY_OPERATORS constant and guard in expr_query
  • spec/mongoid/criteria/queryable/selectable_where_spec.rb — 14 new examples covering the guard
  • Gemfile — add ostruct for Ruby 4.0+ compatibility

Test plan

  • 14 new examples in selectable_where_spec.rb covering blocked operators, allowlisted operators, and opt-in strict mode
  • All 1681 queryable specs pass with no regressions
  • RuboCop reports no offenses

Jira: https://jira.mongodb.org/browse/MONGOID-5939

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
comandeo-mongo force-pushed the MONGOID-5939-operator-injection-guard branch from 1ec9d21 to c4cf5c8 Compare June 26, 2026 13:38

@jamis jamis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several rubocop complaints from the new code, but otherwise it looks good 👍

@comandeo-mongo
comandeo-mongo marked this pull request as ready for review June 30, 2026 11:06
Copilot AI review requested due to automatic review settings June 30, 2026 11:06
@comandeo-mongo
comandeo-mongo requested a review from a team as a code owner June 30, 2026 11:06
@comandeo-mongo
comandeo-mongo requested a review from jamis June 30, 2026 11:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_operators config option (default true) to preserve existing behavior unless explicitly disabled.
  • Adds a top-level operator allowlist and enforcement in Selectable#expr_query for 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 thread lib/mongoid/config.rb
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.
@comandeo-mongo
comandeo-mongo merged commit 90380d3 into mongodb:master Jul 2, 2026
76 checks passed
@comandeo-mongo comandeo-mongo added the feature Adds a new feature, without breaking compatibility label Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds a new feature, without breaking compatibility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants