-
Notifications
You must be signed in to change notification settings - Fork 0
Hash password updates #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,6 +203,12 @@ class User { | |
| */ | ||
| static async findByIdAndUpdate(id, updateData) { | ||
| try { | ||
| // Hash password if it is being updated | ||
| if (updateData.password) { | ||
| const salt = await bcrypt.genSalt(10); | ||
| updateData.password = await bcrypt.hash(updateData.password, salt); | ||
| } | ||
|
|
||
| // Convert to snake_case for Supabase | ||
| const snakeCaseData = {}; | ||
|
|
||
|
Comment on lines
203
to
214
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential Issue with User Existence CheckThe method Recommendation:
Comment on lines
212
to
214
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactoring Suggestion for Key ConversionThe method manually converts keys from camelCase to snake_case using a regex directly within the method. This approach is repeated across different methods, which could lead to code duplication and maintenance challenges. Recommendation:
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Mock bcrypt functions | ||
| jest.mock('bcrypt', () => ({ | ||
| genSalt: jest.fn(() => Promise.resolve('salt')), | ||
| hash: jest.fn(() => Promise.resolve('hashedPassword')) | ||
| })); | ||
|
|
||
| const bcrypt = require('bcrypt'); | ||
|
|
||
| // Mock database utilities | ||
| const updateMock = jest.fn(() => ({ | ||
| eq: jest.fn(() => ({ | ||
| select: jest.fn(() => ({ | ||
| single: jest.fn().mockResolvedValue({ data: { id: '1', password: 'hashedPassword' }, error: null }) | ||
| })) | ||
| })) | ||
| })); | ||
|
|
||
| const fromMock = jest.fn(() => ({ update: updateMock })); | ||
|
|
||
| jest.mock('../../../server/utils/database', () => ({ | ||
| supabase: { from: fromMock }, | ||
| supabaseAdmin: { from: fromMock } | ||
| })); | ||
|
|
||
| const User = require('../../../server/models/User'); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('User.findByIdAndUpdate', () => { | ||
| it('hashes password when provided', async () => { | ||
| await User.findByIdAndUpdate('1', { password: 'secret' }); | ||
|
|
||
| expect(bcrypt.genSalt).toHaveBeenCalledWith(10); | ||
| expect(bcrypt.hash).toHaveBeenCalledWith('secret', 'salt'); | ||
| expect(updateMock).toHaveBeenCalled(); | ||
| const arg = updateMock.mock.calls[0][0]; | ||
| expect(arg.password).toBe('hashedPassword'); | ||
| }); | ||
|
|
||
| it('does not hash when password not provided', async () => { | ||
| await User.findByIdAndUpdate('1', { displayName: 'Test' }); | ||
|
|
||
| expect(bcrypt.hash).not.toHaveBeenCalled(); | ||
| const arg = updateMock.mock.calls[0][0]; | ||
| expect(arg.display_name).toBe('Test'); | ||
| }); | ||
| }); | ||
|
Comment on lines
+31
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current tests in |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,18 +9,23 @@ jest.mock('../../../server/models/User', () => ({ | |
| findRecent: jest.fn().mockResolvedValue([ | ||
| { username: 'testuser1', displayName: 'Test User 1' }, | ||
| { username: 'testuser2', displayName: 'Test User 2' } | ||
| ]), | ||
| countDocuments: jest.fn().mockResolvedValue(2), | ||
| find: jest.fn().mockResolvedValue([ | ||
| { username: 'testuser1', lastActive: new Date() } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
| ]) | ||
| })); | ||
|
|
||
| jest.mock('../../../server/models/Item', () => ({ | ||
| jest.mock('../../../server/models/ScrapyardItem', () => ({ | ||
| findRecent: jest.fn().mockResolvedValue([ | ||
| { id: 1, title: 'Test Item 1' }, | ||
| { id: 2, title: 'Test Item 2' } | ||
| ]), | ||
| findFeatured: jest.fn().mockResolvedValue([ | ||
| { id: 3, title: 'Featured Item 1' }, | ||
| { id: 4, title: 'Featured Item 2' } | ||
| ]) | ||
| ]), | ||
| countDocuments: jest.fn().mockResolvedValue(2) | ||
| })); | ||
|
|
||
| // Mock express-handlebars | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Falsy password check may skip hashing and store plaintext
Checking
if (updateData.password)may miss empty or falsy passwords, resulting in unhashed plaintext storage. Use property existence checks or validate for empty values before hashing.