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!

Avoid using email regex

Blue Mouse403350dd695e
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 d3bf6dfb5..75b1b7d19 100644
--- a/backend/apps/cloud/src/notification-channel/notification-channel.service.ts
+++ b/backend/apps/cloud/src/notification-channel/notification-channel.service.ts
@@ -23,7 +23,29 @@ import {
import { WebhookChannelService } from './dispatchers/webhook-channel.service'
const MAX_CHANNELS_PER_SCOPE = 50
-const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
+const MAX_EMAIL_LENGTH = 254
+const MAX_EMAIL_LOCAL_PART_LENGTH = 64
+
+const hasWhitespace = (value: string) => {
+ for (const char of value) {
+ if (char.trim() === '') return true
+ }
+ return false
+}
+
+const isValidEmailAddress = (address: string) => {
+ if (!address || address.length > MAX_EMAIL_LENGTH || hasWhitespace(address)) {
+ return false
+ }
+
+ const atIndex = address.indexOf('@')
+ if (atIndex <= 0 || atIndex !== address.lastIndexOf('@')) return false
+ if (atIndex > MAX_EMAIL_LOCAL_PART_LENGTH) return false
+
+ const domain = address.slice(atIndex + 1)
+ const dotIndex = domain.indexOf('.')
+ return dotIndex > 0 && dotIndex < domain.length - 1
+}
const isValidHttpsUrl = (url: string, allowedHostSuffix: string[] = []) => {
try {
@@ -342,7 +364,7 @@ export class NotificationChannelService {
const address = String(raw.address || '')
.trim()
.toLowerCase()
- if (!EMAIL_REGEX.test(address)) {
+ if (!isValidEmailAddress(address)) {
throw new BadRequestException('Invalid email address')
}
return { address, unsubscribed: false }