-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath_test.go
More file actions
143 lines (120 loc) · 3.75 KB
/
Copy pathpath_test.go
File metadata and controls
143 lines (120 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package hime
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuildPath(t *testing.T) {
t.Parallel()
cases := []struct {
Input string
Output string
}{
{"", "/"},
{"/", "/"},
{"/p", "/p"},
{"/p/123", "/p/123"},
{"https://google.com", "https://google.com/"},
{"https://google.com/test", "https://google.com/test"},
{"https://google.com/test/", "https://google.com/test/"},
{"https://google.com/test?p=1", "https://google.com/test?p=1"},
{"http://google.com/test?p=1", "http://google.com/test?p=1"},
{"//a?p=1", "//a/?p=1"},
{"app:///a?p=1", "app:///a?p=1"},
}
for _, c := range cases {
assert.Equal(t, c.Output, buildPath(c.Input))
}
}
func TestBuildPathParams(t *testing.T) {
t.Parallel()
cases := []struct {
Base string
Params []any
Output string
}{
{"", []any{""}, "/"},
{"/", []any{"/"}, "/"},
{"/a", []any{}, "/a"},
{"/a", []any{"/b"}, "/a/b"},
{"/a?x=1", []any{"/b"}, "/a/b?x=1"},
{"/a/", []any{"/b/", "/c/"}, "/a/b/c"},
{"/a", []any{url.Values{"id": []string{"10"}}}, "/a?id=10"},
{"/a", []any{"/b", url.Values{"id": []string{"10"}}}, "/a/b?id=10"},
{"/a", []any{"/b/", url.Values{"id": []string{"10"}}}, "/a/b?id=10"},
{"/a", []any{"/b/", map[string]string{"id": "10"}}, "/a/b?id=10"},
{"/a", []any{"/b/", map[string]any{"id": 10}}, "/a/b?id=10"},
{"/a", []any{"/b", &Param{Name: "id", Value: 3456}}, "/a/b?id=3456"},
{"/a?x=1", []any{"/b", &Param{Name: "id", Value: 3456}}, "/a/b?id=3456&x=1"},
}
for _, c := range cases {
assert.Equal(t, c.Output, buildPath(c.Base, c.Params...))
}
}
func TestSafeRedirectPath(t *testing.T) {
t.Parallel()
cases := []struct {
Input string
Output string
}{
{"", "/"},
{"/", "/"},
{"/p", "/p"},
{"/p/123", "/p/123"},
{"https://google.com", "/"},
{"https://google.com/test", "/test"},
{"https://google.com/test?p=1", "/test?p=1"},
{"http://google.com/test?p=1", "/test?p=1"},
{"//a?p=1", "/a?p=1"},
{"app:///a?p=1", "/a?p=1"},
{"/p/123?id=3", "/p/123?id=3"},
{"/p/123/?id=3", "/p/123/?id=3"},
}
for _, c := range cases {
assert.Equal(t, c.Output, SafeRedirectPath(c.Input))
}
}
func TestBuildPathInvalidURL(t *testing.T) {
t.Parallel()
// invalid percent-encoding makes url.Parse fail, which buildPath turns
// into a panic.
assert.Panics(t, func() { buildPath("%zz") })
}
func TestSafeRedirectPathInvalid(t *testing.T) {
t.Parallel()
// url.ParseRequestURI rejects the input, so SafeRedirectPath defaults to "/".
assert.Equal(t, "/", SafeRedirectPath("%zz"))
}
func TestSafeRedirectPathClean(t *testing.T) {
t.Parallel()
// SafeRedirectPath runs path.Clean, normalizing traversal sequences.
cases := []struct {
Input string
Output string
}{
{"/a/../../etc/passwd", "/etc/passwd"},
{"/a/./b", "/a/b"},
{"/foo/..", "/"},
}
for _, c := range cases {
assert.Equal(t, c.Output, SafeRedirectPath(c.Input), "input: %s", c.Input)
}
}
func TestBuildPathParamMultiValue(t *testing.T) {
t.Parallel()
// Same query key from base and params is appended, not replaced.
assert.Equal(t, "/a?x=1&x=2", buildPath("/a?x=1", url.Values{"x": []string{"2"}}))
assert.Equal(t, "/a?x=1&x=2", buildPath("/a", map[string]string{"x": "1"}, map[string]string{"x": "2"}))
}
func TestBuildPathTraversalNotCleaned(t *testing.T) {
t.Parallel()
// Unlike SafeRedirectPath, buildPath uses path.Join (not path.Clean) for
// path params, so leading ".." segments are preserved.
assert.Equal(t, "/a/../../etc/passwd", buildPath("/a", "../../etc/passwd"))
}
func TestBuildPathZeroValues(t *testing.T) {
t.Parallel()
assert.Equal(t, "/a?z=0", buildPath("/a", map[string]any{"z": 0}))
assert.Equal(t, "/a?z=false", buildPath("/a", map[string]any{"z": false}))
assert.Equal(t, "/a?z=%3Cnil%3E", buildPath("/a", map[string]any{"z": nil}))
}