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

Support sanitising the URL by removing extra slashes in the URL #21333

Merged
merged 18 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions routers/common/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package common
import (
"fmt"
"net/http"
"path"
"strings"

"code.gitea.io/gitea/modules/context"
Expand All @@ -16,7 +17,7 @@ import (
"code.gitea.io/gitea/modules/web/routing"

"github.com/chi-middleware/proxy"
"github.com/go-chi/chi/v5/middleware"
chi "github.com/go-chi/chi/v5"
)

// Middlewares returns common middlewares
Expand Down Expand Up @@ -48,7 +49,8 @@ func Middlewares() []func(http.Handler) http.Handler {
handlers = append(handlers, proxy.ForwardedHeaders(opt))
}

handlers = append(handlers, middleware.StripSlashes)
// Strip slashes.
handlers = append(handlers, stripSlashesMiddleware)

if !setting.DisableRouterLog {
handlers = append(handlers, routing.NewLoggerHandler())
Expand Down Expand Up @@ -81,3 +83,31 @@ func Middlewares() []func(http.Handler) http.Handler {
})
return handlers
}

func stripSlashesMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
var urlPath string
rctx := chi.RouteContext(req.Context())
if rctx != nil && rctx.RoutePath != "" {
urlPath = rctx.RoutePath
} else {
urlPath = req.URL.Path
}
wolfogre marked this conversation as resolved.
Show resolved Hide resolved

var sanitizedPath string
if len(urlPath) > 1 {
sanitizedPath = path.Clean(urlPath)
sandyydk marked this conversation as resolved.
Show resolved Hide resolved

if string(urlPath[len(urlPath)-1]) == "/" {
sanitizedPath += "/"
}
sandyydk marked this conversation as resolved.
Show resolved Hide resolved

if rctx == nil {
req.URL.Path = sanitizedPath
} else {
rctx.RoutePath = sanitizedPath
}
}
next.ServeHTTP(resp, req)
})
}
66 changes: 66 additions & 0 deletions routers/common/middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
wolfogre marked this conversation as resolved.
Show resolved Hide resolved
package common

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestStripSlashesMiddleware(t *testing.T) {
type test struct {
name string
expectedPath string
inputPath string
}

tests := []test{
{
name: "path with multiple slashes",
inputPath: "https://github.com///go-gitea//gitea.git",
expectedPath: "https://github.com/go-gitea/gitea.git",
},
{
name: "path with no slashes",
inputPath: "https://github.com/go-gitea/gitea.git",
expectedPath: "https://github.com/go-gitea/gitea.git",
},
{
name: "path with slashes in the middle",
inputPath: "https://git.data.coop//halfd/new-website.git",
expectedPath: "https://git.data.coop/halfd/new-website.git",
},
{
name: "path with slashes in the middle",
inputPath: "https://git.data.coop//halfd/new-website.git",
expectedPath: "https://git.data.coop/halfd/new-website.git",
},
{
name: "path with slashes in the end",
inputPath: "/user2//repo1/",
expectedPath: "/user2/repo1/",
},
{
name: "path with slashes and query params",
inputPath: "/repo//migrate?service_type=3",
expectedPath: "/repo/migrate?service_type=3",
},
}

for _, tt := range tests {
testMiddleware := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, tt.expectedPath, r.URL.String())
})

// pass the test middleware to validate the changes
handlerToTest := stripSlashesMiddleware(testMiddleware)
// create a mock request to use
req := httptest.NewRequest("GET", tt.inputPath, nil)
// call the handler using a mock response recorder
handlerToTest.ServeHTTP(httptest.NewRecorder(), req)
}
}