Skip to content

Commit db1a30e

Browse files
author
Chris Elder
committed
[FAB-8877] Validate Reserved Fields Name in CouchDB
Ledger removed the "data" wrapper when using CouchDB as the state database. This change means that documents in the state database must meet CouchDB's restrictions for top level field names. The validation for simulation and commit needs to be updated for the following invalid field names: - Any field beginning with an underscore, "_" - ~version - ~metadata Change-Id: I9203691553de8c15888ce93ade99337bad5222a9 Signed-off-by: Chris Elder <chris.elder@us.ibm.com>
1 parent 8b19d4e commit db1a30e

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ const (
2323
revField = "_rev"
2424
versionField = "~version"
2525
deletedField = "_deleted"
26+
metadataField = "~metadata"
2627
)
2728

28-
var reservedFields = [4]string{idField, revField, versionField, deletedField}
29-
3029
type keyValue struct {
3130
key string
3231
*statedb.VersionedValue
@@ -47,9 +46,9 @@ func castToJSON(b []byte) (jsonValue, error) {
4746
}
4847

4948
func (v jsonValue) checkReservedFieldsNotPresent() error {
50-
for _, fieldName := range reservedFields {
51-
if _, fieldFound := v[fieldName]; fieldFound {
52-
return fmt.Errorf("The field %s is present", fieldName)
49+
for fieldName := range v {
50+
if fieldName == versionField || fieldName == metadataField || strings.HasPrefix(fieldName, "_") {
51+
return fmt.Errorf("The field [%s] is not valid for the CouchDB state database", fieldName)
5352
}
5453
}
5554
return nil

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

+13
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,26 @@ func TestUtilityFunctions(t *testing.T) {
198198
err = db.ValidateKeyValue(string([]byte{0xff, 0xfe, 0xfd}), []byte("Some random bytes"))
199199
testutil.AssertError(t, err, "ValidateKey should have thrown an error for an invalid utf-8 string")
200200

201+
reservedFields := []string{"~version", "~metadata", "_id", "_test"}
202+
201203
// ValidateKey should return an error for a json value that already contains one of the reserved fields
204+
// at the top level
202205
for _, reservedField := range reservedFields {
203206
testVal := fmt.Sprintf(`{"%s":"dummyVal"}`, reservedField)
204207
err = db.ValidateKeyValue("testKey", []byte(testVal))
205208
testutil.AssertError(t, err, fmt.Sprintf(
206209
"ValidateKey should have thrown an error for a json value %s, as contains one of the rserved fields", testVal))
207210
}
211+
212+
// ValidateKey should not return an error for a json value that already contains one of the reserved fields
213+
// if not at the top level
214+
for _, reservedField := range reservedFields {
215+
testVal := fmt.Sprintf(`{"data.%s":"dummyVal"}`, reservedField)
216+
err = db.ValidateKeyValue("testKey", []byte(testVal))
217+
testutil.AssertNoError(t, err, fmt.Sprintf(
218+
"ValidateKey should not have thrown an error the json value %s since the reserved field was not at the top level", testVal))
219+
}
220+
208221
}
209222

210223
// TestInvalidJSONFields tests for invalid JSON fields

0 commit comments

Comments
 (0)