@@ -114,9 +114,14 @@ func TestEmailInjection_HeaderInjectionPrevention(t *testing.T) {
114114 if err != nil {
115115 t .Fatalf ("SendEmail failed: %v" , err )
116116 }
117+ // After sanitization, net/mail will typically remove the invalid parts or quote them
118+ // In our case, it should at least remove the CRLF
117119 if strings .Contains (capturedMessage , "\r " ) || strings .Contains (capturedMessage , "\n " ) {
118120 t .Errorf ("CRLF characters not sanitized from recipient: %s" , capturedMessage )
119121 }
122+ // Note: net/mail.ParseAddress might return "<victim@example.comBcc:attacker@evil.com>"
123+ // because it strips control characters via sanitizeHeader fallback if parsing fails,
124+ // or it might just error. Our implementation currently falls back to sanitizeHeader if ParseAddress fails.
120125}
121126
122127// TestEmailInjection_BodyControlCharactersPrevention tests that control characters in body are removed
@@ -136,9 +141,9 @@ func TestEmailInjection_BodyControlCharactersPrevention(t *testing.T) {
136141 t .Fatalf ("SendEmail failed: %v" , err )
137142 }
138143
139- // Body should have control characters removed
140- if strings .Contains (capturedBody , "\r " ) || strings . Contains ( capturedBody , " \n " ) {
141- t .Errorf ("CRLF characters not sanitized from body: %q" , capturedBody )
144+ // Body should have null bytes removed, but CRLF should REMAIN
145+ if ! strings .Contains (capturedBody , "\r \n " ) {
146+ t .Errorf ("CRLF characters SHOULD remain in body for multi-line support : %q" , capturedBody )
142147 }
143148 if strings .Contains (capturedBody , "\x00 " ) {
144149 t .Errorf ("Null bytes not sanitized from body: %q" , capturedBody )
@@ -255,8 +260,11 @@ func TestEmailInjection_TransactionalEmailSanitization(t *testing.T) {
255260 if strings .Contains (capturedSubject , "\r " ) || strings .Contains (capturedSubject , "\n " ) {
256261 t .Errorf ("Subject CRLF not sanitized: %q" , capturedSubject )
257262 }
258- if strings .Contains (capturedBody , "\r " ) || strings .Contains (capturedBody , "\n " ) || strings .Contains (capturedBody , "\x00 " ) {
259- t .Errorf ("Body control characters not sanitized: %q" , capturedBody )
263+ if ! strings .Contains (capturedBody , "\r \n " ) {
264+ t .Errorf ("Body CRLF SHOULD remain for multi-line support: %q" , capturedBody )
265+ }
266+ if strings .Contains (capturedBody , "\x00 " ) {
267+ t .Errorf ("Body null bytes not sanitized: %q" , capturedBody )
260268 }
261269}
262270
0 commit comments