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): prevent CSRF cookie from expiring before access token
johaven40f816deff74
diff --git a/backend/src/authentication/auth.service.spec.ts b/backend/src/authentication/auth.service.spec.tsindex c1e342c5..e5c36ed8 100755--- a/backend/src/authentication/auth.service.spec.ts+++ b/backend/src/authentication/auth.service.spec.ts@@ -3,8 +3,11 @@ import { JwtService } from '@nestjs/jwt'import { Test, TestingModule } from '@nestjs/testing'import { USER_ROLE } from '../applications/users/constants/user'import { UserModel } from '../applications/users/models/user.model'+import { convertHumanTimeToSeconds } from '../common/functions'import { currentTimeStamp } from '../common/shared'+import { configuration } from '../configuration/config.environment'import { AuthManager } from './auth.service'+import { TOKEN_TYPE } from './interfaces/token.interface'import { AUTH_SESSION } from './providers/auth-providers.constants'describe(AuthManager.name, () => {@@ -76,4 +79,27 @@ describe(AuthManager.name, () => {expect.any(Object))})++ it('should keep the CSRF cookie alive as long as the access cookie when refreshing near expiration', async () => {+ const user = new UserModel({+ id: 1,+ login: 'alice',+ email: '[email protected]',+ firstName: 'Alice',+ lastName: 'Doe',+ language: 'en',+ role: USER_ROLE.USER,+ exp: currentTimeStamp() + 30+ } as any)+ const res = { setCookie: vi.fn() }++ await authManager.refreshCookies(user, res as any)++ const accessExpiration = convertHumanTimeToSeconds(configuration.auth.token[TOKEN_TYPE.ACCESS].expiration)+ expect(res.setCookie).toHaveBeenCalledWith(+ configuration.auth.token[TOKEN_TYPE.CSRF].name,+ expect.any(String),+ expect.objectContaining({ maxAge: accessExpiration })+ )+ })})diff --git a/backend/src/authentication/auth.service.ts b/backend/src/authentication/auth.service.tsindex 94c5451c..3fd3d496 100755--- a/backend/src/authentication/auth.service.ts+++ b/backend/src/authentication/auth.service.ts@@ -88,10 +88,12 @@ export class AuthManager {throw new HttpException('Token has expired', HttpStatus.FORBIDDEN)}const refreshTokenExpiration = user.exp - currentTime+ const accessTokenExpiration = convertHumanTimeToSeconds(configuration.auth.token[TOKEN_TYPE.ACCESS].expiration)+ const csrfTokenExpiration = Math.max(accessTokenExpiration, refreshTokenExpiration)const csrfToken: string = crypto.randomUUID()for (const type of TOKEN_TYPES) {const tokenExpiration =- type === TOKEN_TYPE.ACCESS ? convertHumanTimeToSeconds(configuration.auth.token[TOKEN_TYPE.ACCESS].expiration) : refreshTokenExpiration+ type === TOKEN_TYPE.ACCESS ? accessTokenExpiration : type === TOKEN_TYPE.CSRF ? csrfTokenExpiration : refreshTokenExpirationconst cookieValue: string = type === TOKEN_TYPE.CSRF ? csrfToken : await this.jwtSign(user, type, tokenExpiration, csrfToken)res.setCookie(configuration.auth.token[type].name, cookieValue, {signed: type === TOKEN_TYPE.CSRF,