From fb2db19de05dabe97a6413e395d0350381ca2bdf Mon Sep 17 00:00:00 2001 From: Mais Date: Fri, 24 Nov 2023 12:56:25 +0300 Subject: [PATCH] fix: throw error when the URI is invalid --- firestore-send-email/functions/src/helpers.ts | 41 +++++++++++-------- firestore-send-email/functions/src/index.ts | 5 +-- firestore-send-email/functions/src/logs.ts | 4 +- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/firestore-send-email/functions/src/helpers.ts b/firestore-send-email/functions/src/helpers.ts index 674476448..bea8ffb0b 100644 --- a/firestore-send-email/functions/src/helpers.ts +++ b/firestore-send-email/functions/src/helpers.ts @@ -1,7 +1,8 @@ -import { createTransport } from "nodemailer"; +import { createTransport, Transporter } from "nodemailer"; import { URL } from "url"; import { invalidURI } from "./logs"; import { Config } from "./types"; +import { logger } from "firebase-functions/v1"; function compileUrl($: string): URL | null { try { @@ -17,9 +18,23 @@ function checkMicrosoftServer($: string): boolean { ); } -export const setSmtpCredentials = (config: Config) => { +export function parseTlsOptions(tlsOptions: string) { + let tls = { rejectUnauthorized: false }; + + try { + tls = JSON.parse(tlsOptions); + } catch (ex) { + logger.warn( + "Invalid TLS options provided, using default TLS options instead: `{ rejectUnauthorized: false }`" + ); + } + + return tls; +} + +export function setSmtpCredentials(config: Config): Transporter { let url: URL; - let transport; + let transport: Transporter; const { smtpConnectionUri, smtpPassword } = config; @@ -28,9 +43,10 @@ export const setSmtpCredentials = (config: Config) => { /** return null if invalid url */ if (!url) { - invalidURI(smtpConnectionUri); - - return null; + invalidURI(); + throw new Error( + `Invalid URI: please reconfigure with a valid SMTP connection URI` + ); } /** encode uri password if exists */ @@ -59,15 +75,4 @@ export const setSmtpCredentials = (config: Config) => { } return transport; -}; - -export const parseTlsOptions = (tlsOptions: string) => { - let tls = { rejectUnauthorized: false }; - try { - tls = JSON.parse(tlsOptions); - } catch (ex) { - console.log("Error parsing tls options, invalid JSON: " + ex); - } - - return tls; -}; +} diff --git a/firestore-send-email/functions/src/index.ts b/firestore-send-email/functions/src/index.ts index 3471f15d3..d2eb0fcfd 100644 --- a/firestore-send-email/functions/src/index.ts +++ b/firestore-send-email/functions/src/index.ts @@ -52,16 +52,13 @@ async function initialize() { events.setupEventChannel(); } -/** Extract JSON tsl options */ -const tls = parseTlsOptions(config.tls); - async function transportLayer() { if (config.testing) { return nodemailer.createTransport({ host: "127.0.0.1", port: 8132, secure: false, - tls, + tls: parseTlsOptions(config.tls), }); } diff --git a/firestore-send-email/functions/src/logs.ts b/firestore-send-email/functions/src/logs.ts index 3d36f4899..b64e7b8f1 100644 --- a/firestore-send-email/functions/src/logs.ts +++ b/firestore-send-email/functions/src/logs.ts @@ -105,8 +105,8 @@ export function foundMissingTemplate(name) { logger.log(`template '${name}' has been found`); } -export function invalidURI(uri) { +export function invalidURI() { logger.warn( - `invalid url: '${uri}' , please reconfigure with a valid SMTP connection URI` + `Invalid URI: please reconfigure with a valid SMTP connection URI` ); }