From 9bd3a27e803f122b57ded9a479a49a7ac8ba5dcb Mon Sep 17 00:00:00 2001 From: michuan Date: Tue, 25 Aug 2020 23:36:15 +0800 Subject: [PATCH 01/11] fix #3776 --- go.mod | 3 ++- go.sum | 11 ++--------- orm/models_test.go | 20 ++++++++++++++------ orm/orm_raw.go | 22 ++++++++++++++++++++-- orm/orm_test.go | 14 ++++++++++++++ 5 files changed, 52 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index ec500f51..6d4da958 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/lib/pq v1.0.0 github.com/mattn/go-sqlite3 v2.0.3+incompatible github.com/pelletier/go-toml v1.2.0 // indirect + github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.7.0 github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec @@ -29,7 +30,7 @@ require ( github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c // indirect github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b // indirect golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 - golang.org/x/tools v0.0.0-20200117065230-39095c1d176c + golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect gopkg.in/yaml.v2 v2.2.8 ) diff --git a/go.sum b/go.sum index c7b861ac..55c926cf 100644 --- a/go.sum +++ b/go.sum @@ -53,7 +53,6 @@ github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8w github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/go-yaml/yaml v0.0.0-20180328195020-5420a8b6744d h1:xy93KVe+KrIIwWDEAfQBdIfsiHJkepbYsDr+VY3g9/o= github.com/go-yaml/yaml v0.0.0-20180328195020-5420a8b6744d/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -114,11 +113,10 @@ github.com/pelletier/go-toml v1.0.1/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9 github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/peterh/liner v1.0.1-0.20171122030339-3681c2a91233/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc= -github.com/pingcap/tidb v2.0.11+incompatible/go.mod h1:I8C6jrPINP2rrVunTRd7C9fRRhQrtR43S1/CL5ix/yQ= -github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -164,9 +162,7 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -175,7 +171,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -189,8 +184,6 @@ golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20200117065230-39095c1d176c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= diff --git a/orm/models_test.go b/orm/models_test.go index e3a635f2..05f438ea 100644 --- a/orm/models_test.go +++ b/orm/models_test.go @@ -53,18 +53,24 @@ func (e *SliceStringField) FieldType() int { } func (e *SliceStringField) SetRaw(value interface{}) error { - switch d := value.(type) { - case []string: - e.Set(d) - case string: - if len(d) > 0 { - parts := strings.Split(d, ",") + f := func(str string) { + if len(str) > 0 { + parts := strings.Split(str, ",") v := make([]string, 0, len(parts)) for _, p := range parts { v = append(v, strings.TrimSpace(p)) } e.Set(v) } + } + + switch d := value.(type) { + case []string: + e.Set(d) + case string: + f(d) + case []byte: + f(string(d)) default: return fmt.Errorf(" unknown value `%v`", value) } @@ -96,6 +102,8 @@ func (e *JSONFieldTest) SetRaw(value interface{}) error { switch d := value.(type) { case string: return json.Unmarshal([]byte(d), e) + case []byte: + return json.Unmarshal(d, e) default: return fmt.Errorf(" unknown value `%v`", value) } diff --git a/orm/orm_raw.go b/orm/orm_raw.go index 3325a7ea..1bdefc78 100644 --- a/orm/orm_raw.go +++ b/orm/orm_raw.go @@ -19,6 +19,8 @@ import ( "fmt" "reflect" "time" + + "github.com/pkg/errors" ) // raw sql string prepared statement @@ -368,7 +370,15 @@ func (o *rawSet) QueryRow(containers ...interface{}) error { field.Set(mf) field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex) } - o.setFieldValue(field, value) + if fi.isFielder { + fd := field.Addr().Interface().(Fielder) + err := fd.SetRaw(value) + if err != nil { + return errors.Errorf("set raw error:%s", err) + } + } else { + o.setFieldValue(field, value) + } } } } else { @@ -509,7 +519,15 @@ func (o *rawSet) QueryRows(containers ...interface{}) (int64, error) { field.Set(mf) field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex) } - o.setFieldValue(field, value) + if fi.isFielder { + fd := field.Addr().Interface().(Fielder) + err := fd.SetRaw(value) + if err != nil { + return 0, errors.Errorf("set raw error:%s", err) + } + } else { + o.setFieldValue(field, value) + } } } } else { diff --git a/orm/orm_test.go b/orm/orm_test.go index bdb430b6..f96fb941 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -769,6 +769,20 @@ func TestCustomField(t *testing.T) { throwFailNow(t, AssertIs(user.Extra.Name, "beego")) throwFailNow(t, AssertIs(user.Extra.Data, "orm")) + + var users []User + Q := dDbBaser.TableQuote() + n, err := dORM.Raw(fmt.Sprintf("SELECT * FROM %suser%s where id=?", Q, Q), 2).QueryRows(&users) + throwFailNow(t, err) + throwFailNow(t, AssertIs(n, 1)) + throwFailNow(t, AssertIs(users[0].Extra.Name, "beego")) + throwFailNow(t, AssertIs(users[0].Extra.Data, "orm")) + + user = User{} + err = dORM.Raw(fmt.Sprintf("SELECT * FROM %suser%s where id=?", Q, Q), 2).QueryRow(&user) + throwFailNow(t, err) + throwFailNow(t, AssertIs(user.Extra.Name, "beego")) + throwFailNow(t, AssertIs(user.Extra.Data, "orm")) } func TestExpr(t *testing.T) { From 8736ffaf6ff87334153ed38591af30136fadd76c Mon Sep 17 00:00:00 2001 From: CadenGuo <411189077@qq.com> Date: Sun, 30 Aug 2020 23:38:52 +0800 Subject: [PATCH 02/11] use 'BINARY' key word for exact operator for mysql db --- orm/db_mysql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orm/db_mysql.go b/orm/db_mysql.go index 6e99058e..ff6516b7 100644 --- a/orm/db_mysql.go +++ b/orm/db_mysql.go @@ -22,7 +22,7 @@ import ( // mysql operators. var mysqlOperators = map[string]string{ - "exact": "= ?", + "exact": "= BINARY ?", "iexact": "LIKE ?", "contains": "LIKE BINARY ?", "icontains": "LIKE ?", From 60bb0577839444a0944d39e4a4456155a4f5124a Mon Sep 17 00:00:00 2001 From: CadenGuo <411189077@qq.com> Date: Mon, 31 Aug 2020 03:40:18 +0000 Subject: [PATCH 03/11] add a new mysql operator for force case sensitie query --- orm/db.go | 11 ++++++----- orm/db_mysql.go | 9 +++++---- orm/orm_test.go | 8 ++++++++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/orm/db.go b/orm/db.go index 9a1827e8..7536a422 100644 --- a/orm/db.go +++ b/orm/db.go @@ -36,10 +36,11 @@ var ( var ( operators = map[string]bool{ - "exact": true, - "iexact": true, - "contains": true, - "icontains": true, + "exact": true, + "iexact": true, + "strictexact": true, + "contains": true, + "icontains": true, // "regex": true, // "iregex": true, "gt": true, @@ -1202,7 +1203,7 @@ func (d *dbBase) GenerateOperatorSQL(mi *modelInfo, fi *fieldInfo, operator stri } sql = d.ins.OperatorSQL(operator) switch operator { - case "exact": + case "exact", "strictexact": if arg == nil { params[0] = "IS NULL" } diff --git a/orm/db_mysql.go b/orm/db_mysql.go index ff6516b7..8dd1e755 100644 --- a/orm/db_mysql.go +++ b/orm/db_mysql.go @@ -22,10 +22,11 @@ import ( // mysql operators. var mysqlOperators = map[string]string{ - "exact": "= BINARY ?", - "iexact": "LIKE ?", - "contains": "LIKE BINARY ?", - "icontains": "LIKE ?", + "exact": "= ?", + "iexact": "LIKE ?", + "strictexact": "= BINARY ?", + "contains": "LIKE BINARY ?", + "icontains": "LIKE ?", // "regex": "REGEXP BINARY ?", // "iregex": "REGEXP ?", "gt": "> ?", diff --git a/orm/orm_test.go b/orm/orm_test.go index f96fb941..18e48288 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -822,6 +822,14 @@ func TestOperators(t *testing.T) { throwFail(t, err) throwFail(t, AssertIs(num, 1)) + num, err = qs.Filter("user_name__strictexact", "Slene").Count() + throwFail(t, err) + throwFail(t, AssertIs(num, 0)) + + num, err = qs.Filter("user_name__strictexact", "slene").Count() + throwFail(t, err) + throwFail(t, AssertIs(num, 1)) + num, err = qs.Filter("user_name__contains", "e").Count() throwFail(t, err) throwFail(t, AssertIs(num, 2)) From ff53e12191a6ab4ec27ec5cbbb301c2e6046d6fd Mon Sep 17 00:00:00 2001 From: CadenGuo <411189077@qq.com> Date: Mon, 31 Aug 2020 15:29:48 +0000 Subject: [PATCH 04/11] skip strictexact operator test for db drivers other than mysql --- orm/orm_test.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/orm/orm_test.go b/orm/orm_test.go index 18e48288..b18a7082 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -822,13 +822,16 @@ func TestOperators(t *testing.T) { throwFail(t, err) throwFail(t, AssertIs(num, 1)) - num, err = qs.Filter("user_name__strictexact", "Slene").Count() - throwFail(t, err) - throwFail(t, AssertIs(num, 0)) + if dORM.Driver().Name() == "mysql" { + // Now only mysql support `strictexact` + num, err = qs.Filter("user_name__strictexact", "Slene").Count() + throwFail(t, err) + throwFail(t, AssertIs(num, 0)) - num, err = qs.Filter("user_name__strictexact", "slene").Count() - throwFail(t, err) - throwFail(t, AssertIs(num, 1)) + num, err = qs.Filter("user_name__strictexact", "slene").Count() + throwFail(t, err) + throwFail(t, AssertIs(num, 1)) + } num, err = qs.Filter("user_name__contains", "e").Count() throwFail(t, err) From 91410be72279e01e4acd82a369663cf714130f28 Mon Sep 17 00:00:00 2001 From: CadenGuo <411189077@qq.com> Date: Mon, 31 Aug 2020 15:47:37 +0000 Subject: [PATCH 05/11] orm_test:use predefined variable to check db driver --- orm/orm_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orm/orm_test.go b/orm/orm_test.go index b18a7082..93668235 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -822,7 +822,7 @@ func TestOperators(t *testing.T) { throwFail(t, err) throwFail(t, AssertIs(num, 1)) - if dORM.Driver().Name() == "mysql" { + if IsMysql { // Now only mysql support `strictexact` num, err = qs.Filter("user_name__strictexact", "Slene").Count() throwFail(t, err) From 26208a53e6e6443674c68dac3fb27bf79d0de76a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcio=20Augusto?= Date: Tue, 15 Sep 2020 18:05:33 -0300 Subject: [PATCH 06/11] session: adds CookieSameSite to ManagerConfig --- session/session.go | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/session/session.go b/session/session.go index eb85360a..4532a959 100644 --- a/session/session.go +++ b/session/session.go @@ -92,20 +92,21 @@ func GetProvider(name string) (Provider, error) { // ManagerConfig define the session config type ManagerConfig struct { - CookieName string `json:"cookieName"` - EnableSetCookie bool `json:"enableSetCookie,omitempty"` - Gclifetime int64 `json:"gclifetime"` - Maxlifetime int64 `json:"maxLifetime"` - DisableHTTPOnly bool `json:"disableHTTPOnly"` - Secure bool `json:"secure"` - CookieLifeTime int `json:"cookieLifeTime"` - ProviderConfig string `json:"providerConfig"` - Domain string `json:"domain"` - SessionIDLength int64 `json:"sessionIDLength"` - EnableSidInHTTPHeader bool `json:"EnableSidInHTTPHeader"` - SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"` - EnableSidInURLQuery bool `json:"EnableSidInURLQuery"` - SessionIDPrefix string `json:"sessionIDPrefix"` + CookieName string `json:"cookieName"` + EnableSetCookie bool `json:"enableSetCookie,omitempty"` + Gclifetime int64 `json:"gclifetime"` + Maxlifetime int64 `json:"maxLifetime"` + DisableHTTPOnly bool `json:"disableHTTPOnly"` + Secure bool `json:"secure"` + CookieLifeTime int `json:"cookieLifeTime"` + ProviderConfig string `json:"providerConfig"` + Domain string `json:"domain"` + SessionIDLength int64 `json:"sessionIDLength"` + EnableSidInHTTPHeader bool `json:"EnableSidInHTTPHeader"` + SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"` + EnableSidInURLQuery bool `json:"EnableSidInURLQuery"` + SessionIDPrefix string `json:"sessionIDPrefix"` + CookieSameSite http.SameSite `json:"cookieSameSite"` } // Manager contains Provider and its configuration. @@ -232,6 +233,7 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se HttpOnly: !manager.config.DisableHTTPOnly, Secure: manager.isSecure(r), Domain: manager.config.Domain, + SameSite: manager.config.CookieSameSite, } if manager.config.CookieLifeTime > 0 { cookie.MaxAge = manager.config.CookieLifeTime @@ -271,7 +273,9 @@ func (manager *Manager) SessionDestroy(w http.ResponseWriter, r *http.Request) { HttpOnly: !manager.config.DisableHTTPOnly, Expires: expiration, MaxAge: -1, - Domain: manager.config.Domain} + Domain: manager.config.Domain, + SameSite: manager.config.CookieSameSite, + } http.SetCookie(w, cookie) } @@ -306,6 +310,7 @@ func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Reque HttpOnly: !manager.config.DisableHTTPOnly, Secure: manager.isSecure(r), Domain: manager.config.Domain, + SameSite: manager.config.CookieSameSite, } } else { oldsid, _ := url.QueryUnescape(cookie.Value) From 6ffbc0a2b8e1e2edd2bce59a1d2ac357f9004de8 Mon Sep 17 00:00:00 2001 From: "Allen.M" Date: Wed, 30 Sep 2020 15:50:40 +0800 Subject: [PATCH 07/11] testing: fix temporary create failed on Windows We are creating temporary files on the root directory of beego now This PR using system temporary directory for testing. Fixes #4243 --- controller_test.go | 4 ++-- template_test.go | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/controller_test.go b/controller_test.go index 1e53416d..215a7411 100644 --- a/controller_test.go +++ b/controller_test.go @@ -125,8 +125,8 @@ func TestGetUint64(t *testing.T) { } func TestAdditionalViewPaths(t *testing.T) { - dir1 := "_beeTmp" - dir2 := "_beeTmp2" + dir1 := tmpDir("TestAdditionalViewPaths1") + dir2 := tmpDir("TestAdditionalViewPaths2") defer os.RemoveAll(dir1) defer os.RemoveAll(dir2) diff --git a/template_test.go b/template_test.go index 287faadc..049655db 100644 --- a/template_test.go +++ b/template_test.go @@ -45,8 +45,12 @@ var block = `{{define "block"}}

Hello, blocks!

{{end}}` +func tmpDir(s string) string { + return filepath.Join(os.TempDir(), s) +} + func TestTemplate(t *testing.T) { - dir := "_beeTmp" + dir := tmpDir("TestTemplate") files := []string{ "header.tpl", "index.tpl", @@ -107,7 +111,7 @@ var user = ` ` func TestRelativeTemplate(t *testing.T) { - dir := "_beeTmp" + dir := tmpDir("TestRelativeTemplate") //Just add dir to known viewPaths if err := AddViewPath(dir); err != nil { @@ -218,7 +222,7 @@ var output = ` ` func TestTemplateLayout(t *testing.T) { - dir := "_beeTmp" + dir := tmpDir("TestTemplateLayout") files := []string{ "add.tpl", "layout_blog.tpl", From d66321fe4ed35864e86bb7ebdc9f0e49b25631d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcio=20Augusto?= Date: Mon, 5 Oct 2020 11:39:20 -0300 Subject: [PATCH 08/11] session: adds CookieSameSite config to hooks.go#registerSession --- config.go | 7 +++++-- hooks.go | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 0c995293..bd5bf2a1 100644 --- a/config.go +++ b/config.go @@ -15,13 +15,14 @@ package beego import ( + "crypto/tls" "fmt" + "net/http" "os" "path/filepath" "reflect" "runtime" "strings" - "crypto/tls" "github.com/astaxie/beego/config" "github.com/astaxie/beego/context" @@ -108,6 +109,7 @@ type SessionConfig struct { SessionEnableSidInHTTPHeader bool // enable store/get the sessionId into/from http headers SessionNameInHTTPHeader string SessionEnableSidInURLQuery bool // enable get the sessionId from Url Query params + SessionCookieSameSite http.SameSite } // LogConfig holds Log related config @@ -153,7 +155,7 @@ func init() { } appConfigPath = filepath.Join(WorkPath, "conf", filename) if configPath := os.Getenv("BEEGO_CONFIG_PATH"); configPath != "" { - appConfigPath = configPath + appConfigPath = configPath } if !utils.FileExists(appConfigPath) { appConfigPath = filepath.Join(AppPath, "conf", filename) @@ -267,6 +269,7 @@ func newBConfig() *Config { SessionEnableSidInHTTPHeader: false, // enable store/get the sessionId into/from http headers SessionNameInHTTPHeader: "Beegosessionid", SessionEnableSidInURLQuery: false, // enable get the sessionId from Url Query params + SessionCookieSameSite: http.SameSiteDefaultMode, }, }, Log: LogConfig{ diff --git a/hooks.go b/hooks.go index 49c42d5a..0a51e0da 100644 --- a/hooks.go +++ b/hooks.go @@ -61,6 +61,7 @@ func registerSession() error { conf.EnableSidInHTTPHeader = BConfig.WebConfig.Session.SessionEnableSidInHTTPHeader conf.SessionNameInHTTPHeader = BConfig.WebConfig.Session.SessionNameInHTTPHeader conf.EnableSidInURLQuery = BConfig.WebConfig.Session.SessionEnableSidInURLQuery + conf.CookieSameSite = BConfig.WebConfig.Session.SessionCookieSameSite } else { if err = json.Unmarshal([]byte(sessionConfig), conf); err != nil { return err From 91e18996bd0c04bee848502398b52547360a4d8d Mon Sep 17 00:00:00 2001 From: sc0vu Date: Tue, 6 Oct 2020 18:20:06 +0800 Subject: [PATCH 09/11] Fix typo --- admin_test.go | 2 +- config/ini_test.go | 4 ++-- httplib/httplib.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/admin_test.go b/admin_test.go index 3f3612e4..c5f6eeaf 100644 --- a/admin_test.go +++ b/admin_test.go @@ -154,7 +154,7 @@ func TestBuildHealthCheckResponseList(t *testing.T) { []string{ "error", "Database", - "Error occured whie starting the db", + "Error occurred while starting the db", }, []string{ "success", diff --git a/config/ini_test.go b/config/ini_test.go index ffcdb294..60f1febd 100644 --- a/config/ini_test.go +++ b/config/ini_test.go @@ -145,7 +145,7 @@ httpport = 8080 # enable db [dbinfo] # db type name -# suport mysql,sqlserver +# support mysql,sqlserver name = mysql ` @@ -161,7 +161,7 @@ httpport=8080 # enable db [dbinfo] # db type name -# suport mysql,sqlserver +# support mysql,sqlserver name=mysql ` ) diff --git a/httplib/httplib.go b/httplib/httplib.go index 60aa4e8b..17493970 100644 --- a/httplib/httplib.go +++ b/httplib/httplib.go @@ -518,7 +518,7 @@ func (b *BeegoHTTPRequest) DoRequest() (resp *http.Response, err error) { // retries default value is 0, it will run once. // retries equal to -1, it will run forever until success // retries is setted, it will retries fixed times. - // Sleeps for a 400ms inbetween calls to reduce spam + // Sleeps for a 400ms in between calls to reduce spam for i := 0; b.setting.Retries == -1 || i <= b.setting.Retries; i++ { resp, err = client.Do(b.req) if err == nil { From 1dffa20435c6f5fc4f2d4e49923249acd0cdf07e Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Sat, 10 Oct 2020 21:34:02 +0800 Subject: [PATCH 10/11] make stmt cache smaller --- build_info.go | 8 ++--- cache/redis/redis.go | 5 ++-- config/yaml/yaml.go | 2 +- context/param/parsers_test.go | 8 +++-- controller_test.go | 3 +- logs/accesslog.go | 2 +- logs/file.go | 30 +++++++++---------- logs/file_test.go | 30 +++++++++---------- metric/prometheus.go | 6 ++-- orm/cmd_utils.go | 6 ++-- orm/db_alias.go | 9 ++++-- orm/orm_log.go | 2 +- plugins/authz/authz.go | 3 +- plugins/authz/authz_test.go | 7 +++-- session/redis_cluster/redis_cluster.go | 16 +++++----- session/redis_sentinel/sess_redis_sentinel.go | 5 ++-- session/sess_file_test.go | 5 ++-- staticfile.go | 2 +- template_test.go | 5 ++-- templatefunc.go | 2 +- testdata/bindata.go | 3 +- validation/util.go | 2 +- validation/validators.go | 3 +- 23 files changed, 89 insertions(+), 75 deletions(-) diff --git a/build_info.go b/build_info.go index 6dc2835e..c31152ea 100644 --- a/build_info.go +++ b/build_info.go @@ -15,11 +15,11 @@ package beego var ( - BuildVersion string + BuildVersion string BuildGitRevision string - BuildStatus string - BuildTag string - BuildTime string + BuildStatus string + BuildTag string + BuildTime string GoVersion string diff --git a/cache/redis/redis.go b/cache/redis/redis.go index 56faf211..dde760ea 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -38,8 +38,9 @@ import ( "github.com/gomodule/redigo/redis" - "github.com/astaxie/beego/cache" "strings" + + "github.com/astaxie/beego/cache" ) var ( @@ -57,7 +58,7 @@ type Cache struct { maxIdle int //the timeout to a value less than the redis server's timeout. - timeout time.Duration + timeout time.Duration } // NewRedisCache create new redis cache with default collection name. diff --git a/config/yaml/yaml.go b/config/yaml/yaml.go index 5def2da3..a5644c7b 100644 --- a/config/yaml/yaml.go +++ b/config/yaml/yaml.go @@ -296,7 +296,7 @@ func (c *ConfigContainer) getData(key string) (interface{}, error) { case map[string]interface{}: { tmpData = v.(map[string]interface{}) - if idx == len(keys) - 1 { + if idx == len(keys)-1 { return tmpData, nil } } diff --git a/context/param/parsers_test.go b/context/param/parsers_test.go index 7065a28e..81a821f1 100644 --- a/context/param/parsers_test.go +++ b/context/param/parsers_test.go @@ -1,8 +1,10 @@ package param -import "testing" -import "reflect" -import "time" +import ( + "reflect" + "testing" + "time" +) type testDefinition struct { strValue string diff --git a/controller_test.go b/controller_test.go index 215a7411..a5888f62 100644 --- a/controller_test.go +++ b/controller_test.go @@ -19,9 +19,10 @@ import ( "strconv" "testing" - "github.com/astaxie/beego/context" "os" "path/filepath" + + "github.com/astaxie/beego/context" ) func TestGetInt(t *testing.T) { diff --git a/logs/accesslog.go b/logs/accesslog.go index 3ff9e20f..9011b602 100644 --- a/logs/accesslog.go +++ b/logs/accesslog.go @@ -16,9 +16,9 @@ package logs import ( "bytes" - "strings" "encoding/json" "fmt" + "strings" "time" ) diff --git a/logs/file.go b/logs/file.go index 222db989..40a3572a 100644 --- a/logs/file.go +++ b/logs/file.go @@ -373,21 +373,21 @@ func (w *fileLogWriter) deleteOldLog() { if info == nil { return } - if w.Hourly { - if !info.IsDir() && info.ModTime().Add(1 * time.Hour * time.Duration(w.MaxHours)).Before(time.Now()) { - if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) && - strings.HasSuffix(filepath.Base(path), w.suffix) { - os.Remove(path) - } - } - } else if w.Daily { - if !info.IsDir() && info.ModTime().Add(24 * time.Hour * time.Duration(w.MaxDays)).Before(time.Now()) { - if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) && - strings.HasSuffix(filepath.Base(path), w.suffix) { - os.Remove(path) - } - } - } + if w.Hourly { + if !info.IsDir() && info.ModTime().Add(1*time.Hour*time.Duration(w.MaxHours)).Before(time.Now()) { + if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) && + strings.HasSuffix(filepath.Base(path), w.suffix) { + os.Remove(path) + } + } + } else if w.Daily { + if !info.IsDir() && info.ModTime().Add(24*time.Hour*time.Duration(w.MaxDays)).Before(time.Now()) { + if strings.HasPrefix(filepath.Base(path), filepath.Base(w.fileNameOnly)) && + strings.HasSuffix(filepath.Base(path), w.suffix) { + os.Remove(path) + } + } + } return }) } diff --git a/logs/file_test.go b/logs/file_test.go index e7c2ca9a..385eac43 100644 --- a/logs/file_test.go +++ b/logs/file_test.go @@ -186,7 +186,7 @@ func TestFileDailyRotate_06(t *testing.T) { //test file mode func TestFileHourlyRotate_01(t *testing.T) { log := NewLogger(10000) - log.SetLogger("file", `{"filename":"test3.log","hourly":true,"maxlines":4}`) + log.SetLogger("file", `{"filename":"test3.log","hourly":true,"maxlines":4}`) log.Debug("debug") log.Info("info") log.Notice("notice") @@ -237,7 +237,7 @@ func TestFileHourlyRotate_05(t *testing.T) { func TestFileHourlyRotate_06(t *testing.T) { //test file mode log := NewLogger(10000) - log.SetLogger("file", `{"filename":"test3.log", "hourly":true, "maxlines":4}`) + log.SetLogger("file", `{"filename":"test3.log", "hourly":true, "maxlines":4}`) log.Debug("debug") log.Info("info") log.Notice("notice") @@ -269,19 +269,19 @@ func testFileRotate(t *testing.T, fn1, fn2 string, daily, hourly bool) { RotatePerm: "0440", } - if daily { - fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) - fw.dailyOpenTime = time.Now().Add(-24 * time.Hour) - fw.dailyOpenDate = fw.dailyOpenTime.Day() - } + if daily { + fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) + fw.dailyOpenTime = time.Now().Add(-24 * time.Hour) + fw.dailyOpenDate = fw.dailyOpenTime.Day() + } - if hourly { - fw.Init(fmt.Sprintf(`{"filename":"%v","maxhours":1}`, fn1)) - fw.hourlyOpenTime = time.Now().Add(-1 * time.Hour) - fw.hourlyOpenDate = fw.hourlyOpenTime.Day() - } + if hourly { + fw.Init(fmt.Sprintf(`{"filename":"%v","maxhours":1}`, fn1)) + fw.hourlyOpenTime = time.Now().Add(-1 * time.Hour) + fw.hourlyOpenDate = fw.hourlyOpenTime.Day() + } - fw.WriteMsg(time.Now(), "this is a msg for test", LevelDebug) + fw.WriteMsg(time.Now(), "this is a msg for test", LevelDebug) for _, file := range []string{fn1, fn2} { _, err := os.Stat(file) @@ -328,8 +328,8 @@ func testFileDailyRotate(t *testing.T, fn1, fn2 string) { func testFileHourlyRotate(t *testing.T, fn1, fn2 string) { fw := &fileLogWriter{ - Hourly: true, - MaxHours: 168, + Hourly: true, + MaxHours: 168, Rotate: true, Level: LevelTrace, Perm: "0660", diff --git a/metric/prometheus.go b/metric/prometheus.go index 7722240b..86e2c1b1 100644 --- a/metric/prometheus.go +++ b/metric/prometheus.go @@ -57,15 +57,15 @@ func registerBuildInfo() { Subsystem: "build_info", Help: "The building information", ConstLabels: map[string]string{ - "appname": beego.BConfig.AppName, + "appname": beego.BConfig.AppName, "build_version": beego.BuildVersion, "build_revision": beego.BuildGitRevision, "build_status": beego.BuildStatus, "build_tag": beego.BuildTag, - "build_time": strings.Replace(beego.BuildTime, "--", " ", 1), + "build_time": strings.Replace(beego.BuildTime, "--", " ", 1), "go_version": beego.GoVersion, "git_branch": beego.GitBranch, - "start_time": time.Now().Format("2006-01-02 15:04:05"), + "start_time": time.Now().Format("2006-01-02 15:04:05"), }, }, []string{}) diff --git a/orm/cmd_utils.go b/orm/cmd_utils.go index 61f17346..692a079f 100644 --- a/orm/cmd_utils.go +++ b/orm/cmd_utils.go @@ -197,9 +197,9 @@ func getDbCreateSQL(al *alias) (sqls []string, tableIndexes map[string][]dbIndex if strings.Contains(column, "%COL%") { column = strings.Replace(column, "%COL%", fi.column, -1) } - - if fi.description != "" && al.Driver!=DRSqlite { - column += " " + fmt.Sprintf("COMMENT '%s'",fi.description) + + if fi.description != "" && al.Driver != DRSqlite { + column += " " + fmt.Sprintf("COMMENT '%s'", fi.description) } columns = append(columns, column) diff --git a/orm/db_alias.go b/orm/db_alias.go index bf6c350c..369802a7 100644 --- a/orm/db_alias.go +++ b/orm/db_alias.go @@ -18,10 +18,11 @@ import ( "context" "database/sql" "fmt" - lru "github.com/hashicorp/golang-lru" "reflect" "sync" "time" + + lru "github.com/hashicorp/golang-lru" ) // DriverType database driver constant int. @@ -424,7 +425,7 @@ func GetDB(aliasNames ...string) (*sql.DB, error) { } type stmtDecorator struct { - wg sync.WaitGroup + wg sync.WaitGroup stmt *sql.Stmt } @@ -459,7 +460,9 @@ func newStmtDecorator(sqlStmt *sql.Stmt) *stmtDecorator { } func newStmtDecoratorLruWithEvict() *lru.Cache { - cache, _ := lru.NewWithEvict(1000, func(key interface{}, value interface{}) { + // temporarily solution + // we fixed this problem in v2.x + cache, _ := lru.NewWithEvict(50, func(key interface{}, value interface{}) { value.(*stmtDecorator).destroy() }) return cache diff --git a/orm/orm_log.go b/orm/orm_log.go index f107bb59..5bb3a24f 100644 --- a/orm/orm_log.go +++ b/orm/orm_log.go @@ -61,7 +61,7 @@ func debugLogQueies(alias *alias, operaton, query string, t time.Time, err error con += " - " + err.Error() } logMap["sql"] = fmt.Sprintf("%s-`%s`", query, strings.Join(cons, "`, `")) - if LogFunc != nil{ + if LogFunc != nil { LogFunc(logMap) } DebugLog.Println(con) diff --git a/plugins/authz/authz.go b/plugins/authz/authz.go index 9dc0db76..879fdea6 100644 --- a/plugins/authz/authz.go +++ b/plugins/authz/authz.go @@ -40,10 +40,11 @@ package authz import ( + "net/http" + "github.com/astaxie/beego" "github.com/astaxie/beego/context" "github.com/casbin/casbin" - "net/http" ) // NewAuthorizer returns the authorizer. diff --git a/plugins/authz/authz_test.go b/plugins/authz/authz_test.go index 49aed84c..e9d397a2 100644 --- a/plugins/authz/authz_test.go +++ b/plugins/authz/authz_test.go @@ -15,13 +15,14 @@ package authz import ( + "net/http" + "net/http/httptest" + "testing" + "github.com/astaxie/beego" "github.com/astaxie/beego/context" "github.com/astaxie/beego/plugins/auth" "github.com/casbin/casbin" - "net/http" - "net/http/httptest" - "testing" ) func testRequest(t *testing.T, handler *beego.ControllerRegister, user string, path string, method string, code int) { diff --git a/session/redis_cluster/redis_cluster.go b/session/redis_cluster/redis_cluster.go index 2fe300df..802c1c39 100644 --- a/session/redis_cluster/redis_cluster.go +++ b/session/redis_cluster/redis_cluster.go @@ -31,14 +31,16 @@ // // more docs: http://beego.me/docs/module/session.md package redis_cluster + import ( "net/http" "strconv" "strings" "sync" + "time" + "github.com/astaxie/beego/session" rediss "github.com/go-redis/redis" - "time" ) var redispder = &Provider{} @@ -101,7 +103,7 @@ func (rs *SessionStore) SessionRelease(w http.ResponseWriter) { return } c := rs.p - c.Set(rs.sid, string(b), time.Duration(rs.maxlifetime) * time.Second) + c.Set(rs.sid, string(b), time.Duration(rs.maxlifetime)*time.Second) } // Provider redis_cluster session provider @@ -146,10 +148,10 @@ func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error { } else { rp.dbNum = 0 } - + rp.poollist = rediss.NewClusterClient(&rediss.ClusterOptions{ Addrs: strings.Split(rp.savePath, ";"), - Password: rp.password, + Password: rp.password, PoolSize: rp.poolsize, }) return rp.poollist.Ping().Err() @@ -186,15 +188,15 @@ func (rp *Provider) SessionExist(sid string) bool { // SessionRegenerate generate new sid for redis_cluster session func (rp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) { c := rp.poollist - + if existed, err := c.Exists(oldsid).Result(); err != nil || existed == 0 { // oldsid doesn't exists, set the new sid directly // ignore error here, since if it return error // the existed value will be 0 - c.Set(sid, "", time.Duration(rp.maxlifetime) * time.Second) + c.Set(sid, "", time.Duration(rp.maxlifetime)*time.Second) } else { c.Rename(oldsid, sid) - c.Expire(sid, time.Duration(rp.maxlifetime) * time.Second) + c.Expire(sid, time.Duration(rp.maxlifetime)*time.Second) } return rp.SessionRead(sid) } diff --git a/session/redis_sentinel/sess_redis_sentinel.go b/session/redis_sentinel/sess_redis_sentinel.go index 6ecb2977..29284d03 100644 --- a/session/redis_sentinel/sess_redis_sentinel.go +++ b/session/redis_sentinel/sess_redis_sentinel.go @@ -33,13 +33,14 @@ package redis_sentinel import ( - "github.com/astaxie/beego/session" - "github.com/go-redis/redis" "net/http" "strconv" "strings" "sync" "time" + + "github.com/astaxie/beego/session" + "github.com/go-redis/redis" ) var redispder = &Provider{} diff --git a/session/sess_file_test.go b/session/sess_file_test.go index 0cf021db..021c43fc 100644 --- a/session/sess_file_test.go +++ b/session/sess_file_test.go @@ -369,8 +369,7 @@ func TestFileSessionStore_SessionRelease(t *testing.T) { t.Error(err) } - - s.Set(i,i) + s.Set(i, i) s.SessionRelease(nil) } @@ -384,4 +383,4 @@ func TestFileSessionStore_SessionRelease(t *testing.T) { t.Error() } } -} \ No newline at end of file +} diff --git a/staticfile.go b/staticfile.go index 84e9aa7b..e26776c5 100644 --- a/staticfile.go +++ b/staticfile.go @@ -202,7 +202,7 @@ func searchFile(ctx *context.Context) (string, os.FileInfo, error) { if !strings.Contains(requestPath, prefix) { continue } - if prefix != "/" && len(requestPath) > len(prefix) && requestPath[len(prefix)] != '/' { + if prefix != "/" && len(requestPath) > len(prefix) && requestPath[len(prefix)] != '/' { continue } filePath := path.Join(staticDir, requestPath[len(prefix):]) diff --git a/template_test.go b/template_test.go index 049655db..b688c8a0 100644 --- a/template_test.go +++ b/template_test.go @@ -16,12 +16,13 @@ package beego import ( "bytes" - "github.com/astaxie/beego/testdata" - "github.com/elazarl/go-bindata-assetfs" "net/http" "os" "path/filepath" "testing" + + "github.com/astaxie/beego/testdata" + "github.com/elazarl/go-bindata-assetfs" ) var header = `{{define "header"}} diff --git a/templatefunc.go b/templatefunc.go index ba1ec5eb..6f02b8d6 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -362,7 +362,7 @@ func parseFormToStruct(form url.Values, objT reflect.Type, objV reflect.Value) e value = value[:25] t, err = time.ParseInLocation(time.RFC3339, value, time.Local) } else if strings.HasSuffix(strings.ToUpper(value), "Z") { - t, err = time.ParseInLocation(time.RFC3339, value, time.Local) + t, err = time.ParseInLocation(time.RFC3339, value, time.Local) } else if len(value) >= 19 { if strings.Contains(value, "T") { value = value[:19] diff --git a/testdata/bindata.go b/testdata/bindata.go index beade103..ccfb51d4 100644 --- a/testdata/bindata.go +++ b/testdata/bindata.go @@ -11,13 +11,14 @@ import ( "bytes" "compress/gzip" "fmt" - "github.com/elazarl/go-bindata-assetfs" "io" "io/ioutil" "os" "path/filepath" "strings" "time" + + "github.com/elazarl/go-bindata-assetfs" ) func bindataRead(data []byte, name string) ([]byte, error) { diff --git a/validation/util.go b/validation/util.go index 82206f4f..918b206c 100644 --- a/validation/util.go +++ b/validation/util.go @@ -213,7 +213,7 @@ func parseFunc(vfunc, key string, label string) (v ValidFunc, err error) { return } - tParams, err := trim(name, key+"."+ name + "." + label, params) + tParams, err := trim(name, key+"."+name+"."+label, params) if err != nil { return } diff --git a/validation/validators.go b/validation/validators.go index 38b6f1aa..a0e4bcbb 100644 --- a/validation/validators.go +++ b/validation/validators.go @@ -16,13 +16,14 @@ package validation import ( "fmt" - "github.com/astaxie/beego/logs" "reflect" "regexp" "strings" "sync" "time" "unicode/utf8" + + "github.com/astaxie/beego/logs" ) // CanSkipFuncs will skip valid if RequiredFirst is true and the struct field's value is empty From b2a96234ab3ebd10989bb2f5a16c3a4b860467c5 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Tue, 3 Nov 2020 22:21:32 +0800 Subject: [PATCH 11/11] Upgrade version --- beego.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beego.go b/beego.go index 8ebe0bab..44184c25 100644 --- a/beego.go +++ b/beego.go @@ -23,7 +23,7 @@ import ( const ( // VERSION represent beego web framework version. - VERSION = "1.12.2" + VERSION = "1.12.3" // DEV is for develop DEV = "dev"