The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Require client authentication for OAuth token introspection (#26023)
Charles Bochetb56329258f2c
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-oauth/oauth.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-oauth/oauth.service.tsindex eb23b082362..e2779470a4e 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-oauth/oauth.service.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-oauth/oauth.service.ts@@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';import crypto from 'crypto';+import { isNonEmptyString } from '@sniptt/guards';import ms from 'ms';import { Repository } from 'typeorm';import { base64UrlEncode, isDefined } from 'twenty-shared/utils';@@ -19,6 +20,7 @@ import { ApplicationEntity } from 'src/engine/core-modules/application/applicatiimport { ApplicationService } from 'src/engine/core-modules/application/application.service';import { OAuthErrorResponse } from 'src/engine/core-modules/application/application-oauth/types/oauth-error-response.type';import { OAuthTokenResponse } from 'src/engine/core-modules/application/application-oauth/types/oauth-token-response.type';+import { isConfidentialApplicationOAuthClient } from 'src/engine/core-modules/application/application-oauth/utils/is-confidential-application-oauth-client.util';import { ApplicationTokenService } from 'src/engine/core-modules/auth/token/services/application-token.service';import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';import { UserWorkspaceEntity } from 'src/engine/core-modules/user-workspace/user-workspace.entity';@@ -72,7 +74,10 @@ export class OAuthService {const applicationRegistration = clientValidation;- if (applicationRegistration.oAuthClientSecretHash && !clientSecret) {+ if (+ isConfidentialApplicationOAuthClient(applicationRegistration) &&+ !isNonEmptyString(clientSecret)+ ) {return this.errorResponse('invalid_client','Client authentication required for confidential clients',@@ -333,7 +338,10 @@ export class OAuthService {const applicationRegistration = clientValidation;// Confidential clients (those with a secret) must authenticate- if (applicationRegistration.oAuthClientSecretHash && !clientSecret) {+ if (+ isConfidentialApplicationOAuthClient(applicationRegistration) &&+ !isNonEmptyString(clientSecret)+ ) {return this.errorResponse('invalid_client','Client authentication required for confidential clients',@@ -490,6 +498,14 @@ export class OAuthService {return { active: false };}+ // RFC 7662 §2.1: a confidential client must authenticate to introspect; without its secret we disclose nothing.+ if (+ isConfidentialApplicationOAuthClient(clientValidation) &&+ !isNonEmptyString(clientSecret)+ ) {+ return { active: false };+ }+if (clientSecret) {const secretError = await this.validateClientSecret(clientValidation,diff --git a/packages/twenty-server/src/engine/core-modules/application/application-oauth/utils/__tests__/is-confidential-application-oauth-client.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-oauth/utils/__tests__/is-confidential-application-oauth-client.util.spec.tsnew file mode 100644index 00000000000..b80e964942a--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/application/application-oauth/utils/__tests__/is-confidential-application-oauth-client.util.spec.ts@@ -0,0 +1,21 @@+import { isConfidentialApplicationOAuthClient } from 'src/engine/core-modules/application/application-oauth/utils/is-confidential-application-oauth-client.util';++describe('isConfidentialApplicationOAuthClient', () => {+ it('is confidential when a secret hash is registered', () => {+ expect(+ isConfidentialApplicationOAuthClient({ oAuthClientSecretHash: 'hashed' }),+ ).toBe(true);+ });++ it('is public when there is no secret hash', () => {+ expect(+ isConfidentialApplicationOAuthClient({ oAuthClientSecretHash: null }),+ ).toBe(false);+ });++ it('is public when the secret hash is an empty string', () => {+ expect(+ isConfidentialApplicationOAuthClient({ oAuthClientSecretHash: '' }),+ ).toBe(false);+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/application/application-oauth/utils/is-confidential-application-oauth-client.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-oauth/utils/is-confidential-application-oauth-client.util.tsnew file mode 100644index 00000000000..4ff45cce421--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/application/application-oauth/utils/is-confidential-application-oauth-client.util.ts@@ -0,0 +1,10 @@+import { isNonEmptyString } from '@sniptt/guards';++import { type ApplicationRegistrationEntity } from 'src/engine/core-modules/application/application-registration/application-registration.entity';++export const isConfidentialApplicationOAuthClient = (+ applicationRegistration: Pick<+ ApplicationRegistrationEntity,+ 'oAuthClientSecretHash'+ >,+): boolean => isNonEmptyString(applicationRegistration.oAuthClientSecretHash);