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 12 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
46 changes: 44 additions & 2 deletions routers/common/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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 +48,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.Log.DisableRouterLog {
handlers = append(handlers, routing.NewLoggerHandler())
Expand Down Expand Up @@ -81,3 +82,44 @@ 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 := make([]rune, 0, len(urlPath))

prevWasSlash := false
for _, chr := range urlPath {
if chr != '/' {
sanitizedPath = append(sanitizedPath, chr)
prevWasSlash = false
continue
}
if prevWasSlash {
continue
}
sanitizedPath = append(sanitizedPath, chr)
prevWasSlash = true
}

// Always remove the leading slash.
modifiedPath := strings.TrimSuffix(string(sanitizedPath), "/")

if rctx == nil {
req.URL.Path = modifiedPath
} else {
rctx.RoutePath = modifiedPath
}
}
wolfogre marked this conversation as resolved.
Show resolved Hide resolved
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())
wolfogre marked this conversation as resolved.
Show resolved Hide resolved
})

// 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)
}
}