Skip to content

Commit

Permalink
Do proper check if SMTP_ADDR is local IP
Browse files Browse the repository at this point in the history
  • Loading branch information
clarfonthey committed May 3, 2022
1 parent d60c438 commit bc7b454
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions modules/setting/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,15 @@ func newMailService() {
}
}

// we want to warn if users use SMTP on a non-local IP;
// we might as well take the opportunity to check that it has an IP at all
ips := tryResolveAddr(MailService.SMTPAddr)
if MailService.Protocol == "smtp" {
switch MailService.SMTPAddr {
case "localhost":
case "127.0.0.1":
case "::1":
case "[::1]":
// this is a local address, so, we're fine
break
default:
log.Warn("connect via insecure SMTP to non-local address")
for _, ip := range ips {
if !ip.IsLoopback() {
log.Warn("connect via insecure SMTP to non-local address")
break
}
}
}

Expand Down Expand Up @@ -246,3 +245,21 @@ func newNotifyMailService() {
Service.EnableNotifyMail = true
log.Info("Notify Mail Service Enabled")
}

func tryResolveAddr(addr string) []net.IP {
if strings.HasPrefix(addr, "[") && strings.HasSuffix(addr, "]") {
addr = addr[1 : len(addr)-1]
}
ip := net.ParseIP(addr)
if ip != nil {
ips := make([]net.IP, 1)
ips[0] = ip
return ips
}
ips, err := net.LookupIP(addr)
if err != nil {
log.Warn("could not look up mailer.SMTP_ADDR: %v", err)
return make([]net.IP, 0)
}
return ips
}

0 comments on commit bc7b454

Please sign in to comment.