feat(gin): add option to use escaped path (#4420)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Aeddis Desauw · committed by GitHub · parent 52ecf02 · Browse files · 2 files·+63 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Diff stats
2 files+63 added−2 removed
- other:+63 added−2 removed
Provenance
- Commit
771dcc6- Tree
857b592- Author
Aeddis Desauw <89919264+ldesauw@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
52ecf02
2 files changed+63 added−2 removed
modified:gin_test.go+49 added
718 unmodified lines719 assert.Equal(t, int64(2), middlewareCounterV1)720 assert.Equal(t, int64(1), handlerCounterV2)721 }722 723+func TestEngineHandleContextUseEscapedPathPercentEncoded(t *testing.T) {724+ r := New()725+ r.UseEscapedPath = true726+ r.UnescapePathValues = false727+728+ r.GET("/v1/:path", func(c *Context) {729+ // Path is Escaped, the %25 is not interpreted as %730+ assert.Equal(t, "foo%252Fbar", c.Param("path"))731+ c.Status(http.StatusOK)732+ })733+734+ req := httptest.NewRequest(http.MethodGet, "/v1/foo%252Fbar", nil)735+ w := httptest.NewRecorder()736+ r.ServeHTTP(w, req)737+}738+739+func TestEngineHandleContextUseRawPathPercentEncoded(t *testing.T) {740+ r := New()741+ r.UseRawPath = true742+ r.UnescapePathValues = false743+744+ r.GET("/v1/:path", func(c *Context) {745+ // Path is used, the %25 is interpreted as %746+ assert.Equal(t, "foo%2Fbar", c.Param("path"))747+ c.Status(http.StatusOK)748+ })749+750+ req := httptest.NewRequest(http.MethodGet, "/v1/foo%252Fbar", nil)751+ w := httptest.NewRecorder()752+ r.ServeHTTP(w, req)753+}754+755+func TestEngineHandleContextUseEscapedPathOverride(t *testing.T) {756+ r := New()757+ r.UseEscapedPath = true758+ r.UseRawPath = true759+ r.UnescapePathValues = false760+761+ r.GET("/v1/:path", func(c *Context) {762+ assert.Equal(t, "foo%25bar", c.Param("path"))763+ c.Status(http.StatusOK)764+ })765+766+ assert.NotPanics(t, func() {767+ w := PerformRequest(r, http.MethodGet, "/v1/foo%25bar")768+ assert.Equal(t, 200, w.Code)769+ })770+}771+772 func TestPrepareTrustedCIRDsWith(t *testing.T) {773 r := New()774 775 // valid ipv4 cidr288 unmodified linesmodified:gin.go+14 added-2 removed
133 unmodified lines134 // 'X-AppEngine...' for better integration with that PaaS.135 AppEngine bool136 137 // UseRawPath if enabled, the url.RawPath will be used to find parameters.138+ // The RawPath is only a hint, EscapedPath() should be use instead. (https://pkg.go.dev/net/url@master#URL)139+ // Only use RawPath if you know what you are doing.140 UseRawPath bool141 142+ // UseEscapedPath if enable, the url.EscapedPath() will be used to find parameters143+ // It overrides UseRawPath144+ UseEscapedPath bool145+146 // UnescapePathValues if true, the path value will be unescaped.141- // If UseRawPath is false (by default), the UnescapePathValues effectively is true,147+ // If UseRawPath and UseEscapedPath are false (by default), the UnescapePathValues effectively is true,148 // as url.Path gonna be used, which is already unescaped.149 UnescapePathValues bool150 151 // RemoveExtraSlash a parameter can be parsed from the URL even with extra slashes.44 unmodified lines196 // - RedirectFixedPath: false197 // - HandleMethodNotAllowed: false198 // - ForwardedByClientIP: true199 // - UseRawPath: false200+// - UseEscapedPath: false201 // - UnescapePathValues: true202 func New(opts ...OptionFunc) *Engine {203 debugPrintWARNINGNew()204 engine := &Engine{9 unmodified lines214 ForwardedByClientIP: true,215 RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},216 TrustedPlatform: defaultPlatform,217 UseRawPath: false,218+ UseEscapedPath: false,219 RemoveExtraSlash: false,220 UnescapePathValues: true,221 MaxMultipartMemory: defaultMultipartMemory,222 trees: make(methodTrees, 0, 9),467 unmodified lines690 func (engine *Engine) handleHTTPRequest(c *Context) {691 httpMethod := c.Request.Method692 rPath := c.Request.URL.Path693 unescape := false694+695+ if engine.UseEscapedPath {696+ rPath = c.Request.URL.EscapedPath()697+ unescape = engine.UnescapePathValues686- if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {698+ } else if engine.UseRawPath && len(c.Request.URL.RawPath) > 0 {699 rPath = c.Request.URL.RawPath700 unescape = engine.UnescapePathValues701 }702 130 unmodified lines