@vicacha-devs/medusa-plugin-notifications 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.medusa/server/src/admin/index.cjs +4215 -0
- package/.medusa/server/src/admin/index.js +4193 -0
- package/.medusa/server/src/api/admin/mpn/broadcasts/route.js +99 -0
- package/.medusa/server/src/api/admin/mpn/channels/route.js +109 -0
- package/.medusa/server/src/api/admin/mpn/logs/route.js +33 -0
- package/.medusa/server/src/api/admin/mpn/mappings/[id]/route.js +33 -0
- package/.medusa/server/src/api/admin/mpn/mappings/route.js +51 -0
- package/.medusa/server/src/api/admin/mpn/render-template/route.js +37 -0
- package/.medusa/server/src/api/admin/mpn/sandbox/route.js +36 -0
- package/.medusa/server/src/api/admin/mpn/send/route.js +135 -0
- package/.medusa/server/src/api/admin/mpn/templates/[id]/route.js +77 -0
- package/.medusa/server/src/api/admin/mpn/templates/route.js +71 -0
- package/.medusa/server/src/api/admin/plugin/route.js +4 -0
- package/.medusa/server/src/api/middlewares.js +28 -0
- package/.medusa/server/src/api/store/mpn/consent/route.js +37 -0
- package/.medusa/server/src/api/store/mpn/webhooks/[provider]/route.js +97 -0
- package/.medusa/server/src/api/store/plugin/route.js +4 -0
- package/.medusa/server/src/api/validators.js +118 -0
- package/.medusa/server/src/hooks/index.js +7 -0
- package/.medusa/server/src/hooks/use-mpn-channels.js +13 -0
- package/.medusa/server/src/hooks/use-mpn-dispatch.js +45 -0
- package/.medusa/server/src/hooks/use-mpn-mappings.js +21 -0
- package/.medusa/server/src/hooks/use-mpn-templates.js +34 -0
- package/.medusa/server/src/hooks/use-order-notifications-widget-state.js +79 -0
- package/.medusa/server/src/hooks/use-sdk-primitives.js +113 -0
- package/.medusa/server/src/index.js +14 -0
- package/.medusa/server/src/integrations/automations-action-handler.js +112 -0
- package/.medusa/server/src/jobs/abandoned-cart-recovery.js +123 -0
- package/.medusa/server/src/jobs/log-retention-and-broadcasts.js +15 -0
- package/.medusa/server/src/modules/notifications/i18n/index.js +65 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/ar.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/de.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/en.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/es.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/fr.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/it.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/nl.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/pl.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/pt.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/tr.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/types.js +2 -0
- package/.medusa/server/src/modules/notifications/index.js +9 -0
- package/.medusa/server/src/modules/notifications/integrations/documents-and-b2b-resolver.js +129 -0
- package/.medusa/server/src/modules/notifications/migrations/Migration20261010000000.js +184 -0
- package/.medusa/server/src/modules/notifications/models/index.js +118 -0
- package/.medusa/server/src/modules/notifications/renderer/block-helpers.js +36 -0
- package/.medusa/server/src/modules/notifications/renderer/email.js +161 -0
- package/.medusa/server/src/modules/notifications/renderer/index.js +240 -0
- package/.medusa/server/src/modules/notifications/renderer/slack.js +114 -0
- package/.medusa/server/src/modules/notifications/renderer/telegram.js +99 -0
- package/.medusa/server/src/modules/notifications/renderer/whatsapp.js +91 -0
- package/.medusa/server/src/modules/notifications/service-helpers/channel-config.js +45 -0
- package/.medusa/server/src/modules/notifications/service-helpers/consent.js +65 -0
- package/.medusa/server/src/modules/notifications/service-helpers/delivery-log.js +29 -0
- package/.medusa/server/src/modules/notifications/service-helpers/event-conditions.js +46 -0
- package/.medusa/server/src/modules/notifications/service-helpers/external-templates.js +80 -0
- package/.medusa/server/src/modules/notifications/service-helpers/recipient-resolution.js +85 -0
- package/.medusa/server/src/modules/notifications/service-helpers/sender-profiles.js +29 -0
- package/.medusa/server/src/modules/notifications/service-helpers/template-blocks.js +81 -0
- package/.medusa/server/src/modules/notifications/service-helpers/template-catalog.js +129 -0
- package/.medusa/server/src/modules/notifications/service-helpers/template-render.js +65 -0
- package/.medusa/server/src/modules/notifications/service.js +340 -0
- package/.medusa/server/src/modules/notifications/types.js +2 -0
- package/.medusa/server/src/modules/notifications/utils/auth-presets.js +140 -0
- package/.medusa/server/src/modules/notifications/utils/interpolation.js +178 -0
- package/.medusa/server/src/modules/notifications/utils/security.js +312 -0
- package/.medusa/server/src/modules/notifications/utils/system-presets.js +651 -0
- package/.medusa/server/src/modules/notifications/utils/transformers.js +358 -0
- package/.medusa/server/src/providers/dummy-sandbox/index.js +96 -0
- package/.medusa/server/src/providers/email-smtp/index.js +187 -0
- package/.medusa/server/src/providers/index.js +7 -0
- package/.medusa/server/src/providers/slack/index.js +54 -0
- package/.medusa/server/src/providers/sms/index.js +254 -0
- package/.medusa/server/src/providers/telegram/index.js +119 -0
- package/.medusa/server/src/providers/whatsapp/index.js +371 -0
- package/.medusa/server/src/rbac.js +109 -0
- package/.medusa/server/src/sdk/client.js +186 -0
- package/.medusa/server/src/sdk/index.js +3 -0
- package/.medusa/server/src/sdk/query-keys.js +61 -0
- package/.medusa/server/src/subscribers/customer-and-document-events.js +33 -0
- package/.medusa/server/src/subscribers/fulfillment-and-inventory-events.js +30 -0
- package/.medusa/server/src/subscribers/order-events.js +28 -0
- package/.medusa/server/src/types/constants.js +129 -0
- package/.medusa/server/src/types/core.js +2 -0
- package/.medusa/server/src/types/domain.js +2 -0
- package/.medusa/server/src/types/enums.js +53 -0
- package/.medusa/server/src/types/errors.js +28 -0
- package/.medusa/server/src/types/hooks.js +2 -0
- package/.medusa/server/src/types/index.js +12 -0
- package/.medusa/server/src/types/providers.js +2 -0
- package/.medusa/server/src/types/records.js +2 -0
- package/.medusa/server/src/types/render.js +2 -0
- package/.medusa/server/src/types/sdk-api.js +2 -0
- package/.medusa/server/src/types/translations.js +2 -0
- package/.medusa/server/src/workflows/index.js +461 -0
- package/LICENSE +21 -0
- package/README.md +389 -2
- package/package.json +133 -3
- package/src/admin/README.md +123 -0
- package/src/admin/components/mpn/broadcast-consent-section.tsx +62 -0
- package/src/admin/components/mpn/broadcasts-tab.tsx +208 -0
- package/src/admin/components/mpn/channels-main-section.tsx +205 -0
- package/src/admin/components/mpn/channels-side-section.tsx +88 -0
- package/src/admin/components/mpn/channels-tab.tsx +130 -0
- package/src/admin/components/mpn/constants.ts +8 -0
- package/src/admin/components/mpn/delivery-logs-tab.tsx +70 -0
- package/src/admin/components/mpn/event-mapping-form.tsx +191 -0
- package/src/admin/components/mpn/event-mappings-tab.tsx +165 -0
- package/src/admin/components/mpn/header-bar.tsx +35 -0
- package/src/admin/components/mpn/index.ts +17 -0
- package/src/admin/components/mpn/overview-cards.tsx +103 -0
- package/src/admin/components/mpn/sandbox-tab.tsx +93 -0
- package/src/admin/components/mpn/template-editor-section.tsx +203 -0
- package/src/admin/components/mpn/template-preview-section.tsx +122 -0
- package/src/admin/components/mpn/templates-list-section.tsx +115 -0
- package/src/admin/components/mpn/templates-tab.tsx +188 -0
- package/src/admin/components/mpn/use-notification-studio.ts +84 -0
- package/src/admin/components/rbac-guard.tsx +153 -0
- package/src/admin/i18n/README.md +134 -0
- package/src/admin/i18n/index.ts +23 -0
- package/src/admin/i18n/json/ar.json +157 -0
- package/src/admin/i18n/json/de.json +157 -0
- package/src/admin/i18n/json/en.json +157 -0
- package/src/admin/i18n/json/es.json +157 -0
- package/src/admin/i18n/json/fr.json +157 -0
- package/src/admin/i18n/json/it.json +157 -0
- package/src/admin/i18n/json/nl.json +157 -0
- package/src/admin/i18n/json/pl.json +157 -0
- package/src/admin/i18n/json/pt.json +157 -0
- package/src/admin/i18n/json/tr.json +157 -0
- package/src/admin/routes/mpn/broadcasts/page.tsx +75 -0
- package/src/admin/routes/mpn/channels/page.tsx +66 -0
- package/src/admin/routes/mpn/logs/page.tsx +50 -0
- package/src/admin/routes/mpn/mappings/page.tsx +62 -0
- package/src/admin/routes/mpn/page.tsx +200 -0
- package/src/admin/routes/mpn/sandbox/page.tsx +53 -0
- package/src/admin/routes/mpn/templates/page.tsx +55 -0
- package/src/admin/tsconfig.json +23 -0
- package/src/admin/vite-env.d.ts +1 -0
- package/src/admin/widgets/order-notifications-widget.tsx +178 -0
- package/src/api/README.md +98 -0
- package/src/api/admin/mpn/broadcasts/route.ts +130 -0
- package/src/api/admin/mpn/channels/route.ts +143 -0
- package/src/api/admin/mpn/logs/route.ts +41 -0
- package/src/api/admin/mpn/mappings/[id]/route.ts +45 -0
- package/src/api/admin/mpn/mappings/route.ts +68 -0
- package/src/api/admin/mpn/render-template/route.ts +46 -0
- package/src/api/admin/mpn/sandbox/route.ts +51 -0
- package/src/api/admin/mpn/send/route.ts +163 -0
- package/src/api/admin/mpn/templates/[id]/route.ts +101 -0
- package/src/api/admin/mpn/templates/route.ts +99 -0
- package/src/api/admin/plugin/route.ts +5 -0
- package/src/api/middlewares.ts +42 -0
- package/src/api/store/mpn/consent/route.ts +45 -0
- package/src/api/store/mpn/webhooks/[provider]/route.ts +119 -0
- package/src/api/store/plugin/route.ts +5 -0
- package/src/api/validators.ts +149 -0
- package/src/hooks/index.ts +14 -0
- package/src/hooks/use-mpn-channels.ts +43 -0
- package/src/hooks/use-mpn-dispatch.ts +149 -0
- package/src/hooks/use-mpn-mappings.ts +75 -0
- package/src/hooks/use-mpn-templates.ts +120 -0
- package/src/hooks/use-order-notifications-widget-state.ts +103 -0
- package/src/hooks/use-sdk-primitives.ts +157 -0
- package/src/index.ts +16 -0
- package/src/integrations/automations-action-handler.ts +156 -0
- package/src/jobs/README.md +41 -0
- package/src/jobs/abandoned-cart-recovery.ts +152 -0
- package/src/jobs/log-retention-and-broadcasts.ts +23 -0
- package/src/links/README.md +52 -0
- package/src/modules/README.md +99 -0
- package/src/modules/notifications/i18n/index.ts +83 -0
- package/src/modules/notifications/i18n/locales/ar.ts +84 -0
- package/src/modules/notifications/i18n/locales/de.ts +83 -0
- package/src/modules/notifications/i18n/locales/en.ts +84 -0
- package/src/modules/notifications/i18n/locales/es.ts +84 -0
- package/src/modules/notifications/i18n/locales/fr.ts +82 -0
- package/src/modules/notifications/i18n/locales/it.ts +82 -0
- package/src/modules/notifications/i18n/locales/nl.ts +83 -0
- package/src/modules/notifications/i18n/locales/pl.ts +84 -0
- package/src/modules/notifications/i18n/locales/pt.ts +82 -0
- package/src/modules/notifications/i18n/locales/tr.ts +86 -0
- package/src/modules/notifications/i18n/types.ts +1 -0
- package/src/modules/notifications/index.ts +10 -0
- package/src/modules/notifications/integrations/documents-and-b2b-resolver.ts +168 -0
- package/src/modules/notifications/migrations/Migration20261010000000.ts +185 -0
- package/src/modules/notifications/models/index.ts +125 -0
- package/src/modules/notifications/renderer/block-helpers.ts +51 -0
- package/src/modules/notifications/renderer/email.ts +281 -0
- package/src/modules/notifications/renderer/index.ts +301 -0
- package/src/modules/notifications/renderer/slack.ts +168 -0
- package/src/modules/notifications/renderer/telegram.ts +161 -0
- package/src/modules/notifications/renderer/whatsapp.ts +156 -0
- package/src/modules/notifications/service-helpers/channel-config.ts +65 -0
- package/src/modules/notifications/service-helpers/consent.ts +88 -0
- package/src/modules/notifications/service-helpers/delivery-log.ts +56 -0
- package/src/modules/notifications/service-helpers/event-conditions.ts +66 -0
- package/src/modules/notifications/service-helpers/external-templates.ts +127 -0
- package/src/modules/notifications/service-helpers/recipient-resolution.ts +124 -0
- package/src/modules/notifications/service-helpers/sender-profiles.ts +34 -0
- package/src/modules/notifications/service-helpers/template-blocks.ts +116 -0
- package/src/modules/notifications/service-helpers/template-catalog.ts +170 -0
- package/src/modules/notifications/service-helpers/template-render.ts +113 -0
- package/src/modules/notifications/service.ts +589 -0
- package/src/modules/notifications/types.ts +1 -0
- package/src/modules/notifications/utils/auth-presets.ts +145 -0
- package/src/modules/notifications/utils/interpolation.ts +231 -0
- package/src/modules/notifications/utils/security.ts +378 -0
- package/src/modules/notifications/utils/system-presets.ts +695 -0
- package/src/modules/notifications/utils/transformers.ts +456 -0
- package/src/providers/README.md +92 -0
- package/src/providers/dummy-sandbox/index.ts +143 -0
- package/src/providers/email-smtp/index.ts +257 -0
- package/src/providers/index.ts +38 -0
- package/src/providers/slack/index.ts +82 -0
- package/src/providers/sms/index.ts +342 -0
- package/src/providers/telegram/index.ts +181 -0
- package/src/providers/whatsapp/index.ts +505 -0
- package/src/rbac.ts +123 -0
- package/src/sdk/client.ts +319 -0
- package/src/sdk/index.ts +48 -0
- package/src/sdk/query-keys.ts +89 -0
- package/src/subscribers/README.md +50 -0
- package/src/subscribers/customer-and-document-events.ts +36 -0
- package/src/subscribers/fulfillment-and-inventory-events.ts +35 -0
- package/src/subscribers/order-events.ts +31 -0
- package/src/types/constants.ts +143 -0
- package/src/types/core.ts +96 -0
- package/src/types/domain.ts +84 -0
- package/src/types/enums.ts +73 -0
- package/src/types/errors.ts +32 -0
- package/src/types/hooks.ts +48 -0
- package/src/types/index.ts +11 -0
- package/src/types/providers.ts +96 -0
- package/src/types/records.ts +100 -0
- package/src/types/render.ts +74 -0
- package/src/types/sdk-api.ts +228 -0
- package/src/types/translations.ts +75 -0
- package/src/workflows/README.md +67 -0
- package/src/workflows/index.ts +619 -0
- package/tsconfig.json +40 -0
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { createHash, createHmac, timingSafeEqual } from "node:crypto"
|
|
2
|
+
|
|
3
|
+
const E164_PHONE_REGEX = /^\+[1-9]\d{6,14}$/
|
|
4
|
+
const TELEGRAM_CHANNEL_OR_CHAT_REGEX =
|
|
5
|
+
/^(-100\d{7,15}|-\d{5,15}|@[a-zA-Z0-9_]{5,32}|\d{5,15})$/
|
|
6
|
+
|
|
7
|
+
const SENSITIVE_FIELD_PATTERNS =
|
|
8
|
+
/(token|secret|password|pass|authorization|api[-_]?key|private[-_]?key|signature|otp|cookie|session)/i
|
|
9
|
+
|
|
10
|
+
const GSM7_BASIC_CHARS =
|
|
11
|
+
"@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ\x1BÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà"
|
|
12
|
+
const GSM7_EXTENDED_CHARS = "^{}\\[~]|€"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Normalizes a phone string into strict E.164 format (`+1234567890`).
|
|
16
|
+
* Strips spaces, dashes, and parentheses; converts leading `00` to `+`.
|
|
17
|
+
*/
|
|
18
|
+
export function normalizeE164Phone(input?: string | null): string | null {
|
|
19
|
+
if (!input || typeof input !== "string") return null
|
|
20
|
+
let cleaned = input.trim().replace(/[\s\-().]/g, "")
|
|
21
|
+
if (cleaned.startsWith("00")) {
|
|
22
|
+
cleaned = `+${cleaned.slice(2)}`
|
|
23
|
+
} else if (!cleaned.startsWith("+") && /^[1-9]\d{7,14}$/.test(cleaned)) {
|
|
24
|
+
cleaned = `+${cleaned}`
|
|
25
|
+
}
|
|
26
|
+
return E164_PHONE_REGEX.test(cleaned) ? cleaned : null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function isValidE164Phone(input?: string | null): boolean {
|
|
30
|
+
if (!input || typeof input !== "string") return false
|
|
31
|
+
return E164_PHONE_REGEX.test(input.trim())
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Meta WhatsApp Cloud API requires digits only (country code + subscriber number)
|
|
36
|
+
* without the leading `+` so messages are not silently dropped after HTTP 200.
|
|
37
|
+
*/
|
|
38
|
+
export function toWhatsAppDigitsOnly(input?: string | null): string | null {
|
|
39
|
+
const e164 = normalizeE164Phone(input)
|
|
40
|
+
if (!e164) return null
|
|
41
|
+
return e164.slice(1)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Validates Telegram recipient:
|
|
46
|
+
* - Supergroup/Channel ID (`-1001234567890`)
|
|
47
|
+
* - Public channel handle (`@my_store_alerts`)
|
|
48
|
+
* - Direct user/bot chat ID (`123456789`)
|
|
49
|
+
*/
|
|
50
|
+
export function isValidTelegramRecipient(input?: string | null): boolean {
|
|
51
|
+
if (!input || typeof input !== "string") return false
|
|
52
|
+
return TELEGRAM_CHANNEL_OR_CHAT_REGEX.test(input.trim())
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Masks PII (email addresses, phone numbers, chat IDs) before persisting to logs
|
|
57
|
+
* or displaying in unprivileged UI labels.
|
|
58
|
+
*/
|
|
59
|
+
export function maskRecipientPii(recipient?: string | null): string {
|
|
60
|
+
if (!recipient || typeof recipient !== "string") return "***"
|
|
61
|
+
const trimmed = recipient.trim()
|
|
62
|
+
if (trimmed.includes("@") && !trimmed.startsWith("@")) {
|
|
63
|
+
const [local, domain] = trimmed.split("@")
|
|
64
|
+
if (!local || !domain) return "***@***"
|
|
65
|
+
const maskedLocal =
|
|
66
|
+
local.length <= 2
|
|
67
|
+
? `${local[0] || "*"}*`
|
|
68
|
+
: `${local[0]}***${local[local.length - 1]}`
|
|
69
|
+
return `${maskedLocal}@${domain}`
|
|
70
|
+
}
|
|
71
|
+
if (trimmed.startsWith("@")) {
|
|
72
|
+
return trimmed
|
|
73
|
+
}
|
|
74
|
+
if (trimmed.length > 4) {
|
|
75
|
+
return `***-***-${trimmed.slice(-4)}`
|
|
76
|
+
}
|
|
77
|
+
return "***"
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function hashRecipient(recipient: string): string {
|
|
81
|
+
return createHash("sha256")
|
|
82
|
+
.update(recipient.trim().toLowerCase())
|
|
83
|
+
.digest("hex")
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Redacts secrets, tokens, OTP codes, and large base64 attachment buffers from payload objects.
|
|
88
|
+
*/
|
|
89
|
+
export function redactSensitivePayload(value: unknown, depth = 0): unknown {
|
|
90
|
+
if (depth > 8) return "[Truncated]"
|
|
91
|
+
if (value === null || value === undefined) return value
|
|
92
|
+
if (Array.isArray(value)) {
|
|
93
|
+
return value.map((item) => redactSensitivePayload(item, depth + 1))
|
|
94
|
+
}
|
|
95
|
+
if (typeof value === "object") {
|
|
96
|
+
const out: Record<string, unknown> = {}
|
|
97
|
+
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
|
|
98
|
+
if (SENSITIVE_FIELD_PATTERNS.test(k)) {
|
|
99
|
+
out[k] = "[REDACTED]"
|
|
100
|
+
} else if (k === "content" && typeof v === "string" && v.length > 256) {
|
|
101
|
+
out[k] = `[REDACTED_BASE64_${v.length}_CHARS]`
|
|
102
|
+
} else if (
|
|
103
|
+
(k === "to" || k === "recipient" || k === "phone") &&
|
|
104
|
+
typeof v === "string"
|
|
105
|
+
) {
|
|
106
|
+
out[k] = maskRecipientPii(v)
|
|
107
|
+
} else {
|
|
108
|
+
out[k] = redactSensitivePayload(v, depth + 1)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return out
|
|
112
|
+
}
|
|
113
|
+
return value
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function escapeHtml(input: unknown): string {
|
|
117
|
+
if (input === null || input === undefined) return ""
|
|
118
|
+
return String(input)
|
|
119
|
+
.replace(/&/g, "&")
|
|
120
|
+
.replace(/</g, "<")
|
|
121
|
+
.replace(/>/g, ">")
|
|
122
|
+
.replace(/"/g, """)
|
|
123
|
+
.replace(/'/g, "'")
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Sanitizes custom HTML templates by stripping executable tags (<script>, <iframe>, <object>, <embed>),
|
|
128
|
+
* inline event handlers (onload=, onerror=, onclick=), and dangerous URI schemes (javascript:, data:text/html).
|
|
129
|
+
*/
|
|
130
|
+
export function sanitizeNotificationHtml(html: string): string {
|
|
131
|
+
if (!html || typeof html !== "string") return ""
|
|
132
|
+
return html
|
|
133
|
+
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "")
|
|
134
|
+
.replace(/<iframe\b[^<]*(?:(?!<\/iframe>)<[^<]*)*<\/iframe>/gi, "")
|
|
135
|
+
.replace(/<object\b[^<]*(?:(?!<\/object>)<[^<]*)*<\/object>/gi, "")
|
|
136
|
+
.replace(/<embed\b[^>]*>/gi, "")
|
|
137
|
+
.replace(/\son[a-z]+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/gi, "")
|
|
138
|
+
.replace(
|
|
139
|
+
/(href|src|action)\s*=\s*(['"]?)\s*(?:javascript|vbscript|data\s*:\s*text\/html)[^'"\s>]*\2/gi,
|
|
140
|
+
'$1="#"'
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Escapes untrusted text for Telegram HTML parse_mode while optionally preserving
|
|
146
|
+
* safe formatting tags (`<b>`, `<i>`, `<u>`, `<s>`, `<code>`, `<pre>`, `<a href="https://...">`).
|
|
147
|
+
*/
|
|
148
|
+
export function sanitizeTelegramHtml(input: string): string {
|
|
149
|
+
if (!input || typeof input !== "string") return ""
|
|
150
|
+
const placeholders: string[] = []
|
|
151
|
+
const withPlaceholders = input.replace(
|
|
152
|
+
/<(\/(?:b|strong|i|em|u|ins|s|strike|del|code|pre|a)|(?:b|strong|i|em|u|ins|s|strike|del|code|pre)|a\s+href="https?:\/\/[^"<>\s]+")>/gi,
|
|
153
|
+
(match) => {
|
|
154
|
+
const idx = placeholders.length
|
|
155
|
+
placeholders.push(match)
|
|
156
|
+
return `\u0000TG_TAG_${idx}\u0000`
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
const escaped = escapeHtml(withPlaceholders)
|
|
160
|
+
return escaped.replace(/\u0000TG_TAG_(\d+)\u0000/g, (_, idxStr) => {
|
|
161
|
+
return placeholders[Number(idxStr)] || ""
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Validates external URLs (e.g., OpenWA/WAHA gateway URL, MMS mediaUrl, webhook target)
|
|
167
|
+
* to prevent Server-Side Request Forgery (SSRF / CWE-918).
|
|
168
|
+
*/
|
|
169
|
+
export function validateSafeExternalUrl(
|
|
170
|
+
rawUrl: string,
|
|
171
|
+
options?: { allowLocalhost?: boolean }
|
|
172
|
+
): { valid: boolean; reason?: string; parsed?: URL } {
|
|
173
|
+
if (!rawUrl || typeof rawUrl !== "string") {
|
|
174
|
+
return { valid: false, reason: "URL is empty" }
|
|
175
|
+
}
|
|
176
|
+
let parsed: URL
|
|
177
|
+
try {
|
|
178
|
+
parsed = new URL(rawUrl.trim())
|
|
179
|
+
} catch {
|
|
180
|
+
return { valid: false, reason: "Malformed URL" }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
|
|
184
|
+
return {
|
|
185
|
+
valid: false,
|
|
186
|
+
reason: `Disallowed URL protocol: ${parsed.protocol}`,
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const host = parsed.hostname.toLowerCase().replace(/^\[|\]$/g, "")
|
|
191
|
+
const allowLocalhost = Boolean(options?.allowLocalhost)
|
|
192
|
+
|
|
193
|
+
if (
|
|
194
|
+
host === "localhost" ||
|
|
195
|
+
host === "127.0.0.1" ||
|
|
196
|
+
host === "::1" ||
|
|
197
|
+
host === "0.0.0.0"
|
|
198
|
+
) {
|
|
199
|
+
if (!allowLocalhost) {
|
|
200
|
+
return { valid: false, reason: "Loopback addresses are not permitted" }
|
|
201
|
+
}
|
|
202
|
+
return { valid: true, parsed }
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Block cloud metadata & private RFC1918 IPv4 ranges
|
|
206
|
+
if (
|
|
207
|
+
host === "169.254.169.254" ||
|
|
208
|
+
host === "metadata.google.internal" ||
|
|
209
|
+
host.startsWith("169.254.") ||
|
|
210
|
+
host.startsWith("10.") ||
|
|
211
|
+
host.startsWith("192.168.") ||
|
|
212
|
+
/^172\.(1[6-9]|2\d|3[0-1])\./.test(host) ||
|
|
213
|
+
host.startsWith("fc") ||
|
|
214
|
+
host.startsWith("fd") ||
|
|
215
|
+
host.startsWith("fe80:")
|
|
216
|
+
) {
|
|
217
|
+
return {
|
|
218
|
+
valid: false,
|
|
219
|
+
reason:
|
|
220
|
+
"Private network and cloud metadata hosts are blocked (SSRF guard)",
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return { valid: true, parsed }
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Verifies HMAC-SHA256 webhook signatures using constant-time comparison (`crypto.timingSafeEqual`).
|
|
229
|
+
*/
|
|
230
|
+
export function verifyWebhookHmacSignature(
|
|
231
|
+
rawBody: string | Buffer,
|
|
232
|
+
signatureHeader: string | undefined | null,
|
|
233
|
+
secret: string | undefined | null
|
|
234
|
+
): boolean {
|
|
235
|
+
if (!rawBody || !signatureHeader || !secret) {
|
|
236
|
+
return false
|
|
237
|
+
}
|
|
238
|
+
const cleanHeader = signatureHeader
|
|
239
|
+
.trim()
|
|
240
|
+
.replace(/^sha256=/i, "")
|
|
241
|
+
.replace(/^v1=/i, "")
|
|
242
|
+
|
|
243
|
+
const expectedHex = createHmac("sha256", secret)
|
|
244
|
+
.update(typeof rawBody === "string" ? rawBody : new Uint8Array(rawBody))
|
|
245
|
+
.digest("hex")
|
|
246
|
+
|
|
247
|
+
const expectedBuf = new Uint8Array(Buffer.from(expectedHex, "utf8"))
|
|
248
|
+
const actualBuf = new Uint8Array(Buffer.from(cleanHeader, "utf8"))
|
|
249
|
+
|
|
250
|
+
if (expectedBuf.length !== actualBuf.length) {
|
|
251
|
+
return false
|
|
252
|
+
}
|
|
253
|
+
return timingSafeEqual(expectedBuf, actualBuf)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Calculates SMS encoding (GSM-7 vs UCS-2), septet/character count, and multipart segment count.
|
|
258
|
+
*/
|
|
259
|
+
export function calculateSmsSegments(text: string): {
|
|
260
|
+
encoding: "GSM-7" | "UCS-2"
|
|
261
|
+
character_count: number
|
|
262
|
+
segments: number
|
|
263
|
+
} {
|
|
264
|
+
if (!text) {
|
|
265
|
+
return { encoding: "GSM-7", character_count: 0, segments: 0 }
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
let isGsm7 = true
|
|
269
|
+
let gsm7Septets = 0
|
|
270
|
+
|
|
271
|
+
for (const char of text) {
|
|
272
|
+
if (GSM7_BASIC_CHARS.includes(char)) {
|
|
273
|
+
gsm7Septets += 1
|
|
274
|
+
} else if (GSM7_EXTENDED_CHARS.includes(char)) {
|
|
275
|
+
gsm7Septets += 2
|
|
276
|
+
} else {
|
|
277
|
+
isGsm7 = false
|
|
278
|
+
break
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (isGsm7) {
|
|
283
|
+
const segments =
|
|
284
|
+
gsm7Septets === 0
|
|
285
|
+
? 0
|
|
286
|
+
: gsm7Septets <= 160
|
|
287
|
+
? 1
|
|
288
|
+
: Math.ceil(gsm7Septets / 153)
|
|
289
|
+
return {
|
|
290
|
+
encoding: "GSM-7",
|
|
291
|
+
character_count: gsm7Septets,
|
|
292
|
+
segments,
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const ucs2Length = Array.from(text).length
|
|
297
|
+
const segments =
|
|
298
|
+
ucs2Length === 0 ? 0 : ucs2Length <= 70 ? 1 : Math.ceil(ucs2Length / 67)
|
|
299
|
+
return {
|
|
300
|
+
encoding: "UCS-2",
|
|
301
|
+
character_count: ucs2Length,
|
|
302
|
+
segments,
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Extracts a 4 to 8 digit OTP verification code from a message body for the Dummy Sandbox UI.
|
|
308
|
+
*/
|
|
309
|
+
export function extractOtpCode(text?: string | null): string | null {
|
|
310
|
+
if (!text || typeof text !== "string") return null
|
|
311
|
+
const match = text.match(/\b(\d{4,8})\b/)
|
|
312
|
+
return match ? match[1] : null
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Builds a deterministic idempotency key for a notification event dispatch.
|
|
317
|
+
*/
|
|
318
|
+
export function buildIdempotencyKey(params: {
|
|
319
|
+
event_name: string
|
|
320
|
+
entity_id?: string | null
|
|
321
|
+
channel: string
|
|
322
|
+
template_id: string
|
|
323
|
+
recipient: string
|
|
324
|
+
}): string {
|
|
325
|
+
const raw = [
|
|
326
|
+
params.event_name.trim(),
|
|
327
|
+
(params.entity_id || "global").trim(),
|
|
328
|
+
params.channel.trim(),
|
|
329
|
+
params.template_id.trim(),
|
|
330
|
+
params.recipient.trim().toLowerCase(),
|
|
331
|
+
].join(":")
|
|
332
|
+
return createHash("sha256").update(raw).digest("hex")
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Non-dropping token-bucket / sliding-window rate limiter calculator.
|
|
337
|
+
* Returns `delayMs = 0` when immediate send is allowed, or the exact millisecond delay
|
|
338
|
+
* required before sending (e.g., Telegram 30 msg/sec global + 1 msg/sec per chat).
|
|
339
|
+
*/
|
|
340
|
+
export class ChannelRateLimiter {
|
|
341
|
+
private nextAvailableGlobalMs = 0
|
|
342
|
+
private nextAvailablePerRecipientMs = new Map<string, number>()
|
|
343
|
+
|
|
344
|
+
scheduleSend(params: {
|
|
345
|
+
recipient: string
|
|
346
|
+
globalRatePerSec: number
|
|
347
|
+
perRecipientRatePerSec?: number
|
|
348
|
+
nowMs?: number
|
|
349
|
+
}): { allowedImmediately: boolean; delayMs: number; scheduledAtMs: number } {
|
|
350
|
+
const now = params.nowMs ?? Date.now()
|
|
351
|
+
const globalIntervalMs = Math.ceil(
|
|
352
|
+
1000 / Math.max(1, params.globalRatePerSec)
|
|
353
|
+
)
|
|
354
|
+
const recipientIntervalMs = Math.ceil(
|
|
355
|
+
1000 / Math.max(0.1, params.perRecipientRatePerSec ?? 1)
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
const globalReadyAt = Math.max(now, this.nextAvailableGlobalMs)
|
|
359
|
+
const recipientReadyAt = Math.max(
|
|
360
|
+
now,
|
|
361
|
+
this.nextAvailablePerRecipientMs.get(params.recipient) ?? 0
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
const scheduledAtMs = Math.max(globalReadyAt, recipientReadyAt)
|
|
365
|
+
this.nextAvailableGlobalMs = scheduledAtMs + globalIntervalMs
|
|
366
|
+
this.nextAvailablePerRecipientMs.set(
|
|
367
|
+
params.recipient,
|
|
368
|
+
scheduledAtMs + recipientIntervalMs
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
const delayMs = Math.max(0, scheduledAtMs - now)
|
|
372
|
+
return {
|
|
373
|
+
allowedImmediately: delayMs === 0,
|
|
374
|
+
delayMs,
|
|
375
|
+
scheduledAtMs,
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|