Repository navigation
Expand file tree
/
Copy pathstatic_path_security_test.go
More file actions
239 lines (231 loc) · 9.19 KB
/
Copy pathstatic_path_security_test.go
File metadata and controls
239 lines (231 loc) · 9.19 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
package echo_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/fstest"
"github.com/labstack/echo/v5"
"github.com/labstack/echo/v5/middleware"
)
func TestStaticRejectsEncodedEmptySegments(t *testing.T) {
for _, mode := range []string{"Static", "StaticFS", "prefix", "Group.Static", "Group.StaticFS", "middleware", "group middleware"} {
for _, decodedMatching := range []bool{false, true} {
for _, unescapeParams := range []bool{false, true} {
for _, unescapeFiles := range []bool{false, true} {
name := fmt.Sprintf("%s/decoded=%t/params=%t/files=%t", mode, decodedMatching, unescapeParams, unescapeFiles)
t.Run(name, func(t *testing.T) {
filesystem := fstest.MapFS{
"public/text.txt": {Data: []byte("PUBLIC")},
"public/admin/private.txt": {Data: []byte("PRIVATE_MARKER")},
"public/100%.txt": {Data: []byte("PERCENT")},
"public/literal%2e%2e.txt": {Data: []byte("ENCODED_NAME")},
"public/%2e%2e/literal.txt": {Data: []byte("ENCODED_DIRECTORY")},
"outside.txt": {Data: []byte("OUTSIDE_MARKER")},
}
e := echo.NewWithConfig(echo.Config{
Router: echo.NewRouter(echo.RouterConfig{
UseEscapedPathForMatching: decodedMatching,
UnescapePathParamValues: unescapeParams,
}),
Filesystem: filesystem,
EnablePathUnescapingStaticFiles: unescapeFiles,
})
guard := func(c *echo.Context) error { return echo.ErrForbidden }
prefix := ""
switch mode {
case "Static":
e.Static("/", "public")
case "StaticFS":
e.StaticFS("/", echo.MustSubFS(filesystem, "public"))
case "prefix":
prefix = "/assets"
e.Static(prefix, "public")
case "Group.Static":
prefix = "/assets"
e.Group(prefix).Static("/", "public")
case "Group.StaticFS":
prefix = "/assets"
e.Group(prefix).StaticFS("/", echo.MustSubFS(filesystem, "public"))
case "middleware":
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "public", Filesystem: filesystem, EnablePathUnescaping: unescapeFiles,
}))
case "group middleware":
prefix = "/assets"
group := e.Group(prefix)
group.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "public", Filesystem: filesystem, EnablePathUnescaping: unescapeFiles,
}))
group.GET("/*", func(c *echo.Context) error { return echo.ErrNotFound })
group.GET("/admin/*", guard)
}
if mode != "group middleware" {
e.GET(prefix+"/admin/*", guard)
}
for _, tc := range []struct {
path string
status int
body string
}{
{path: "/text.txt", status: http.StatusOK, body: "PUBLIC"},
{path: "/admin/private.txt", status: http.StatusForbidden},
{path: "/%2fadmin/private.txt"},
{path: "/%2Fadmin/private.txt"},
{path: "/%2fadmin%2fprivate.txt"},
{path: "/%252fadmin/private.txt"},
{path: "/%252Fadmin%252fprivate.txt"},
{path: "//admin/private.txt"},
{path: "/../outside.txt"},
{path: "/%2f..%2f..%2foutside.txt"},
} {
t.Run(tc.path, func(t *testing.T) {
rec := httptest.NewRecorder()
e.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, prefix+tc.path, nil))
body := rec.Body.String()
if tc.status != 0 && rec.Code != tc.status {
t.Errorf("status=%d, want %d; body=%q", rec.Code, tc.status, body)
}
if tc.body != "" && body != tc.body {
t.Errorf("body=%q, want %q", body, tc.body)
}
if strings.Contains(body, "PRIVATE_MARKER") || strings.Contains(body, "OUTSIDE_MARKER") {
t.Errorf("protected content leaked: status=%d body=%q", rec.Code, body)
}
})
}
// Literal percent sequences remain usable with the default static-file decoding policy.
if !unescapeFiles {
for path, want := range map[string]string{
"/100%25.txt": "PERCENT",
"/literal%252e%252e.txt": "ENCODED_NAME",
"/%252e%252e/literal.txt": "ENCODED_DIRECTORY",
} {
rec := httptest.NewRecorder()
e.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, prefix+path, nil))
if rec.Code != http.StatusOK || rec.Body.String() != want {
t.Errorf("GET %s: status=%d body=%q, want 200 and %q", path, rec.Code, rec.Body.String(), want)
}
}
}
})
}
}
}
}
}
func TestStaticEncodedEmptySegmentOverHTTP(t *testing.T) {
e := echo.NewWithConfig(echo.Config{
Router: echo.NewRouter(echo.RouterConfig{UseEscapedPathForMatching: true}),
Filesystem: fstest.MapFS{"public/admin/private.txt": {Data: []byte("PRIVATE_MARKER")}},
})
e.Static("/", "public")
e.GET("/admin/*", func(c *echo.Context) error { return echo.ErrForbidden })
server := httptest.NewServer(e)
defer server.Close()
response, err := http.Get(server.URL + "/%2fadmin/private.txt")
if err != nil {
t.Fatal(err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if response.StatusCode != http.StatusNotFound || strings.Contains(string(body), "PRIVATE_MARKER") {
t.Errorf("status=%d body=%q, want 404 without protected content", response.StatusCode, body)
}
}
func TestStaticRootGuardRejectsEncodedEmptySegment(t *testing.T) {
for _, groupedMiddleware := range []bool{false, true} {
for _, decodedMatching := range []bool{false, true} {
for _, unescapeFiles := range []bool{false, true} {
name := fmt.Sprintf("middleware=%t/decoded=%t/files=%t", groupedMiddleware, decodedMatching, unescapeFiles)
t.Run(name, func(t *testing.T) {
filesystem := fstest.MapFS{"public/index.html": {Data: []byte("PRIVATE_INDEX")}}
e := echo.NewWithConfig(echo.Config{
Router: echo.NewRouter(echo.RouterConfig{UseEscapedPathForMatching: decodedMatching}),
Filesystem: filesystem, EnablePathUnescapingStaticFiles: unescapeFiles,
})
prefix := ""
guard := func(c *echo.Context) error { return echo.ErrForbidden }
if groupedMiddleware {
prefix = "/assets"
group := e.Group(prefix)
group.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "public", Filesystem: filesystem, EnablePathUnescaping: unescapeFiles,
}))
group.GET("/*", func(c *echo.Context) error { return echo.ErrNotFound })
group.GET("/", guard)
} else {
e.Static("/", "public")
e.GET("/", guard)
}
for _, path := range []string{"/", "//", "/%2f", "/%252f"} {
rec := httptest.NewRecorder()
e.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, prefix+path, nil))
if path == "/" && rec.Code != http.StatusForbidden {
t.Fatalf("root guard returned %d, want 403", rec.Code)
}
if rec.Code == http.StatusOK || strings.Contains(rec.Body.String(), "PRIVATE_INDEX") {
t.Errorf("GET %s bypassed root guard: status=%d body=%q", path, rec.Code, rec.Body.String())
}
}
})
}
}
}
}
// Regression for GHSA-76qj-xf66-r429: a Static prefix without a trailing slash must not serve a sibling path.
func TestStaticPrefixDoesNotMatchSiblingPath(t *testing.T) {
filesystem := fstest.MapFS{
"public/text.txt": {Data: []byte("PUBLIC")},
"public/admin/private.txt": {Data: []byte("PRIVATE_MARKER")},
}
for _, mode := range []string{"Static", "StaticFS", "Group.Static", "Group.Static empty"} {
t.Run(mode, func(t *testing.T) {
e := echo.NewWithConfig(echo.Config{Filesystem: filesystem})
prefix := "/assets"
switch mode {
case "Static":
e.Static(prefix, "public")
case "StaticFS":
e.StaticFS(prefix, echo.MustSubFS(filesystem, "public"))
case "Group.Static":
prefix = "/files/assets"
e.Group("/files").Static("/assets", "public")
case "Group.Static empty":
e.Group(prefix).Static("", "public")
}
e.GET(prefix+"/admin/*", func(c *echo.Context) error { return echo.ErrForbidden })
for target, want := range map[string]int{
prefix: http.StatusMovedPermanently,
prefix + "/text.txt": http.StatusOK,
prefix + "/admin/private.txt": http.StatusForbidden,
prefix + "admin/private.txt": http.StatusNotFound,
} {
rec := httptest.NewRecorder()
e.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, target, nil))
if rec.Code != want || strings.Contains(rec.Body.String(), "PRIVATE_MARKER") {
t.Errorf("%s: status=%d body=%q, want %d", target, rec.Code, rec.Body.String(), want)
}
}
})
}
}
func TestStatic_KeepsExistingPrefixRoute(t *testing.T) {
e := echo.NewWithConfig(echo.Config{Filesystem: fstest.MapFS{"public/text.txt": {Data: []byte("PUBLIC")}}})
e.Any("assets", func(c *echo.Context) error { return c.String(http.StatusOK, "MINE") })
e.Static("/assets", "public")
for target, body := range map[string]string{"/assets": "MINE", "/assets/text.txt": "PUBLIC"} {
rec := httptest.NewRecorder()
e.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, target, nil))
if rec.Body.String() != body {
t.Errorf("%s: body=%q, want %q", target, rec.Body.String(), body)
}
}
}