Skip to content

Fix: Harden MongoDB query and options handling (fixes #58) - #59

Merged
taylortom merged 5 commits into
masterfrom
fix/harden-query-options
Jun 6, 2026
Merged

Fix: Harden MongoDB query and options handling (fixes #58)#59
taylortom merged 5 commits into
masterfrom
fix/harden-query-options

Conversation

@taylortom

Copy link
Copy Markdown
Collaborator

Fixes #58

Fix

  • Block dangerous MongoDB query operators ($where, $function, $accumulator) that allow arbitrary JS execution on the server
  • Add allowlist for MongoDB driver options — only collation, includeResultMetadata, limit, projection, returnCursor, returnDocument, skip, sort, upsert are permitted
  • Validate option value types (integers, booleans, plain objects, enums) before passing to the driver
  • Return copies from parameter processing instead of mutating caller inputs

Update

  • Extract processParams utility that consolidates query validation, ObjectId conversion, _id stripping, and options parsing into a single function
  • Add assertSafeQuery utility for recursive detection of blocked query operators
  • Consolidate barrel file (lib/utils.js) with all utility exports
  • Simplify MongoDBModule methods by replacing scattered inline processing with single processParams call

Testing

  • 16 tests for assertSafeQuery (safe operators, blocked operators, nesting, edge cases)
  • 24 tests for processParams (query/data/options processing, immutability, allowlist, value validation)
  • All 56 tests passing

🤖 Generated with Claude Code

@taylortom
taylortom force-pushed the fix/harden-query-options branch from fc123aa to e1007c4 Compare March 24, 2026 21:48

Copilot AI left a comment

Copy link
Copy Markdown

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 hardens MongoDBModule parameter handling by centralizing query/data/options processing into a new processParams utility, adding recursive detection of dangerous MongoDB operators, and tightening which driver options are permitted before calls reach the MongoDB driver.

Changes:

  • Add assertSafeQuery to block $where, $function, and $accumulator anywhere in query objects.
  • Add processParams to validate/normalize query, data, and an allowlisted set of options, and refactor MongoDBModule methods to use it.
  • Add/adjust test coverage for the new utilities and remove the old parseOptions tests.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/utils-processParams.spec.js New tests covering query/data/options processing and allowlist behavior.
tests/utils-assertSafeQuery.spec.js New tests for recursive detection of blocked operators.
tests/MongoDBModule.spec.js Removes obsolete parseOptions tests and updates module test description.
lib/utils/processParams.js New shared utility for query/data/options processing and allowlist validation.
lib/utils/assertSafeQuery.js New recursive query validator that blocks JS-execution operators.
lib/utils.js Consolidates utility exports, including processParams/assertSafeQuery.
lib/MongoDBModule.js Refactors CRUD methods to use processParams instead of inline processing.
errors/errors.json Adds new MONGO_BLOCKED_OPERATOR error definition.

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

Comment thread lib/utils/processParams.js
Comment thread lib/utils/processParams.js Outdated
Comment thread lib/MongoDBModule.js
Comment thread lib/MongoDBModule.js
taylortom added 2 commits June 6, 2026 13:27
…on for limit/skip

Co-Authored-By: Thomas Taylor <thomas.taylor@kineo.com>
…ances

structuredClone strips the ObjectId prototype, breaking preserved _id values.
deepClone recurses only into plain objects and arrays, passing class instances through.
@taylortom
taylortom merged commit bfe3981 into master Jun 6, 2026
2 checks passed
@taylortom
taylortom deleted the fix/harden-query-options branch June 6, 2026 12:51
github-actions Bot pushed a commit that referenced this pull request Jun 6, 2026
## [3.3.1](v3.3.0...v3.3.1) (2026-06-06)

### Fix

* Harden MongoDB query and options handling (fixes #58) (#59) ([bfe3981](bfe3981)), closes [#58](#58) [#59](#59)
@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 3.3.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

github-actions Bot pushed a commit that referenced this pull request Jun 6, 2026
## [3.3.2](v3.3.1...v3.3.2) (2026-06-06)

### Fix

* import convertObjectIds in count() (fixes #59) ([080e6f1](080e6f1)), closes [#59](#59)
@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 3.3.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harden MongoDB driver query and options handling

2 participants