From e9df04014231b763495ecbb7a63d78bc70b16a74 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Tue, 27 Apr 2021 23:48:11 +0800 Subject: [PATCH 1/3] Fix 4590: Forget to check URL when FilterChain invoke next() --- server/web/filter_chain_test.go | 25 ++++++++++++++++++++++++- server/web/router.go | 6 +++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/server/web/filter_chain_test.go b/server/web/filter_chain_test.go index 2a428b78..29a6d82a 100644 --- a/server/web/filter_chain_test.go +++ b/server/web/filter_chain_test.go @@ -15,16 +15,18 @@ package web import ( + "github.com/beego/beego/v2/core/logs" "net/http" "net/http/httptest" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/beego/beego/v2/server/web/context" ) -func TestControllerRegister_InsertFilterChain(t *testing.T) { +func TestControllerRegisterInsertFilterChain(t *testing.T) { InsertFilterChain("/*", func(next FilterFunc) FilterFunc { return func(ctx *context.Context) { @@ -46,3 +48,24 @@ func TestControllerRegister_InsertFilterChain(t *testing.T) { assert.Equal(t, "filter-chain", w.Header().Get("filter")) } + +func TestFilterChainRouter(t *testing.T) { + InsertFilterChain("/app/hello1/*", func(next FilterFunc) FilterFunc { + return func(ctx *context.Context) { + logs.Info("aaa") + next(ctx) + } + }) + + InsertFilterChain("/app/*", func(next FilterFunc) FilterFunc { + return func(ctx *context.Context) { + start := time.Now() + ctx.Input.SetData("start", start) + logs.Info("start_time", start) + next(ctx) + logs.Info("run_time", time.Since(start).String()) + } + }) + + Run() +} diff --git a/server/web/router.go b/server/web/router.go index 7e78c76c..93c77e7d 100644 --- a/server/web/router.go +++ b/server/web/router.go @@ -486,7 +486,11 @@ func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter Filter // } func (p *ControllerRegister) InsertFilterChain(pattern string, chain FilterChain, opts ...FilterOpt) { root := p.chainRoot - filterFunc := chain(root.filterFunc) + //filterFunc := chain(root.filterFunc) + filterFunc := chain(func(ctx *beecontext.Context) { + var preFilterParams map[string]string + root.filter(ctx, p.getUrlPath(ctx), preFilterParams) + }) opts = append(opts, WithCaseSensitive(p.cfg.RouterCaseSensitive)) p.chainRoot = newFilterRouter(pattern, filterFunc, opts...) p.chainRoot.next = root From 7b2ef8c7acd8a5bdea2f78208c323cda9cc9ecbb Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Tue, 27 Apr 2021 23:53:08 +0800 Subject: [PATCH 2/3] Add change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc786fb1..ea3a4bc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Fix 4480: log format incorrect. [4482](https://github.com/beego/beego/pull/4482) - Remove `duration` from prometheus labels. [4391](https://github.com/beego/beego/pull/4391) - Fix `unknown escape sequence` in generated code. [4385](https://github.com/beego/beego/pull/4385) +- Fix 4590: Forget to check URL when FilterChain invoke `next()`. [4593](https://github.com/beego/beego/pull/4593) - Using fixed name `commentRouter.go` as generated file name. [4385](https://github.com/beego/beego/pull/4385) - Fix 4383: ORM Adapter produces panic when using orm.RegisterModelWithPrefix. [4386](https://github.com/beego/beego/pull/4386) - Fix 4444: panic when 404 not found. [4446](https://github.com/beego/beego/pull/4446) From 822e0df7875f88d5f19ab147525be7255bceb7bc Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Tue, 27 Apr 2021 23:58:30 +0800 Subject: [PATCH 3/3] Add more tests --- server/web/filter_chain_test.go | 55 ++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/server/web/filter_chain_test.go b/server/web/filter_chain_test.go index 29a6d82a..ee19b1e9 100644 --- a/server/web/filter_chain_test.go +++ b/server/web/filter_chain_test.go @@ -15,13 +15,10 @@ package web import ( - "github.com/beego/beego/v2/core/logs" + "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "testing" - "time" - - "github.com/stretchr/testify/assert" "github.com/beego/beego/v2/server/web/context" ) @@ -50,22 +47,56 @@ func TestControllerRegisterInsertFilterChain(t *testing.T) { } func TestFilterChainRouter(t *testing.T) { - InsertFilterChain("/app/hello1/*", func(next FilterFunc) FilterFunc { + + + app := NewHttpSever() + + const filterNonMatch = "filter-chain-non-match" + app.InsertFilterChain("/app/nonMatch/before/*", func(next FilterFunc) FilterFunc { return func(ctx *context.Context) { - logs.Info("aaa") + ctx.Output.Header("filter", filterNonMatch) next(ctx) } }) - InsertFilterChain("/app/*", func(next FilterFunc) FilterFunc { + const filterAll = "filter-chain-all" + app.InsertFilterChain("/*", func(next FilterFunc) FilterFunc { return func(ctx *context.Context) { - start := time.Now() - ctx.Input.SetData("start", start) - logs.Info("start_time", start) + ctx.Output.Header("filter", filterAll) next(ctx) - logs.Info("run_time", time.Since(start).String()) } }) - Run() + app.InsertFilterChain("/app/nonMatch/after/*", func(next FilterFunc) FilterFunc { + return func(ctx *context.Context) { + ctx.Output.Header("filter", filterNonMatch) + next(ctx) + } + }) + + app.InsertFilterChain("/app/match/*", func(next FilterFunc) FilterFunc { + return func(ctx *context.Context) { + ctx.Output.Header("match", "yes") + next(ctx) + } + }) + + r, _ := http.NewRequest("GET", "/app/match", nil) + w := httptest.NewRecorder() + + app.Handlers.ServeHTTP(w, r) + assert.Equal(t, filterAll, w.Header().Get("filter")) + assert.Equal(t, "yes", w.Header().Get("match")) + + r, _ = http.NewRequest("GET", "/app/match1", nil) + w = httptest.NewRecorder() + app.Handlers.ServeHTTP(w, r) + assert.Equal(t, filterAll, w.Header().Get("filter")) + assert.NotEqual(t, "yes", w.Header().Get("match")) + + r, _ = http.NewRequest("GET", "/app/nonMatch", nil) + w = httptest.NewRecorder() + app.Handlers.ServeHTTP(w, r) + assert.Equal(t, filterAll, w.Header().Get("filter")) + assert.NotEqual(t, "yes", w.Header().Get("match")) }