Skip to content

Commit 571bdef

Browse files
committed
Fix for an empty string as a couchdb key
FAB-12836 #done Change-Id: I93d55fb4dc07a92a4cd082fb6ecb285381bd8783 Signed-off-by: manish <manish.sethi@gmail.com>
1 parent 1aa5b47 commit 571bdef

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

core/ledger/kvledger/txmgmt/statedb/statecouchdb/couchdoc_conv.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ func validateKey(key string) error {
216216
if strings.HasPrefix(key, "_") {
217217
return errors.Errorf("invalid key [%s], cannot begin with \"_\"", key)
218218
}
219+
if key == "" {
220+
return errors.New("invalid key. Empty string is not supported as a key by couchdb")
221+
}
219222
return nil
220223
}
221224

core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,14 @@ func TestUtilityFunctions(t *testing.T) {
160160
err = db.ValidateKeyValue("testKey", []byte("Some random bytes"))
161161
assert.Nil(t, err)
162162

163-
// ValidateKey should return an error for an invalid key
163+
// ValidateKeyValue should return an error for a key that is not a utf-8 valid string
164164
err = db.ValidateKeyValue(string([]byte{0xff, 0xfe, 0xfd}), []byte("Some random bytes"))
165165
assert.Error(t, err, "ValidateKey should have thrown an error for an invalid utf-8 string")
166166

167+
// ValidateKeyValue should return an error for a key that is an empty string
168+
assert.EqualError(t, db.ValidateKeyValue("", []byte("validValue")),
169+
"invalid key. Empty string is not supported as a key by couchdb")
170+
167171
reservedFields := []string{"~version", "_id", "_test"}
168172

169173
// ValidateKeyValue should return an error for a json value that contains one of the reserved fields

0 commit comments

Comments
 (0)