Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:auth): prevent user enumeration via timing attacks

718580eebf3f9cee
diff --git a/backend/src/applications/users/services/users-queries.service.ts b/backend/src/applications/users/services/users-queries.service.ts
index 930ad961..9beda22f 100644
--- a/backend/src/applications/users/services/users-queries.service.ts
+++ b/backend/src/applications/users/services/users-queries.service.ts
@@ -90,8 +90,7 @@ export class UsersQueries {
async compareUserPassword(userId: number, password: string): Promise<boolean> {
const [hash] = (await this.selectUsers(['password'], [eq(users.id, userId)])) as { password: string }[]
- if (!hash) return false
- return comparePassword(password, hash.password)
+ return comparePassword(password, hash?.password)
}
async from(userId?: number, loginOrEmail?: string): Promise<User> {
diff --git a/backend/src/common/functions.ts b/backend/src/common/functions.ts
index f479d049..851ad6cd 100755
--- a/backend/src/common/functions.ts
+++ b/backend/src/common/functions.ts
@@ -9,6 +9,8 @@ import { setTimeout } from 'node:timers/promises'
import { SPACE_PERMS_SEP } from '../applications/spaces/constants/spaces'
import { decodeUrl } from './shared'
+const DUMMY_PASSWORD_HASH = '$2a$10$tjgA0v/cGe.vAfAJgNHpZeNrIdMxu82i0kGEjbtYkaVUCDkVzHRjG'
+
export const regexpEscape = /[.*+?^${}()|[\]\\]/g
export const regexSpecialChars = /[-[\]{}()*+!<=:?./\\^$|#,]/g
export const regexSpecialCharsWithSpace = /[-[\]{}()*+!<=:?./\\^$|#\s,]/g
@@ -66,7 +68,12 @@ export async function hashPassword(password: string): Promise<string> {
return await bcrypt.hash(password, 10)
}
-export async function comparePassword(password: string, hash: string): Promise<boolean> {
+export async function comparePassword(password: string, hash?: string | null): Promise<boolean> {
+ if (!hash) {
+ // No hash, waste time for time-based attacks
+ await bcrypt.compare(password, DUMMY_PASSWORD_HASH)
+ return false
+ }
return await bcrypt.compare(password, hash)
}