Skip to content

Commit addc746

Browse files
authored
static: clean the path URL before redirecting (#199)
1 parent 002c0ce commit addc746

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Diff for: static.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ func staticHandler(ctx *Context, log *log.Logger, opt StaticOptions) bool {
149149
// Try to serve index file
150150
if fi.IsDir() {
151151
// Redirect if missing trailing slash.
152-
if !strings.HasSuffix(ctx.Req.URL.Path, "/") {
153-
http.Redirect(ctx.Resp, ctx.Req.Request, ctx.Req.URL.Path+"/", http.StatusFound)
152+
redirPath := path.Clean(ctx.Req.URL.Path)
153+
if !strings.HasSuffix(redirPath, "/") {
154+
http.Redirect(ctx.Resp, ctx.Req.Request, redirPath+"/", http.StatusFound)
154155
return true
155156
}
156157

Diff for: static_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ func Test_Static_Redirect(t *testing.T) {
218218
So(resp.Code, ShouldEqual, http.StatusFound)
219219
So(resp.Header().Get("Location"), ShouldEqual, "/public/")
220220
})
221+
222+
Convey("Serve static files with improper request", t, func() {
223+
m := New()
224+
m.Use(Static(currentRoot))
225+
226+
resp := httptest.NewRecorder()
227+
req, err := http.NewRequest("GET", `http://localhost:4000//example.com%2f..`, nil)
228+
So(err, ShouldBeNil)
229+
m.ServeHTTP(resp, req)
230+
231+
So(resp.Code, ShouldEqual, http.StatusNotFound)
232+
})
221233
}
222234

223235
func Test_Statics(t *testing.T) {

0 commit comments

Comments
 (0)