Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FIX (notifiers/email): strip CR/LF/NUL from header-bound values to prevent SMTP header injection and clear CodeQL "email content injection" finding

Rostislav Dugin00adb9c9c0d8
diff --git a/backend/internal/features/notifiers/models/email_notifier/model.go b/backend/internal/features/notifiers/models/email_notifier/model.go
index 36d4ef2c..6cef98ab 100644
--- a/backend/internal/features/notifiers/models/email_notifier/model.go
+++ b/backend/internal/features/notifiers/models/email_notifier/model.go
@@ -10,6 +10,7 @@ import (
"net"
"net/smtp"
"os"
+ "strings"
"time"
"github.com/google/uuid"
@@ -139,13 +140,29 @@ func encodeRFC2047(s string) string {
return mime.QEncoding.Encode("UTF-8", s)
}
+// sanitizeHeaderValue strips CR, LF, and NUL to prevent SMTP header injection:
+// a CRLF inside a header value would start a new header (e.g. Bcc:) or
+// terminate the header block, letting an attacker rewrite the envelope.
+func sanitizeHeaderValue(value string) string {
+ value = strings.ReplaceAll(value, "\r", "")
+ value = strings.ReplaceAll(value, "\n", "")
+ value = strings.ReplaceAll(value, "\x00", "")
+
+ return value
+}
+
func (e *EmailNotifier) buildEmailContent(heading, message, from string) []byte {
+ safeHeading := sanitizeHeaderValue(heading)
+ safeFrom := sanitizeHeaderValue(from)
+ safeTargetEmail := sanitizeHeaderValue(e.TargetEmail)
+ safeSMTPHost := sanitizeHeaderValue(e.SMTPHost)
+
// Encode Subject header using RFC 2047 to avoid SMTPUTF8 requirement
// This ensures compatibility with SMTP servers that don't support SMTPUTF8
- encodedSubject := encodeRFC2047(heading)
+ encodedSubject := encodeRFC2047(safeHeading)
subject := fmt.Sprintf("Subject: %s\r\n", encodedSubject)
dateHeader := fmt.Sprintf("Date: %s\r\n", time.Now().UTC().Format(time.RFC1123Z))
- messageID := fmt.Sprintf("Message-ID: <%s@%s>\r\n", uuid.New().String(), e.SMTPHost)
+ messageID := fmt.Sprintf("Message-ID: <%s@%s>\r\n", uuid.New().String(), safeSMTPHost)
mimeHeaders := fmt.Sprintf(
"MIME-version: 1.0;\nContent-Type: %s; charset=\"%s\";\n\n",
@@ -154,10 +171,10 @@ func (e *EmailNotifier) buildEmailContent(heading, message, from string) []byte
)
// Encode From header display name if it contains non-ASCII
- encodedFrom := encodeRFC2047(from)
+ encodedFrom := encodeRFC2047(safeFrom)
fromHeader := fmt.Sprintf("From: %s\r\n", encodedFrom)
- toHeader := fmt.Sprintf("To: %s\r\n", e.TargetEmail)
+ toHeader := fmt.Sprintf("To: %s\r\n", safeTargetEmail)
return []byte(fromHeader + toHeader + subject + dateHeader + messageID + mimeHeaders + message)
}
diff --git a/backend/internal/features/notifiers/models/email_notifier/model_test.go b/backend/internal/features/notifiers/models/email_notifier/model_test.go
new file mode 100644
index 00000000..2598a1d7
--- /dev/null
+++ b/backend/internal/features/notifiers/models/email_notifier/model_test.go
@@ -0,0 +1,63 @@
+package email_notifier
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/google/uuid"
+)
+
+func Test_SanitizeHeaderValue_StripsCRLFAndNUL(t *testing.T) {
+ cases := []struct {
+ input string
+ expected string
+ }{
+ {"plain", "plain"},
+ {"with\rcr", "withcr"},
+ {"with\nlf", "withlf"},
+ {"with\r\ncrlf", "withcrlf"},
+ {"with\x00nul", "withnul"},
+ {"a\r\nBcc: [email protected]\r\n", "aBcc: [email protected]"},
+ }
+
+ for _, c := range cases {
+ got := sanitizeHeaderValue(c.input)
+ if got != c.expected {
+ t.Errorf("sanitizeHeaderValue(%q) = %q, want %q", c.input, got, c.expected)
+ }
+ }
+}
+
+func Test_BuildEmailContent_DropsInjectedHeadersFromTargetEmail(t *testing.T) {
+ notifier := &EmailNotifier{
+ NotifierID: uuid.New(),
+ TargetEmail: "[email protected]\r\nBcc: [email protected]",
+ SMTPHost: "smtp.example.com",
+ SMTPPort: 587,
+ }
+
+ content := string(notifier.buildEmailContent("subject", "<p>body</p>", "[email protected]"))
+
+ if strings.Contains(content, "\r\nBcc:") || strings.Contains(content, "\nBcc:") {
+ t.Errorf("Bcc header line was injected via TargetEmail: %q", content)
+ }
+
+ if !strings.Contains(content, "To: [email protected]: [email protected]\r\n") {
+ t.Errorf("expected sanitized To header without CRLF, got: %q", content)
+ }
+}
+
+func Test_BuildEmailContent_DropsInjectedHeadersFromSMTPHost(t *testing.T) {
+ notifier := &EmailNotifier{
+ NotifierID: uuid.New(),
+ TargetEmail: "[email protected]",
+ SMTPHost: "smtp.example.com>\r\nX-Injected: 1",
+ SMTPPort: 587,
+ }
+
+ content := string(notifier.buildEmailContent("subject", "<p>body</p>", "[email protected]"))
+
+ if strings.Contains(content, "\r\nX-Injected:") || strings.Contains(content, "\nX-Injected:") {
+ t.Errorf("injected header line leaked via SMTPHost: %q", content)
+ }
+}