From bfdce820e9292fb441d516a2defa12b8861190de Mon Sep 17 00:00:00 2001 From: Manuel Alejandro de Brito Fontes Date: Wed, 17 Jan 2018 10:26:32 -0200 Subject: [PATCH] Random string function should only contains letters (#1906) --- cmd/nginx/main.go | 3 +++ .../ingress/controller/template/template.go | 27 ++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/cmd/nginx/main.go b/cmd/nginx/main.go index cbc52573c..d7edf8118 100644 --- a/cmd/nginx/main.go +++ b/cmd/nginx/main.go @@ -19,6 +19,7 @@ package main import ( "encoding/json" "fmt" + "math/rand" "net" "net/http" "net/http/pprof" @@ -47,6 +48,8 @@ import ( ) func main() { + rand.Seed(time.Now().UnixNano()) + fmt.Println(version.String()) showVersion, conf, err := parseFlags() diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index 76755df9c..a1cf9a7d2 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -17,10 +17,10 @@ limitations under the License. package template import ( - "crypto/rand" "encoding/base64" "encoding/json" "fmt" + "math/rand" "net" "net/url" "os" @@ -28,6 +28,7 @@ import ( "strconv" "strings" text_template "text/template" + "time" "github.com/golang/glog" "github.com/pkg/errors" @@ -493,12 +494,7 @@ func buildDenyVariable(a interface{}) string { } if _, ok := denyPathSlugMap[l]; !ok { - s, err := randomString() - if err != nil { - return "" - } - - denyPathSlugMap[l] = s + denyPathSlugMap[l] = randomString() } return fmt.Sprintf("$deny_%v", denyPathSlugMap[l]) @@ -693,12 +689,17 @@ func buildAuthSignURL(input interface{}) string { return fmt.Sprintf("%v&rd=$pass_access_scheme://$http_host$request_uri", s) } -func randomString() (string, error) { - b := make([]byte, 16) - _, err := rand.Read(b) - if err != nil { - return "", err +var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + +func init() { + rand.Seed(time.Now().UnixNano()) +} + +func randomString() string { + b := make([]rune, 32) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] } - return string(b), nil + return string(b) }