Skip to content

[LEGIT] Fix - js/code-injection - #106

Open
liorn-test-app[bot] wants to merge 1 commit into
masterfrom
legit-security-4722aa
Open

[LEGIT] Fix - js/code-injection#106
liorn-test-app[bot] wants to merge 1 commit into
masterfrom
legit-security-4722aa

Conversation

@liorn-test-app

@liorn-test-app liorn-test-app Bot commented Jul 7, 2026

Copy link
Copy Markdown

🔍 The problem

Code injection
See issue in Legit

🔒 Fix Details

Fixed a NoSQL injection vulnerability in the getByUserIdAndThreshold method. The original code used MongoDB's $where operator with string interpolation, allowing attackers to inject arbitrary JavaScript code through the threshold parameter (e.g., 0';while(true){}').

The fix replaces the dangerous $where clause with native MongoDB query operators ($gt for threshold comparison). The threshold parameter is now parsed to an integer and validated to be within the 0-99 range before being used in a structured query object. This eliminates JavaScript evaluation entirely, preventing code injection while maintaining the same query logic.

--- a/app/data/allocations-dao.js
+++ b/app/data/allocations-dao.js
@@ -74,8 +74,13 @@
                 }
                 throw `The user supplied threshold: ${parsedThreshold} was not valid.`;
                 */
+                const parsedThreshold = parseInt(threshold, 10);
+                if (isNaN(parsedThreshold) || parsedThreshold < 0 || parsedThreshold > 99) {
+                    throw new Error(`Invalid threshold value: ${threshold}`);
+                }
                 return {
-                    $where: `this.userId == ${parsedUserId} && this.stocks > '${threshold}'`
+                    userId: parsedUserId,
+                    stocks: { $gt: parsedThreshold }
                 };
             }
             return {

@sonarqubecloud

sonarqubecloud Bot commented Jul 7, 2026

Copy link
Copy Markdown

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants