-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Support sanitising the URL by removing extra slashes in the URL #21333
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c03b292
Support sanitising the URL by removing extra slashes in the URL
sandyydk 65f2a22
Merge branch 'main' into support-url-slashes
sandyydk c31a914
Fix linting issues
sandyydk 61a875c
Replace strings split with inbuilt path package
sandyydk f01d054
Retain backslash suffix
sandyydk a5b865c
Fix linting comments
sandyydk 9d36d6b
Add more test cases. Replace chi v1 with chi v5
sandyydk cf24559
Merge branch 'main' into support-url-slashes
sandyydk dfe8666
Fix inclusion of trailing slash
sandyydk 4ee7fce
Merge branch 'main' into support-url-slashes
sandyydk 6ca6526
Merge branch 'main' into support-url-slashes
zeripath 7fd586d
Merge branch 'main' into support-url-slashes
wolfogre 04b5a90
Apply suggestions from code review
wolfogre 8064d38
Merge branch 'main' into support-url-slashes
wolfogre e8b5930
Update routers/common/middleware.go
wolfogre 9bb94cd
Merge branch 'main' into support-url-slashes
wolfogre e9b6d09
Update routers/common/middleware_test.go
wolfogre 58ccfa2
Merge branch 'main' into support-url-slashes
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,65 @@ | ||
// 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: "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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.