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
Next Next commit
just single slash
  • Loading branch information
mtojek committed Feb 11, 2025
commit efd0965236824ee1e692a3aa1fe1a1ce03326db2
14 changes: 7 additions & 7 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"path/filepath"
Expand Down Expand Up @@ -788,7 +789,7 @@ func New(options *Options) *API {
httpmw.AttachRequestID,
httpmw.ExtractRealIP(api.RealIPConfig),
httpmw.Logger(api.Logger),
stripSlashesMW,
singleSlashMW,
rolestore.CustomRoleMW,
prometheusMW,
// Build-Version is helpful for debugging.
Expand Down Expand Up @@ -1735,8 +1736,12 @@ func ReadExperiments(log slog.Logger, raw []string) codersdk.Experiments {

var multipleSlashesRe = regexp.MustCompile(`/+`)

func stripSlashesMW(next http.Handler) http.Handler {
func singleSlashMW(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "test-app-owner") {
log.Println(r.URL.Path)
}

var path string
rctx := chi.RouteContext(r.Context())
if rctx != nil && rctx.RoutePath != "" {
Expand All @@ -1748,11 +1753,6 @@ func stripSlashesMW(next http.Handler) http.Handler {
// Normalize multiple slashes to a single slash
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!


// Ensure it doesn't strip the root `/`
if len(newPath) > 1 && newPath[len(newPath)-1] == '/' {
newPath = strings.TrimSuffix(newPath, "/")
}

// Apply the cleaned path
if rctx != nil {
rctx.RoutePath = newPath
Expand Down
5 changes: 2 additions & 3 deletions coderd/coderd_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ func TestStripSlashesMW(t *testing.T) {
wantPath string
}{
{"No changes", "/api/v1/buildinfo", "/api/v1/buildinfo"},
{"Single trailing slash", "/api/v2/buildinfo/", "/api/v2/buildinfo"},
{"Double slashes", "/api//v2//buildinfo", "/api/v2/buildinfo"},
{"Triple slashes", "/api///v2///buildinfo///", "/api/v2/buildinfo"},
{"Triple slashes", "/api///v2///buildinfo", "/api/v2/buildinfo"},
{"Leading slashes", "///api/v2/buildinfo", "/api/v2/buildinfo"},
{"Root path", "/", "/"},
{"Double slashes root", "//", "/"},
Expand All @@ -46,7 +45,7 @@ func TestStripSlashesMW(t *testing.T) {
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))

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

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