Skip to content

The open alternative to Salesforce, designed for AI.

fix: make tokenBucketThrottleOrThrow atomic for every caller (#27012)

Abdul Rahman8ab3829ec18c
diff --git a/packages/twenty-server/src/engine/core-modules/throttler/constants/token-bucket-throttle-key-prefix.constant.ts b/packages/twenty-server/src/engine/core-modules/throttler/constants/token-bucket-throttle-key-prefix.constant.ts
new file mode 100644
index 00000000000..36439dd979f
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/throttler/constants/token-bucket-throttle-key-prefix.constant.ts
@@ -0,0 +1 @@
+export const TOKEN_BUCKET_THROTTLE_KEY_PREFIX = 'token-bucket';
diff --git a/packages/twenty-server/src/engine/core-modules/throttler/throttler.service.ts b/packages/twenty-server/src/engine/core-modules/throttler/throttler.service.ts
index 9c9e07f1473..79fc2932eb8 100644
--- a/packages/twenty-server/src/engine/core-modules/throttler/throttler.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/throttler/throttler.service.ts
@@ -7,6 +7,7 @@ import {
ThrottlerException,
ThrottlerExceptionCode,
} from 'src/engine/core-modules/throttler/throttler.exception';
+import { TOKEN_BUCKET_THROTTLE_KEY_PREFIX } from 'src/engine/core-modules/throttler/constants/token-bucket-throttle-key-prefix.constant';
import {
TOKEN_BUCKETS_DENY_PARTIAL_ARG,
TRY_CONSUME_TOKEN_BUCKETS_SCRIPT,
@@ -24,48 +25,12 @@ export class ThrottlerService {
tokensToConsume: number,
maxTokens: number,
timeWindow: number,
- ): Promise<number> {
- const now = Date.now();
- const availableTokens = await this.getAvailableTokensCount(
- key,
- maxTokens,
- timeWindow,
- now,
- );
-
- if (availableTokens < tokensToConsume) {
- throw new ThrottlerException(
- `Limit reached (${maxTokens} tokens per ${timeWindow} ms)`,
- ThrottlerExceptionCode.LIMIT_REACHED,
- );
- }
-
- await this.cacheStorage.set(
- key,
- {
- tokens: availableTokens - tokensToConsume,
- lastRefillAt: now,
- },
- timeWindow * 2,
- );
-
- return availableTokens - tokensToConsume;
- }
-
- async atomicTokenBucketThrottleOrThrow({
- key,
- maxTokens,
- timeWindow,
- }: {
- key: string;
- maxTokens: number;
- timeWindow: number;
- }): Promise<void> {
+ ): Promise<void> {
const [admittedCount] = await this.cacheStorage.runScript<number[]>({
script: TRY_CONSUME_TOKEN_BUCKETS_SCRIPT,
- keys: [key],
+ keys: [`${TOKEN_BUCKET_THROTTLE_KEY_PREFIX}:${key}`],
args: [
- '1',
+ String(tokensToConsume),
JSON.stringify([
{ burst: maxTokens, refill: maxTokens, windowMs: timeWindow },
]),
@@ -73,7 +38,7 @@ export class ThrottlerService {
],
});
- if (admittedCount !== 1) {
+ if (admittedCount !== tokensToConsume) {
throw new ThrottlerException(
`Limit reached (${maxTokens} tokens per ${timeWindow} ms)`,
ThrottlerExceptionCode.LIMIT_REACHED,
diff --git a/packages/twenty-server/src/engine/core-modules/two-factor-authentication/two-factor-authentication.service.ts b/packages/twenty-server/src/engine/core-modules/two-factor-authentication/two-factor-authentication.service.ts
index 1d31b6047ff..40e54d82797 100644
--- a/packages/twenty-server/src/engine/core-modules/two-factor-authentication/two-factor-authentication.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/two-factor-authentication/two-factor-authentication.service.ts
@@ -160,11 +160,12 @@ export class TwoFactorAuthenticationService {
workspaceId: WorkspaceEntity['id'],
twoFactorAuthenticationStrategy: TwoFactorAuthenticationStrategy,
) {
- await this.throttlerService.atomicTokenBucketThrottleOrThrow({
- key: buildTwoFactorAuthenticationOtpRateLimitKey({ userId, workspaceId }),
- maxTokens: TWO_FACTOR_AUTHENTICATION_OTP_RATE_LIMIT_MAX,
- timeWindow: TWO_FACTOR_AUTHENTICATION_OTP_RATE_LIMIT_WINDOW_MS,
- });
+ await this.throttlerService.tokenBucketThrottleOrThrow(
+ buildTwoFactorAuthenticationOtpRateLimitKey({ userId, workspaceId }),
+ 1,
+ TWO_FACTOR_AUTHENTICATION_OTP_RATE_LIMIT_MAX,
+ TWO_FACTOR_AUTHENTICATION_OTP_RATE_LIMIT_WINDOW_MS,
+ );
const userTwoFactorAuthenticationMethod =
await this.twoFactorAuthenticationMethodRepository.findOne(workspaceId, {
diff --git a/packages/twenty-server/test/integration/graphql/suites/auth/two-factor-authentication/two-factor-authentication-otp-rate-limit.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/auth/two-factor-authentication/two-factor-authentication-otp-rate-limit.integration-spec.ts
index a4ea0faad18..a9069700fc0 100644
--- a/packages/twenty-server/test/integration/graphql/suites/auth/two-factor-authentication/two-factor-authentication-otp-rate-limit.integration-spec.ts
+++ b/packages/twenty-server/test/integration/graphql/suites/auth/two-factor-authentication/two-factor-authentication-otp-rate-limit.integration-spec.ts
@@ -3,6 +3,7 @@ import { isDefined } from 'twenty-shared/utils';
import { verifyTwoFactorAuthenticationMethod } from 'test/integration/graphql/utils/verify-two-factor-authentication-method.util';
import { CacheStorageNamespace } from 'src/engine/core-modules/cache-storage/types/cache-storage-namespace.enum';
+import { TOKEN_BUCKET_THROTTLE_KEY_PREFIX } from 'src/engine/core-modules/throttler/constants/token-bucket-throttle-key-prefix.constant';
import { TWO_FACTOR_AUTHENTICATION_OTP_RATE_LIMIT_MAX } from 'src/engine/core-modules/two-factor-authentication/constants/two-factor-authentication-otp-rate-limit.constant';
import { buildTwoFactorAuthenticationOtpRateLimitKey } from 'src/engine/core-modules/two-factor-authentication/utils/build-two-factor-authentication-otp-rate-limit-key.util';
import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';
@@ -13,7 +14,7 @@ const RATE_LIMIT_KEY = buildTwoFactorAuthenticationOtpRateLimitKey({
workspaceId: SEED_APPLE_WORKSPACE_ID,
});
-const RATE_LIMIT_CACHE_KEY = `${CacheStorageNamespace.IntegrationTests}:${CacheStorageNamespace.EngineWorkspace}:${RATE_LIMIT_KEY}`;
+const RATE_LIMIT_CACHE_KEY = `${CacheStorageNamespace.IntegrationTests}:${CacheStorageNamespace.EngineWorkspace}:${TOKEN_BUCKET_THROTTLE_KEY_PREFIX}:${RATE_LIMIT_KEY}`;
const clearRateLimitBucket = async (): Promise<void> => {
const redis = new IORedis(process.env.REDIS_URL ?? 'redis://localhost:6379');