Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions spec/ParseUser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ describe('Parse.User testing', () => {
}
});

it('normalizes login response time for non-existent and existing users', async () => {
const passwordCrypto = require('../lib/password');
const compareSpy = spyOn(passwordCrypto, 'compare').and.callThrough();
await Parse.User.signUp('existinguser', 'password123');
compareSpy.calls.reset();

// Login with non-existent user
try {
await Parse.User.logIn('nonexistentuser', 'wrongpassword');
} catch (e) {
expect(e.code).toBe(Parse.Error.OBJECT_NOT_FOUND);
}
// bcrypt.compare should have been called even for non-existent user
expect(compareSpy).toHaveBeenCalledTimes(1);
compareSpy.calls.reset();

// Login with existing user but wrong password
try {
await Parse.User.logIn('existinguser', 'wrongpassword');
} catch (e) {
expect(e.code).toBe(Parse.Error.OBJECT_NOT_FOUND);
}
// bcrypt.compare should have been called for existing user
expect(compareSpy).toHaveBeenCalledTimes(1);
});

it('logs username taken with configured log level', async () => {
await reconfigureServer({ logLevels: { signupUsernameTaken: 'warn' } });
const logger = require('../lib/logger').default;
Expand Down
8 changes: 7 additions & 1 deletion src/Routers/UsersRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ export class UsersRouter extends ClassesRouter {
.find('_User', query, {}, Auth.maintenance(req.config))
.then(results => {
if (!results.length) {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Invalid username/password.');
// Perform a dummy bcrypt compare to normalize response timing,
// preventing user enumeration via timing side-channel
return passwordCrypto
.compare(password, passwordCrypto.dummyHash)
.then(() => {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Invalid username/password.');
});
}

if (results.length > 1) {
Expand Down
6 changes: 6 additions & 0 deletions src/password.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ function compare(password, hashedPassword) {
return bcrypt.compare(password, hashedPassword);
}

// Pre-computed bcrypt hash (cost factor 10) used for timing normalization.
// The actual value is irrelevant; it ensures bcrypt.compare() runs with
// realistic cost even when no real password hash is available.
const dummyHash = '$2b$10$Wd1gvrMYPnQv5pHBbXCwCehxXmJSEzRqNON0ev98L6JJP5296S35i';

module.exports = {
hash: hash,
compare: compare,
dummyHash: dummyHash,
};
Loading