Skip to content

Conversation

@comandeo-mongo
Copy link
Contributor

No description provided.

Copy link
Contributor

Copilot AI left a comment

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 tightens the behavior of Hash#to_criteria to mitigate security risks (MONGOID-5919) by restricting which Mongoid::Criteria methods can be invoked via a hash, and adds tests around this behavior.

Changes:

  • Introduces an ALLOWED_TO_CRITERIA_METHODS constant in Mongoid::Extensions::Hash and makes it a private constant.
  • Updates Hash#to_criteria to check each hash key against the allowlist, raising ArgumentError for disallowed methods and using public_send for dispatch.
  • Adds comprehensive specs for Hash#to_criteria, covering normal usage, missing klass, allowed methods, disallowed/dangerous methods, and a broader security validation suite.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
spec/mongoid/extensions/hash_spec.rb Adds detailed tests ensuring Hash#to_criteria correctly sets klass/selector, allows a curated set of query/aggregation methods, and rejects a broad set of dangerous or non-query methods with clear ArgumentErrors.
lib/mongoid/extensions/hash.rb Defines the ALLOWED_TO_CRITERIA_METHODS allowlist and changes Hash#to_criteria to enforce it, raising on any non-whitelisted method before calling into Mongoid::Criteria.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 192 to 194
# Convert this hash to a criteria. Will iterate over each keys in the
# hash which must correspond to method on a criteria object. The hash
# must also include a "klass" key.
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

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

The comment states that the hash "must also include a klass key", but the new #to_criteria specs introduce and validate behavior where klass is optional; please update this documentation to reflect that klass may be omitted (resulting in a criteria with klass set to nil).

Suggested change
# Convert this hash to a criteria. Will iterate over each keys in the
# hash which must correspond to method on a criteria object. The hash
# must also include a "klass" key.
# Convert this hash to a criteria. Will iterate over each key in the
# hash which must correspond to a method on a criteria object. The hash
# may include a "klass" key; if omitted, the resulting criteria will have
# its klass set to nil.

Copilot uses AI. Check for mistakes.
@comandeo-mongo comandeo-mongo merged commit 9a39d4d into mongodb:8.1-stable Jan 22, 2026
59 checks passed
@comandeo-mongo comandeo-mongo added the bug Fixes a bug, with no new features or broken compatibility label Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Fixes a bug, with no new features or broken compatibility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants