[Aikido] Prevent NoSQL injection in login handler by validating string types - #44
Open
aikido-autofix[bot] wants to merge 1 commit into
Open
[Aikido] Prevent NoSQL injection in login handler by validating string types#44aikido-autofix[bot] wants to merge 1 commit into
aikido-autofix[bot] wants to merge 1 commit into
Conversation
…y adding type validation for username and password fields.
|
|
||
| exports.loginHandler = function (req, res, next) { | ||
| // Validate that username and password are strings to prevent NoSQL injection | ||
| if (typeof req.body.username !== 'string' || typeof req.body.password !== 'string') { |
| tap.test('NoSQL Injection Prevention - Type Validation Tests', (t) => { | ||
|
|
||
| // Helper function to test type validation | ||
| function testTypeValidation(username, password) { |
| t.test('should reject login when username is an object', (t) => { | ||
| const { req, res, next } = createMockReqRes({ | ||
| username: { '$ne': null }, | ||
| password: 'password123' |
| t.test('should reject login when username is an array', (t) => { | ||
| const { req, res, next } = createMockReqRes({ | ||
| username: ['admin@snyk.io'], | ||
| password: 'password123' |
| t.test('should reject login when username is null', (t) => { | ||
| const { req, res, next } = createMockReqRes({ | ||
| username: null, | ||
| password: 'password123' |
| t.test('should reject login when username is undefined', (t) => { | ||
| const { req, res, next } = createMockReqRes({ | ||
| username: undefined, | ||
| password: 'password123' |
| t.test('should reject login when username is not a valid email (even if string)', (t) => { | ||
| const { req, res, next } = createMockReqRes({ | ||
| username: 'not-an-email', | ||
| password: 'password123' |
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.
This patch addresses a critical NoSQL injection vulnerability in the login handler that could allow attackers to bypass authentication by injecting MongoDB operators (such as $ne) through the username or password fields. The fix implements type validation to ensure both username and password are strings before they are passed to the database query. The changes were made to routes/index.js and include comprehensive test coverage in tests/nosql-injection.test.js and tests/nosql-injection-validation.test.js to verify the mitigation is effective.