Skip to content

fix: handle urls with multiple slashes #16527

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 6 commits into from
Feb 12, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: correct approach
  • Loading branch information
mtojek committed Feb 12, 2025
commit cdfef4ddec91620a93d1b36a3da8f2052cdfa7c2
4 changes: 3 additions & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,10 +1749,12 @@ func singleSlashMW(next http.Handler) http.Handler {
newPath := multipleSlashesRe.ReplaceAllString(path, "/")
Copy link
Member

Choose a reason for hiding this comment

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

nit, non-blocking: I wonder what the cost of a regex replace is versus iterating over the string once?

Copy link
Member Author

Choose a reason for hiding this comment

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

I had that thought to fiddle with chars, but blindly assumed that regexp is safer and easier for devs to comprehend. Thanks for the comment anyway!


// Apply the cleaned path
// The approach is consistent with: https://github.com/go-chi/chi/blob/e846b8304c769c4f1a51c9de06bebfaa4576bd88/middleware/strip.go#L24-L28
if rctx != nil {
rctx.RoutePath = newPath
} else {
r.URL.Path = newPath
}
r.URL.Path = newPath

next.ServeHTTP(w, r)
}
Expand Down
29 changes: 20 additions & 9 deletions coderd/coderd_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,37 @@ func TestStripSlashesMW(t *testing.T) {

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

t.Run("chi/"+tt.name, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest("GET", tt.inputPath, nil)
rec := httptest.NewRecorder()

// Create a chi RouteContext and attach it to the request
// given
rctx := chi.NewRouteContext()
rctx.RoutePath = tt.inputPath // Simulate chi route path
rctx.RoutePath = tt.inputPath
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

// Pass the request through the middleware
// when
singleSlashMW(handler).ServeHTTP(rec, req)

// Get the updated chi RouteContext after middleware processing
updatedCtx := chi.RouteContext(req.Context())

// Validate URL path
assert.Equal(t, tt.wantPath, req.URL.Path)
// then
assert.Equal(t, tt.inputPath, req.URL.Path)
assert.Equal(t, tt.wantPath, updatedCtx.RoutePath)
})

t.Run("stdlib/"+tt.name, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest("GET", tt.inputPath, nil)
rec := httptest.NewRecorder()

// when
singleSlashMW(handler).ServeHTTP(rec, req)

// then
assert.Equal(t, tt.wantPath, req.URL.Path)
assert.Nil(t, chi.RouteContext(req.Context()))
})
}
}
Loading