Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: error handling to the unsubscribe endpoint

Blue Mousee5bce45e7531
diff --git a/backend/apps/cloud/src/notification-channel/dispatchers/email-channel.service.ts b/backend/apps/cloud/src/notification-channel/dispatchers/email-channel.service.ts
index 532369f8f..aafc2a2e4 100644
--- a/backend/apps/cloud/src/notification-channel/dispatchers/email-channel.service.ts
+++ b/backend/apps/cloud/src/notification-channel/dispatchers/email-channel.service.ts
@@ -8,6 +8,7 @@ import {
} from '../entity/notification-channel.entity'
import { ChannelDispatcher, RenderedAlertMessage } from './types'
import { buildNotificationChannelUnsubscribeUrl } from '../notification-channel.paths'
+import { JWT_ACCESS_TOKEN_SECRET } from '../../common/constants'
const wrapEmailHtml = (
body: string,
@@ -82,13 +83,11 @@ export class EmailChannelService implements ChannelDispatcher {
) {}
private getUnsubscribeSecret(): string {
- const secret =
- this.configService.get<string>('JWT_SECRET') ||
- this.configService.get<string>('JWT_ACCESS_TOKEN_SECRET')
+ const secret = JWT_ACCESS_TOKEN_SECRET
if (!secret) {
throw new Error(
- 'JWT_SECRET or JWT_ACCESS_TOKEN_SECRET is required for email unsubscribe tokens',
+ 'JWT_ACCESS_TOKEN_SECRET is required for email unsubscribe tokens',
)
}
diff --git a/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts b/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts
index 4a0946299..49131c55c 100644
--- a/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts
+++ b/backend/apps/cloud/src/notification-channel/notification-channel.controller.ts
@@ -9,6 +9,7 @@ import {
Post,
Query,
Res,
+ HttpException,
HttpStatus,
NotFoundException,
BadRequestException,
@@ -219,7 +220,21 @@ export class NotificationChannelController {
@Public()
@Get('/unsubscribe/:token')
async unsubscribeEmail(@Param('token') token: string, @Res() res: Response) {
- const channelId = this.emailDispatcher.verifyUnsubscribeToken(token)
+ let channelId: string | null
+ try {
+ channelId = this.emailDispatcher.verifyUnsubscribeToken(token)
+ } catch (reason) {
+ if (reason instanceof Error) {
+ throw new HttpException(
+ {
+ statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
+ message: reason.message,
+ },
+ HttpStatus.INTERNAL_SERVER_ERROR,
+ )
+ }
+ throw reason
+ }
if (channelId) {
await this.channelService.setEmailUnsubscribed(channelId, true)
}
diff --git a/backend/apps/cloud/src/notification-channel/notification-channel.module.ts b/backend/apps/cloud/src/notification-channel/notification-channel.module.ts
index 835442256..56c04bedf 100644
--- a/backend/apps/cloud/src/notification-channel/notification-channel.module.ts
+++ b/backend/apps/cloud/src/notification-channel/notification-channel.module.ts
@@ -1,4 +1,4 @@
-import { forwardRef, Module } from '@nestjs/common'
+import { forwardRef, Module, OnModuleInit } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { NotificationChannel } from './entity/notification-channel.entity'
@@ -21,6 +21,7 @@ import { AppLoggerModule } from '../logger/logger.module'
import { TelegramModule } from '../integrations/telegram/telegram.module'
import { DiscordModule } from '../integrations/discord/discord.module'
import { SlackModule } from '../integrations/slack/slack.module'
+import { JWT_ACCESS_TOKEN_SECRET } from '../common/constants'
const telegramImports =
process.env.ENABLE_TELEGRAM_INTEGRATION === 'true'
@@ -56,4 +57,12 @@ const telegramImports =
ChannelDispatcherService,
],
})
-export class NotificationChannelModule {}
+export class NotificationChannelModule implements OnModuleInit {
+ onModuleInit() {
+ if (!JWT_ACCESS_TOKEN_SECRET) {
+ throw new Error(
+ 'JWT_ACCESS_TOKEN_SECRET must be set for email unsubscribe tokens',
+ )
+ }
+ }
+}
diff --git a/backend/apps/cloud/src/notification-channel/notification-channel.service.ts b/backend/apps/cloud/src/notification-channel/notification-channel.service.ts
index 8509480fe..97ee54acf 100644
--- a/backend/apps/cloud/src/notification-channel/notification-channel.service.ts
+++ b/backend/apps/cloud/src/notification-channel/notification-channel.service.ts
@@ -7,6 +7,7 @@ import {
import { InjectRepository } from '@nestjs/typeorm'
import { EntityManager, In, Repository } from 'typeorm'
import { randomBytes } from 'crypto'
+import { isDeepStrictEqual } from 'util'
import {
NotificationChannel,
@@ -476,24 +477,28 @@ export class NotificationChannelService {
}
: dto.config
- channel.config = this.normaliseConfig(channel.type, config)
- // Re-set isVerified based on type rules
- if (
- channel.type === NotificationChannelType.SLACK ||
- channel.type === NotificationChannelType.DISCORD ||
- channel.type === NotificationChannelType.TELEGRAM ||
- channel.type === NotificationChannelType.WEBPUSH
- ) {
- channel.isVerified = true
- channel.disabledReason = null
- } else if (channel.type === NotificationChannelType.EMAIL) {
- channel.isVerified = false
- channel.verificationToken = randomBytes(24).toString('hex')
- channel.disabledReason = null
- } else if (channel.type === NotificationChannelType.WEBHOOK) {
- channel.isVerified = false
- channel.verificationToken = randomBytes(24).toString('hex')
- channel.disabledReason = null
+ const normalizedConfig = this.normaliseConfig(channel.type, config)
+
+ if (!isDeepStrictEqual(normalizedConfig, channel.config)) {
+ channel.config = normalizedConfig
+ // Re-set isVerified based on type rules
+ if (
+ channel.type === NotificationChannelType.SLACK ||
+ channel.type === NotificationChannelType.DISCORD ||
+ channel.type === NotificationChannelType.TELEGRAM ||
+ channel.type === NotificationChannelType.WEBPUSH
+ ) {
+ channel.isVerified = true
+ channel.disabledReason = null
+ } else if (channel.type === NotificationChannelType.EMAIL) {
+ channel.isVerified = false
+ channel.verificationToken = randomBytes(24).toString('hex')
+ channel.disabledReason = null
+ } else if (channel.type === NotificationChannelType.WEBHOOK) {
+ channel.isVerified = false
+ channel.verificationToken = randomBytes(24).toString('hex')
+ channel.disabledReason = null
+ }
}
}
return this.channelRepository.save(channel)