Skip to content

Commit 2992f54

Browse files
committed
Merge branch 'master' into issue_13171
2 parents 4b1d4a8 + 9659808 commit 2992f54

31 files changed

+492
-170
lines changed

MAINTAINERS

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Mura Li <[email protected]> (@typeless)
3636
6543 <[email protected]> (@6543)
3737
jaqra <[email protected]> (@jaqra)
3838
David Svantesson <[email protected]> (@davidsvantesson)
39-
CirnoT <[email protected]> (@CirnoT)
4039
a1012112796 <[email protected]> (@a1012112796)
4140
Karl Heinz Marbaise <[email protected]> (@khmarbaise)
4241
Norwin Roosen <[email protected]> (@noerw)

cmd/admin.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,11 @@ func runChangePassword(c *cli.Context) error {
349349
if err != nil {
350350
return err
351351
}
352-
if user.Salt, err = models.GetUserSalt(); err != nil {
352+
if err = user.SetPassword(c.String("password")); err != nil {
353353
return err
354354
}
355-
user.HashPassword(c.String("password"))
356355

357-
if err := models.UpdateUserCols(user, "passwd", "passwd_hash_algo", "salt"); err != nil {
356+
if err = models.UpdateUserCols(user, "passwd", "passwd_hash_algo", "salt"); err != nil {
358357
return err
359358
}
360359

integrations/admin_user_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"strconv"
10+
"testing"
11+
12+
"code.gitea.io/gitea/models"
13+
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func TestAdminViewUsers(t *testing.T) {
18+
prepareTestEnv(t)
19+
20+
session := loginUser(t, "user1")
21+
req := NewRequest(t, "GET", "/admin/users")
22+
session.MakeRequest(t, req, http.StatusOK)
23+
24+
session = loginUser(t, "user2")
25+
req = NewRequest(t, "GET", "/admin/users")
26+
session.MakeRequest(t, req, http.StatusForbidden)
27+
}
28+
29+
func TestAdminViewUser(t *testing.T) {
30+
prepareTestEnv(t)
31+
32+
session := loginUser(t, "user1")
33+
req := NewRequest(t, "GET", "/admin/users/1")
34+
session.MakeRequest(t, req, http.StatusOK)
35+
36+
session = loginUser(t, "user2")
37+
req = NewRequest(t, "GET", "/admin/users/1")
38+
session.MakeRequest(t, req, http.StatusForbidden)
39+
}
40+
41+
func TestAdminEditUser(t *testing.T) {
42+
prepareTestEnv(t)
43+
44+
testSuccessfullEdit(t, models.User{ID: 2, Name: "newusername", LoginName: "otherlogin", Email: "[email protected]"})
45+
}
46+
47+
func testSuccessfullEdit(t *testing.T, formData models.User) {
48+
makeRequest(t, formData, http.StatusFound)
49+
}
50+
51+
func makeRequest(t *testing.T, formData models.User, headerCode int) {
52+
session := loginUser(t, "user1")
53+
csrf := GetCSRF(t, session, "/admin/users/"+strconv.Itoa(int(formData.ID)))
54+
req := NewRequestWithValues(t, "POST", "/admin/users/"+strconv.Itoa(int(formData.ID)), map[string]string{
55+
"_csrf": csrf,
56+
"user_name": formData.Name,
57+
"login_name": formData.LoginName,
58+
"login_type": "0-0",
59+
"email": formData.Email,
60+
})
61+
62+
session.MakeRequest(t, req, headerCode)
63+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: formData.ID}).(*models.User)
64+
assert.Equal(t, formData.Name, user.Name)
65+
assert.Equal(t, formData.LoginName, user.LoginName)
66+
assert.Equal(t, formData.Email, user.Email)
67+
}
68+
69+
func TestAdminDeleteUser(t *testing.T) {
70+
defer prepareTestEnv(t)()
71+
72+
session := loginUser(t, "user1")
73+
74+
csrf := GetCSRF(t, session, "/admin/users/8")
75+
req := NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{
76+
"_csrf": csrf,
77+
})
78+
session.MakeRequest(t, req, http.StatusOK)
79+
80+
assertUserDeleted(t, 8)
81+
models.CheckConsistencyFor(t, &models.User{})
82+
}

integrations/delete_user_test.go

-15
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,6 @@ func assertUserDeleted(t *testing.T, userID int64) {
2424
models.AssertNotExistsBean(t, &models.Star{UID: userID})
2525
}
2626

27-
func TestAdminDeleteUser(t *testing.T) {
28-
defer prepareTestEnv(t)()
29-
30-
session := loginUser(t, "user1")
31-
32-
csrf := GetCSRF(t, session, "/admin/users/8")
33-
req := NewRequestWithValues(t, "POST", "/admin/users/8/delete", map[string]string{
34-
"_csrf": csrf,
35-
})
36-
session.MakeRequest(t, req, http.StatusOK)
37-
38-
assertUserDeleted(t, 8)
39-
models.CheckConsistencyFor(t, &models.User{})
40-
}
41-
4227
func TestUserDeleteAccount(t *testing.T) {
4328
defer prepareTestEnv(t)()
4429

models/issue_comment.go

+45-20
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ func (opts *FindCommentsOptions) toConds() builder.Cond {
979979
if opts.Type != CommentTypeUnknown {
980980
cond = cond.And(builder.Eq{"comment.type": opts.Type})
981981
}
982-
if opts.Line > 0 {
982+
if opts.Line != 0 {
983983
cond = cond.And(builder.Eq{"comment.line": opts.Line})
984984
}
985985
if len(opts.TreePath) > 0 {
@@ -1078,18 +1078,35 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
10781078
if review == nil {
10791079
review = &Review{ID: 0}
10801080
}
1081-
//Find comments
10821081
opts := FindCommentsOptions{
10831082
Type: CommentTypeCode,
10841083
IssueID: issue.ID,
10851084
ReviewID: review.ID,
10861085
}
1086+
1087+
comments, err := findCodeComments(e, opts, issue, currentUser, review)
1088+
if err != nil {
1089+
return nil, err
1090+
}
1091+
1092+
for _, comment := range comments {
1093+
if pathToLineToComment[comment.TreePath] == nil {
1094+
pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment)
1095+
}
1096+
pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment)
1097+
}
1098+
return pathToLineToComment, nil
1099+
}
1100+
1101+
func findCodeComments(e Engine, opts FindCommentsOptions, issue *Issue, currentUser *User, review *Review) ([]*Comment, error) {
1102+
var comments []*Comment
1103+
if review == nil {
1104+
review = &Review{ID: 0}
1105+
}
10871106
conds := opts.toConds()
10881107
if review.ID == 0 {
10891108
conds = conds.And(builder.Eq{"invalidated": false})
10901109
}
1091-
1092-
var comments []*Comment
10931110
if err := e.Where(conds).
10941111
Asc("comment.created_unix").
10951112
Asc("comment.id").
@@ -1117,7 +1134,19 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
11171134
return nil, err
11181135
}
11191136

1137+
n := 0
11201138
for _, comment := range comments {
1139+
if re, ok := reviews[comment.ReviewID]; ok && re != nil {
1140+
// If the review is pending only the author can see the comments (except if the review is set)
1141+
if review.ID == 0 && re.Type == ReviewTypePending &&
1142+
(currentUser == nil || currentUser.ID != re.ReviewerID) {
1143+
continue
1144+
}
1145+
comment.Review = re
1146+
}
1147+
comments[n] = comment
1148+
n++
1149+
11211150
if err := comment.LoadResolveDoer(); err != nil {
11221151
return nil, err
11231152
}
@@ -1126,25 +1155,21 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
11261155
return nil, err
11271156
}
11281157

1129-
if re, ok := reviews[comment.ReviewID]; ok && re != nil {
1130-
// If the review is pending only the author can see the comments (except the review is set)
1131-
if review.ID == 0 {
1132-
if re.Type == ReviewTypePending &&
1133-
(currentUser == nil || currentUser.ID != re.ReviewerID) {
1134-
continue
1135-
}
1136-
}
1137-
comment.Review = re
1138-
}
1139-
11401158
comment.RenderedContent = string(markdown.Render([]byte(comment.Content), issue.Repo.Link(),
11411159
issue.Repo.ComposeMetas()))
1142-
if pathToLineToComment[comment.TreePath] == nil {
1143-
pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment)
1144-
}
1145-
pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment)
11461160
}
1147-
return pathToLineToComment, nil
1161+
return comments[:n], nil
1162+
}
1163+
1164+
// FetchCodeCommentsByLine fetches the code comments for a given treePath and line number
1165+
func FetchCodeCommentsByLine(issue *Issue, currentUser *User, treePath string, line int64) ([]*Comment, error) {
1166+
opts := FindCommentsOptions{
1167+
Type: CommentTypeCode,
1168+
IssueID: issue.ID,
1169+
TreePath: treePath,
1170+
Line: line,
1171+
}
1172+
return findCodeComments(x, opts, issue, currentUser, nil)
11481173
}
11491174

11501175
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line

models/login_source.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,10 @@ func UserSignIn(username, password string) (*User, error) {
771771

772772
// Update password hash if server password hash algorithm have changed
773773
if user.PasswdHashAlgo != setting.PasswordHashAlgo {
774-
user.HashPassword(password)
775-
if err := UpdateUserCols(user, "passwd", "passwd_hash_algo"); err != nil {
774+
if err = user.SetPassword(password); err != nil {
775+
return nil, err
776+
}
777+
if err = UpdateUserCols(user, "passwd", "passwd_hash_algo", "salt"); err != nil {
776778
return nil, err
777779
}
778780
}

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ var migrations = []Migration{
277277
NewMigration("Add scope and nonce columns to oauth2_grant table", addScopeAndNonceColumnsToOAuth2Grant),
278278
// v165 -> v166
279279
NewMigration("Convert hook task type from char(16) to varchar(16) and trim the column", convertHookTaskTypeToVarcharAndTrim),
280+
// v166 -> v167
281+
NewMigration("Where Password is Valid with Empty String delete it", recalculateUserEmptyPWD),
280282
}
281283

282284
// GetCurrentDBVersion returns the current db version

models/migrations/v166.go

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"crypto/sha256"
9+
"fmt"
10+
11+
"golang.org/x/crypto/argon2"
12+
"golang.org/x/crypto/bcrypt"
13+
"golang.org/x/crypto/pbkdf2"
14+
"golang.org/x/crypto/scrypt"
15+
"xorm.io/builder"
16+
"xorm.io/xorm"
17+
)
18+
19+
func recalculateUserEmptyPWD(x *xorm.Engine) (err error) {
20+
const (
21+
algoBcrypt = "bcrypt"
22+
algoScrypt = "scrypt"
23+
algoArgon2 = "argon2"
24+
algoPbkdf2 = "pbkdf2"
25+
)
26+
27+
type User struct {
28+
ID int64 `xorm:"pk autoincr"`
29+
Passwd string `xorm:"NOT NULL"`
30+
PasswdHashAlgo string `xorm:"NOT NULL DEFAULT 'argon2'"`
31+
MustChangePassword bool `xorm:"NOT NULL DEFAULT false"`
32+
LoginType int
33+
LoginName string
34+
Type int
35+
Salt string `xorm:"VARCHAR(10)"`
36+
}
37+
38+
// hashPassword hash password based on algo and salt
39+
// state 461406070c
40+
hashPassword := func(passwd, salt, algo string) string {
41+
var tempPasswd []byte
42+
43+
switch algo {
44+
case algoBcrypt:
45+
tempPasswd, _ = bcrypt.GenerateFromPassword([]byte(passwd), bcrypt.DefaultCost)
46+
return string(tempPasswd)
47+
case algoScrypt:
48+
tempPasswd, _ = scrypt.Key([]byte(passwd), []byte(salt), 65536, 16, 2, 50)
49+
case algoArgon2:
50+
tempPasswd = argon2.IDKey([]byte(passwd), []byte(salt), 2, 65536, 8, 50)
51+
case algoPbkdf2:
52+
fallthrough
53+
default:
54+
tempPasswd = pbkdf2.Key([]byte(passwd), []byte(salt), 10000, 50, sha256.New)
55+
}
56+
57+
return fmt.Sprintf("%x", tempPasswd)
58+
}
59+
60+
// ValidatePassword checks if given password matches the one belongs to the user.
61+
// state 461406070c, changed since it's not necessary to be time constant
62+
ValidatePassword := func(u *User, passwd string) bool {
63+
tempHash := hashPassword(passwd, u.Salt, u.PasswdHashAlgo)
64+
65+
if u.PasswdHashAlgo != algoBcrypt && u.Passwd == tempHash {
66+
return true
67+
}
68+
if u.PasswdHashAlgo == algoBcrypt && bcrypt.CompareHashAndPassword([]byte(u.Passwd), []byte(passwd)) == nil {
69+
return true
70+
}
71+
return false
72+
}
73+
74+
sess := x.NewSession()
75+
defer sess.Close()
76+
77+
const batchSize = 100
78+
79+
for start := 0; ; start += batchSize {
80+
users := make([]*User, 0, batchSize)
81+
if err = sess.Limit(batchSize, start).Where(builder.Neq{"passwd": ""}, 0).Find(&users); err != nil {
82+
return
83+
}
84+
if len(users) == 0 {
85+
break
86+
}
87+
88+
if err = sess.Begin(); err != nil {
89+
return
90+
}
91+
92+
for _, user := range users {
93+
if ValidatePassword(user, "") {
94+
user.Passwd = ""
95+
user.Salt = ""
96+
user.PasswdHashAlgo = ""
97+
if _, err = sess.ID(user.ID).Cols("passwd", "salt", "passwd_hash_algo").Update(user); err != nil {
98+
return err
99+
}
100+
}
101+
}
102+
103+
if err = sess.Commit(); err != nil {
104+
return
105+
}
106+
}
107+
108+
// delete salt and algo where password is empty
109+
if _, err = sess.Where(builder.Eq{"passwd": ""}.And(builder.Neq{"salt": ""}.Or(builder.Neq{"passwd_hash_algo": ""}))).
110+
Cols("salt", "passwd_hash_algo").Update(&User{}); err != nil {
111+
return err
112+
}
113+
114+
return sess.Commit()
115+
}

0 commit comments

Comments
 (0)