ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
fix(notify): parse the sender and reject header line breaks
BaaRaif58365c83eea
diff --git a/notify/email.go b/notify/email.goindex c3e8336..a3113fe 100644--- a/notify/email.go+++ b/notify/email.go@@ -9,6 +9,7 @@ import ("fmt""mime""net"+ "net/mail""net/smtp""strconv""strings"@@ -58,6 +59,14 @@ func (emailChannel) Validate(cfg map[string]any) error {if len(cfgStrings(cfg, "to")) == 0 {return errors.New("at least one recipient address is required")}+ for _, rcpt := range cfgStrings(cfg, "to") {+ if hasLineBreak(rcpt) {+ return errors.New("recipient address must not contain line breaks")+ }+ }+ if _, err := senderAddress(cfgString(cfg, "from")); err != nil {+ return err+ }return nil}@@ -73,6 +82,10 @@ func (c emailChannel) Send(ctx context.Context, cfg map[string]any, m Message) (password := cfgString(cfg, "password")implicitTLS := cfgBool(cfg, "tls")+ sender, err := senderAddress(from)+ if err != nil {+ return 0, Permanent(err)+ }msg, err := buildEmailMessage(from, to, m)if err != nil {return 0, Permanent(err)@@ -107,8 +120,9 @@ func (c emailChannel) Send(ctx context.Context, cfg map[string]any, m Message) (return 0, Permanent(fmt.Errorf("SMTP authentication failed: %w", err))}}- if err := client.Mail(from); err != nil {- return 0, smtpStageError(fmt.Sprintf("sender %s rejected", from), err)+ // The envelope sender is the bare address; the display name stays in the header.+ if err := client.Mail(sender.Address); err != nil {+ return 0, smtpStageError(fmt.Sprintf("sender %s rejected", sender.Address), err)}for _, rcpt := range to {if err := client.Rcpt(rcpt); err != nil {@@ -218,6 +232,15 @@ func smtpReplyCode(text string) int {// over-long lines; second, base64 never produces a line starting with ".",// avoiding SMTP dot-stuffing entirely.func buildEmailMessage(from string, to []string, m Message) (string, error) {+ // From and To go into header lines: a CR or LF would start a new header.+ if hasLineBreak(from) {+ return "", errors.New("sender address must not contain line breaks")+ }+ for _, rcpt := range to {+ if hasLineBreak(rcpt) {+ return "", errors.New("recipient address must not contain line breaks")+ }+ }var b strings.Builderfmt.Fprintf(&b, "From: %s\r\n", from)fmt.Fprintf(&b, "To: %s\r\n", strings.Join(to, ", "))@@ -243,13 +266,34 @@ func buildEmailMessage(from string, to []string, m Message) (string, error) {return b.String(), nil}-// messageIDDomain is the right-hand side of a Message-ID: the sender's domain,-// or "localhost" when the sender address has none.+// senderAddress parses the configured sender with net/mail, so a display name and+// comments are accepted and the bare address is what gets used. A line break is+// refused before parsing: the value lands in header lines.+func senderAddress(from string) (*mail.Address, error) {+ if hasLineBreak(from) {+ return nil, errors.New("sender address must not contain line breaks")+ }+ addr, err := mail.ParseAddress(from)+ if err != nil {+ return nil, fmt.Errorf("invalid sender address: %w", err)+ }+ return addr, nil+}++// hasLineBreak reports whether s contains CR or LF.+func hasLineBreak(s string) bool {+ return strings.ContainsAny(s, "\r\n")+}++// messageIDDomain is the right-hand side of a Message-ID: the domain of the parsed+// sender address, or "localhost" when there is none (or the sender does not parse).func messageIDDomain(from string) string {- if i := strings.LastIndex(from, "@"); i >= 0 {- if d := strings.TrimRight(strings.TrimSpace(from[i+1:]), ">"); d != "" {- return d- }+ addr, err := senderAddress(from)+ if err != nil {+ return "localhost"+ }+ if i := strings.LastIndex(addr.Address, "@"); i >= 0 && i+1 < len(addr.Address) {+ return addr.Address[i+1:]}return "localhost"}diff --git a/notify/email_sender_test.go b/notify/email_sender_test.gonew file mode 100644index 0000000..c01cc72--- /dev/null+++ b/notify/email_sender_test.go@@ -0,0 +1,81 @@+package notify++import (+ "strings"+ "testing"+)++func TestMessageIDDomainFromCommentedAddress(t *testing.T) {+ if got := messageIDDomain("Name <[email protected]> (comment)"); got != "example.com" {+ t.Fatalf("domain = %q, want example.com", got)+ }+ if got := messageIDDomain("[email protected]"); got != "example.org" {+ t.Fatalf("domain = %q, want example.org", got)+ }+}++func TestMessageIDDomainFallsBackToLocalhost(t *testing.T) {+ for _, from := range []string{"no-at-sign", "a@", "Name <a@>", ""} {+ if got := messageIDDomain(from); got != "localhost" {+ t.Fatalf("messageIDDomain(%q) = %q, want localhost", from, got)+ }+ }+}++func TestBuildEmailMessageRefusesLineBreaksInHeaders(t *testing.T) {+ cases := []struct {+ name string+ from string+ to []string+ }{+ {"CRLF in recipient", "[email protected]", []string{"[email protected]\r\nBcc: [email protected]"}},+ }+ for _, c := range cases {+ t.Run(c.name, func(t *testing.T) {+ msg, err := buildEmailMessage(c.from, c.to, Message{})+ if err == nil {+ t.Fatalf("expected an error, got message:\n%s", msg)+ }+ if strings.Contains(msg, "Bcc:") {+ t.Fatal("no header may be produced from an injected value")+ }+ })+ }+}++func TestSenderAddressValidation(t *testing.T) {+ if _, err := senderAddress("[email protected]\r\nX: y"); err == nil {+ t.Fatal("CRLF sender must be refused")+ }+ if _, err := senderAddress("not an address"); err == nil {+ t.Fatal("unparseable sender must be refused")+ }+ addr, err := senderAddress("Name <[email protected]> (comment)")+ if err != nil {+ t.Fatalf("commented sender refused: %v", err)+ }+ if addr.Address != "[email protected]" {+ t.Fatalf("envelope address = %q, want [email protected]", addr.Address)+ }+}++func TestEmailValidateRejectsBadSenderAndRecipients(t *testing.T) {+ base := map[string]any{"host": "smtp.example.com", "port": 587, "to": []any{"[email protected]"}}+ ok := map[string]any{"host": "smtp.example.com", "port": 587, "from": "[email protected]", "to": []any{"[email protected]"}}+ if err := (emailChannel{}).Validate(ok); err != nil {+ t.Fatalf("valid config refused: %v", err)+ }+ if err := (emailChannel{}).Validate(base); err == nil {+ t.Fatal("empty sender must be refused")+ }+ bad := map[string]any{"host": "smtp.example.com", "port": 587, "from": "not an address", "to": []any{"[email protected]"}}+ if err := (emailChannel{}).Validate(bad); err == nil {+ t.Fatal("invalid sender must be refused")+ }+ crlf := map[string]any{"host": "smtp.example.com", "port": 587, "from": "[email protected]", "to": []any{"[email protected]\r\nX: y"}}+ if err := (emailChannel{}).Validate(crlf); err == nil {+ t.Fatal("CRLF recipient must be refused")+ }+}