2017-07-12 10:58:52 -04:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2017-07-12 10:58:52 -04:00
2022-09-02 15:18:23 -04:00
package integration
2017-07-12 10:58:52 -04:00
import (
"net/http"
"testing"
2021-11-16 16:53:21 +08:00
"code.gitea.io/gitea/models/unittest"
2021-11-24 17:49:20 +08:00
user_model "code.gitea.io/gitea/models/user"
2022-09-02 15:18:23 -04:00
"code.gitea.io/gitea/tests"
2017-07-12 10:58:52 -04:00
"github.com/stretchr/testify/assert"
)
func TestXSSUserFullName ( t * testing . T ) {
2022-09-02 15:18:23 -04:00
defer tests . PrepareTestEnv ( t ) ( )
2022-08-16 10:22:25 +08:00
user := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 2 } )
2017-07-12 10:58:52 -04:00
const fullName = ` name & <script class="evil">alert('Oh no!');</script> `
session := loginUser ( t , user . Name )
req := NewRequestWithValues ( t , "POST" , "/user/settings" , map [ string ] string {
"_csrf" : GetCSRF ( t , session , "/user/settings" ) ,
"name" : user . Name ,
"full_name" : fullName ,
"email" : user . Email ,
2020-12-04 07:20:30 +01:00
"language" : "en-US" ,
2017-07-12 10:58:52 -04:00
} )
2022-03-23 05:54:07 +01:00
session . MakeRequest ( t , req , http . StatusSeeOther )
2017-07-12 10:58:52 -04:00
req = NewRequestf ( t , "GET" , "/%s" , user . Name )
resp := session . MakeRequest ( t , req , http . StatusOK )
htmlDoc := NewHTMLParser ( t , resp . Body )
assert . EqualValues ( t , 0 , htmlDoc . doc . Find ( "script.evil" ) . Length ( ) )
assert . EqualValues ( t , fullName ,
htmlDoc . doc . Find ( "div.content" ) . Find ( ".header.text.center" ) . Text ( ) ,
)
}