-
Notifications
You must be signed in to change notification settings - Fork 0
Hash password on update #54
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,11 @@ class User { | |
| */ | ||
| static async findByIdAndUpdate(id, updateData) { | ||
| try { | ||
| // Hash password if provided | ||
| 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 = {}; | ||
|
|
||
|
|
@@ -236,6 +241,10 @@ class User { | |
| */ | ||
| async save() { | ||
| try { | ||
| if (this.password) { | ||
| const salt = await bcrypt.genSalt(10); | ||
| this.password = await bcrypt.hash(this.password, salt); | ||
| } | ||
| // Convert user object to snake_case for Supabase | ||
| const userData = { | ||
| username: this.username, | ||
|
Comment on lines
249
to
250
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 method |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| const bcrypt = require('bcrypt'); | ||
|
|
||
| jest.mock('../../../server/utils/database', () => { | ||
| const chain = { | ||
| from: jest.fn(() => chain), | ||
| update: jest.fn(() => chain), | ||
| eq: jest.fn(() => chain), | ||
| select: jest.fn(() => chain), | ||
| single: jest.fn(() => Promise.resolve({ data: { id: 'user1', password: 'hashed' }, error: null })) | ||
| }; | ||
| return { supabase: chain, supabaseAdmin: chain }; | ||
| }); | ||
|
|
||
| const User = require('../../../server/models/User'); | ||
|
|
||
| describe('User password hashing', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('hashes password on findByIdAndUpdate', async () => { | ||
| jest.spyOn(bcrypt, 'genSalt').mockResolvedValue('salt'); | ||
| jest.spyOn(bcrypt, 'hash').mockResolvedValue('hashed_pw'); | ||
| await User.findByIdAndUpdate('user1', { password: 'plain' }); | ||
| expect(bcrypt.hash).toHaveBeenCalledWith('plain', 'salt'); | ||
| const { supabaseAdmin } = require('../../../server/utils/database'); | ||
| expect(supabaseAdmin.update).toHaveBeenCalledWith({ password: 'hashed_pw' }); | ||
| }); | ||
|
|
||
| it('hashes password on save', async () => { | ||
| jest.spyOn(bcrypt, 'genSalt').mockResolvedValue('salt2'); | ||
| jest.spyOn(bcrypt, 'hash').mockResolvedValue('hashed_pw2'); | ||
| const user = new User(); | ||
| user.id = 'user2'; | ||
| user.username = ''; | ||
| user.email = ''; | ||
| user.password = 'plain2'; | ||
| await user.save(); | ||
| expect(bcrypt.hash).toHaveBeenCalledWith('plain2', 'salt2'); | ||
| const { supabaseAdmin } = require('../../../server/utils/database'); | ||
| expect(supabaseAdmin.update).toHaveBeenCalledWith(expect.objectContaining({ password: 'hashed_pw2' })); | ||
| }); | ||
|
Comment on lines
+41
to
+42
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 test uses Recommended Solution: expect(supabaseAdmin.update).toHaveBeenCalledWith({ id: 'user2', password: 'hashed_pw2' }); |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,10 @@ | |
| const request = require('supertest'); | ||
| const express = require('express'); | ||
|
|
||
| process.env.SUPABASE_URL = process.env.SUPABASE_URL || 'http://localhost'; | ||
| process.env.SUPABASE_KEY = process.env.SUPABASE_KEY || 'key'; | ||
| process.env.SUPABASE_SERVICE_KEY = process.env.SUPABASE_SERVICE_KEY || 'service'; | ||
|
Comment on lines
+7
to
+9
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. Security Issue: Hardcoded Sensitive KeysThe environment variables for Supabase ( Recommendation:
|
||
|
|
||
| // Mock dependencies | ||
| jest.mock('../../../server/models/User', () => ({ | ||
| findRecent: jest.fn().mockResolvedValue([ | ||
|
|
@@ -21,7 +25,7 @@ jest.mock('../../../server/models/Item', () => ({ | |
| { id: 3, title: 'Featured Item 1' }, | ||
| { id: 4, title: 'Featured Item 2' } | ||
| ]) | ||
| })); | ||
| }), { virtual: true }); | ||
|
|
||
| // Mock express-handlebars | ||
| jest.mock('express-handlebars', () => ({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,10 @@ | |
| * Tests for the WIR currency system and transactions | ||
| */ | ||
|
|
||
| process.env.SUPABASE_URL = process.env.SUPABASE_URL || 'http://localhost'; | ||
| process.env.SUPABASE_KEY = process.env.SUPABASE_KEY || 'key'; | ||
| process.env.SUPABASE_SERVICE_KEY = process.env.SUPABASE_SERVICE_KEY || 'service'; | ||
|
Comment on lines
+6
to
+8
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. Hardcoding default values for sensitive keys (
Comment on lines
+6
to
+8
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. Direct manipulation of environment variables within the test file ( |
||
|
|
||
| const { supabase } = require('../server/utils/database'); | ||
| const WIRTransaction = require('../server/models/WIRTransaction'); | ||
| const createWIRTransactionsTable = require('../scripts/migrations/create-wir-transactions-table'); | ||
|
|
||
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): Hashing all passwords on save may cause double-hashing
Currently, the
savemethod re-hashes the password every time, even if it hasn't changed. This can result in double-hashing and lock users out. Only hash the password if it was modified.