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 1 commit
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,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: {}, | ||
|
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 | ||
| } | ||
|
DJAndries marked this conversation as resolved.
Resolved
|
||
| if entity.GetServerDefinedUniqueTag() != "" { | ||
| return nil | ||
| } | ||
| if hasPlaintextSpecifics(entity.GetSpecifics()) { | ||
|
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 | ||
| } | ||
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,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) | ||
| } |
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.