Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static: clean the path URL before redirecting #199

Merged
merged 5 commits into from May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions static.go
Expand Up @@ -149,8 +149,9 @@ func staticHandler(ctx *Context, log *log.Logger, opt StaticOptions) bool {
// Try to serve index file
if fi.IsDir() {
// Redirect if missing trailing slash.
if !strings.HasSuffix(ctx.Req.URL.Path, "/") {
http.Redirect(ctx.Resp, ctx.Req.Request, ctx.Req.URL.Path+"/", http.StatusFound)
redirPath := path.Clean(ctx.Req.URL.Path)
if !strings.HasSuffix(redirPath, "/") {
http.Redirect(ctx.Resp, ctx.Req.Request, redirPath+"/", http.StatusFound)
return true
}

Expand Down
12 changes: 12 additions & 0 deletions static_test.go
Expand Up @@ -218,6 +218,18 @@ func Test_Static_Redirect(t *testing.T) {
So(resp.Code, ShouldEqual, http.StatusFound)
So(resp.Header().Get("Location"), ShouldEqual, "/public/")
})

Convey("Serve static files with improper request", t, func() {
m := New()
m.Use(Static(currentRoot))

resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", `http://localhost:4000//example.com%2f..`, nil)
So(err, ShouldBeNil)
m.ServeHTTP(resp, req)

So(resp.Code, ShouldEqual, http.StatusNotFound)
unknwon marked this conversation as resolved.
Show resolved Hide resolved
})
}

func Test_Statics(t *testing.T) {
Expand Down