Skip to content

[Aikido] Prevent NoSQL injection in login handler by validating string types - #44

Open
aikido-autofix[bot] wants to merge 1 commit into
mainfrom
fix/aikido-security-code-audit-93742468-agcs
Open

[Aikido] Prevent NoSQL injection in login handler by validating string types#44
aikido-autofix[bot] wants to merge 1 commit into
mainfrom
fix/aikido-security-code-audit-93742468-agcs

Conversation

@aikido-autofix

Copy link
Copy Markdown

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.

…y adding type validation for username and password fields.
Comment thread routes/index.js

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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant