Skip to content

fix(security): prevent nested NoSQL injection in sanitize()#173

Merged
yash-pouranik merged 2 commits into
geturbackend:mainfrom
NAYAMATVISION:fix/nosql-sanitize
May 17, 2026
Merged

fix(security): prevent nested NoSQL injection in sanitize()#173
yash-pouranik merged 2 commits into
geturbackend:mainfrom
NAYAMATVISION:fix/nosql-sanitize

Conversation

@NAYAMATVISION
Copy link
Copy Markdown
Contributor

@NAYAMATVISION NAYAMATVISION commented May 17, 2026

Summary

This PR fixes a NoSQL injection vulnerability in the sanitize() utility by recursively removing dangerous MongoDB operator keys from nested objects and arrays.

Changes Made

  • Added recursive sanitization for nested objects

  • Added array traversal support

  • Blocked dangerous keys:

    • $-prefixed MongoDB operators
    • __proto__
    • constructor
    • prototype
  • Preserved backward compatibility for normal user payloads

Security Impact

Previously, nested MongoDB operators such as:

{
  "profile": {
    "$gt": ""
  }
}

could bypass sanitization and potentially lead to NoSQL injection in update operations.

This PR ensures dangerous keys are removed at all nesting levels.

Testing

Tested locally with:

  • top-level MongoDB operators
  • nested operators
  • deeply nested payloads
  • arrays containing malicious objects
  • prototype pollution attempts
  • normal user payload preservation

Verified that malicious keys are removed while legitimate data remains unaffected.
Screenshot 2026-05-17 115551
Screenshot 2026-05-17 115536

Summary by CodeRabbit

  • Bug Fixes
    • Improved input sanitization to recursively cleanse nested objects and arrays.
    • Broadened blocking of dangerous keys (including prototype-related keys and keys starting with $) to prevent prototype-pollution vectors.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4e2c976e-c795-47d7-b28e-d2cc01b338c4

📥 Commits

Reviewing files that changed from the base of the PR and between 38a2170 and cef0e86.

📒 Files selected for processing (1)
  • packages/common/src/utils/input.validation.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/common/src/utils/input.validation.js

📝 Walkthrough

Walkthrough

The sanitize function was changed from top-level-only filtering to a recursive sanitizer that omits dangerous keys (__proto__, constructor, prototype) and any keys starting with $ in nested objects and arrays.

Changes

Input sanitization security

Layer / File(s) Summary
Recursive sanitizer with prototype pollution defense
packages/common/src/utils/input.validation.js
The sanitize function replaces its top-level-only filtering with recursive traversal that blocks prototype-pollution attack vectors (__proto__, constructor, prototype) and dangerous keys starting with $ throughout nested object and array structures.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A rabbit hops through input fields with care,
Blocking __proto__ tricks hiding there,
Recursion guards each nested layer deep,
No dangerous keys shall sneak a peep,
Sanitized data, safe and sound to share!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title directly and clearly describes the main security fix: preventing nested NoSQL injection in the sanitize() function through recursive sanitization.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/common/src/utils/input.validation.js`:
- Around line 286-287: sanitize currently returns arrays unchanged, allowing
root-array payloads like [{"$gt":""}] to bypass filtering; update sanitize (the
sanitize function) to detect when obj is an Array at the top-level and instead
return a new array with sanitize applied to each element (recursively) so array
payloads are sanitized rather than short-circuited; ensure primitives and null
still return unchanged and that the implementation avoids mutating the original
objects.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 225f8e20-82f5-425b-8692-33a0e47fbf79

📥 Commits

Reviewing files that changed from the base of the PR and between f04b600 and 38a2170.

📒 Files selected for processing (1)
  • packages/common/src/utils/input.validation.js

Comment thread packages/common/src/utils/input.validation.js Outdated
@NAYAMATVISION
Copy link
Copy Markdown
Contributor Author

Good catch — root-level array payloads were bypassing sanitization due to the early array return condition.

Updated the implementation to recursively sanitize root arrays as well and verified the fix with additional test coverage for array payloads.

@yash-pouranik
Copy link
Copy Markdown
Collaborator

Hello @NAYAMATVISION
can u please resolve the merge conflict in this file - packages/common/src/utils/input.validation.js

@yash-pouranik
Copy link
Copy Markdown
Collaborator

your branch is 300+ commits behind the main
image

@NAYAMATVISION
Copy link
Copy Markdown
Contributor Author

NAYAMATVISION commented May 17, 2026 via email

@yash-pouranik
Copy link
Copy Markdown
Collaborator

can u pelase resolve merge conflict and then commit again?

@yash-pouranik yash-pouranik merged commit d45208d into geturbackend:main May 17, 2026
7 of 8 checks passed
@yash-pouranik
Copy link
Copy Markdown
Collaborator

Thank you for the PR @NAYAMATVISION
This was really nice implementation for no sql injection.

@yash-pouranik yash-pouranik linked an issue May 17, 2026 that may be closed by this pull request
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.

NoSQL Injection Risk

2 participants