From 0e42695ca9d547d879e6cee048391d89bb87f94f Mon Sep 17 00:00:00 2001 From: Xingwang Liao Date: Mon, 19 Apr 2021 06:29:05 +0000 Subject: [PATCH 1/2] Remove padding characters from random strings * base64.RawURLEncoding added in go 1.15, can remove padding '=' characters from base64 strings --- modules/context/secret.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/context/secret.go b/modules/context/secret.go index fcb488d211a0a..1cfa07f079058 100644 --- a/modules/context/secret.go +++ b/modules/context/secret.go @@ -34,7 +34,7 @@ func randomBytes(len int64) ([]byte, error) { func randomString(len int64) (string, error) { b, err := randomBytes(len) - return base64.URLEncoding.EncodeToString(b), err + return base64.RawURLEncoding.EncodeToString(b), err } // AesEncrypt encrypts text and given key with AES. From 45527386b452ea66497d49928e3a394e69aa6c83 Mon Sep 17 00:00:00 2001 From: Xingwang Liao Date: Mon, 19 Apr 2021 06:52:45 +0000 Subject: [PATCH 2/2] go 1.14 compatible --- modules/context/secret.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/context/secret.go b/modules/context/secret.go index 1cfa07f079058..db4d9436b7a80 100644 --- a/modules/context/secret.go +++ b/modules/context/secret.go @@ -12,6 +12,7 @@ import ( "encoding/base64" "errors" "io" + "strings" ) // NewSecret creates a new secret @@ -34,7 +35,11 @@ func randomBytes(len int64) ([]byte, error) { func randomString(len int64) (string, error) { b, err := randomBytes(len) - return base64.RawURLEncoding.EncodeToString(b), err + if err != nil { + return "", err + } + encoded := base64.URLEncoding.EncodeToString(b) + return strings.TrimRight(encoded, "="), nil } // AesEncrypt encrypts text and given key with AES.