From b99aaab42e1c4a893c196a3de58ba80bd17f2f2d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 23 Oct 2018 03:41:06 -0700 Subject: [PATCH] Sid value can be any unicode character support it (#6676) Fixes #6476 --- pkg/policy/id.go | 11 ++--------- pkg/policy/id_test.go | 10 ++++------ 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/pkg/policy/id.go b/pkg/policy/id.go index 9040ea40a..8971c1a79 100644 --- a/pkg/policy/id.go +++ b/pkg/policy/id.go @@ -19,22 +19,15 @@ package policy import ( "encoding/json" "fmt" - "regexp" + "unicode/utf8" ) -var idRegexp = regexp.MustCompile("^[[:alnum:]]+$") - // ID - policy ID. type ID string // IsValid - checks if ID is valid or not. func (id ID) IsValid() bool { - // Allow empty string as ID. - if string(id) == "" { - return true - } - - return idRegexp.MatchString(string(id)) + return utf8.ValidString(string(id)) } // MarshalJSON - encodes ID to JSON data. diff --git a/pkg/policy/id_test.go b/pkg/policy/id_test.go index 74065ff31..3e30c9a24 100644 --- a/pkg/policy/id_test.go +++ b/pkg/policy/id_test.go @@ -29,13 +29,14 @@ func TestIDIsValid(t *testing.T) { }{ {ID("DenyEncryptionSt1"), true}, {ID(""), true}, + {ID("aa\xe2"), false}, } for i, testCase := range testCases { result := testCase.id.IsValid() if result != testCase.expectedResult { - t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) + t.Errorf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) } } } @@ -46,10 +47,9 @@ func TestIDMarshalJSON(t *testing.T) { expectedResult []byte expectErr bool }{ - {ID("foo"), []byte(`"foo"`), false}, - {ID("1234"), []byte(`"1234"`), false}, {ID("DenyEncryptionSt1"), []byte(`"DenyEncryptionSt1"`), false}, {ID(""), []byte(`""`), false}, + {ID("aa\xe2"), nil, true}, // invalid utf-8 } for i, testCase := range testCases { @@ -74,11 +74,9 @@ func TestIDUnmarshalJSON(t *testing.T) { expectedResult ID expectErr bool }{ - {[]byte(`"foo"`), ID("foo"), false}, - {[]byte(`"1234"`), ID("1234"), false}, {[]byte(`"DenyEncryptionSt1"`), ID("DenyEncryptionSt1"), false}, {[]byte(`""`), ID(""), false}, - {[]byte(`"foo bar"`), ID(""), true}, + {[]byte(`"aa\xe2"`), ID(""), true}, } for i, testCase := range testCases {