Open platform to collect and prioritize feedback
feature: allow blacklisting email based on RegExp (#523)
Guilherme Oenning04765e31f5e7
diff --git a/app/pkg/email/email.go b/app/pkg/email/email.goindex 56d172ebc..949924cd6 100644--- a/app/pkg/email/email.go+++ b/app/pkg/email/email.go@@ -98,6 +98,8 @@ func NewRecipient(name, address string, params Params) Recipient {var whitelist = env.GetEnvOrDefault("EMAIL_WHITELIST", "")var whitelistRegex = regexp.MustCompile(whitelist)+var blacklist = env.GetEnvOrDefault("EMAIL_BLACKLIST", "")+var blacklistRegex = regexp.MustCompile(blacklist)// SetWhitelist can be used to change email whitelist during rutimefunc SetWhitelist(s string) {@@ -105,15 +107,27 @@ func SetWhitelist(s string) {whitelistRegex = regexp.MustCompile(whitelist)}+// SetBlacklist can be used to change email blacklist during rutime+func SetBlacklist(s string) {+ blacklist = s+ blacklistRegex = regexp.MustCompile(blacklist)+}+// CanSendTo returns true if Fider is allowed to send email to given addressfunc CanSendTo(address string) bool {if strings.TrimSpace(address) == "" {return false}- if whitelist == "" {- return true++ if whitelist != "" {+ return whitelistRegex.MatchString(address)+ }++ if blacklist != "" {+ return !blacklistRegex.MatchString(address)}- return whitelistRegex.MatchString(address)++ return true}// Sender is used to send emailsdiff --git a/app/pkg/email/email_test.go b/app/pkg/email/email_test.goindex 7e7e3dc88..e7432dff3 100644--- a/app/pkg/email/email_test.go+++ b/app/pkg/email/email_test.go@@ -52,32 +52,65 @@ func TestRenderMessage(t *testing.T) {</html>`)}-func TestEmailWhitelist_Valid(t *testing.T) {+func TestCanSendTo(t *testing.T) {RegisterT(t)- email.SetWhitelist("(^[email protected]$)|(^darthvader\\.fider(\\+.*)?@gmail\\.com$)")-- for _, address := range []string{- "[email protected]",- "[email protected]",- "[email protected]",- "[email protected]",- "[email protected]",- "[email protected]",- } {- Expect(email.CanSendTo(address)).IsTrue()+ testCases := []struct {+ whitelist string+ blacklist string+ input []string+ canSend bool+ }{+ {+ whitelist: "(^[email protected]$)|(^darthvader\\.fider(\\+.*)?@gmail\\.com$)",+ blacklist: "",+ input: []string{"[email protected]", "[email protected]", "[email protected]", "[email protected]"},+ canSend: true,+ },+ {+ whitelist: "(^[email protected]$)|(^darthvader\\.fider(\\+.*)?@gmail\\.com$)",+ blacklist: "",+ canSend: false,+ },+ {+ whitelist: "(^[email protected]$)|(^darthvader\\.fider(\\+.*)?@gmail\\.com$)",+ blacklist: "(^[email protected]$)",+ input: []string{"[email protected]"},+ canSend: true,+ },+ {+ whitelist: "",+ blacklist: "(^[email protected]$)",+ input: []string{"[email protected]", "[email protected]"},+ canSend: false,+ },+ {+ whitelist: "",+ blacklist: "(^[email protected]$)",+ input: []string{"[email protected]", "[email protected]"},+ canSend: true,+ },+ {+ whitelist: "",+ blacklist: "",+ input: []string{"[email protected]"},+ canSend: true,+ },+ {+ whitelist: "",+ blacklist: "",+ input: []string{"", " "},+ canSend: false,+ },}- for _, address := range []string{- "",- "[email protected]",- "@fider.io",- "[email protected]",- "[email protected]",- "[email protected]",- "[email protected]",- } {- Expect(email.CanSendTo(address)).IsFalse()+ for _, testCase := range testCases {+ email.SetWhitelist(testCase.whitelist)+ email.SetBlacklist(testCase.blacklist)+ for _, input := range testCase.input {+ Expect(email.CanSendTo(input)).Equals(testCase.canSend)+ }}}diff --git a/app/pkg/email/mailgun/mailgun.go b/app/pkg/email/mailgun/mailgun.goindex 7cb11f9db..dccb42f3b 100644--- a/app/pkg/email/mailgun/mailgun.go+++ b/app/pkg/email/mailgun/mailgun.go@@ -72,7 +72,7 @@ func (s *Sender) BatchSend(ctx email.Context, templateName string, params email.form.Add("to", fmt.Sprintf("%s <%s>", r.Name, r.Address))recipientVariables[r.Address] = r.Params} else {- s.logger.Warnf("Skipping email to '@{Name} <@{Address}>' due to whitelist.", log.Props{+ s.logger.Warnf("Skipping email to '@{Name} <@{Address}>'.", log.Props{"Name": r.Name,"Address": r.Address,})@@ -80,7 +80,7 @@ func (s *Sender) BatchSend(ctx email.Context, templateName string, params email.}}- // If we skipped all recipients due to whitelist, just return+ // If we skipped all recipients, just returnif len(recipientVariables) == 0 {return nil}diff --git a/app/pkg/email/smtp/smtp.go b/app/pkg/email/smtp/smtp.goindex c1329079d..0d529b2b9 100644--- a/app/pkg/email/smtp/smtp.go+++ b/app/pkg/email/smtp/smtp.go@@ -59,7 +59,7 @@ func (s *Sender) Send(ctx email.Context, templateName string, params email.Param}if !email.CanSendTo(to.Address) {- s.logger.Warnf("Skipping email to '@{Name} <@{Address}>' due to whitelist.", log.Props{+ s.logger.Warnf("Skipping email to '@{Name} <@{Address}>'.", log.Props{"Name": to.Name,"Address": to.Address,})