From 14b3344a419012130d8879cfe449316203327020 Mon Sep 17 00:00:00 2001 From: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:22:45 +0300 Subject: [PATCH] fix: optional auth guard only secret provided (#23977) --- .../optional-api-auth.guard.ts | 6 ++++ .../api-auth/api-auth.strategy.e2e-spec.ts | 29 ++++++++++++++++++- .../strategies/api-auth/api-auth.strategy.ts | 15 +++++++--- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/apps/api/v2/src/modules/auth/guards/optional-api-auth/optional-api-auth.guard.ts b/apps/api/v2/src/modules/auth/guards/optional-api-auth/optional-api-auth.guard.ts index 6cdd594daa..8e3d070e08 100644 --- a/apps/api/v2/src/modules/auth/guards/optional-api-auth/optional-api-auth.guard.ts +++ b/apps/api/v2/src/modules/auth/guards/optional-api-auth/optional-api-auth.guard.ts @@ -2,6 +2,7 @@ import { ApiAuthGuard } from "@/modules/auth/guards/api-auth/api-auth.guard"; import { NO_AUTH_PROVIDED_MESSAGE, ONLY_CLIENT_ID_PROVIDED_MESSAGE, + ONLY_CLIENT_SECRET_PROVIDED_MESSAGE, } from "@/modules/auth/strategies/api-auth/api-auth.strategy"; export class OptionalApiAuthGuard extends ApiAuthGuard { @@ -9,11 +10,16 @@ export class OptionalApiAuthGuard extends ApiAuthGuard { // note(Lauris): optional means that auth is not required but if it is invalid then still throw error. const noAuthProvided = error && error.message.includes(NO_AUTH_PROVIDED_MESSAGE); const onlyClientIdProvided = error && error.message.includes(ONLY_CLIENT_ID_PROVIDED_MESSAGE); + const onlyClientSecretProvided = error && error.message.includes(ONLY_CLIENT_SECRET_PROVIDED_MESSAGE); if (onlyClientIdProvided) { return null; } + if (onlyClientSecretProvided) { + return null; + } + if (user || noAuthProvided || !error) { return user || null; } else { diff --git a/apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.e2e-spec.ts b/apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.e2e-spec.ts index 72ba40b14e..7d19212110 100644 --- a/apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.e2e-spec.ts +++ b/apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.e2e-spec.ts @@ -32,7 +32,11 @@ import { randomString } from "test/utils/randomString"; import { X_CAL_CLIENT_ID, X_CAL_SECRET_KEY } from "@calcom/platform-constants"; import type { PlatformOAuthClient, Team, User } from "@calcom/prisma/client"; -import { ApiAuthGuardRequest, ONLY_CLIENT_ID_PROVIDED_MESSAGE } from "./api-auth.strategy"; +import { + ApiAuthGuardRequest, + ONLY_CLIENT_ID_PROVIDED_MESSAGE, + ONLY_CLIENT_SECRET_PROVIDED_MESSAGE, +} from "./api-auth.strategy"; import { ApiAuthStrategy } from "./api-auth.strategy"; describe("ApiAuthStrategy", () => { @@ -267,6 +271,29 @@ describe("ApiAuthStrategy", () => { } }); + it("should throw 401 if only OAuth Client Secret is provided", async () => { + const context: ExecutionContext = { + switchToHttp: () => ({ + getRequest: () => ({ + headers: { + [X_CAL_SECRET_KEY]: `${oAuthClient.secret}gibberish`, + }, + get: (key: string) => ({ origin: "http://localhost:3000" }[key]), + }), + }), + } as ExecutionContext; + const request = context.switchToHttp().getRequest(); + + try { + await strategy.authenticate(request); + } catch (error) { + if (error instanceof HttpException) { + expect(error.getStatus()).toEqual(401); + expect(error.message).toContain(ONLY_CLIENT_SECRET_PROVIDED_MESSAGE); + } + } + }); + it("should throw 401 if OAuth ID is invalid", async () => { const context: ExecutionContext = { switchToHttp: () => ({ diff --git a/apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.ts b/apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.ts index 3b1ecb5378..9f24bea005 100644 --- a/apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.ts +++ b/apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.ts @@ -29,11 +29,11 @@ export type ApiAuthGuardRequest = Request & { user: ApiAuthGuardUser; allowedAuthMethods?: AllowedAuthMethod[]; }; -export const NO_AUTH_PROVIDED_MESSAGE = - "No authentication method provided. Either pass an API key as 'Bearer' header or OAuth client credentials as 'x-cal-secret-key' and 'x-cal-client-id' headers"; +export const NO_AUTH_PROVIDED_MESSAGE = `No authentication method provided. Either pass an API key as 'Bearer' header or OAuth client credentials as '${X_CAL_SECRET_KEY}' and '${X_CAL_CLIENT_ID}' headers`; -export const ONLY_CLIENT_ID_PROVIDED_MESSAGE = - "Only 'x-cal-client-id' header provided. Please also provide 'x-cal-secret-key' header or Auth bearer token as 'Authentication' header"; +export const ONLY_CLIENT_ID_PROVIDED_MESSAGE = `Only '${X_CAL_CLIENT_ID}' header provided. Please also provide '${X_CAL_SECRET_KEY}' header or Auth bearer token as 'Authentication' header`; + +export const ONLY_CLIENT_SECRET_PROVIDED_MESSAGE = `Only '${X_CAL_SECRET_KEY}' header provided. Please also provide '${X_CAL_CLIENT_ID}' header or Auth bearer token as 'Authentication' header`; @Injectable() export class ApiAuthStrategy extends PassportStrategy(BaseStrategy, "api-auth") { @@ -121,6 +121,9 @@ export class ApiAuthStrategy extends PassportStrategy(BaseStrategy, "api-auth") const noAuthProvided = !oAuthClientId && !oAuthClientSecret && !bearerToken && !nextAuthToken; const onlyClientIdProvided = !!oAuthClientId && !oAuthClientSecret && !bearerToken && !nextAuthToken; + const onlyClientSecretProvided = + !oAuthClientId && !!oAuthClientSecret && !bearerToken && !nextAuthToken; + if (noAuthProvided) { throw new UnauthorizedException(`ApiAuthStrategy - ${NO_AUTH_PROVIDED_MESSAGE}`); } @@ -129,6 +132,10 @@ export class ApiAuthStrategy extends PassportStrategy(BaseStrategy, "api-auth") throw new UnauthorizedException(`ApiAuthStrategy - ${ONLY_CLIENT_ID_PROVIDED_MESSAGE}`); } + if (onlyClientSecretProvided) { + throw new UnauthorizedException(`ApiAuthStrategy - ${ONLY_CLIENT_SECRET_PROVIDED_MESSAGE}`); + } + throw new UnauthorizedException( `ApiAuthStrategy - Invalid authentication method. Please provide one of the allowed methods: ${ allowedMethods && allowedMethods.length > 0 ? allowedMethods.join(", ") : "Any supported method"