diff --git a/CHANGELOG.md b/CHANGELOG.md index 027ced422a..d210ec4012 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Improved the validation of the request bodies in the authentication and subscription endpoints - Upgraded `dotenv` from version `18.0.9` to `18.0.10` - Upgraded `ng-extract-i18n-merge` from version `3.4.0` to `3.5.0` - Upgraded `prisma` from version `7.10.0` to `7.10.1` diff --git a/apps/api/src/app/endpoints/auth/access-token-login.dto.ts b/apps/api/src/app/endpoints/auth/access-token-login.dto.ts new file mode 100644 index 0000000000..2fb3dc6cbd --- /dev/null +++ b/apps/api/src/app/endpoints/auth/access-token-login.dto.ts @@ -0,0 +1,6 @@ +import { IsString } from 'class-validator'; + +export class AccessTokenLoginDto { + @IsString() + accessToken: string; +} diff --git a/apps/api/src/app/endpoints/auth/auth.controller.ts b/apps/api/src/app/endpoints/auth/auth.controller.ts index f0292167c2..fb6c16d580 100644 --- a/apps/api/src/app/endpoints/auth/auth.controller.ts +++ b/apps/api/src/app/endpoints/auth/auth.controller.ts @@ -5,11 +5,7 @@ import { OAuthCallbackGuard } from '@ghostfolio/api/guards/oauth-callback.guard' import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config'; import { AuthDeviceDto } from '@ghostfolio/common/dtos'; -import { - AssertionCredentialJSON, - AttestationCredentialJSON, - OAuthResponse -} from '@ghostfolio/common/interfaces'; +import { OAuthResponse } from '@ghostfolio/common/interfaces'; import { Body, @@ -31,8 +27,11 @@ import type { import { Request, Response } from 'express'; import { getReasonPhrase, StatusCodes } from 'http-status-codes'; +import { AccessTokenLoginDto } from './access-token-login.dto'; import { AuthService } from './auth.service'; import { GenerateAuthenticationOptionsDto } from './generate-authentication-options.dto'; +import { VerifyAttestationDto } from './verify-attestation.dto'; +import { VerifyAuthenticationDto } from './verify-authentication.dto'; import { WebAuthService } from './web-auth.service'; @AllowDuringImpersonation() @@ -47,7 +46,7 @@ export class AuthController { @Post('anonymous') @UseGuards(CustomThrottlerGuard) public async accessTokenLogin( - @Body() body: { accessToken: string } + @Body() body: AccessTokenLoginDto ): Promise { try { const authToken = await this.authService.validateAnonymousLogin( @@ -142,7 +141,7 @@ export class AuthController { @Post('webauthn/verify-attestation') @UseGuards(AuthGuard('jwt'), HasPermissionGuard) public async verifyAttestation( - @Body() body: { deviceName: string; credential: AttestationCredentialJSON } + @Body() body: VerifyAttestationDto ): Promise { return this.webAuthService.verifyAttestation(body.credential); } @@ -150,7 +149,7 @@ export class AuthController { @Post('webauthn/verify-authentication') @UseGuards(CustomThrottlerGuard) public async verifyAuthentication( - @Body() body: { deviceId: string; credential: AssertionCredentialJSON } + @Body() body: VerifyAuthenticationDto ): Promise { try { const authToken = await this.webAuthService.verifyAuthentication( diff --git a/apps/api/src/app/endpoints/auth/verify-attestation.dto.ts b/apps/api/src/app/endpoints/auth/verify-attestation.dto.ts new file mode 100644 index 0000000000..13b5e07f57 --- /dev/null +++ b/apps/api/src/app/endpoints/auth/verify-attestation.dto.ts @@ -0,0 +1,12 @@ +import type { AttestationCredentialJSON } from '@ghostfolio/common/interfaces'; + +import { IsObject, IsOptional, IsString } from 'class-validator'; + +export class VerifyAttestationDto { + @IsObject() + credential: AttestationCredentialJSON; + + @IsOptional() + @IsString() + deviceName?: string; +} diff --git a/apps/api/src/app/endpoints/auth/verify-authentication.dto.ts b/apps/api/src/app/endpoints/auth/verify-authentication.dto.ts new file mode 100644 index 0000000000..3f643a060a --- /dev/null +++ b/apps/api/src/app/endpoints/auth/verify-authentication.dto.ts @@ -0,0 +1,11 @@ +import type { AssertionCredentialJSON } from '@ghostfolio/common/interfaces'; + +import { IsObject, IsString } from 'class-validator'; + +export class VerifyAuthenticationDto { + @IsObject() + credential: AssertionCredentialJSON; + + @IsString() + deviceId: string; +} diff --git a/apps/api/src/app/endpoints/subscription/create-stripe-checkout-session.dto.ts b/apps/api/src/app/endpoints/subscription/create-stripe-checkout-session.dto.ts new file mode 100644 index 0000000000..4f30eda03b --- /dev/null +++ b/apps/api/src/app/endpoints/subscription/create-stripe-checkout-session.dto.ts @@ -0,0 +1,10 @@ +import { IsOptional, IsString } from 'class-validator'; + +export class CreateStripeCheckoutSessionDto { + @IsOptional() + @IsString() + couponId?: string; + + @IsString() + priceId: string; +} diff --git a/apps/api/src/app/endpoints/subscription/redeem-coupon.dto.ts b/apps/api/src/app/endpoints/subscription/redeem-coupon.dto.ts new file mode 100644 index 0000000000..dc375a5c20 --- /dev/null +++ b/apps/api/src/app/endpoints/subscription/redeem-coupon.dto.ts @@ -0,0 +1,6 @@ +import { IsString } from 'class-validator'; + +export class RedeemCouponDto { + @IsString() + couponCode: string; +} diff --git a/apps/api/src/app/endpoints/subscription/subscription.controller.ts b/apps/api/src/app/endpoints/subscription/subscription.controller.ts index 0d989265a7..cfeb33aabf 100644 --- a/apps/api/src/app/endpoints/subscription/subscription.controller.ts +++ b/apps/api/src/app/endpoints/subscription/subscription.controller.ts @@ -33,6 +33,9 @@ import { AuthGuard } from '@nestjs/passport'; import { Request, Response } from 'express'; import { StatusCodes, getReasonPhrase } from 'http-status-codes'; +import { CreateStripeCheckoutSessionDto } from './create-stripe-checkout-session.dto'; +import { RedeemCouponDto } from './redeem-coupon.dto'; + @AllowDuringImpersonation() @Controller('subscription') export class SubscriptionController { @@ -49,7 +52,7 @@ export class SubscriptionController { @HttpCode(StatusCodes.OK) @UseGuards(AuthGuard('jwt'), CustomThrottlerGuard, HasPermissionGuard) public async redeemCoupon( - @Body() { couponCode }: { couponCode: string } + @Body() { couponCode }: RedeemCouponDto ): Promise { if (!this.request.user) { throw new HttpException( @@ -124,7 +127,7 @@ export class SubscriptionController { @Post('stripe/checkout-session') @UseGuards(AuthGuard('jwt'), CustomThrottlerGuard, HasPermissionGuard) public async createStripeCheckoutSession( - @Body() { couponId, priceId }: { couponId?: string; priceId: string } + @Body() { couponId, priceId }: CreateStripeCheckoutSessionDto ): Promise { try { return await this.subscriptionService.createStripeCheckoutSession({