Skip to content

Commit 7d598e4

Browse files
committed
fix: rewrite urls with multiple slashes
1 parent 34b46f9 commit 7d598e4

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

coderd/coderd.go

+32
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ func New(options *Options) *API {
788788
httpmw.AttachRequestID,
789789
httpmw.ExtractRealIP(api.RealIPConfig),
790790
httpmw.Logger(api.Logger),
791+
stripSlashesMW,
791792
rolestore.CustomRoleMW,
792793
prometheusMW,
793794
// Build-Version is helpful for debugging.
@@ -1731,3 +1732,34 @@ func ReadExperiments(log slog.Logger, raw []string) codersdk.Experiments {
17311732
}
17321733
return exps
17331734
}
1735+
1736+
var multipleSlashesRe = regexp.MustCompile(`/+`)
1737+
1738+
func stripSlashesMW(next http.Handler) http.Handler {
1739+
fn := func(w http.ResponseWriter, r *http.Request) {
1740+
var path string
1741+
rctx := chi.RouteContext(r.Context())
1742+
if rctx != nil && rctx.RoutePath != "" {
1743+
path = rctx.RoutePath
1744+
} else {
1745+
path = r.URL.Path
1746+
}
1747+
1748+
// Normalize multiple slashes to a single slash
1749+
newPath := multipleSlashesRe.ReplaceAllString(path, "/")
1750+
1751+
// Ensure it doesn't strip the root `/`
1752+
if len(newPath) > 1 && newPath[len(newPath)-1] == '/' {
1753+
newPath = strings.TrimSuffix(newPath, "/")
1754+
}
1755+
1756+
// Apply the cleaned path
1757+
if rctx != nil {
1758+
rctx.RoutePath = newPath
1759+
}
1760+
r.URL.Path = newPath
1761+
1762+
next.ServeHTTP(w, r)
1763+
}
1764+
return http.HandlerFunc(fn)
1765+
}

site/vite.config.mts

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ export default defineConfig({
5252
"csrf_token=JXm9hOUdZctWt0ZZGAy9xiS/gxMKYOThdxjjMnMUyn4=; Path=/; HttpOnly; SameSite=Lax",
5353
},
5454
proxy: {
55+
"//": {
56+
changeOrigin: true,
57+
target: process.env.CODER_HOST || "http://localhost:3000",
58+
secure: process.env.NODE_ENV === "production",
59+
rewrite: (path) => path.replace(/\/+/g, "/"),
60+
},
5561
"/api": {
5662
ws: true,
5763
changeOrigin: true,

0 commit comments

Comments
 (0)