chi icon indicating copy to clipboard operation
chi copied to clipboard

Encoded path will be decoded by `middleware.StripSlashes` and mismatch routers

Open wolfogre opened this issue 3 years ago • 0 comments

To reproduce it:

package main

import (
	"net/http"
	"time"

	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
)

func main() {
	go server()
	time.Sleep(time.Second)

	_, _ = http.Get("http://localhost:3000/abc%2Fefg")  // 200
	_, _ = http.Get("http://localhost:3000/abc%2Fefg/") // 404
	_, _ = http.Get("http://localhost:3000/abcefg/")    // 200
}

func server() {
	r := chi.NewRouter()
	r.Use(middleware.Logger)
	r.Use(middleware.StripSlashes)
	r.Get("/{name}", func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte("welcome"))
	})
	_ = http.ListenAndServe(":3000", r)
}

And the log:

2023/03/02 17:39:42 "GET http://localhost:3000/abc%2Fefg HTTP/1.1" from [::1]:49977 - 200 7B in 15.338µs
2023/03/02 17:39:42 "GET http://localhost:3000/abc%2Fefg/ HTTP/1.1" from [::1]:49978 - 404 19B in 5.918µs
2023/03/02 17:39:42 "GET http://localhost:3000/abcefg/ HTTP/1.1" from [::1]:49979 - 200 7B in 6.94µs

However, /abc%2Fefg/ should return 200.

wolfogre avatar Mar 02 '23 09:03 wolfogre