Blame SOURCES/017-fix-CVE-2022-39229.patch

14d768
From 5aa2c77ac1ac544ed6b3a2c5efa767e53b810c3b Mon Sep 17 00:00:00 2001
14d768
From: linoman <2051016+linoman@users.noreply.github.com>
14d768
Date: Fri, 16 Sep 2022 10:46:44 +0200
14d768
Subject: [PATCH] fix CVE-2022-39229
14d768
14d768
Swap order of login fields
14d768
14d768
(cherry picked from commit 5ec176cada3d8adf651f844e3f707bc469495abd)
14d768
14d768
Add test for username/login field conflict
14d768
14d768
(cherry picked from commit 7aabcf26944835b0418eec6b057a0b186ff206bf)
14d768
14d768
Co-authored-by: linoman <2051016+linoman@users.noreply.github.com>
14d768
Co-authored-by: dsotirakis <dimitrios.sotirakis@grafana.com>
14d768
14d768
diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go
14d768
index 3dba16a75e..d773bd9dfe 100644
14d768
--- a/pkg/services/sqlstore/user.go
14d768
+++ b/pkg/services/sqlstore/user.go
14d768
@@ -298,19 +298,24 @@ func GetUserByLogin(query *models.GetUserByLoginQuery) error {
14d768
 		return models.ErrUserNotFound
14d768
 	}
14d768
 
14d768
-	// Try and find the user by login first.
14d768
-	// It's not sufficient to assume that a LoginOrEmail with an "@" is an email.
14d768
+	var has bool
14d768
+	var err error
14d768
 	user := &models.User{Login: query.LoginOrEmail}
14d768
-	has, err := x.Get(user)
14d768
 
14d768
-	if err != nil {
14d768
-		return err
14d768
+	// Since username can be an email address, attempt login with email address
14d768
+	// first if the login field has the "@" symbol.
14d768
+	if strings.Contains(query.LoginOrEmail, "@") {
14d768
+		user = &models.User{Email: query.LoginOrEmail}
14d768
+		has, err = x.Get(user)
14d768
+
14d768
+		if err != nil {
14d768
+			return err
14d768
+		}
14d768
 	}
14d768
 
14d768
-	if !has && strings.Contains(query.LoginOrEmail, "@") {
14d768
-		// If the user wasn't found, and it contains an "@" fallback to finding the
14d768
-		// user by email.
14d768
-		user = &models.User{Email: query.LoginOrEmail}
14d768
+	// Lookup the login field instead of email field
14d768
+	if !has {
14d768
+		user = &models.User{Login: query.LoginOrEmail}
14d768
 		has, err = x.Get(user)
14d768
 	}
14d768
 
14d768
diff --git a/pkg/services/sqlstore/user_test.go b/pkg/services/sqlstore/user_test.go
14d768
index aa796ffb02..7fb9d9be2a 100644
14d768
--- a/pkg/services/sqlstore/user_test.go
14d768
+++ b/pkg/services/sqlstore/user_test.go
14d768
@@ -42,6 +43,45 @@ func TestUserDataAccess(t *testing.T) {
14d768
 			})
14d768
 		})
14d768
 
14d768
+		Convey("Get User by login - user_2 uses user_1.email as login", func() {
14d768
+			ss = InitTestDB(t)
14d768
+
14d768
+			// create user_1
14d768
+			cmd1 := &models.CreateUserCommand{
14d768
+				Email:      "user_1@mail.com",
14d768
+				Name:       "user_1",
14d768
+				Login:      "user_1",
14d768
+				Password:   "user_1_password",
14d768
+				IsDisabled: true,
14d768
+			}
14d768
+			err := CreateUser(context.Background(), cmd1)
14d768
+			So(err, ShouldBeNil)
14d768
+
14d768
+			// create user_2
14d768
+			cmd2 := &models.CreateUserCommand{
14d768
+				Email:      "user_2@mail.com",
14d768
+				Name:       "user_2",
14d768
+				Login:      "user_1@mail.com",
14d768
+				Password:   "user_2_password",
14d768
+				IsDisabled: true,
14d768
+			}
14d768
+			err = CreateUser(context.Background(), cmd2)
14d768
+			So(err, ShouldBeNil)
14d768
+
14d768
+			// query user database for user_1 email
14d768
+			query := models.GetUserByLoginQuery{LoginOrEmail: "user_1@mail.com"}
14d768
+			err = GetUserByLogin(&query)
14d768
+			So(err, ShouldBeNil)
14d768
+
14d768
+			// expect user_1 as result
14d768
+			So(query.Result.Email, ShouldEqual, cmd1.Email)
14d768
+			So(query.Result.Login, ShouldEqual, cmd1.Login)
14d768
+			So(query.Result.Name, ShouldEqual, cmd1.Name)
14d768
+			So(query.Result.Email, ShouldNotEqual, cmd2.Email)
14d768
+			So(query.Result.Login, ShouldNotEqual, cmd2.Login)
14d768
+			So(query.Result.Name, ShouldNotEqual, cmd2.Name)
14d768
+		})
14d768
+
14d768
 		Convey("Creates disabled user", func() {
14d768
 			cmd := &models.CreateUserCommand{
14d768
 				Email:      "usertest@test.com",