Skip to content
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

fix: patch secure email change (double confirm) response format. #1241

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions internal/api/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func (a *API) verifyPost(w http.ResponseWriter, r *http.Request, params *VerifyP
grantParams models.GrantParams
token *AccessTokenResponse
)
var isSingleConfirmationResponse = false

err := db.Transaction(func(tx *storage.Connection) error {
var terr error
Expand All @@ -253,10 +254,8 @@ func (a *API) verifyPost(w http.ResponseWriter, r *http.Request, params *VerifyP
case emailChangeVerification:
user, terr = a.emailChangeVerify(r, ctx, tx, params, user)
if user == nil && terr == nil {
return sendJSON(w, http.StatusOK, map[string]string{
"msg": singleConfirmationAccepted,
"code": strconv.Itoa(http.StatusOK),
})
isSingleConfirmationResponse = true
return nil
}
case smsVerification, phoneChangeVerification:
user, terr = a.smsVerify(r, ctx, tx, user, params.Type)
Expand All @@ -280,6 +279,12 @@ func (a *API) verifyPost(w http.ResponseWriter, r *http.Request, params *VerifyP
if err != nil {
return err
}
if isSingleConfirmationResponse {
return sendJSON(w, http.StatusOK, map[string]string{
"msg": singleConfirmationAccepted,
"code": strconv.Itoa(http.StatusOK),
})
}
return sendJSON(w, http.StatusOK, token)
}

Expand Down Expand Up @@ -448,9 +453,9 @@ func (a *API) emailChangeVerify(r *http.Request, ctx context.Context, conn *stor
if config.Mailer.SecureEmailChangeEnabled && user.EmailChangeConfirmStatus == zeroConfirmation && user.GetEmail() != "" {
err := conn.Transaction(func(tx *storage.Connection) error {
user.EmailChangeConfirmStatus = singleConfirmation
if params.Token == user.EmailChangeTokenCurrent {
if params.Token == user.EmailChangeTokenCurrent || params.TokenHash == user.EmailChangeTokenCurrent {
user.EmailChangeTokenCurrent = ""
} else if params.Token == user.EmailChangeTokenNew {
} else if params.Token == user.EmailChangeTokenNew || params.TokenHash == user.EmailChangeTokenNew {
user.EmailChangeTokenNew = ""
}
if terr := tx.UpdateOnly(user, "email_change_confirm_status", "email_change_token_current", "email_change_token_new"); terr != nil {
Expand Down
78 changes: 77 additions & 1 deletion internal/api/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,6 @@ func (ts *VerifyTestSuite) TestVerifySignupWithredirectURLContainedPath() {
}

func (ts *VerifyTestSuite) TestVerifyPKCEOTP() {

u, err := models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
u.ConfirmationToken = "pkce_confirmation_token"
Expand Down Expand Up @@ -935,6 +934,83 @@ func (ts *VerifyTestSuite) TestVerifyValidOtp() {
}
}

func (ts *VerifyTestSuite) TestSecureEmailChangeWithTokenHash() {
ts.Config.Mailer.SecureEmailChangeEnabled = true
u, err := models.FindUserByEmailAndAudience(ts.API.db, "[email protected]", ts.Config.JWT.Aud)
require.NoError(ts.T(), err)
u.EmailChange = "[email protected]"
require.NoError(ts.T(), ts.API.db.Update(u))

currentEmailChangeToken := crypto.GenerateTokenHash(string(u.Email), "123456")
newEmailChangeToken := crypto.GenerateTokenHash(u.EmailChange, "123456")

cases := []struct {
desc string
firstVerificationBody map[string]interface{}
secondVerificationBody map[string]interface{}
expectedStatus int
}{
{
desc: "Secure Email Change with Token Hash (Success)",
firstVerificationBody: map[string]interface{}{
"type": emailChangeVerification,
"token_hash": currentEmailChangeToken,
},
secondVerificationBody: map[string]interface{}{
"type": emailChangeVerification,
"token_hash": newEmailChangeToken,
},
expectedStatus: http.StatusOK,
},
{
desc: "Secure Email Change with Token Hash. Reusing a token hash twice should fail",
firstVerificationBody: map[string]interface{}{
"type": emailChangeVerification,
"token_hash": currentEmailChangeToken,
},
secondVerificationBody: map[string]interface{}{
"type": emailChangeVerification,
"token_hash": currentEmailChangeToken,
},
expectedStatus: http.StatusUnauthorized,
},
}
for _, c := range cases {
ts.Run(c.desc, func() {
// Set the corresponding email change tokens
u.EmailChangeTokenCurrent = currentEmailChangeToken
u.EmailChangeTokenNew = newEmailChangeToken

currentTime := time.Now()
u.EmailChangeSentAt = &currentTime
require.NoError(ts.T(), ts.API.db.Update(u))

var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(c.firstVerificationBody))

// Setup request
req := httptest.NewRequest(http.MethodPost, "http://localhost/verify", &buffer)
req.Header.Set("Content-Type", "application/json")

// Setup response recorder
w := httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(c.secondVerificationBody))

// Setup second request
req = httptest.NewRequest(http.MethodPost, "http://localhost/verify", &buffer)
req.Header.Set("Content-Type", "application/json")

// Setup second response recorder
w = httptest.NewRecorder()
ts.API.handler.ServeHTTP(w, req)
assert.Equal(ts.T(), c.expectedStatus, w.Code)
})

}

}

func (ts *VerifyTestSuite) TestPrepRedirectURL() {
escapedMessage := url.QueryEscape(singleConfirmationAccepted)
cases := []struct {
Expand Down
Loading