A local-first personal finance app
[AI] Further hardening of "/change-password" endpoint (#7207)
Matiss Janis Aboltins53cdc6fa484e
diff --git a/packages/sync-server/src/app-account.js b/packages/sync-server/src/app-account.jsindex 93d904abff..0ab17614ca 100644--- a/packages/sync-server/src/app-account.js+++ b/packages/sync-server/src/app-account.js@@ -121,7 +121,16 @@ app.post('/change-password', (req, res) => {const session = validateSession(req, res);if (!session) return;- if (getActiveLoginMethod() !== 'password') {+ if (!isAdmin(session.user_id)) {+ res.status(403).send({+ status: 'error',+ reason: 'forbidden',+ details: 'permission-not-found',+ });+ return;+ }++ if (session.auth_method !== 'password') {res.status(403).send({status: 'error',reason: 'forbidden',diff --git a/packages/sync-server/src/app-account.test.js b/packages/sync-server/src/app-account.test.jsindex 4bdbc734f5..8a55d1fdde 100644--- a/packages/sync-server/src/app-account.test.js+++ b/packages/sync-server/src/app-account.test.js@@ -21,10 +21,10 @@ const deleteUser = userId => {getAccountDb().mutate('DELETE FROM users WHERE id = ?', [userId]);};-const createSession = (userId, sessionToken) => {+const createSession = (userId, sessionToken, authMethod = null) => {getAccountDb().mutate(- 'INSERT INTO sessions (token, user_id, expires_at) VALUES (?, ?, ?)',- [sessionToken, userId, Math.floor(Date.now() / 1000) + 60 * 60], // Expire in 1 hour (stored in seconds)+ 'INSERT INTO sessions (token, user_id, expires_at, auth_method) VALUES (?, ?, ?, ?)',+ [sessionToken, userId, Math.floor(Date.now() / 1000) + 60 * 60, authMethod], // Expire in 1 hour (stored in seconds));};@@ -46,17 +46,28 @@ const clearAuth = () => {};describe('/change-password', () => {- let userId, sessionToken;+ let adminUserId,+ basicUserId,+ adminPasswordToken,+ adminOpenidToken,+ basicPasswordToken;beforeEach(() => {- userId = uuidv4();- sessionToken = generateSessionToken();- createUser(userId, 'testuser', ADMIN_ROLE);- createSession(userId, sessionToken);+ adminUserId = uuidv4();+ basicUserId = uuidv4();+ adminPasswordToken = generateSessionToken();+ adminOpenidToken = generateSessionToken();+ basicPasswordToken = generateSessionToken();+ createUser(adminUserId, 'admin', ADMIN_ROLE);+ createUser(basicUserId, 'basic', BASIC_ROLE);+ createSession(adminUserId, adminPasswordToken, 'password');+ createSession(adminUserId, adminOpenidToken, 'openid');+ createSession(basicUserId, basicPasswordToken, 'password');});afterEach(() => {- deleteUser(userId);+ deleteUser(adminUserId);+ deleteUser(basicUserId);clearAuth();});@@ -70,12 +81,28 @@ describe('/change-password', () => {expect(res.body).toHaveProperty('reason', 'unauthorized');});- it('should return 403 when active auth method is openid', async () => {- insertAuthRow('openid', 1);+ it('should return 403 when user is not an admin', async () => {+ bootstrapPassword('oldpassword');++ const res = await request(app)+ .post('/change-password')+ .set('x-actual-token', basicPasswordToken)+ .send({ password: 'newpassword' });++ expect(res.statusCode).toEqual(403);+ expect(res.body).toEqual({+ status: 'error',+ reason: 'forbidden',+ details: 'permission-not-found',+ });+ });++ it('should return 403 when admin session uses openid auth method', async () => {+ bootstrapPassword('oldpassword');const res = await request(app).post('/change-password')- .set('x-actual-token', sessionToken)+ .set('x-actual-token', adminOpenidToken).send({ password: 'newpassword' });expect(res.statusCode).toEqual(403);@@ -86,24 +113,24 @@ describe('/change-password', () => {});});- it('should return 400 when active method is password but password is empty', async () => {+ it('should return 400 when admin password-auth session sends empty password', async () => {bootstrapPassword('oldpassword');const res = await request(app).post('/change-password')- .set('x-actual-token', sessionToken)+ .set('x-actual-token', adminPasswordToken).send({ password: '' });expect(res.statusCode).toEqual(400);expect(res.body).toEqual({ status: 'error', reason: 'invalid-password' });});- it('should return 200 when active method is password and new password is valid', async () => {+ it('should return 200 when admin with password-auth session sends valid password', async () => {bootstrapPassword('oldpassword');const res = await request(app).post('/change-password')- .set('x-actual-token', sessionToken)+ .set('x-actual-token', adminPasswordToken).send({ password: 'newpassword' });expect(res.statusCode).toEqual(200);diff --git a/upcoming-release-notes/7207.md b/upcoming-release-notes/7207.mdnew file mode 100644index 0000000000..cfe38b25d6--- /dev/null+++ b/upcoming-release-notes/7207.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [MatissJanis]+---++Add admin and password authentication requirements for changing passwords in sessions.