Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions internal/serve/smtp_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"net/http"
"strings"
"time"

"msgraphtool/internal/common/logger"
Expand All @@ -19,6 +20,26 @@ type smtpSendRequest struct {
Body string `json:"body,omitempty"`
}

func sanitizeEmailSubjectInput(subject string) string {
subject = strings.ReplaceAll(subject, "\r", "")
subject = strings.ReplaceAll(subject, "\n", "")
return strings.TrimSpace(subject)
}

func sanitizeEmailBodyInput(body string) string {
body = strings.ReplaceAll(body, "\r\n", "\n")
body = strings.ReplaceAll(body, "\r", "\n")

var b strings.Builder
b.Grow(len(body))
for _, r := range body {
if r == '\n' || r == '\t' || r >= 0x20 {
b.WriteRune(r)
}
}
return b.String()
}

func (s *Server) handleSMTPSendMail(w http.ResponseWriter, r *http.Request) {
if s.smtpBase == nil {
writeJSON(w, http.StatusServiceUnavailable, apiResponse{Status: "error", Message: "SMTP not configured (set SMTPHOST and related env vars)"})
Expand Down Expand Up @@ -61,8 +82,8 @@ func (s *Server) handleSMTPSendMail(w http.ResponseWriter, r *http.Request) {
// Clone base config and overlay request content
cfg := *s.smtpBase
cfg.To = req.To
cfg.Subject = req.Subject
cfg.Body = req.Body
cfg.Subject = sanitizeEmailSubjectInput(req.Subject)
cfg.Body = sanitizeEmailBodyInput(req.Body)
cfg.Action = smtp.ActionSendMail
if req.From != "" {
cfg.From = req.From
Expand Down
Loading