nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

decorators param for Stripe webhook not works

Open anteqkois opened this issue 1 year ago • 1 comments

I tried to use SkipThrottle decorator, but this don't work and throttling still works.

@Module({
  imports: [
    GolevelupStripeModule.forRootAsync(GolevelupStripeModule, {
      inject: [ConfigService],
      useFactory(configService: ConfigService) {
        return {
          apiVersion: '2024-04-10',
          apiKey: configService.getOrThrow('STRIPE_API_KEY'),
          webhookConfig: {
          decorators: [SkipThrottle()],
            stripeSecrets: {
              account: configService.getOrThrow('STRIPE_WEBHOOK_SECRET'),
              accountTest: configService.getOrThrow('STRIPE_WEBHOOK_SECRET'),
            },
            requestBodyProperty: 'rawBody',
          },
        }
      },
    }),
    ConfigModule,
  ],
  controllers: [paymentsController],
  providers: [StripeService],
  exports: [StripeService],
})
export class PaymentsModule {}

And I get errors:

TypeError: Cannot read properties of undefined (reading 'header')
    at ThrottlerGuard.handleRequest (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected]/node_modules/@nestjs/throttler/src/throttler.guard.ts:175:9)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at ThrottlerGuard.canActivate (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected]/node_modules/@nestjs/throttler/src/throttler.guard.ts:121:9)
    at GuardsConsumer.tryActivate (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@nestjs+platform-expre_35rvlrdz2ifbn6ucpg7pclzyxe/node_modules/@nestjs/core/guards/guards-consumer.js:16:17)
    at canActivateFn (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@nestjs+platform-expre_35rvlrdz2ifbn6ucpg7pclzyxe/node_modules/@nestjs/core/helpers/external-context-creator.js:155:33)
    at target (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@nestjs+platform-expre_35rvlrdz2ifbn6ucpg7pclzyxe/node_modules/@nestjs/core/helpers/external-context-creator.js:73:31)
    at Object.handler (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@nestjs+platform-expre_35rvlrdz2ifbn6ucpg7pclzyxe/node_modules/@nestjs/core/helpers/external-proxy.js:9:24)
    at async Promise.all (index 0)
    at StripeWebhookService.handleWebhook (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected][email protected]/node_modules/@golevelup/nestjs-stripe/src/stripe.module.ts:162:9)
    at StripeWebhookController.handleWebhook (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected][email protected]/node_modules/@golevelup/nestjs-stripe/src/stripe.webhook.controller.ts:33:5)
[Nest] 92586  - 06.06.2024, 23:17:24   ERROR [AllExceptionsFilter] [undefined] undefined - 500
    Internal server error occurred
    User: "unknown"
		TypeError: Cannot read properties of undefined (reading 'header')
    at ThrottlerGuard.handleRequest (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected]/node_modules/@nestjs/throttler/src/throttler.guard.ts:175:9)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at ThrottlerGuard.canActivate (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected]/node_modules/@nestjs/throttler/src/throttler.guard.ts:121:9)
    at GuardsConsumer.tryActivate (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@nestjs+platform-expre_35rvlrdz2ifbn6ucpg7pclzyxe/node_modules/@nestjs/core/guards/guards-consumer.js:16:17)
    at canActivateFn (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@nestjs+platform-expre_35rvlrdz2ifbn6ucpg7pclzyxe/node_modules/@nestjs/core/helpers/external-context-creator.js:155:33)
    at target (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@nestjs+platform-expre_35rvlrdz2ifbn6ucpg7pclzyxe/node_modules/@nestjs/core/helpers/external-context-creator.js:73:31)
    at Object.handler (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@nestjs+platform-expre_35rvlrdz2ifbn6ucpg7pclzyxe/node_modules/@nestjs/core/helpers/external-proxy.js:9:24)
    at async Promise.all (index 0)
    at StripeWebhookService.handleWebhook (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected][email protected]/node_modules/@golevelup/nestjs-stripe/src/stripe.module.ts:162:9)
    at StripeWebhookController.handleWebhook (/Users/.../Code/Projects/me/.../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected][email protected][email protected]/node_modules/@golevelup/nestjs-stripe/src/stripe.webhook.controller.ts:33:5)

anteqkois avatar Jun 06 '24 21:06 anteqkois

I have encountered the same issue. @anteqkois Did you solve it ?

Semenchuk1997 avatar Jun 18 '24 10:06 Semenchuk1997

If you want to bypass any guard/custom interceptor you're responsible for evaluating the execution context. The library provides the functionality but we avoided at all cost injecting any opinionated guard, instead you have full control through the contextType token.

https://github.com/golevelup/nestjs/blob/ce44d4dfaad05333cacd916c95dbf20089c91790/packages/stripe/src/stripe.module.ts#L143

https://docs.nestjs.com/fundamentals/execution-context

underfisk avatar Sep 26 '24 12:09 underfisk

@Semenchuk1997 @anteqkois

Did you guys manage to solve it?

tomast1337 avatar Feb 08 '25 00:02 tomast1337

For those who have the same problem, the trick is to also add decorators for each webhook individually, e.g.:

  @Public()
  @SkipThrottle()
  @StripeWebhookHandler('invoice.payment_succeeded')

gong-it avatar Feb 19 '25 05:02 gong-it

I also got it working like this:

  // Throttler
  ThrottlerModule.forRoot([
    {
      ttl: 60,
      limit: 256, // 256 requests per minute
      skipIf: (context: ExecutionContext): boolean => {
        const contextType = context.getType<'stripe_webhook' | ContextType>();

        if (contextType === 'stripe_webhook') {
          return true;
        }

        return false;
      },
    },
  ]),

tomast1337 avatar Feb 19 '25 23:02 tomast1337