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

CBG-3238 perform a length check on raw bytes #6438

Merged
merged 8 commits into from
Sep 21, 2023
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
3 changes: 3 additions & 0 deletions base/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ var (
// ErrXattrNotFound is returned if a requested xattr is not present on a DCP event
ErrXattrNotFound = &sgError{"Xattr Not Found"}

// ErrXattrInvalidLen is returned if the xattr is corrupt.
ErrXattrInvalidLen = &sgError{"Xattr stream length"}

// ErrPartialViewErrors is returned if the view call contains any partial errors.
// This is more of a warning, and inspecting ViewResult.Errors is required for detail.
ErrPartialViewErrors = &sgError{"Partial errors in view"}
Expand Down
2 changes: 1 addition & 1 deletion db/attachment_compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func getAttachmentSyncData(dataType uint8, data []byte) (*AttachmentCompactionDa
if dataType&base.MemcachedDataTypeXattr != 0 {
body, xattr, _, err := parseXattrStreamData(base.SyncXattrName, "", data)
if err != nil {
if errors.Is(err, base.ErrXattrNotFound) {
if errors.Is(err, base.ErrXattrNotFound) || errors.Is(err, base.ErrXattrInvalidLen) {
return nil, nil
}
return nil, err
Expand Down
9 changes: 9 additions & 0 deletions db/change_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2255,3 +2255,12 @@ func BenchmarkDocChanged(b *testing.B) {
})
}
}

func TestInvalidXattrStream(t *testing.T) {

body, xattr, userXattr, err := parseXattrStreamData(base.SyncXattrName, "", []byte("abcde"))
require.Error(t, err)
require.Nil(t, body)
require.Nil(t, xattr)
require.Nil(t, userXattr)
}
3 changes: 3 additions & 0 deletions db/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ func parseXattrStreamData(xattrName string, userXattrName string, data []byte) (
}

xattrsLen := binary.BigEndian.Uint32(data[0:4])
if int(xattrsLen+4) > len(data) {
return nil, nil, nil, fmt.Errorf("%w (%d) from bytes %+v", base.ErrXattrInvalidLen, xattrsLen, data[0:4])
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are we able to write a test for what happens if data[0:4] doesn't represent xattrsLen (i.e. document doesn't have xattrs, it's just the body), but the Uint32 value of data[0:4] is less than len(data)? I'm assuming we'll return an error while trying to parse the xattr key/value pairs below, but would be good to verify there aren't any subsequent panics in that case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a test for this.

body = data[xattrsLen+4:]
if xattrsLen == 0 {
return body, nil, nil, nil
Expand Down
59 changes: 59 additions & 0 deletions db/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/couchbase/sync_gateway/base"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TODO: Could consider checking this in as a file and include it into the compiled test binary using something like https://github.com/jteeuwen/go-bindata
Expand Down Expand Up @@ -291,3 +292,61 @@ func TestGetDeepMutableBody(t *testing.T) {
})
}
}

func TestInvalidXattrStreamDataLen(t *testing.T) {
testCases := []struct {
name string
body []byte
expectedErr error
}{
{
name: "bad value",
body: []byte("abcde"),
expectedErr: base.ErrXattrInvalidLen,
},
{
name: "xattr length 4, overflow",
body: []byte{0x00, 0x00, 0x00, 0x04, 0x01},
expectedErr: base.ErrXattrInvalidLen,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
// parseXattrStreamData is the underlying function
body, xattr, userXattr, err := parseXattrStreamData(base.SyncXattrName, "", test.body)
require.Error(t, err)
require.ErrorIs(t, err, test.expectedErr)
require.Nil(t, body)
require.Nil(t, xattr)
require.Nil(t, userXattr)
// UnmarshalDocumentSyncData wraps parseXattrStreamData
result, rawBody, rawXattr, rawUserXattr, err := UnmarshalDocumentSyncDataFromFeed(test.body, base.MemcachedDataTypeXattr, "", false)
require.ErrorIs(t, err, base.ErrXattrInvalidLen)
require.Nil(t, result)
require.Nil(t, rawBody)
require.Nil(t, rawXattr)
require.Nil(t, rawUserXattr)

})
}
}

func TestInvalidXattrStreamEmptyBody(t *testing.T) {
inputStream := []byte{0x00, 0x00, 0x00, 0x01, 0x01}
emptyBody := []byte{}
// parseXattrStreamData is the underlying function
body, xattr, userXattr, err := parseXattrStreamData(base.SyncXattrName, "", inputStream)
require.NoError(t, err)
require.Equal(t, emptyBody, body)
require.Nil(t, xattr)
require.Nil(t, userXattr)

// UnmarshalDocumentSyncData wraps parseXattrStreamData
result, rawBody, rawXattr, rawUserXattr, err := UnmarshalDocumentSyncDataFromFeed(inputStream, base.MemcachedDataTypeXattr, "", false)
require.Error(t, err) // unexpected end of JSON input
require.Nil(t, result)
require.Equal(t, emptyBody, rawBody)
require.Nil(t, rawXattr)
require.Nil(t, rawUserXattr)

}