The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(application): throttle failed application token renewals by session (#26544)
Paul Rastoinf94e80e869d1
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-oauth/application-oauth.resolver.ts b/packages/twenty-server/src/engine/core-modules/application/application-oauth/application-oauth.resolver.tsindex e0d40657421..fa9b6777900 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-oauth/application-oauth.resolver.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-oauth/application-oauth.resolver.ts@@ -43,6 +43,13 @@ export class ApplicationOAuthResolver {@AuthUser() user: AuthContextUser,@AuthUserWorkspaceId() userWorkspaceId: string,): Promise<ApplicationTokenPairDTO> {+ await this.throttlerService.tokenBucketThrottleOrThrow(+ `app-renew:${workspaceId}:${userWorkspaceId}`,+ 1,+ APPLICATION_TOKEN_RATE_LIMIT_MAX,+ APPLICATION_TOKEN_RATE_LIMIT_WINDOW_MS,+ );+const applicationRefreshTokenPayload =await this.applicationTokenService.validateApplicationRefreshTokenForSessionOrThrow({@@ -53,13 +60,6 @@ export class ApplicationOAuthResolver {},);- await this.throttlerService.tokenBucketThrottleOrThrow(- `app-renew:${workspaceId}:${userWorkspaceId}:${applicationRefreshTokenPayload.applicationId}`,- 1,- APPLICATION_TOKEN_RATE_LIMIT_MAX,- APPLICATION_TOKEN_RATE_LIMIT_WINDOW_MS,- );-return this.applicationTokenService.renewApplicationTokens(applicationRefreshTokenPayload,);diff --git a/packages/twenty-server/test/integration/metadata/suites/application/failing-application-token-renewal.integration-spec.ts b/packages/twenty-server/test/integration/metadata/suites/application/failing-application-token-renewal.integration-spec.tsindex 1829aeebe1c..2ee88066511 100644--- a/packages/twenty-server/test/integration/metadata/suites/application/failing-application-token-renewal.integration-spec.ts+++ b/packages/twenty-server/test/integration/metadata/suites/application/failing-application-token-renewal.integration-spec.ts@@ -3,12 +3,14 @@ import { findManyApplications } from 'test/integration/graphql/utils/find-many-aimport { generateAppleAdminApplicationTokenPair } from 'test/integration/utils/generate-apple-admin-application-token-pair.util';import { renewApplicationToken } from 'test/integration/metadata/suites/application/utils/renew-application-token.util';import { generateApplicationTokenPair } from 'test/integration/utils/generate-application-token-pair.util';+import { getAppProviderByClassName } from 'test/integration/utils/get-app-provider-by-class-name.util';import { jestExpectToBeDefined } from 'test/utils/jest-expect-to-be-defined.util.test';import {eachTestingContextFilter,type EachTestingContext,} from 'twenty-shared/testing';+import { type ThrottlerService } from 'src/engine/core-modules/throttler/throttler.service';import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';import { USER_WORKSPACE_DATA_SEED_IDS } from 'src/engine/workspace-manager/dev-seeder/core/utils/seed-user-workspaces.util';import { USER_DATA_SEED_IDS } from 'src/engine/workspace-manager/dev-seeder/core/utils/seed-users.util';@@ -133,6 +135,10 @@ describe('Application token renewal should fail', () => {};});+ afterEach(() => {+ jest.restoreAllMocks();+ });+it.each(eachTestingContextFilter(failingApplicationTokenRenewalTestCases))('$title',async ({ context }) => {@@ -147,4 +153,26 @@ describe('Application token renewal should fail', () => {expectOneNotInternalServerErrorSnapshot({ errors });},);++ it('should consume session rate limit bucket on failed renewal attempts', async () => {+ const tokenBucketThrottleOrThrowSpy = jest.spyOn(+ getAppProviderByClassName<ThrottlerService>('ThrottlerService'),+ 'tokenBucketThrottleOrThrow',+ );++ await renewApplicationToken({+ input: {+ applicationRefreshToken: 'invalid-malformed-token',+ },+ expectToFail: true,+ });++ expect(tokenBucketThrottleOrThrowSpy).toHaveBeenCalledTimes(1);+ expect(tokenBucketThrottleOrThrowSpy).toHaveBeenCalledWith(+ `app-renew:${SEED_APPLE_WORKSPACE_ID}:${USER_WORKSPACE_DATA_SEED_IDS.JANE}`,+ 1,+ 30,+ 30_000,+ );+ });});diff --git a/packages/twenty-server/test/integration/metadata/suites/application/successful-application-token-renewal.integration-spec.ts b/packages/twenty-server/test/integration/metadata/suites/application/successful-application-token-renewal.integration-spec.tsindex 373102912bb..a710fbb96ac 100644--- a/packages/twenty-server/test/integration/metadata/suites/application/successful-application-token-renewal.integration-spec.ts+++ b/packages/twenty-server/test/integration/metadata/suites/application/successful-application-token-renewal.integration-spec.ts@@ -70,8 +70,9 @@ describe('Application token renewal should succeed', () => {...expectedBinding,type: JwtTokenTypeEnum.APPLICATION_REFRESH,});+ expect(tokenBucketThrottleOrThrowSpy).toHaveBeenCalledTimes(1);expect(tokenBucketThrottleOrThrowSpy).toHaveBeenCalledWith(- `app-renew:${SEED_APPLE_WORKSPACE_ID}:${USER_WORKSPACE_DATA_SEED_IDS.JANE}:${applicationId}`,+ `app-renew:${SEED_APPLE_WORKSPACE_ID}:${USER_WORKSPACE_DATA_SEED_IDS.JANE}`,1,30,30_000,