[Security Fix] SAST: NoSQL/server-side JS injection via $where with unsanitized threshold query ...#413
Open
okaypatrick wants to merge 1 commit into
Open
Conversation
… `threshold` query parameter.
The previous code built a MongoDB `$where` clause by interpolating the raw `threshold` query parameter directly into a JavaScript string evaluated server-side by MongoDB. An attacker could inject arbitrary JS (e.g. `0';while(true){}'` for a DoS, or `1'; return 1 == '1` to bypass filtering and exfiltrate all allocations), since the value was wrapped only in single quotes with no validation or escaping.
The fix removes `$where` entirely and replaces it with a standard, safely-typed MongoDB query:
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.
Security Fix
Type: SAST
Generated by: AI-Powered Fix Generator
Finding: NoSQL/server-side JS injection via $where with unsanitized
thresholdquery parameter.Rule: claude-nosql-injection-allocations (oogway-scanner)
File:
app/data/allocations-dao.js(line 70)Severity: HIGH
Explanation
The previous code built a MongoDB
$whereclause by interpolating the rawthresholdquery parameter directly into a JavaScript string evaluated server-side by MongoDB. An attacker could inject arbitrary JS (e.g.0';while(true){}'for a DoS, or1'; return 1 == '1to bypass filtering and exfiltrate all allocations), since the value was wrapped only in single quotes with no validation or escaping.The fix removes
$whereentirely and replaces it with a standard, safely-typed MongoDB query:thresholdis parsed withparseIntand validated to be an integer in the expected 0–99 range, then used with the$gtoperator alongside the already-parseduserId. This preserves the original filtering semantics (same userId, stocks greater than threshold) while making server-side JS injection impossible. Invalid thresholds still throw, matching the previously-intended behavior.Changes
app/data/allocations-dao.js: Replace the unsafe $where clause built via string interpolation of the user-suppliedthresholdwith a validated integer and a standard MongoDB query operator ($gt). This eliminates server-side JavaScript injection while preserving the filtering behavior.Test Suggestions