Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: correctly close SMTP message and await response#14495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dannykopping merged 1 commit intomainfromdk/smtp-close
Aug 30, 2024

Conversation

dannykopping
Copy link
Contributor

We originally took heavy inspiration fromAlertmanager's SMTP dispatch functionality due to the number of options we need to support.

In their dispatch code, they defer the call tomessage.Close but in fact this is necessary to complete the SMTP submission and await any errors; we do the same in our code.

NOTE: We useemersion/go-smtp as our SMTP library while Alertmanager usesnet/smtp, but the former extends the latter, so the code that follows is effectively the same.

// coderd/notifications/dispatch/smtp.go// Our client calls Data(), which returns a `dataCloser`.message,err:=c.Data()...// emersion/go-smtp@v0.21.2/client.gofunc (c*Client)Data() (io.WriteCloser,error) {_,_,err:=c.cmd(354,"DATA")iferr!=nil {returnnil,err}return&dataCloser{c:c,WriteCloser:c.text.DotWriter()},nil}...func (d*dataCloser)Close()error {...ifd.c.lmtp {...}else {_,_,err:=d.c.readResponse(250)// <---- this is the crucial lineiferr!=nil {returnerr}}d.closed=truereturnnil}

We cannot defer a call toClose because we depend on the error which is returned to determine if the message was successfully sent or not.

_,_,err:=d.c.readResponse(250)

This line checks if the response was successful (docs); if not, we need to fail this dispatch and mark it for retry.


I discovered this bug by incorrectly configuringCODER_NOTIFICATIONS_EMAIL_FROM as my@coder.com address (which is backed by GSuite) and testing a dispatch via an outlook.com SMTP server. The server was rejecting the message with554 5.2.252 SendAsDenied; <redacted>@outlook.com not allowed to send as <redacted>@coder.com, but the message was being marked successful, because we were not waiting for the actual outcome of the mail dispatch.

This has quite worrying implications for Alertmanager, and I plan to raise a PR in that repo too to address this.

Signed-off-by: Danny Kopping <danny@coder.com>
Copy link
Member

@johnstcnjohnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

👍 Nice catch!

return &smtp.SMTPError{Code: 501, EnhancedCode: smtp.EnhancedCode{5, 5, 4}, Message: "Rejected!"}
},
expectedErr: "SMTP error 501: Rejected!",
retryable: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Isn't this error non-retryable?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Not apriori; it could be a temporary failure.

Comment on lines +151 to +153
if s.backend.cfg.FailOnDataFn != nil {
return s.backend.cfg.FailOnDataFn()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

At some point in the future, would it make sense to test with something likemocktools/go-smtp-mock instead?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I evaluated it, but it does not support authentication.

@dannykoppingdannykopping merged commitc90be9b intomainAug 30, 2024
28 checks passed
@dannykoppingdannykopping deleted the dk/smtp-close branchAugust 30, 2024 09:37
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsAug 30, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@johnstcnjohnstcnjohnstcn approved these changes

@mtojekmtojekAwaiting requested review from mtojek

Assignees

@dannykoppingdannykopping

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@dannykopping@johnstcn

[8]ページ先頭

©2009-2025 Movatter.jp