@@ -7,32 +7,65 @@ import (
7
7
"testing"
8
8
9
9
"code.gitea.io/gitea/modules/setting"
10
+ "code.gitea.io/gitea/modules/test"
10
11
11
12
"github.com/stretchr/testify/assert"
12
13
)
13
14
14
- func TestIsRiskyRedirectURL (t * testing.T ) {
15
- setting .AppURL = "http://localhost:3000/"
16
- tests := []struct {
17
- input string
18
- want bool
19
- }{
20
- {"" , false },
21
- {"foo" , false },
22
- {"/" , false },
23
- {"/foo?k=%20#abc" , false },
15
+ func TestIsRelativeURL (t * testing.T ) {
16
+ defer test .MockVariableValue (& setting .AppURL , "http://localhost:3000/sub/" )()
17
+ defer test .MockVariableValue (& setting .AppSubURL , "/sub" )()
18
+ rel := []string {
19
+ "" ,
20
+ "foo" ,
21
+ "/" ,
22
+ "/foo?k=%20#abc" ,
23
+ }
24
+ for _ , s := range rel {
25
+ assert .True (t , IsRelativeURL (s ), "rel = %q" , s )
26
+ }
27
+ abs := []string {
28
+ "//" ,
29
+ "\\ \\ " ,
30
+ "/\\ " ,
31
+ "\\ /" ,
32
+ "mailto:a@b.com" ,
33
+ "https://test.com" ,
34
+ }
35
+ for _ , s := range abs {
36
+ assert .False (t , IsRelativeURL (s ), "abs = %q" , s )
37
+ }
38
+ }
24
39
25
- {"//" , true },
26
- {"\\ \\ " , true },
27
- {"/\\ " , true },
28
- {"\\ /" , true },
29
- {"mail:a@b.com" , true },
30
- {"https://test.com" , true },
31
- {setting .AppURL + "/foo" , false },
32
- }
33
- for _ , tt := range tests {
34
- t .Run (tt .input , func (t * testing.T ) {
35
- assert .Equal (t , tt .want , IsRiskyRedirectURL (tt .input ))
36
- })
40
+ func TestIsCurrentGiteaSiteURL (t * testing.T ) {
41
+ defer test .MockVariableValue (& setting .AppURL , "http://localhost:3000/sub/" )()
42
+ defer test .MockVariableValue (& setting .AppSubURL , "/sub" )()
43
+ good := []string {
44
+ "?key=val" ,
45
+ "/sub" ,
46
+ "/sub/" ,
47
+ "/sub/foo" ,
48
+ "/sub/foo/" ,
49
+ "http://localhost:3000/sub?key=val" ,
50
+ "http://localhost:3000/sub/" ,
37
51
}
52
+ for _ , s := range good {
53
+ assert .True (t , IsCurrentGiteaSiteURL (s ), "good = %q" , s )
54
+ }
55
+ bad := []string {
56
+ "/" ,
57
+ "//" ,
58
+ "\\ \\ " ,
59
+ "/foo" ,
60
+ "http://localhost:3000/sub/.." ,
61
+ "http://localhost:3000/other" ,
62
+ "http://other/" ,
63
+ }
64
+ for _ , s := range bad {
65
+ assert .False (t , IsCurrentGiteaSiteURL (s ), "bad = %q" , s )
66
+ }
67
+
68
+ setting .AppURL = "http://localhost:3000/"
69
+ setting .AppSubURL = ""
70
+ assert .True (t , IsCurrentGiteaSiteURL ("http://localhost:3000?key=val" ))
38
71
}
0 commit comments