Skip to content
Open
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
49 changes: 45 additions & 4 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@ func getNigoriSpecifics() *sync_pb.EntitySpecifics {
}
}

func getEncryptedData() *sync_pb.EncryptedData {
return &sync_pb.EncryptedData{KeyName: aws.String("key"), Blob: aws.String("rubbish")}
}

func getBookmarkSpecifics() *sync_pb.EntitySpecifics {
specifics := getServerDefinedBookmarkSpecifics()
specifics.Encrypted = getEncryptedData()
return specifics
}

func getServerDefinedBookmarkSpecifics() *sync_pb.EntitySpecifics {
bookmarkEntitySpecifics := &sync_pb.EntitySpecifics_Bookmark{
Bookmark: &sync_pb.BookmarkSpecifics{},
}
Expand All @@ -112,6 +122,7 @@ func getDeviceInfoSpecifics() *sync_pb.EntitySpecifics {
DeviceInfo: &sync_pb.DeviceInfoSpecifics{},
}
return &sync_pb.EntitySpecifics{
Encrypted: getEncryptedData(),
SpecificsVariant: deviceInfoEntitySpecifics,
}
}
Expand Down Expand Up @@ -225,6 +236,36 @@ func assertGetUpdatesResponse(suite *CommandTestSuite, rsp *sync_pb.GetUpdatesRe
suite.Equal(expectedChangesRemaining, *rsp.ChangesRemaining)
}

func (suite *CommandTestSuite) TestHandleClientToServerMessage_RejectsPlaintextSpecifics() {
entries := []*sync_pb.SyncEntity{
getCommitEntity("id1_bookmark", 0, false, getBookmarkSpecifics()),
getCommitEntity("id2_bookmark", 0, false, &sync_pb.EntitySpecifics{
SpecificsVariant: &sync_pb.EntitySpecifics_Bookmark{
Bookmark: &sync_pb.BookmarkSpecifics{Url: aws.String("https://example.com")},
},
}),
getCommitEntity("id3_device_info", 0, false, &sync_pb.EntitySpecifics{
SpecificsVariant: &sync_pb.EntitySpecifics_DeviceInfo{
DeviceInfo: &sync_pb.DeviceInfoSpecifics{CacheGuid: aws.String("guid")},
},
}),
}
msg := getClientToServerCommitMsg(entries)
rsp := &sync_pb.ClientToServerResponse{}

suite.Require().NoError(
command.HandleClientToServerMessage(context.Background(), suite.cache, msg, rsp, suite.dynamo, clientID),
"HandleClientToServerMessage should succeed")
assertCommonResponse(suite, rsp, true)
suite.Require().Len(rsp.Commit.Entryresponse, 3)

suite.Equal(sync_pb.CommitResponse_SUCCESS, rsp.Commit.Entryresponse[0].GetResponseType())
for _, entryRsp := range rsp.Commit.Entryresponse[1:] {
suite.Equal(sync_pb.CommitResponse_INVALID_MESSAGE, entryRsp.GetResponseType())
suite.Contains(entryRsp.GetErrorMessage(), "entity must not contain plaintext specifics")
}
}

func (suite *CommandTestSuite) TestHandleClientToServerMessage_Basic() {
// Prepare to commit 2 entries in 2 types.
entries := []*sync_pb.SyncEntity{
Expand Down Expand Up @@ -371,16 +412,16 @@ func (suite *CommandTestSuite) TestHandleClientToServerMessage_NewClient() {
getNigoriSpecifics()),
NewPBSyncAttrs(aws.String(command.BookmarksName), aws.Int64(1),
aws.Bool(false), aws.Bool(true), aws.String(command.BookmarksTag),
getBookmarkSpecifics()),
getServerDefinedBookmarkSpecifics()),
NewPBSyncAttrs(aws.String(command.OtherBookmarksName), aws.Int64(1),
aws.Bool(false), aws.Bool(true), aws.String(command.OtherBookmarksTag),
getBookmarkSpecifics()),
getServerDefinedBookmarkSpecifics()),
NewPBSyncAttrs(aws.String(command.SyncedBookmarksName), aws.Int64(1),
aws.Bool(false), aws.Bool(true), aws.String(command.SyncedBookmarksTag),
getBookmarkSpecifics()),
getServerDefinedBookmarkSpecifics()),
NewPBSyncAttrs(aws.String(command.BookmarkBarName), aws.Int64(1),
aws.Bool(false), aws.Bool(true), aws.String(command.BookmarkBarTag),
getBookmarkSpecifics()),
getServerDefinedBookmarkSpecifics()),
}
newMarker := marker // Initialize expected NewProgressMarker with tokens = 0.
assertGetUpdatesResponse(suite, rsp.GetUpdates, &newMarker, expectedPBSyncAttrs, 0)
Expand Down
64 changes: 64 additions & 0 deletions datastore/encrypt_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package datastore

import (
"errors"
"strconv"

sentry "github.com/getsentry/sentry-go"
"google.golang.org/protobuf/proto"

"github.com/brave/go-sync/schema/protobuf/sync_pb"
)

// Data type IDs are EntitySpecifics field numbers from entity_specifics.proto.
const (
nigoriTypeID int = 47745
passwordTypeID int = 45873
)

var ErrEntityHasPlaintextSpecifics = errors.New("entity must not contain plaintext specifics")

// checkEntityEncrypted returns an error if the entity carries plaintext
// specifics, reporting it to Sentry.
func checkEntityEncrypted(entity *sync_pb.SyncEntity, dataType int) error {
if isEncryptionExempt(entity.GetSpecifics(), dataType) {
return nil
}
if !hasPlaintextSpecifics(entity.GetSpecifics()) {
return nil
}
sentry.WithScope(func(scope *sentry.Scope) {
scope.SetTag("data_type", strconv.Itoa(dataType))
sentry.CaptureException(ErrEntityHasPlaintextSpecifics)
})
return ErrEntityHasPlaintextSpecifics
}

func isEncryptionExempt(specifics *sync_pb.EntitySpecifics, dataType int) bool {
switch dataType {
case nigoriTypeID:
// Carries the key bag that protects every other type.
return true
case HistoryDeleteDirectiveTypeID:
// The server may read these in the future to delete its own stored
// entities. URL directives would expose browsing history, so they are
// not exempt.
return specifics.GetHistoryDeleteDirective().GetUrlDirective().GetUrl() == ""
case passwordTypeID:
// Encrypted by its own scheme into its own specifics fields, so
// `specifics.encrypted` stays unset. See
// DataTypeWorker::EncryptPasswordSpecificsData().
return specifics.GetPassword().GetUnencryptedMetadata() == nil &&
specifics.GetPassword().GetClientOnlyEncryptedData() == nil
}
return false
}

func hasPlaintextSpecifics(specifics *sync_pb.EntitySpecifics) bool {
m := specifics.ProtoReflect()
field := m.WhichOneof(m.Descriptor().Oneofs().ByName("specifics_variant"))
if field == nil {
return false
}
return proto.Size(m.Get(field).Message().Interface()) > 0
}
144 changes: 144 additions & 0 deletions datastore/encrypt_check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package datastore_test

import (
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/stretchr/testify/assert"

"github.com/brave/go-sync/datastore"
"github.com/brave/go-sync/schema/protobuf/sync_pb"
)

func emptyBookmarkSpecifics() *sync_pb.EntitySpecifics {
return &sync_pb.EntitySpecifics{
SpecificsVariant: &sync_pb.EntitySpecifics_Bookmark{Bookmark: &sync_pb.BookmarkSpecifics{}},
}
}

func plaintextBookmarkSpecifics() *sync_pb.EntitySpecifics {
return &sync_pb.EntitySpecifics{
SpecificsVariant: &sync_pb.EntitySpecifics_Bookmark{
Bookmark: &sync_pb.BookmarkSpecifics{Url: aws.String("https://example.com")},
},
}
}

func newPBEntity(specifics *sync_pb.EntitySpecifics) *sync_pb.SyncEntity {
return &sync_pb.SyncEntity{
IdString: aws.String("id"),
Version: aws.Int64(0),
ClientTagHash: aws.String("client_tag"),
Specifics: specifics,
}
}

func TestCreateDBSyncEntity_EmptySpecificsVariant(t *testing.T) {
_, err := datastore.CreateDBSyncEntity(newPBEntity(emptyBookmarkSpecifics()), nil, "client1")
assert.NoError(t, err)
}

func TestCreateDBSyncEntity_EncryptedOnly(t *testing.T) {
specifics := emptyBookmarkSpecifics()
specifics.Encrypted = &sync_pb.EncryptedData{KeyName: aws.String("key"), Blob: aws.String("blob")}
_, err := datastore.CreateDBSyncEntity(newPBEntity(specifics), nil, "client1")
assert.NoError(t, err)
}

func TestCreateDBSyncEntity_PlaintextSpecifics(t *testing.T) {
_, err := datastore.CreateDBSyncEntity(newPBEntity(plaintextBookmarkSpecifics()), nil, "client1")
assert.ErrorIs(t, err, datastore.ErrEntityHasPlaintextSpecifics)
}

func TestCreateDBSyncEntity_EncryptedWithPlaintextSpecifics(t *testing.T) {
specifics := plaintextBookmarkSpecifics()
specifics.Encrypted = &sync_pb.EncryptedData{}
_, err := datastore.CreateDBSyncEntity(newPBEntity(specifics), nil, "client1")
assert.ErrorIs(t, err, datastore.ErrEntityHasPlaintextSpecifics)
}

func TestCreateDBSyncEntity_TombstoneWithPlaintextSpecifics(t *testing.T) {
entity := newPBEntity(plaintextBookmarkSpecifics())
entity.Deleted = aws.Bool(true)
_, err := datastore.CreateDBSyncEntity(entity, nil, "client1")
assert.ErrorIs(t, err, datastore.ErrEntityHasPlaintextSpecifics)
}

func TestCreateDBSyncEntity_EncryptionExemptTypes(t *testing.T) {
for name, specifics := range map[string]*sync_pb.EntitySpecifics{
"nigori": {SpecificsVariant: &sync_pb.EntitySpecifics_Nigori{
Nigori: &sync_pb.NigoriSpecifics{KeystoreMigrationTime: aws.Int64(1)},
}},
"history_delete_directive_global_id": {SpecificsVariant: &sync_pb.EntitySpecifics_HistoryDeleteDirective{
HistoryDeleteDirective: &sync_pb.HistoryDeleteDirectiveSpecifics{
GlobalIdDirective: &sync_pb.GlobalIdDirective{GlobalId: []int64{1, 2}},
},
}},
"history_delete_directive_time_range": {SpecificsVariant: &sync_pb.EntitySpecifics_HistoryDeleteDirective{
HistoryDeleteDirective: &sync_pb.HistoryDeleteDirectiveSpecifics{
TimeRangeDirective: &sync_pb.TimeRangeDirective{StartTimeUsec: aws.Int64(1)},
},
}},
"history_delete_directive_url_directive_without_url": {SpecificsVariant: &sync_pb.EntitySpecifics_HistoryDeleteDirective{
HistoryDeleteDirective: &sync_pb.HistoryDeleteDirectiveSpecifics{
UrlDirective: &sync_pb.UrlDirective{EndTimeUsec: aws.Int64(1)},
},
}},
"password_encrypted": {SpecificsVariant: &sync_pb.EntitySpecifics_Password{
Password: &sync_pb.PasswordSpecifics{
Encrypted: &sync_pb.EncryptedData{KeyName: aws.String("key"), Blob: aws.String("blob")},
},
}},
"password_encrypted_with_notes_backup": {SpecificsVariant: &sync_pb.EntitySpecifics_Password{
Password: &sync_pb.PasswordSpecifics{
Encrypted: &sync_pb.EncryptedData{KeyName: aws.String("key"), Blob: aws.String("blob")},
EncryptedNotesBackup: &sync_pb.EncryptedData{KeyName: aws.String("key"), Blob: aws.String("notes")},
},
}},
} {
t.Run(name, func(t *testing.T) {
_, err := datastore.CreateDBSyncEntity(newPBEntity(specifics), nil, "client1")
assert.NoError(t, err)
})
}
}

func TestCreateDBSyncEntity_EncryptionExemptTypesWithPlaintext(t *testing.T) {
for name, specifics := range map[string]*sync_pb.EntitySpecifics{
"history_delete_directive_url": {SpecificsVariant: &sync_pb.EntitySpecifics_HistoryDeleteDirective{
HistoryDeleteDirective: &sync_pb.HistoryDeleteDirectiveSpecifics{
UrlDirective: &sync_pb.UrlDirective{Url: aws.String("https://example.com")},
},
}},
"password_unencrypted_metadata": {SpecificsVariant: &sync_pb.EntitySpecifics_Password{
Password: &sync_pb.PasswordSpecifics{
UnencryptedMetadata: &sync_pb.PasswordSpecificsMetadata{Url: aws.String("https://example.com")},
},
}},
"password_encrypted_with_unencrypted_metadata": {SpecificsVariant: &sync_pb.EntitySpecifics_Password{
Password: &sync_pb.PasswordSpecifics{
Encrypted: &sync_pb.EncryptedData{KeyName: aws.String("key"), Blob: aws.String("blob")},
UnencryptedMetadata: &sync_pb.PasswordSpecificsMetadata{Url: aws.String("https://example.com")},
},
}},
} {
t.Run(name, func(t *testing.T) {
_, err := datastore.CreateDBSyncEntity(newPBEntity(specifics), nil, "client1")
assert.ErrorIs(t, err, datastore.ErrEntityHasPlaintextSpecifics)
})
}
}

func TestCreateDBSyncEntity_ServerDefinedEmptySpecifics(t *testing.T) {
entity := newPBEntity(emptyBookmarkSpecifics())
entity.ServerDefinedUniqueTag = aws.String("bookmark_bar")
_, err := datastore.CreateDBSyncEntity(entity, nil, "client1")
assert.NoError(t, err)
}

func TestCreateDBSyncEntity_ServerDefinedWithPlaintextSpecifics(t *testing.T) {
entity := newPBEntity(plaintextBookmarkSpecifics())
entity.ServerDefinedUniqueTag = aws.String("bookmark_bar")
_, err := datastore.CreateDBSyncEntity(entity, nil, "client1")
assert.ErrorIs(t, err, datastore.ErrEntityHasPlaintextSpecifics)
}
4 changes: 4 additions & 0 deletions datastore/sync_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,10 @@ func CreateDBSyncEntity(entity *sync_pb.SyncEntity, cacheGUID *string, clientID
s := strings.Split(tag, ",")
dataType, _ := strconv.Atoi(s[1])

if err = checkEntityEncrypted(entity, dataType); err != nil {
return nil, err
}

var uniquePosition []byte
if entity.UniquePosition != nil {
uniquePosition, err = proto.Marshal(entity.UniquePosition)
Expand Down
Loading