A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(api): cache bot token checks (#3735)
Hampus954f08a132b7
diff --git a/fluxer_api/src/api/oauth/BotAuthService.ts b/fluxer_api/src/api/oauth/BotAuthService.tsindex c9a36bc72..a880cd555 100644--- a/fluxer_api/src/api/oauth/BotAuthService.ts+++ b/fluxer_api/src/api/oauth/BotAuthService.ts@@ -1,5 +1,6 @@// SPDX-License-Identifier: AGPL-3.0-or-later+import {createHmac, randomBytes} from 'node:crypto';import type {ApplicationID, UserID} from '@app/api/BrandedTypes';import type {ApplicationRepository} from '@app/api/oauth/repositories/ApplicationRepository';import type {UserRepository} from '@app/api/user/repositories/UserRepository';@@ -7,7 +8,18 @@ import {canOwnerRunBots} from '@app/api/user/UserHelpers';import {hashPassword, verifyPassword} from '@app/api/utils/PasswordUtils';import {randomBase64Url} from '@app/api/utils/SecretHash';+const VERIFIED_TOKEN_CACHE_LIMIT = 10_000;+const VERIFIED_TOKEN_CACHE_TTL_MS = 10 * 60 * 1000;++interface VerifiedTokenEntry {+ readonly valid: boolean;+ readonly expiresAt: number;+}+export class BotAuthService {+ private readonly verifiedTokenKey = randomBytes(32);+ private readonly verifiedTokens = new Map<string, VerifiedTokenEntry>();+constructor(private readonly applicationRepository: ApplicationRepository,private readonly users: Pick<UserRepository, 'findUnique'>,@@ -44,13 +56,37 @@ export class BotAuthService {return null;}const [isValid, owner] = await Promise.all([- verifyPassword({password: secret, passwordHash: application.botTokenHash}).catch(() => false),+ this.verifyBotSecret(token, secret, application.botTokenHash),this.users.findUnique(application.ownerUserId),]);if (!isValid || (owner && !canOwnerRunBots(owner))) return null;return application.botUserId;}+ private async verifyBotSecret(token: string, secret: string, botTokenHash: string): Promise<boolean> {+ const key = createHmac('sha256', this.verifiedTokenKey)+ .update(token)+ .update('\0')+ .update(botTokenHash)+ .digest('base64url');+ const now = Date.now();+ const cached = this.verifiedTokens.get(key);+ if (cached && cached.expiresAt > now) {+ this.verifiedTokens.delete(key);+ this.verifiedTokens.set(key, cached);+ return cached.valid;+ }+ const valid = await verifyPassword({password: secret, passwordHash: botTokenHash}).catch(() => false);+ this.verifiedTokens.delete(key);+ this.verifiedTokens.set(key, {valid, expiresAt: now + VERIFIED_TOKEN_CACHE_TTL_MS});+ while (this.verifiedTokens.size > VERIFIED_TOKEN_CACHE_LIMIT) {+ const oldest = this.verifiedTokens.keys().next().value;+ if (oldest === undefined) break;+ this.verifiedTokens.delete(oldest);+ }+ return valid;+ }+async generateBotToken(applicationId: ApplicationID): Promise<{token: string;hash: string;diff --git a/fluxer_api/src/api/rpc/RpcService.ts b/fluxer_api/src/api/rpc/RpcService.tsindex 7ba556318..aafb69016 100644--- a/fluxer_api/src/api/rpc/RpcService.ts+++ b/fluxer_api/src/api/rpc/RpcService.ts@@ -877,6 +877,7 @@ export class RpcService {{tokenType,tokenHashPrefix,+ ip,retryAfter: rateLimitResult.retryAfter,limit: rateLimitResult.limit,resetTime: rateLimitResult.resetTime,@@ -908,6 +909,7 @@ export class RpcService {{tokenType,tokenHashPrefix,+ ip,},'RPC session token validation failed',);