Potential fix for code scanning alert no. 2: Email content injection#63
Conversation
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Code ReviewOverviewThis PR adds a The approach is sound in intent, but there are several issues to address before merging. Issues1. Existing test will break (high severity)The test case "Body preserves newlines (NOT sanitized)" in The test name also needs updating: the comment "NOT sanitized" is now incorrect, and the expected body value should reflect the normalized output. 2. RFC 5322 line-ending compliance concern (medium severity)
Suggested fix: after stripping control characters, convert func sanitizeEmailBody(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)
}
}
// Re-normalize to CRLF for RFC 5322 compliance
return strings.ReplaceAll(b.String(), "\n", "\r\n")
}3. No unit tests for the new
|
| Issue | Severity | Action Required |
|---|---|---|
Existing bodyPreserve test will fail |
High | Update test expectation and name |
| Body bare-LF violates RFC 5322 | Medium | Convert \n to \r\n after stripping |
No direct tests for sanitizeEmailBody |
Medium | Add TestSanitizeEmailBody |
| C1 Unicode controls pass through | Low | Acceptable for now |
The core logic is correct and the function is clean. With the test fix, RFC 5322 line-ending normalization, and a dedicated unit test, this would be in good shape to merge.
Potential fix for https://github.com/ziembor/gomailtesttool/security/code-scanning/2
General fix: sanitize untrusted email body content before it is inserted into the RFC 5322 message, using a conservative normalization that removes dangerous control characters while preserving expected text/newlines.
Best minimal fix without changing architecture: in
internal/protocols/smtp/sendmail.go, sanitizebodyinsidebuildEmailMessagebefore concatenating it into the message. Keep current header sanitization intact. Add a helper that:\r\nand lone\rto\n,\nand\t,This addresses both alert variants because both taint paths converge to the same sink (
w.Write(data)) throughbuildEmailMessage.Suggested fixes powered by Copilot Autofix. Review carefully before merging.