Repository navigation
Block commits containing unencrypted entity specifics #455
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
Open
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| 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 | ||
| } | ||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.