Skip to content

fix: move email & sms send out of the POST /user transaction #2022

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hf
Copy link
Contributor

@hf hf commented May 14, 2025

Keeping this inside the transaction can exhaust the database pool grinding full Auth to a halt, especially if the SMTP server or hook takes more than a few milliseconds to complete.

@hf hf requested a review from a team as a code owner May 14, 2025 08:14
@coveralls
Copy link

coveralls commented May 14, 2025

Pull Request Test Coverage Report for Build 15084496549

Details

  • 25 of 32 (78.13%) changed or added relevant lines in 3 files are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.01%) to 69.623%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/api/phone.go 1 2 50.0%
internal/api/mail.go 9 11 81.82%
internal/api/user.go 15 19 78.95%
Files with Coverage Reduction New Missed Lines %
internal/api/phone.go 1 83.05%
internal/api/user.go 4 65.8%
Totals Coverage Status
Change from base Build 15084415331: -0.01%
Covered Lines: 10960
Relevant Lines: 15742

💛 - Coveralls

Copy link
Contributor

@cstockton cstockton left a comment

Choose a reason for hiding this comment

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

Apologies if you already considered this, but I think it's worth noting this is a really big behavior change and I worry without extensive tests there could be potential bugs or potential security considerations we are missing. I think it's worth thinking about what is done and rolled back in the current transaction.

For example the transaction in UserUpdate will:

  • UpdatePassword
  • UpdateUserMetadata
  • UpdateAppMetadata
  • Conditionally:
    • Call emailChangeVerify
      • Calls ClearOneTimeTokenForUser
      • Updates confirmation status
      • Update identities and perform email confirmation

Knowing that I think it's worth evaluating what sendEmailChange does and how it fails. For example it will call validateSentWithinFrequencyLimit, have we carefully considered the consequences of leaving the transaction in UserUpdate committed without performing all the write operations within sendEmailChange.

@@ -263,5 +261,19 @@ func (a *API) UserUpdate(w http.ResponseWriter, r *http.Request) error {
return err
}

if sendEmailChange {
// email sending should not hold a database transaction open as latency incurred by SMTP or HTTP hooks can exhaust the database pool
if err := a.sendEmailChange(r, db, user, params.Email, flowType); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Have we thought through the consequences of not rolling back the previous values in the transaction when sendEmailChange fails at every failure point?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In general I think it should be fine. Yea the email send will fail, but the request will also fail and user can ask for re-send when email sending / SMS sending is back online.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we need to do the rate limit prior the transaction as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@hf hf force-pushed the hf/move-email-sms-send-out-of-update-user-transaction branch from 278aa80 to 01c04ef Compare May 17, 2025 10:55
@hf hf requested a review from cstockton May 17, 2025 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants