Skip to content

The Passport-Local Hidden Password Trap #472

@Aryan0819

Description

@Aryan0819

he Architecture Context: Security best practices dictate that sensitive fields like passwords should be excluded from standard database query responses to prevent accidental exposure down the network line (e.g., logging or sending data payloads back to the frontend UI). This is handled via Mongoose's field selection control (select: false).

The Failure Mechanism: When you added select: false to the user schema password field, it worked exactly as intended for general database operations. However, inside your Passport authentication logic, you executed a standard lookup query:

JavaScript
const user = await User.findOne({ email });
Because of the schema restriction, the returned user document completely omitted the password property. When Passport sub-routines attempted to execute your schema method:

JavaScript
const isMatch = await user.comparePassword(password);
The method passed a plain-text string into Bcrypt to compare against an undefined hash reference.

The Impact: Since a plain-text password can never match an undefined hash value, Bcrypt consistently returned false. This completely locked down the application, rejecting every single login attempt across the platform regardless of credential accuracy.

The Solution: You updated the authentication lookup query to use field selection overrides by chaining .select("+password") to the query. This explicitly instructs Mongoose to pull the hidden password hash string into server memory strictly for that specific authentication check, without exposing it to the rest of the application.

Please assign me under gssoc 2026

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions