Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:auth): reject local credentials in query parameters
johaven462f32c39f03
diff --git a/backend/src/authentication/guards/auth-local.guard.spec.ts b/backend/src/authentication/guards/auth-local.guard.spec.tsindex 64b23006..75a7e6c0 100644--- a/backend/src/authentication/guards/auth-local.guard.spec.ts+++ b/backend/src/authentication/guards/auth-local.guard.spec.ts@@ -1,5 +1,5 @@import { createMock, DeepMocked } from '@golevelup/ts-vitest'-import { ExecutionContext } from '@nestjs/common'+import { BadRequestException, ExecutionContext } from '@nestjs/common'import { Test, TestingModule } from '@nestjs/testing'import { PinoLogger } from 'nestjs-pino'import { UserModel } from '../../applications/users/models/user.model'@@ -35,44 +35,29 @@ describe(AuthLocalGuard.name, () => {context = createMock<ExecutionContext>()})- it('should be defined', () => {- expect(authLocalGuard).toBeDefined()- expect(authProvider).toBeDefined()- expect(userTest).toBeDefined()- })-- it('should validate the user authentication', async () => {+ beforeEach(() => {+ userTest.password = ' password 'authProvider.validateUser = vi.fn().mockReturnValueOnce(userTest)context.switchToHttp().getRequest.mockReturnValue({raw: { user: '' },- body: {- login: userTest.login,- password: userTest.password- }+ body: { login: userTest.login, password: userTest.password }})+ })++ it('should validate the user authentication', async () => {expect(await authLocalGuard.canActivate(context)).toBe(true)+ expect(authProvider.validateUser).toHaveBeenCalledWith(userTest.login, ' password ', undefined)expect(userTest.password).toBeUndefined()})it('should not validate the user authentication', async () => {- userTest.password = 'password'authProvider.validateUser = vi.fn().mockReturnValueOnce(null)- context.switchToHttp().getRequest.mockReturnValue({- raw: { user: '' },- body: {- login: userTest.login,- password: userTest.password- }- })await expect(authLocalGuard.canActivate(context)).rejects.toThrow(/password/i)})- it('should throw error due to malformed body', async () => {- authProvider.validateUser = vi.fn().mockReturnValueOnce(null)- context.switchToHttp().getRequest.mockReturnValue({- raw: { user: '' },- body: null- })- await expect(authLocalGuard.canActivate(context)).rejects.toThrow()+ it.each(['login', 'password'])('should reject %s in query parameters', (field) => {+ Object.assign(context.switchToHttp().getRequest(), { query: { [field]: 'value' } })+ expect(() => authLocalGuard.canActivate(context)).toThrow(BadRequestException)+ expect(authProvider.validateUser).not.toHaveBeenCalled()})})diff --git a/backend/src/authentication/guards/auth-local.guard.ts b/backend/src/authentication/guards/auth-local.guard.tsindex 10dbab99..d5eeb0f7 100755--- a/backend/src/authentication/guards/auth-local.guard.ts+++ b/backend/src/authentication/guards/auth-local.guard.ts@@ -1,10 +1,21 @@-import { ExecutionContext, Injectable, Logger } from '@nestjs/common'+import { BadRequestException, ExecutionContext, Injectable, Logger } from '@nestjs/common'import { AuthGuard, IAuthGuard } from '@nestjs/passport'@Injectable()export class AuthLocalGuard extends AuthGuard('local') implements IAuthGuard {private readonly logger = new Logger(AuthLocalGuard.name)+ canActivate(ctx: ExecutionContext) {+ const { query } = this.getRequest(ctx)++ // Passport Local falls back to query parameters when body credentials are missing.+ if (Object.hasOwn(query ?? {}, 'login') || Object.hasOwn(query ?? {}, 'password')) {+ throw new BadRequestException('Credentials must not be provided in query parameters')+ }++ return super.canActivate(ctx)+ }+handleRequest<TUser = any>(err: any, user: any, info: Error, ctx: ExecutionContext, status?: any): TUser {const req = this.getRequest(ctx)req.raw.user = user?.login || 'unauthorized'