Skip to content

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

fix(backend:auth): harden WebDAV cache keys with HMAC

johaven55ade5c2d0d3
diff --git a/backend/src/authentication/auth.e2e-spec.ts b/backend/src/authentication/auth.e2e-spec.ts
index 3ba194d4..ec11405f 100644
--- a/backend/src/authentication/auth.e2e-spec.ts
+++ b/backend/src/authentication/auth.e2e-spec.ts
@@ -3,7 +3,6 @@ import { NestFastifyApplication } from '@nestjs/platform-fastify'
import { Type } from 'class-transformer'
import { IsInt, ValidateNested } from 'class-validator'
import { appBootstrap } from '../app.bootstrap.js'
-import { genHash } from '../applications/files/utils/files.js'
import { USER_PERMISSION, USER_ROLE } from '../applications/users/constants/user.js'
import { DeleteUserDto } from '../applications/users/dto/delete-user.dto.js'
import { UserModel } from '../applications/users/models/user.model.js'
@@ -19,12 +18,12 @@ import { Cache } from '../infrastructure/cache/cache.service.js'
import { dbCheckConnection } from '../infrastructure/database/utils.js'
import { AuthConfig } from './auth.config.js'
import { CSRF_ERROR, TOKEN_PATHS, TOKEN_TYPES } from './constants/auth.js'
-import { CACHE_AUTH_WEBDAV_PREFIX } from './constants/cache.js'
import { API_AUTH_LOGIN, API_AUTH_LOGOUT, API_AUTH_REFRESH, API_AUTH_TOKEN, API_AUTH_TOKEN_REFRESH } from './constants/routes.js'
import { AUTH_SCOPE } from './constants/scope.js'
import { TokenResponseDto } from './dto/token-response.dto.js'
import { JwtPayload } from './interfaces/jwt-payload.interface.js'
import { TOKEN_TYPE } from './interfaces/token.interface.js'
+import { genWebDAVAuthCacheKey } from './utils/auth-cache.js'
class BrowserRefreshUserResponseDto {
@IsInt()
@@ -474,7 +473,7 @@ describe('Auth (e2e)', () => {
}
function webDAVAuthCacheKey(password: string): string {
- return `${CACHE_AUTH_WEBDAV_PREFIX}-${genHash(`${userTest.login}\u0000${password}`, 'sha256')}`
+ return genWebDAVAuthCacheKey(userTest.login, password, authConfig.token.access.secret)
}
async function deleteAppPasswordIfPresent(passwordName: string): Promise<void> {
diff --git a/backend/src/authentication/auth.md b/backend/src/authentication/auth.md
index 416292d7..e39a5948 100644
--- a/backend/src/authentication/auth.md
+++ b/backend/src/authentication/auth.md
@@ -136,6 +136,8 @@ The primary password comparison is still executed for scoped-auth timing, but a
Only successful WebDAV Basic-auth results are cached, for 900 seconds. Failed credentials and refusals caused by account state are never cached, so
credentials can be checked again immediately after an account is unlocked (subject to rate limits). Revoking a WebDAV app password clears cached
successful results for that user so the revoked password cannot continue to authenticate from cache.
+Cache keys use a domain-separated HMAC-SHA-256 keyed with the access-token secret; the cache therefore does not persist an unkeyed fast digest of the
+submitted password.
### Client registration
diff --git a/backend/src/authentication/constants/cache.ts b/backend/src/authentication/constants/cache.ts
index c542d404..6393f780 100644
--- a/backend/src/authentication/constants/cache.ts
+++ b/backend/src/authentication/constants/cache.ts
@@ -1,4 +1,4 @@
-// cache key = `auth-webdav-${sha256(login + '\0' + password)}` => successful UserModel only
+// cache key = `auth-webdav-${HMAC-SHA-256(context + '\0' + login + '\0' + password, access-token secret)}` => successful UserModel only
export const CACHE_AUTH_WEBDAV_PREFIX = 'auth-webdav' as const
export const CACHE_AUTH_WEBDAV_TTL = 900 as const
export const CACHE_AUTH_RATE_LIMIT_PREFIX = 'auth-rate-limit' as const
diff --git a/backend/src/authentication/guards/auth-basic.strategy.ts b/backend/src/authentication/guards/auth-basic.strategy.ts
index 69d810dd..0931a09e 100644
--- a/backend/src/authentication/guards/auth-basic.strategy.ts
+++ b/backend/src/authentication/guards/auth-basic.strategy.ts
@@ -4,15 +4,16 @@ import { ThrottlerException } from '@nestjs/throttler'
import { instanceToPlain, plainToInstance } from 'class-transformer'
import { FastifyRequest } from 'fastify'
import { PinoLogger } from 'nestjs-pino'
-import { genHash } from '../../applications/files/utils/files.js'
import { UserModel } from '../../applications/users/models/user.model.js'
import { SERVER_NAME } from '../../common/shared.js'
+import { configuration } from '../../configuration/config.environment.js'
import { Cache } from '../../infrastructure/cache/cache.service.js'
import { AUTH_RATE_LIMIT_ERROR_MESSAGE } from '../constants/auth.js'
-import { CACHE_AUTH_WEBDAV_PREFIX, CACHE_AUTH_WEBDAV_TTL } from '../constants/cache.js'
+import { CACHE_AUTH_WEBDAV_TTL } from '../constants/cache.js'
import { AUTH_SCOPE } from '../constants/scope.js'
import { AuthPasswordWorkLimitException } from '../errors/auth-password-work-limit.exception.js'
import { AuthProvider } from '../providers/auth-providers.models.js'
+import { genWebDAVAuthCacheKey } from '../utils/auth-cache.js'
import { consumeWebDAVRateLimit } from '../utils/auth-rate-limit.js'
import { HttpBasicStrategy } from './implementations/http-basic.strategy.js'
@@ -29,7 +30,7 @@ export class AuthBasicStrategy extends PassportStrategy(HttpBasicStrategy, 'basi
async validate(req: FastifyRequest, loginOrEmail: string, password: string): Promise<Omit<UserModel, 'password'> | null> {
loginOrEmail = loginOrEmail.trim()
this.logger.assign({ user: loginOrEmail })
- const basicAuthCacheKey = `${CACHE_AUTH_WEBDAV_PREFIX}-${genHash(`${loginOrEmail}\u0000${password}`, 'sha256')}`
+ const basicAuthCacheKey = genWebDAVAuthCacheKey(loginOrEmail, password, configuration.auth.token.access.secret)
const userFromCache: null | undefined | Partial<UserModel> = await this.cache.get(basicAuthCacheKey)
if (userFromCache) {
// Only successful credentials are usable from cache; ignore any stale negative entry.
diff --git a/backend/src/authentication/utils/auth-cache.spec.ts b/backend/src/authentication/utils/auth-cache.spec.ts
new file mode 100644
index 00000000..e23d626c
--- /dev/null
+++ b/backend/src/authentication/utils/auth-cache.spec.ts
@@ -0,0 +1,19 @@
+import crypto from 'node:crypto'
+import { CACHE_AUTH_WEBDAV_PREFIX } from '../constants/cache.js'
+import { genWebDAVAuthCacheKey } from './auth-cache.js'
+
+describe('Authentication cache helpers', () => {
+ const login = '[email protected]'
+ const password = 'correct horse battery staple'
+ const secret = 'access-token-secret'
+
+ it('derives WebDAV cache keys with a domain-separated HMAC', () => {
+ const cacheKey = genWebDAVAuthCacheKey(login, password, secret)
+ const legacyCacheKey = `${CACHE_AUTH_WEBDAV_PREFIX}-${crypto.createHash('sha256').update(`${login}\u0000${password}`).digest('hex')}`
+
+ expect(cacheKey).toMatch(/^auth-webdav-[a-f0-9]{64}$/)
+ expect(cacheKey).toBe(genWebDAVAuthCacheKey(login, password, secret))
+ expect(cacheKey).not.toBe(legacyCacheKey)
+ expect(cacheKey).not.toBe(genWebDAVAuthCacheKey(login, password, 'another-access-token-secret'))
+ })
+})
diff --git a/backend/src/authentication/utils/auth-cache.ts b/backend/src/authentication/utils/auth-cache.ts
new file mode 100644
index 00000000..939db456
--- /dev/null
+++ b/backend/src/authentication/utils/auth-cache.ts
@@ -0,0 +1,18 @@
+import crypto from 'node:crypto'
+import { CACHE_AUTH_WEBDAV_PREFIX } from '../constants/cache.js'
+
+const WEBDAV_AUTH_CACHE_HMAC_CONTEXT = 'sync-in:webdav-auth-cache:v1'
+
+export function genWebDAVAuthCacheKey(loginOrEmail: string, password: string, secret: string): string {
+ // Keep this HMAC domain separate from JWT signatures that use the same configured secret.
+ const digest = crypto
+ .createHmac('sha256', secret)
+ .update(WEBDAV_AUTH_CACHE_HMAC_CONTEXT)
+ .update('\u0000')
+ .update(loginOrEmail)
+ .update('\u0000')
+ .update(password)
+ .digest('hex')
+
+ return `${CACHE_AUTH_WEBDAV_PREFIX}-${digest}`
+}