Skip to content

Commit c1b3d10

Browse files
committed
Fix leak of first recipient to other recipients because mail headers are also cached and re-used
1 parent a4fd43e commit c1b3d10

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

mail_common.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,30 @@ type SmtpSettings struct {
4747
Password string `json:"password"`
4848
}
4949

50-
func sendNotices(recipient string, notices []*WidNotice, template MailTemplate, auth smtp.Auth, smtpConfig SmtpSettings, cache *map[string][]byte) error {
50+
func sendNotices(recipient string, notices []*WidNotice, template MailTemplate, auth smtp.Auth, smtpConfig SmtpSettings, mailContentCache *map[string]*MailContent) error {
5151
logger.debug("Generating and sending mails for recipient " + recipient + " ...")
5252
cacheHits := 0
5353
cacheMisses := 0
54-
mails := [][]byte{}
54+
mails := []*MailContent{}
5555
for _, n := range notices {
56-
var data []byte
57-
cacheResult := (*cache)[n.Uuid]
58-
if len(cacheResult) > 0 {
56+
var mc *MailContent
57+
cacheResult := (*mailContentCache)[n.Uuid]
58+
if cacheResult != nil {
5959
cacheHits++
60-
data = cacheResult
60+
mc = cacheResult
6161
} else {
6262
cacheMisses++
63-
mailContent, err := template.generate(TemplateData{n, Version})
63+
mc_, err := template.generate(TemplateData{n, Version})
6464
if err != nil {
6565
logger.error("Could not create mail from template")
6666
logger.error(err)
67+
} else {
68+
mc = &mc_
69+
// add to cache
70+
(*mailContentCache)[n.Uuid] = mc
6771
}
68-
// serialize mail
69-
data = mailContent.serializeValidMail(smtpConfig.From, recipient)
70-
// add to cache
71-
(*cache)[n.Uuid] = data
7272
}
73-
mails = append(mails, data)
73+
mails = append(mails, mc)
7474
}
7575
logger.debug(fmt.Sprintf("%v mail cache hits, %v misses", cacheHits, cacheMisses))
7676
err := sendMails(

mail_transfer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313

14-
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, data [][]byte) error {
14+
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, mails []*MailContent) error {
1515
addr := fmt.Sprintf("%v:%v", smtpConf.ServerHost, smtpConf.ServerPort)
1616
logger.debug("Connecting to mail server at " + addr + " ...")
1717
connection, err := smtp.Dial(addr)
@@ -32,7 +32,10 @@ func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, data [][]byte)
3232
if logger.LogLevel >= 3 {
3333
fmt.Printf("DEBUG %v Sending mails to server ", time.Now().Format("2006/01/02 15:04:05.000000"))
3434
}
35-
for _, d := range data {
35+
for _, mc := range mails {
36+
// serialize mail
37+
d := mc.serializeValidMail(smtpConf.From, to)
38+
// send mail
3639
err = connection.Mail(smtpConf.From)
3740
if err != nil { return err }
3841
err = connection.Rcpt(to)

mail_transfer_debug.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import (
88
"net/smtp"
99
)
1010

11-
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, data [][]byte) error {
11+
func sendMails(smtpConf SmtpSettings, auth smtp.Auth, to string, mails []*MailContent) error {
1212
logger.warn("Mail Transfer Debugging is active. Not connecting.")
1313
logger.info("MAIL TRANSFER: \n\n")
14-
for _, d := range data {
14+
for _, mc := range mails {
15+
// serialize mail
16+
d := mc.serializeValidMail(smtpConf.From, to)
17+
// output mail
1518
fmt.Println("MAIL FROM:" + smtpConf.From)
1619
fmt.Println("RCPT TO:" + to)
1720
fmt.Println("DATA")

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func main() {
9898
t1 := time.Now().UnixMilli()
9999
newNotices := []WidNotice{}
100100
lastPublished := map[string]time.Time{} // endpoint id : last published timestamp
101-
cache := map[string][]byte{} // cache generated emails for reuse
101+
cache := map[string]*MailContent{} // cache generated emails for reuse
102102
for _, a := range enabledApiEndpoints {
103103
logger.info("Querying endpoint '" + a.Id + "' for new notices ...")
104104
n, t, err := a.getNotices(persistent.data.(PersistentData).LastPublished[a.Id])

0 commit comments

Comments
 (0)