Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 69 additions & 0 deletions datastore/encrypt_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
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"
)

const nigoriTypeID int = 47745

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

// Data types exempt from the specifics.encrypted requirement, keyed by
// EntitySpecifics field number from entity_specifics.proto.
var encryptionExemptTypes = map[int]struct{}{
// Carries the key bag that protects every other type.
nigoriTypeID: {},
// The server has to read these to purge its own stored entities, and it
// must not hold the user's passphrase. The payload is visit ids only.
HistoryDeleteDirectiveTypeID: {},
Comment thread
DJAndries marked this conversation as resolved.
Resolved
// Encrypted by its own scheme into its own specifics fields, so
// `specifics.encrypted` stays unset. See
// DataTypeWorker::EncryptPasswordSpecificsData(). 45873 is the
// PasswordSpecifics field number.
45873: {},
Comment thread
DJAndries marked this conversation as resolved.
Resolved
}

// checkEntityEncrypted returns an error if the entity carries plaintext specifics.
// Exempt types and server-defined entities are skipped since the server
// creates the latter itself.
func checkEntityEncrypted(entity *sync_pb.SyncEntity, dataType int) error {
if _, ok := encryptionExemptTypes[dataType]; ok {
return nil
}
Comment thread
DJAndries marked this conversation as resolved.
Resolved
if entity.GetServerDefinedUniqueTag() != "" {
return nil
}
if hasPlaintextSpecifics(entity.GetSpecifics()) {
Comment thread
DJAndries marked this conversation as resolved.
Resolved
return ErrEntityHasPlaintextSpecifics
}
return nil
}

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
}

// maybeRejectUnencryptedEntity checks the entity's encryption and reports
// failures to Sentry.
func maybeRejectUnencryptedEntity(entity *sync_pb.SyncEntity, dataType int) error {
err := checkEntityEncrypted(entity, dataType)
if err == nil {
return nil
}
sentry.WithScope(func(scope *sentry.Scope) {
scope.SetTag("data_type", strconv.Itoa(dataType))
sentry.CaptureException(err)
})
return err
}
93 changes: 93 additions & 0 deletions datastore/encrypt_check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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": {SpecificsVariant: &sync_pb.EntitySpecifics_HistoryDeleteDirective{
HistoryDeleteDirective: &sync_pb.HistoryDeleteDirectiveSpecifics{
UrlDirective: &sync_pb.UrlDirective{Url: aws.String("https://example.com")},
},
}},
"password": {SpecificsVariant: &sync_pb.EntitySpecifics_Password{
Password: &sync_pb.PasswordSpecifics{Encrypted: &sync_pb.EncryptedData{}},
}},
} {
t.Run(name, func(t *testing.T) {
_, err := datastore.CreateDBSyncEntity(newPBEntity(specifics), nil, "client1")
assert.NoError(t, err)
})
}
}

func TestCreateDBSyncEntity_ServerDefinedSkipsEncryptionCheck(t *testing.T) {
entity := newPBEntity(plaintextBookmarkSpecifics())
entity.ServerDefinedUniqueTag = aws.String("bookmark_bar")
_, err := datastore.CreateDBSyncEntity(entity, nil, "client1")
assert.NoError(t, err)
}
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 = maybeRejectUnencryptedEntity(entity, dataType); err != nil {
return nil, err
}

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