Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

2FA - set MIN_SECRET_BYTES to 10

Blue Mouse7052796a83c8
diff --git a/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.service.ts b/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.service.ts
index 98a4d1c05..b2b0bed49 100644
--- a/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.service.ts
+++ b/backend/apps/cloud/src/twoFactorAuth/twoFactorAuth.service.ts
@@ -1,10 +1,15 @@
import { Injectable } from '@nestjs/common'
-import { generateSecret, generateURI, verify } from 'otplib'
+import { generateSecret, generateURI, verify, createGuardrails } from 'otplib'
import { UserService } from '../user/user.service'
import { User } from '../user/entities/user.entity'
import { TWO_FACTOR_AUTHENTICATION_APP_NAME } from '../common/constants'
+// otplib v13 defaults to MIN_SECRET_BYTES=16, but secrets created under v12
+// are 10 bytes (16 Base32 chars). Relax the guardrail so existing users can
+// still verify.
+const verifyGuardrails = createGuardrails({ MIN_SECRET_BYTES: 10 })
+
@Injectable()
export class TwoFactorAuthService {
constructor(private userService: UserService) {}
@@ -39,6 +44,7 @@ export class TwoFactorAuthService {
token: twoFactorAuthenticationCode,
secret: user.twoFactorAuthenticationSecret,
epochTolerance: 30,
+ guardrails: verifyGuardrails,
})
return result.valid
diff --git a/backend/apps/community/src/twoFactorAuth/twoFactorAuth.service.ts b/backend/apps/community/src/twoFactorAuth/twoFactorAuth.service.ts
index fda5400c2..7799a34ed 100644
--- a/backend/apps/community/src/twoFactorAuth/twoFactorAuth.service.ts
+++ b/backend/apps/community/src/twoFactorAuth/twoFactorAuth.service.ts
@@ -1,11 +1,16 @@
import { Injectable } from '@nestjs/common'
-import { generateSecret, generateURI, verify } from 'otplib'
+import { generateSecret, generateURI, verify, createGuardrails } from 'otplib'
import { genSalt, hash, compare } from 'bcrypt'
import { UserService } from '../user/user.service'
import { User } from '../common/types'
import { TWO_FACTOR_AUTHENTICATION_APP_NAME } from '../common/constants'
+// otplib v13 defaults to MIN_SECRET_BYTES=16, but secrets created under v12
+// are 10 bytes (16 Base32 chars). Relax the guardrail so existing users can
+// still verify.
+const verifyGuardrails = createGuardrails({ MIN_SECRET_BYTES: 10 })
+
@Injectable()
export class TwoFactorAuthService {
constructor(private userService: UserService) {}
@@ -52,6 +57,7 @@ export class TwoFactorAuthService {
token: twoFactorAuthenticationCode,
secret: user.twoFactorAuthenticationSecret,
epochTolerance: 30,
+ guardrails: verifyGuardrails,
})
return result.valid