From be0d3b383802340ec7c23dc2ab3b8097cd1a10bb Mon Sep 17 00:00:00 2001 From: Sander Bruens Date: Wed, 17 Apr 2024 17:55:08 -0400 Subject: [PATCH] fix(server): handle `PasswordConflict` explicitly to avoid a 500 (#1536) --- src/shadowbox/server/manager_service.spec.ts | 11 ++++++++++- src/shadowbox/server/manager_service.ts | 5 ++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/shadowbox/server/manager_service.spec.ts b/src/shadowbox/server/manager_service.spec.ts index 52f73a9cf..ccbc09eb1 100644 --- a/src/shadowbox/server/manager_service.spec.ts +++ b/src/shadowbox/server/manager_service.spec.ts @@ -532,7 +532,16 @@ describe('ShadowsocksManagerService', () => { done(); }); }); - + it('rejects a password that is already in use', async (done) => { + const PASSWORD = 'foobar'; + await repo.createNewAccessKey({password: PASSWORD}); + const res = {send: SEND_NOTHING}; + await serviceMethod({params: {id: accessKeyId, password: PASSWORD}}, res, (error) => { + expect(error.statusCode).toEqual(409); + responseProcessed = true; // required for afterEach to pass. + done(); + }); + }); it('uses the default port for new keys when no port is provided', async (done) => { const res = { send: (httpCode, data) => { diff --git a/src/shadowbox/server/manager_service.ts b/src/shadowbox/server/manager_service.ts index 12721febf..24b37e2f4 100644 --- a/src/shadowbox/server/manager_service.ts +++ b/src/shadowbox/server/manager_service.ts @@ -383,7 +383,10 @@ export class ShadowsocksManagerService { logging.error(error); if (error instanceof errors.InvalidCipher || error instanceof errors.InvalidPortNumber) { throw new restifyErrors.InvalidArgumentError({statusCode: 400}, error.message); - } else if (error instanceof errors.PortUnavailable) { + } else if ( + error instanceof errors.PortUnavailable || + error instanceof errors.PasswordConflict + ) { throw new restifyErrors.ConflictError(error.message); } throw error;