swoosh/swoosh

SMTP: Crashes when email address is invalid

Aperta

#758 aperta il 7 apr 2023

 (4 commenti) (0 reazioni) (0 assegnatari)Elixir (253 fork)batch import
bughelp wanted

Metriche repository

Star
 (1517 stelle)
Metriche merge PR
 (Merge medio 2g 19h) (9 PR mergiate in 30 g)

Descrizione

Describe the bug

When sending an email with Swoosh.Adapters.SMTP, if the email address contains invalid characters, for example [], the adapter crashes with MatchError, rather than reporting an error tuple.

The match error is produced here: https://github.com/gen-smtp/gen_smtp/blob/685fc92893297d8d726cae1029b9568ae79a0ead/src/mimemail.erl#L999

So maybe this is an issue with gen_smtp, or maybe :mimemail.encode expects valid data, so the adapter should call :smtp_util.parse_rfc5322_addresses on the addresses itself before encoding?

Steps to Reproduce the Bug or Issue

Working:

iex> email = Swoosh.Email.new() |> Swoosh.Email.from("test@example.com") |> Swoosh.Email.text_body("text body") |> Swoosh.Email.to("foo@bar.com")
iex> Swoosh.Adapters.SMTP.Helpers.body(email, [])
"Content-Type: text/plain;\r\n\t charset=\"utf-8\"\r\nFrom: test@example.com\r\nTo: foo@bar.com\r\nSubject: \r\nMIME-Version: 1.0\r\nDate: Fri, 7 Apr 2023 11:45:26 +0000\r\nMessage-ID: <fc0cf5f012037f29f3d111b7dc034fea@58ab8ec4cf1d>\r\n\r\ntext body"
iex> Swoosh.Adapters.SMTP.deliver(email, [])
{:error, :no_relay}

Broken:

iex> email = Swoosh.Email.new() |> Swoosh.Email.from("test@example.com") |> Swoosh.Email.text_body("text body") |> Swoosh.Email.to("[foo]@bar.com")
iex> Swoosh.Adapters.SMTP.Helpers.body(email, [])
** (MatchError) no match of right hand side value: {:error, {1, :smtp_rfc5322_parse, ['syntax error before: ', ['"[foo]"']]}}
iex> Swoosh.Adapters.SMTP.deliver(email, [])
** (MatchError) no match of right hand side value: {:error, {1, :smtp_rfc5322_parse, ['syntax error before: ', ['"[foo]"']]}}

Expected behavior

An {:error, _} tuple is returned according to the @spec of deliver/2, rather than a MatchError.

Guida contributor