Repository navigation
feat: Enhance log filtering and user session management with improved encryption and suspension checks #1903
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
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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { FoundUserDto } from '../../../entities/user/dto/found-user.dto.js'; | |
| import { IOtpLogin } from '../../../entities/user/use-cases/user-use-cases.interfaces.js'; | ||
| import { UserHelperService } from '../../../entities/user/user-helper.service.js'; | ||
| import { InTransactionEnum } from '../../../enums/in-transaction.enum.js'; | ||
| import { Messages } from '../../../exceptions/text/messages.js'; | ||
| import { appConfig } from '../../../shared/config/app-config.js'; | ||
| import { SaasOtpLoginDs } from '../data-structures/saas-otp-login.ds.js'; | ||
| import { ISaasOtpLogin } from './saas-use-cases.interface.js'; | ||
|
|
@@ -68,6 +69,9 @@ export class SaasOtpLoginUseCase extends AbstractUseCase<SaasOtpLoginDs, FoundUs | |
| if (!foundUser) { | ||
| throw new UnauthorizedException('JWT verification failed'); | ||
| } | ||
| if (foundUser.suspended) { | ||
| throw new UnauthorizedException(Messages.ACCOUNT_SUSPENDED); | ||
|
Comment on lines
+72
to
+73
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.
|
||
| } | ||
| return await this.userHelperService.buildFoundUserDs(foundUser); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { BadRequestException, Inject, Injectable, NotFoundException } from '@nestjs/common'; | ||
| import { BadRequestException, Inject, Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common'; | ||
| import AbstractUseCase from '../../../common/abstract-use.case.js'; | ||
| import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js'; | ||
| import { BaseType } from '../../../common/data-injection.tokens.js'; | ||
|
|
@@ -76,6 +76,19 @@ export class SaasUsualLoginUseCase extends AbstractUseCase<UsualLoginDs, FoundUs | |
| await this.recordSignInAudit(email, null, SignInStatusEnum.FAILED, ipAddress, userAgent, Messages.USER_NOT_FOUND); | ||
| throw new NotFoundException(Messages.USER_NOT_FOUND); | ||
| } | ||
| // A suspended user must not get a session. The saas never reads the `suspended` flag of the | ||
| // returned user, so the check has to live here, as in UsualLoginUseCase. | ||
| if (user.suspended) { | ||
|
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. Do not disclose suspension before password validation. A caller with access to the SaaS login bridge can submit any password and distinguish a suspended account from an active account by the response. Validate the password first, or return the same failure status and message for both cases. Based on learnings: login responses should not disclose whether an account or email exists.
|
||
| await this.recordSignInAudit( | ||
| email, | ||
| user.id, | ||
| SignInStatusEnum.FAILED, | ||
| ipAddress, | ||
| userAgent, | ||
| Messages.ACCOUNT_SUSPENDED, | ||
| ); | ||
| throw new UnauthorizedException(Messages.ACCOUNT_SUSPENDED); | ||
| } | ||
| if (!userData.password) { | ||
| await this.recordSignInAudit( | ||
| email, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import CryptoJS from 'crypto-js'; | |
| process.env.PRIVATE_KEY = 'test-private-key-12345'; | ||
|
|
||
| const { Encryptor } = await import('../../src/helpers/encryption/encryptor.js'); | ||
| const awaitedAppConfig = await import('../../src/shared/config/app-config.js'); | ||
|
|
||
| const TEST_DATA = 'Hello, World! This is sensitive data.'; | ||
| const TEST_MASTER_PWD = 'my-secure-master-password-123'; | ||
|
|
@@ -198,3 +199,21 @@ test('hashUserPassword: should return empty string for empty password', async (t | |
|
|
||
| t.is(result, ''); | ||
| }); | ||
|
|
||
| test('encryptData: throws instead of returning the plaintext when the private key is missing', (t) => { | ||
| const { appConfig } = awaitedAppConfig; | ||
| const savedKey = appConfig.auth.privateKey; | ||
| appConfig.auth.privateKey = null; | ||
|
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. Replace the assignment to frozen configuration.
|
||
| try { | ||
| const error = t.throws(() => Encryptor.encryptData(TEST_DATA)); | ||
| t.true(error.message.includes('Data encryption failed')); | ||
| t.false(error.message.includes(TEST_DATA)); | ||
| } finally { | ||
| appConfig.auth.privateKey = savedKey; | ||
| } | ||
| }); | ||
|
|
||
| test('isEncrypted: true only for the V2 envelope', (t) => { | ||
| t.true(Encryptor.isEncrypted(Encryptor.encryptData(TEST_DATA))); | ||
| t.false(Encryptor.isEncrypted(TEST_DATA)); | ||
| }); | ||
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.
Preserve decryption of legacy V1 OTP seeds.
If an enrolled user has a V1-encrypted
otpSecretKey,Encryptor.isEncryptedreturns false. This load hook then exposes ciphertext to OTP verification. On a later save, the changed update hook encrypts that ciphertext again. Existing users with V1 seeds can no longer verify their OTP codes. Distinguish legacy V1 ciphertext from the plaintext seeds left by incomplete enrollment, and decrypt V1 before any re-encryption. Add a persisted-V1 regression case. The encryptor tests explicitly cover V1 data as a legacy format. (raw.githubusercontent.com)