-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support sanitising the URL by removing extra slashes in the URL (#21333)
Changes in this PR : Strips incoming request URL of additional slashes (/). For example an input like `https://git.data.coop//halfd/new-website.git` is translated to `https://git.data.coop/halfd/new-website.git` Fixes #20462 Fix #23242 --------- Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Jason Song <i@wolfogre.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
- Loading branch information
1 parent
188c8c1
commit 547c173
Showing
2 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
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: "/go-gitea/gitea.git", | ||
}, | ||
{ | ||
name: "path with no slashes", | ||
inputPath: "https://github.com/go-gitea/gitea.git", | ||
expectedPath: "/go-gitea/gitea.git", | ||
}, | ||
{ | ||
name: "path with slashes in the middle", | ||
inputPath: "https://git.data.coop//halfd/new-website.git", | ||
expectedPath: "/halfd/new-website.git", | ||
}, | ||
{ | ||
name: "path with slashes in the middle", | ||
inputPath: "https://git.data.coop//halfd/new-website.git", | ||
expectedPath: "/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", | ||
}, | ||
{ | ||
name: "path with encoded slash", | ||
inputPath: "/user2/%2F%2Frepo1", | ||
expectedPath: "/user2/%2F%2Frepo1", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
testMiddleware := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, tt.expectedPath, r.URL.Path) | ||
}) | ||
|
||
// 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) | ||
} | ||
} |