@@ -5,7 +5,7 @@ package integration
5
5
6
6
import (
7
7
"context"
8
- "fmt "
8
+ "html "
9
9
"net/http"
10
10
"net/url"
11
11
"os"
@@ -27,7 +27,7 @@ import (
27
27
func TestXSSUserFullName (t * testing.T ) {
28
28
defer tests .PrepareTestEnv (t )()
29
29
user := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
30
- const fullName = `name & <script class="evil">alert('Oh no! ');</script>`
30
+ const fullName = `name & <script class="evil">alert('xss ');</script>`
31
31
32
32
session := loginUser (t , user .Name )
33
33
req := NewRequestWithValues (t , "POST" , "/user/settings" , map [string ]string {
@@ -43,58 +43,55 @@ func TestXSSUserFullName(t *testing.T) {
43
43
resp := session .MakeRequest (t , req , http .StatusOK )
44
44
htmlDoc := NewHTMLParser (t , resp .Body )
45
45
assert .EqualValues (t , 0 , htmlDoc .doc .Find ("script.evil" ).Length ())
46
- assert . EqualValues ( t , fullName ,
47
- htmlDoc . doc . Find ( "div.content" ). Find ( ".header.text.center" ). Text (),
48
- )
46
+ htmlCode , err := htmlDoc . doc . Find ( "div.content" ). Find ( ".header.text.center" ). Html ()
47
+ assert . NoError ( t , err )
48
+ assert . EqualValues ( t , html . EscapeString ( fullName ), htmlCode )
49
49
}
50
50
51
51
func TestXSSWikiLastCommitInfo (t * testing.T ) {
52
52
onGiteaRun (t , func (t * testing.T , u * url.URL ) {
53
- // Prepare the environment.
54
53
dstPath := t .TempDir ()
55
- r := fmt .Sprintf ("%suser2/repo1.wiki.git" , u .String ())
56
- u , err := url .Parse (r )
54
+ cloneWikiURL , err := url .Parse (u .String () + "user2/repo1.wiki.git" )
57
55
assert .NoError (t , err )
58
- u .User = url .UserPassword ("user2" , userPassword )
59
- assert .NoError (t , git .CloneWithArgs (context .Background (), git .AllowLFSFiltersArgs (), u .String (), dstPath , git.CloneRepoOptions {}))
56
+ cloneWikiURL .User = url .UserPassword ("user2" , userPassword )
57
+ assert .NoError (t , git .CloneWithArgs (context .Background (), git .AllowLFSFiltersArgs (), cloneWikiURL .String (), dstPath , git.CloneRepoOptions {}))
60
58
61
59
// Use go-git here, because using git wouldn't work, it has code to remove
62
60
// `<`, `>` and `\n` in user names. Even though this is permitted and
63
61
// wouldn't result in a error by a Git server.
64
62
gitRepo , err := gogit .PlainOpen (dstPath )
65
- if err != nil {
66
- panic ( err )
63
+ if ! assert . NoError ( t , err ) {
64
+ return
67
65
}
68
-
69
66
w , err := gitRepo .Worktree ()
70
- if err != nil {
71
- panic ( err )
67
+ if ! assert . NoError ( t , err ) {
68
+ return
72
69
}
73
70
74
71
filename := filepath .Join (dstPath , "Home.md" )
75
- err = os .WriteFile (filename , []byte ("Oh, a XSS attack? " ), 0o644 )
72
+ err = os .WriteFile (filename , []byte ("dummy content " ), 0o644 )
76
73
if ! assert .NoError (t , err ) {
77
- t . FailNow ()
74
+ return
78
75
}
79
76
80
77
_ , err = w .Add ("Home.md" )
81
78
if ! assert .NoError (t , err ) {
82
- t . FailNow ()
79
+ return
83
80
}
84
81
85
- _ , err = w .Commit ("Yay XSS " , & gogit.CommitOptions {
82
+ _ , err = w .Commit ("dummy message " , & gogit.CommitOptions {
86
83
Author : & object.Signature {
87
- Name : `Gusted <script class="evil">alert('Oh no! ');</script>` ,
84
+ Name : `foo <script class="evil">alert('xss ');</script>bar ` ,
88
85
89
- When : time .Date (2024 , time .January , 31 , 0 , 0 , 0 , 0 , time .UTC ),
86
+ When : time .Date (2001 , time .January , 31 , 0 , 0 , 0 , 0 , time .UTC ),
90
87
},
91
88
})
92
89
if ! assert .NoError (t , err ) {
93
- t . FailNow ()
90
+ return
94
91
}
95
92
96
93
// Push.
97
- _ , _ , err = git .NewCommand (git .DefaultContext , "push" ).AddArguments (git . ToTrustedCmdArgs ([] string { "origin" , "master" }) ... ).RunStdString (& git.RunOpts {Dir : dstPath })
94
+ _ , _ , err = git .NewCommand (git .DefaultContext , "push" ).AddArguments ("origin" , "master" ).RunStdString (& git.RunOpts {Dir : dstPath })
98
95
assert .NoError (t , err )
99
96
100
97
// Check on page view.
@@ -106,7 +103,9 @@ func TestXSSWikiLastCommitInfo(t *testing.T) {
106
103
htmlDoc := NewHTMLParser (t , resp .Body )
107
104
108
105
htmlDoc .AssertElement (t , "script.evil" , false )
109
- assert .EqualValues (t , `Gusted edited this page 0001-01-01 00:00:00 +00:00` , strings .TrimSpace (htmlDoc .Find (".ui.sub.header" ).Text ()))
106
+ htmlCode , err := htmlDoc .Find (".ui.sub.header" ).Html ()
107
+ assert .NoError (t , err )
108
+ assert .EqualValues (t , `foo<script class="evil">alert('xss');</script>bar edited this page <relative-time class="time-since" prefix="" tense="past" datetime="2001-01-31T00:00:00Z" data-tooltip-content="" data-tooltip-interactive="true">2001-01-31 00:00:00 +00:00</relative-time>` , strings .TrimSpace (htmlCode ))
110
109
})
111
110
112
111
// Check on revisions page.
@@ -118,7 +117,9 @@ func TestXSSWikiLastCommitInfo(t *testing.T) {
118
117
htmlDoc := NewHTMLParser (t , resp .Body )
119
118
120
119
htmlDoc .AssertElement (t , "script.evil" , false )
121
- assert .EqualValues (t , `Gusted edited this page 0001-01-01 00:00:00 +00:00` , strings .TrimSpace (htmlDoc .Find (".ui.sub.header" ).Text ()))
120
+ htmlCode , err := htmlDoc .Find (".ui.sub.header" ).Html ()
121
+ assert .NoError (t , err )
122
+ assert .EqualValues (t , `foo<script class="evil">alert('xss');</script>bar edited this page <relative-time class="time-since" prefix="" tense="past" datetime="2001-01-31T00:00:00Z" data-tooltip-content="" data-tooltip-interactive="true">2001-01-31 00:00:00 +00:00</relative-time>` , strings .TrimSpace (htmlCode ))
122
123
})
123
124
})
124
125
}
0 commit comments