Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for invalid key before hitting couchdb #2133

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ func (vdb *VersionedDB) readFromDB(namespace, key string) (*keyValue, error) {
if err != nil {
return nil, err
}
if err := validateKey(key); err != nil {
return nil, err
}
Comment on lines +529 to +531
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the goal is to avoid sending invalid keys to CouchDB but not to the cache, this PR LGTM. Is that the goal? If not, we need to perform this check before accessing the cache also.

Copy link
Contributor Author

@manish-sethi manish-sethi Nov 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache would eventually be populated from DB content only. So, checking in cache does not add any value. Rather, in the current way, it amortizes the cost of this check.

couchDoc, _, err := db.readDoc(key)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,40 @@ func TestDropErrorPath(t *testing.T) {
require.EqualError(t, vdbEnv.DBProvider.Drop(channelName), "internal leveldb error while obtaining db iterator: leveldb: closed")
}

func TestReadFromDBInvalidKey(t *testing.T) {
vdbEnv.init(t, sysNamespaces)
defer vdbEnv.cleanup()
channelName := "test_getstate_invalidkey"
db, err := vdbEnv.DBProvider.GetDBHandle(channelName, nil)
require.NoError(t, err)
vdb := db.(*VersionedDB)

testcase := []struct {
key string
expectedErrorMsg string
}{
{
key: string([]byte{0xff, 0xfe, 0xfd}),
expectedErrorMsg: "invalid key [fffefd], must be a UTF-8 string",
},
{
key: "",
expectedErrorMsg: "invalid key. Empty string is not supported as a key by couchdb",
},
{
key: "_key_starting_with_an_underscore",
expectedErrorMsg: `invalid key [_key_starting_with_an_underscore], cannot begin with "_"`,
},
}

for i, tc := range testcase {
t.Run(fmt.Sprintf("testcase-%d", i), func(t *testing.T) {
_, err = vdb.readFromDB("ns", tc.key)
require.EqualError(t, err, tc.expectedErrorMsg)
})
}
}

type dummyFullScanIter struct {
err error
kv *statedb.VersionedKV
Expand Down