Skip to content

Commit 858324c

Browse files
ethantkoeniglafriks
authored andcommitted
Fix username rendering bug (#2122)
* Fix username rendering bug * XSS integration test * Migration to unescape user full names
1 parent 2c3efd7 commit 858324c

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

integrations/xss_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2017 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+
"testing"
10+
11+
"code.gitea.io/gitea/models"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestXSSUserFullName(t *testing.T) {
17+
prepareTestEnv(t)
18+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
19+
const fullName = `name & <script class="evil">alert('Oh no!');</script>`
20+
21+
session := loginUser(t, user.Name)
22+
req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
23+
"_csrf": GetCSRF(t, session, "/user/settings"),
24+
"name": user.Name,
25+
"full_name": fullName,
26+
"email": user.Email,
27+
})
28+
session.MakeRequest(t, req, http.StatusFound)
29+
30+
req = NewRequestf(t, "GET", "/%s", user.Name)
31+
resp := session.MakeRequest(t, req, http.StatusOK)
32+
htmlDoc := NewHTMLParser(t, resp.Body)
33+
assert.EqualValues(t, 0, htmlDoc.doc.Find("script.evil").Length())
34+
assert.EqualValues(t, fullName,
35+
htmlDoc.doc.Find("div.content").Find(".header.text.center").Text(),
36+
)
37+
}

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ var migrations = []Migration{
122122
NewMigration("adds comment to an action", addCommentIDToAction),
123123
// v36 -> v37
124124
NewMigration("regenerate git hooks", regenerateGitHooks36),
125+
// v37 -> v38
126+
NewMigration("unescape user full names", unescapeUserFullNames),
125127
}
126128

127129
// Migrate database to current version

models/migrations/v37.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2017 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+
"html"
9+
10+
"code.gitea.io/gitea/models"
11+
12+
"github.com/go-xorm/xorm"
13+
)
14+
15+
func unescapeUserFullNames(x *xorm.Engine) (err error) {
16+
const batchSize = 100
17+
for start := 0; ; start += batchSize {
18+
users := make([]*models.User, 0, batchSize)
19+
if err := x.Limit(start, batchSize).Find(users); err != nil {
20+
return err
21+
}
22+
if len(users) == 0 {
23+
return nil
24+
}
25+
for _, user := range users {
26+
user.FullName = html.UnescapeString(user.FullName)
27+
if _, err := x.Cols("full_name").Update(user); err != nil {
28+
return err
29+
}
30+
}
31+
}
32+
}

models/user.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"code.gitea.io/gitea/modules/avatar"
3636
"code.gitea.io/gitea/modules/base"
3737
"code.gitea.io/gitea/modules/log"
38-
"code.gitea.io/gitea/modules/markdown"
3938
"code.gitea.io/gitea/modules/setting"
4039
)
4140

@@ -164,8 +163,6 @@ func (u *User) UpdateDiffViewStyle(style string) error {
164163
// AfterSet is invoked from XORM after setting the value of a field of this object.
165164
func (u *User) AfterSet(colName string, _ xorm.Cell) {
166165
switch colName {
167-
case "full_name":
168-
u.FullName = markdown.Sanitize(u.FullName)
169166
case "created_unix":
170167
u.Created = time.Unix(u.CreatedUnix, 0).Local()
171168
case "updated_unix":
@@ -871,7 +868,6 @@ func updateUser(e Engine, u *User) error {
871868
u.Website = base.TruncateString(u.Website, 255)
872869
u.Description = base.TruncateString(u.Description, 255)
873870

874-
u.FullName = markdown.Sanitize(u.FullName)
875871
_, err := e.Id(u.ID).AllCols().Update(u)
876872
return err
877873
}

0 commit comments

Comments
 (0)