Skip to content

armgit / fider

Public Imported from GitHub
0

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.go
index 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 rutime
func 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 address
func 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 emails
diff --git a/app/pkg/email/email_test.go b/app/pkg/email/email_test.go
index 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{
- } {
- Expect(email.CanSendTo(address)).IsTrue()
+ testCases := []struct {
+ whitelist string
+ blacklist string
+ input []string
+ canSend bool
+ }{
+ {
+ whitelist: "(^[email protected]$)|(^darthvader\\.fider(\\+.*)?@gmail\\.com$)",
+ blacklist: "",
+ canSend: true,
+ },
+ {
+ whitelist: "(^[email protected]$)|(^darthvader\\.fider(\\+.*)?@gmail\\.com$)",
+ blacklist: "",
+ input: []string{"[email protected]", "[email protected]", "[email protected]", "@fider.io"},
+ 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{
- "",
- "@fider.io",
- } {
- 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.go
index 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 return
if len(recipientVariables) == 0 {
return nil
}
diff --git a/app/pkg/email/smtp/smtp.go b/app/pkg/email/smtp/smtp.go
index 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,
})