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 1 commit
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
7 changes: 4 additions & 3 deletions static.go
Expand Up @@ -123,7 +123,7 @@ func staticHandler(ctx *Context, log *log.Logger, opt StaticOptions) bool {
return false
}

file := ctx.Req.URL.Path
file := path.Clean(ctx.Req.URL.Path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the call of path.Clean here is ineffective and needless, so we can remove it.

unknwon marked this conversation as resolved.
Show resolved Hide resolved
// if we have a prefix, filter requests by stripping the prefix
if opt.Prefix != "" {
if !strings.HasPrefix(file, opt.Prefix) {
Expand All @@ -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