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
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
unit tests
  • Loading branch information
mtojek committed Feb 11, 2025
commit 2aef54d9a2e128e145b08b92ba7c7fd986356ef6
47 changes: 47 additions & 0 deletions coderd/coderd_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package coderd

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestStripSlashesMW(t *testing.T) {
t.Parallel()

tests := []struct {
name string
inputPath string
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"},
{"Leading slashes", "///api/v2/buildinfo", "/api/v2/buildinfo"},
{"Root path", "/", "/"},
{"Double slashes root", "//", "/"},
{"Only slashes", "/////", "/"},
}

handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

req := httptest.NewRequest("GET", tt.inputPath, nil)
rec := httptest.NewRecorder()

stripSlashesMW(handler).ServeHTTP(rec, req)

if req.URL.Path != tt.wantPath {
t.Errorf("stripSlashesMW got path = %q, want %q", req.URL.Path, tt.wantPath)
}
})
}
}
Loading