diff options
author | Wesley Bitter <github@wessie.info> | 2018-05-18 18:25:24 +0300 |
---|---|---|
committer | Matt Silverlock <matt@eatsleeprepeat.net> | 2018-05-18 18:25:24 +0300 |
commit | b009e17e9cb1a3b7b447cf9dc0110fcaebcc1b6b (patch) | |
tree | 5ce082fb1f264db2981b218b17493c0ebe59b23c | |
parent | e59506cc896acb7f7bf732d4fdf5e25f7ccd8983 (diff) |
[bugfix] check for an empty hashKey (#52)
this scenario might occur when converting from an empty string to
byte slice, such as when reading from a configuration file.
-rw-r--r-- | securecookie.go | 2 | ||||
-rw-r--r-- | securecookie_test.go | 23 |
2 files changed, 16 insertions, 9 deletions
diff --git a/securecookie.go b/securecookie.go index cd4e097..61af390 100644 --- a/securecookie.go +++ b/securecookie.go @@ -141,7 +141,7 @@ func New(hashKey, blockKey []byte) *SecureCookie { maxLength: 4096, sz: GobEncoder{}, } - if hashKey == nil { + if len(hashKey) == 0 { s.err = errHashKeyNotSet } if blockKey != nil { diff --git a/securecookie_test.go b/securecookie_test.go index 2b0f8a1..c32ff33 100644 --- a/securecookie_test.go +++ b/securecookie_test.go @@ -268,15 +268,22 @@ func TestMultiNoCodecs(t *testing.T) { } func TestMissingKey(t *testing.T) { - s1 := New(nil, nil) - - var dst []byte - err := s1.Decode("sid", "value", &dst) - if err != errHashKeyNotSet { - t.Fatalf("Expected %#v, got %#v", errHashKeyNotSet, err) + emptyKeys := [][]byte{ + nil, + []byte(""), } - if err2, ok := err.(Error); !ok || !err2.IsUsage() { - t.Errorf("Expected missing hash key to be IsUsage(); was %#v", err) + + for _, key := range emptyKeys { + s1 := New(key, nil) + + var dst []byte + err := s1.Decode("sid", "value", &dst) + if err != errHashKeyNotSet { + t.Fatalf("Expected %#v, got %#v", errHashKeyNotSet, err) + } + if err2, ok := err.(Error); !ok || !err2.IsUsage() { + t.Errorf("Expected missing hash key to be IsUsage(); was %#v", err) + } } } |