Skip to content

Commit 6067257

Browse files
committed
document things to reduce false positives
1 parent 01558c4 commit 6067257

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

middleware/static.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
174174
if err != nil {
175175
return
176176
}
177+
// Security: We use path.Clean() (not filepath.Clean()) because:
178+
// 1. HTTP URLs always use forward slashes, regardless of server OS
179+
// 2. path.Clean() provides platform-independent behavior for URL paths
180+
// 3. The "/" prefix forces absolute path interpretation, removing ".." components
181+
// 4. Backslashes are treated as literal characters (not path separators), preventing traversal
182+
// See static_windows.go for Go 1.20+ filepath.Clean compatibility notes
177183
name := path.Join(config.Root, path.Clean("/"+p)) // "/"+ for security
178184

179185
if config.IgnoreBase {

middleware/static_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,48 @@ func TestStatic(t *testing.T) {
100100
expectCode: http.StatusNotFound,
101101
expectContains: "{\"message\":\"Not Found\"}\n",
102102
},
103+
{
104+
name: "nok, URL encoded path traversal (single encoding)",
105+
whenURL: "/%2e%2e%2fmiddleware/basic_auth.go",
106+
expectCode: http.StatusNotFound,
107+
expectContains: "{\"message\":\"Not Found\"}\n",
108+
},
109+
{
110+
name: "nok, URL encoded path traversal (double encoding)",
111+
whenURL: "/%252e%252e%252fmiddleware/basic_auth.go",
112+
expectCode: http.StatusNotFound,
113+
expectContains: "{\"message\":\"Not Found\"}\n",
114+
},
115+
{
116+
name: "nok, URL encoded path traversal (mixed encoding)",
117+
whenURL: "/%2e%2e/middleware/basic_auth.go",
118+
expectCode: http.StatusNotFound,
119+
expectContains: "{\"message\":\"Not Found\"}\n",
120+
},
121+
{
122+
name: "nok, backslash URL encoded",
123+
whenURL: "/..%5c..%5cmiddleware/basic_auth.go",
124+
expectCode: http.StatusNotFound,
125+
expectContains: "{\"message\":\"Not Found\"}\n",
126+
},
127+
{
128+
name: "nok, null byte injection",
129+
whenURL: "/index.html%00.jpg",
130+
expectCode: http.StatusInternalServerError,
131+
expectContains: "{\"message\":\"Internal Server Error\"}\n",
132+
},
133+
{
134+
name: "nok, mixed backslash and forward slash traversal",
135+
whenURL: "/..\\../middleware/basic_auth.go",
136+
expectCode: http.StatusNotFound,
137+
expectContains: "{\"message\":\"Not Found\"}\n",
138+
},
139+
{
140+
name: "nok, trailing dots (Windows edge case)",
141+
whenURL: "/../middleware/basic_auth.go...",
142+
expectCode: http.StatusNotFound,
143+
expectContains: "{\"message\":\"Not Found\"}\n",
144+
},
103145
{
104146
name: "ok, do not serve file, when a handler took care of the request",
105147
whenURL: "/regular-handler",

0 commit comments

Comments
 (0)