Skip to content

Commit

Permalink
Merge pull request #1817 from firebase/fix/send-email-url-validate
Browse files Browse the repository at this point in the history
  • Loading branch information
pr-Mais authored Nov 24, 2023
2 parents 24813d6 + 8ac8477 commit 4516265
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion firestore-send-email/extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ params:
`smtps://username@gmail.com:password@smtp.gmail.com:465`. (username and password)
type: string
example: smtps://username@smtp.hostname.com:465
validationRegex: ^(smtp[s]*://.*?:[0-9]+.*$)
validationRegex: ^(smtp[s]*://(.*?(:[^:@]*)?@)?[^:@]+:[0-9]+(\\?[^ ]*)?)$
validationErrorMessage: Invalid SMTP connection URI. Must be in the form `smtp(s)://username:password@hostname:port` or `smtp(s)://username@hostname:port`.
required: true

Expand Down
54 changes: 53 additions & 1 deletion firestore-send-email/functions/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { Config } from "../src/types";
const { logger } = require("firebase-functions");
const consoleLogSpy = jest.spyOn(logger, "warn").mockImplementation();

// This is a regex to validate the smtpConnectionUri in extension.yaml
const regex = new RegExp(
"^(smtp[s]*://(.*?(:[^:@]*)?@)?[^:@]+:[0-9]+(\\?[^ ]*)?)$"
);

describe("set server credentials helper function", () => {
test(" return smtpServerDomain credentials with new password", () => {
test("return smtpServerDomain credentials with new password", () => {
const config: Config = {
smtpConnectionUri:
"smtps://fakeemail@gmail.com:secret-password@smtp.gmail.com:465",
Expand All @@ -21,6 +26,9 @@ describe("set server credentials helper function", () => {
expect(credentials.options.host).toBe("smtp.gmail.com");
expect(credentials.options.auth.pass).toBe(config.smtpPassword);
expect(credentials.options.secure).toBe(true);

// The regex should match the smtpConnectionUri, it should be valid
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return smtpServerDomain credentials with new password (old deleted)", () => {
Expand All @@ -37,6 +45,9 @@ describe("set server credentials helper function", () => {
expect(credentials.options.host).toBe("smtp.gmail.com");
expect(credentials.options.auth.pass).toBe(config.smtpPassword);
expect(credentials.options.secure).toBe(true);

// The regex should match the smtpConnectionUri, it should be valid
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return smtpConnectionUri credentials with old password", () => {
Expand All @@ -54,6 +65,9 @@ describe("set server credentials helper function", () => {
expect(credentials.options.auth.user).toBe("fakeemail@gmail.com");
expect(credentials.options.auth.pass).toBe("secret-password");
expect(credentials.options.secure).toBe(true);

// The regex should match the smtpConnectionUri, it should be valid
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return smtpConnectionUri credentials without any password", () => {
Expand All @@ -70,6 +84,9 @@ describe("set server credentials helper function", () => {
expect(credentials.options.auth.user).toBe("fakeemail@gmail.com");
expect(credentials.options.auth.pass).toBe("");
expect(credentials.options.secure).toBe(true);

// The regex should match the smtpConnectionUri, it should be valid
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return smtpConnectionUri credentials without any password and username", () => {
Expand All @@ -85,6 +102,9 @@ describe("set server credentials helper function", () => {
expect(credentials.options.host).toBe("smtp.gmail.com");
expect(credentials.options.auth).toBe(undefined);
expect(credentials.options.secure).toBe(false);

// The regex should match the smtpConnectionUri, it should be valid
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return smtpConnectionUri credentials with query params", () => {
Expand All @@ -104,6 +124,9 @@ describe("set server credentials helper function", () => {
expect(credentials.options.secure).toBe(false);
expect(credentials.options.pool).toBe(true);
expect(credentials.options.service).toBe("gmail");

// The regex should match the smtpConnectionUri, it should be valid
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return valid smtpConnectionUri credentials with special chars in password config", () => {
Expand All @@ -124,6 +147,9 @@ describe("set server credentials helper function", () => {
expect(credentials.options.secure).toBe(false);
expect(credentials.options.pool).toBe(true);
expect(credentials.options.service).toBe("gmail");

// The regex should match the smtpConnectionUri, it should be valid
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return valid smtpConnectionUri credentials with valid special chars in connectionUri password", () => {
Expand All @@ -144,6 +170,9 @@ describe("set server credentials helper function", () => {
expect(credentials.options.secure).toBe(false);
expect(credentials.options.pool).toBe(true);
expect(credentials.options.service).toBe("gmail");

// The regex should match the smtpConnectionUri, it should be valid
expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return invalid smtpConnectionUri credentials with invalid special chars in connectionUri password", () => {
Expand All @@ -161,4 +190,27 @@ describe("set server credentials helper function", () => {
`invalid url: '${config.smtpConnectionUri}' , please reconfigure with a valid SMTP connection URI`
);
});
test("return valid smtpConnectionUri credentials with correct seprator", () => {
const config: Config = {
smtpConnectionUri:
"smtp://fakeemail@gmail.com:4,h?dhuNTbv9zMrP4&7&7%*3@smtp.gmail.com:465?pool=true&service=gmail",
location: "",
mailCollection: "",
defaultFrom: "",
};

expect(regex.test(config.smtpConnectionUri)).toBe(true);
});

test("return invalid smtpConnectionUri credentials with invalid seprator", () => {
const config: Config = {
smtpConnectionUri:
"smtp://fakeemail@gmail.com:4,h?dhuNTbv9zMrP4&7&7%*3:smtp.gmail.com:465?pool=true&service=gmail",
location: "",
mailCollection: "",
defaultFrom: "",
};

expect(regex.test(config.smtpConnectionUri)).toBe(false);
});
});

0 comments on commit 4516265

Please sign in to comment.