From 9533351ee93bf640c71f6d8bab415c06496d1f38 Mon Sep 17 00:00:00 2001 From: Darnell Andries Date: Mon, 9 Nov 2026 18:35:55 -0700 Subject: [PATCH 1/3] Block commits containing unencrypted entity specifics --- command/command_test.go | 49 +++++++++++++++-- datastore/encrypt_check.go | 69 ++++++++++++++++++++++++ datastore/encrypt_check_test.go | 93 +++++++++++++++++++++++++++++++++ datastore/sync_entity.go | 4 ++ 4 files changed, 211 insertions(+), 4 deletions(-) create mode 100644 datastore/encrypt_check.go create mode 100644 datastore/encrypt_check_test.go diff --git a/command/command_test.go b/command/command_test.go index 39442cef..89f9b560 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -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{}, } @@ -112,6 +122,7 @@ func getDeviceInfoSpecifics() *sync_pb.EntitySpecifics { DeviceInfo: &sync_pb.DeviceInfoSpecifics{}, } return &sync_pb.EntitySpecifics{ + Encrypted: getEncryptedData(), SpecificsVariant: deviceInfoEntitySpecifics, } } @@ -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{ @@ -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) diff --git a/datastore/encrypt_check.go b/datastore/encrypt_check.go new file mode 100644 index 00000000..05cbe176 --- /dev/null +++ b/datastore/encrypt_check.go @@ -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: {}, + // 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: {}, +} + +// 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 + } + if entity.GetServerDefinedUniqueTag() != "" { + return nil + } + if hasPlaintextSpecifics(entity.GetSpecifics()) { + 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 +} diff --git a/datastore/encrypt_check_test.go b/datastore/encrypt_check_test.go new file mode 100644 index 00000000..783d19a4 --- /dev/null +++ b/datastore/encrypt_check_test.go @@ -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) +} diff --git a/datastore/sync_entity.go b/datastore/sync_entity.go index 4e1f1844..332e5115 100644 --- a/datastore/sync_entity.go +++ b/datastore/sync_entity.go @@ -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) From 7730322e713ffadfde81bfe4a3d279965053c3bb Mon Sep 17 00:00:00 2001 From: Darnell Andries Date: Wed, 11 Nov 2026 15:05:16 -0700 Subject: [PATCH 2/3] Address PR feedback --- datastore/encrypt_check.go | 70 +++++++++++++++------------------ datastore/encrypt_check_test.go | 63 ++++++++++++++++++++++++++--- datastore/sync_entity.go | 2 +- 3 files changed, 90 insertions(+), 45 deletions(-) diff --git a/datastore/encrypt_check.go b/datastore/encrypt_check.go index 05cbe176..80509bf4 100644 --- a/datastore/encrypt_check.go +++ b/datastore/encrypt_check.go @@ -10,39 +10,47 @@ import ( "github.com/brave/go-sync/schema/protobuf/sync_pb" ) -const nigoriTypeID int = 47745 +// 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") -// 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: {}, - // 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: {}, -} - -// 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. +// checkEntityEncrypted returns an error if the entity carries plaintext +// specifics, reporting it to Sentry. func checkEntityEncrypted(entity *sync_pb.SyncEntity, dataType int) error { - if _, ok := encryptionExemptTypes[dataType]; ok { + if isEncryptionExempt(entity.GetSpecifics(), dataType) { return nil } - if entity.GetServerDefinedUniqueTag() != "" { + if !hasPlaintextSpecifics(entity.GetSpecifics()) { return nil } - if hasPlaintextSpecifics(entity.GetSpecifics()) { - return ErrEntityHasPlaintextSpecifics + 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 } - return nil + return false } func hasPlaintextSpecifics(specifics *sync_pb.EntitySpecifics) bool { @@ -53,17 +61,3 @@ func hasPlaintextSpecifics(specifics *sync_pb.EntitySpecifics) bool { } 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 -} diff --git a/datastore/encrypt_check_test.go b/datastore/encrypt_check_test.go index 783d19a4..07f063b8 100644 --- a/datastore/encrypt_check_test.go +++ b/datastore/encrypt_check_test.go @@ -69,13 +69,31 @@ func TestCreateDBSyncEntity_EncryptionExemptTypes(t *testing.T) { "nigori": {SpecificsVariant: &sync_pb.EntitySpecifics_Nigori{ Nigori: &sync_pb.NigoriSpecifics{KeystoreMigrationTime: aws.Int64(1)}, }}, - "history_delete_directive": {SpecificsVariant: &sync_pb.EntitySpecifics_HistoryDeleteDirective{ + "history_delete_directive_global_id": {SpecificsVariant: &sync_pb.EntitySpecifics_HistoryDeleteDirective{ HistoryDeleteDirective: &sync_pb.HistoryDeleteDirectiveSpecifics{ - UrlDirective: &sync_pb.UrlDirective{Url: aws.String("https://example.com")}, + GlobalIdDirective: &sync_pb.GlobalIdDirective{GlobalId: []int64{1, 2}}, }, }}, - "password": {SpecificsVariant: &sync_pb.EntitySpecifics_Password{ - Password: &sync_pb.PasswordSpecifics{Encrypted: &sync_pb.EncryptedData{}}, + "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) { @@ -85,9 +103,42 @@ func TestCreateDBSyncEntity_EncryptionExemptTypes(t *testing.T) { } } -func TestCreateDBSyncEntity_ServerDefinedSkipsEncryptionCheck(t *testing.T) { - entity := newPBEntity(plaintextBookmarkSpecifics()) +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) +} diff --git a/datastore/sync_entity.go b/datastore/sync_entity.go index 332e5115..9befb5e1 100644 --- a/datastore/sync_entity.go +++ b/datastore/sync_entity.go @@ -822,7 +822,7 @@ 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 { + if err = checkEntityEncrypted(entity, dataType); err != nil { return nil, err } From 4f0d87af58171e5f1089dc59851a976440f00710 Mon Sep 17 00:00:00 2001 From: Darnell Andries Date: Wed, 11 Nov 2026 17:33:10 -0700 Subject: [PATCH 3/3] Add client-only encrypted data check in encrypt_check Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- datastore/encrypt_check.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/datastore/encrypt_check.go b/datastore/encrypt_check.go index 80509bf4..53623c57 100644 --- a/datastore/encrypt_check.go +++ b/datastore/encrypt_check.go @@ -48,7 +48,8 @@ func isEncryptionExempt(specifics *sync_pb.EntitySpecifics, dataType int) bool { // Encrypted by its own scheme into its own specifics fields, so // `specifics.encrypted` stays unset. See // DataTypeWorker::EncryptPasswordSpecificsData(). - return specifics.GetPassword().GetUnencryptedMetadata() == nil + return specifics.GetPassword().GetUnencryptedMetadata() == nil && + specifics.GetPassword().GetClientOnlyEncryptedData() == nil } return false }