Skip to content

Commit ec2480f

Browse files
committed
Remove inbox validation for uploading key packages
1 parent 24971f4 commit ec2480f

16 files changed

+122
-203
lines changed

dev/docker/env

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
set -e
33

44
function docker_compose() {
5-
docker-compose -f dev/docker/docker-compose.yml -p xmtpd "$@"
5+
docker compose -f dev/docker/docker-compose.yml -p xmtpd "$@"
66
}

dev/e2e/docker/env

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
set -e
33

44
function docker_compose() {
5-
docker-compose -f dev/e2e/docker/docker-compose.yml -p xmtpd-e2e "$@"
5+
docker compose -f dev/e2e/docker/docker-compose.yml -p xmtpd-e2e "$@"
66
}

dev/lint

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ if [[ $(gofmt -l .) ]]; then
77
echo "gofmt errors, run 'gofmt -w .' and commit"
88
fi
99

10-
golangci-lint --config dev/.golangci.yaml run ./... --deadline=5m
10+
golangci-lint --config dev/.golangci.yaml run ./...
1111

1212
protolint .

pkg/api/interceptor.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,17 @@ func (wa *WalletAuthorizer) requiresAuthorization(req interface{}) bool {
9696
func (wa *WalletAuthorizer) getWallet(ctx context.Context) (types.WalletAddr, error) {
9797
md, ok := metadata.FromIncomingContext(ctx)
9898
if !ok {
99-
return "", status.Errorf(codes.Unauthenticated, "metadata is not provided")
99+
return "", status.Error(codes.Unauthenticated, "metadata is not provided")
100100
}
101101

102102
values := md.Get(authorizationMetadataKey)
103103
if len(values) == 0 {
104-
return "", status.Errorf(codes.Unauthenticated, "authorization token is not provided")
104+
return "", status.Error(codes.Unauthenticated, "authorization token is not provided")
105105
}
106106

107107
words := strings.SplitN(values[0], " ", 2)
108108
if len(words) != 2 {
109-
return "", status.Errorf(codes.Unauthenticated, "invalid authorization header")
109+
return "", status.Error(codes.Unauthenticated, "invalid authorization header")
110110
}
111111
if scheme := strings.TrimSpace(words[0]); scheme != "Bearer" {
112112
return "", status.Errorf(codes.Unauthenticated, "unrecognized authorization scheme %s", scheme)
@@ -127,14 +127,14 @@ func (wa *WalletAuthorizer) authorize(ctx context.Context, req interface{}, wall
127127
if pub, isPublish := req.(*messagev1.PublishRequest); isPublish {
128128
for _, env := range pub.Envelopes {
129129
if !wa.privilegedAddresses[wallet] && !allowedToPublish(env.ContentTopic, wallet) {
130-
return status.Errorf(codes.PermissionDenied, "publishing to restricted topic")
130+
return status.Error(codes.PermissionDenied, "publishing to restricted topic")
131131
}
132132
}
133133
}
134134
if wa.AllowLists {
135135
if wa.AllowLister.IsDenyListed(wallet.String()) {
136136
wa.Log.Debug("wallet deny listed", logging.WalletAddress(wallet.String()))
137-
return status.Errorf(codes.PermissionDenied, ErrDenyListed.Error())
137+
return status.Error(codes.PermissionDenied, ErrDenyListed.Error())
138138
}
139139
}
140140
return nil
@@ -185,7 +185,8 @@ func (wa *WalletAuthorizer) applyLimits(ctx context.Context, fullMethod string,
185185
logging.String("method", method),
186186
logging.String("limit", string(limitType)),
187187
logging.Int("cost", cost))
188-
return status.Errorf(codes.ResourceExhausted, err.Error())
188+
189+
return status.Error(codes.ResourceExhausted, err.Error())
189190
}
190191

191192
const (

pkg/api/message/v1/service.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,17 @@ func (s *Service) Publish(ctx context.Context, req *proto.PublishRequest) (*prot
130130
log.Debug("received message")
131131

132132
if len(env.ContentTopic) > MaxContentTopicNameSize {
133-
return nil, status.Errorf(codes.InvalidArgument, "topic length too big")
133+
return nil, status.Error(codes.InvalidArgument, "topic length too big")
134134
}
135135

136136
if len(env.Message) > MaxMessageSize {
137-
return nil, status.Errorf(codes.InvalidArgument, "message too big")
137+
return nil, status.Error(codes.InvalidArgument, "message too big")
138138
}
139139

140140
if !topic.IsEphemeral(env.ContentTopic) {
141141
_, err := s.store.InsertMessage(env)
142142
if err != nil {
143-
return nil, status.Errorf(codes.Internal, err.Error())
143+
return nil, status.Error(codes.Internal, err.Error())
144144
}
145145
}
146146

@@ -150,7 +150,7 @@ func (s *Service) Publish(ctx context.Context, req *proto.PublishRequest) (*prot
150150
Payload: env.Message,
151151
})
152152
if err != nil {
153-
return nil, status.Errorf(codes.Internal, err.Error())
153+
return nil, status.Error(codes.Internal, err.Error())
154154
}
155155

156156
metrics.EmitPublishedEnvelope(ctx, log, env)
@@ -393,7 +393,7 @@ func (s *Service) BatchQuery(ctx context.Context, req *proto.BatchQueryRequest)
393393
// We execute the query using the existing Query API
394394
resp, err := s.Query(ctx, query)
395395
if err != nil {
396-
return nil, status.Errorf(codes.Internal, err.Error())
396+
return nil, status.Error(codes.Internal, err.Error())
397397
}
398398
responses = append(responses, resp)
399399
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SET statement_timeout = 0;
2+
3+
--bun:split
4+
ALTER TABLE installations
5+
ADD COLUMN inbox_id BYTEA NOT NULL,
6+
ADD COLUMN expiration BIGINT NOT NULL;
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SET statement_timeout = 0;
2+
3+
--bun:split
4+
ALTER TABLE installations
5+
DROP COLUMN IF EXISTS inbox_id,
6+
DROP COLUMN IF EXISTS expiration;
7+

pkg/mls/api/v1/service.go

+19-15
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ func (s *Service) HandleIncomingWakuRelayMessage(wakuMsg *wakupb.WakuMessage) er
113113
return nil
114114
}
115115

116+
/*
117+
*
118+
DEPRECATED: Use UploadKeyPackage instead
119+
*
120+
*/
116121
func (s *Service) RegisterInstallation(ctx context.Context, req *mlsv1.RegisterInstallationRequest) (*mlsv1.RegisterInstallationResponse, error) {
117122
if err := validateRegisterInstallationRequest(req); err != nil {
118123
return nil, err
@@ -126,9 +131,9 @@ func (s *Service) RegisterInstallation(ctx context.Context, req *mlsv1.RegisterI
126131
if len(results) != 1 {
127132
return nil, status.Errorf(codes.Internal, "unexpected number of results: %d", len(results))
128133
}
134+
129135
installationKey := results[0].InstallationKey
130-
credential := results[0].Credential
131-
if err = s.store.CreateInstallation(ctx, installationKey, credential.InboxId, req.KeyPackage.KeyPackageTlsSerialized, results[0].Expiration); err != nil {
136+
if err = s.store.CreateOrUpdateInstallation(ctx, installationKey, req.KeyPackage.KeyPackageTlsSerialized); err != nil {
132137
return nil, err
133138
}
134139
return &mlsv1.RegisterInstallationResponse{
@@ -152,7 +157,7 @@ func (s *Service) FetchKeyPackages(ctx context.Context, req *mlsv1.FetchKeyPacka
152157

153158
idx, ok := keyPackageMap[string(installation.ID)]
154159
if !ok {
155-
return nil, status.Errorf(codes.Internal, "could not find key package for installation")
160+
return nil, status.Error(codes.Internal, "could not find key package for installation")
156161
}
157162

158163
resPackages[idx] = &mlsv1.FetchKeyPackagesResponse_KeyPackage{
@@ -178,21 +183,20 @@ func (s *Service) UploadKeyPackage(ctx context.Context, req *mlsv1.UploadKeyPack
178183
}
179184

180185
installationId := validationResults[0].InstallationKey
181-
expiration := validationResults[0].Expiration
182186

183-
if err = s.store.UpdateKeyPackage(ctx, installationId, keyPackageBytes, expiration); err != nil {
187+
if err = s.store.CreateOrUpdateInstallation(ctx, installationId, keyPackageBytes); err != nil {
184188
return nil, status.Errorf(codes.Internal, "failed to insert key packages: %s", err)
185189
}
186190

187191
return &emptypb.Empty{}, nil
188192
}
189193

190194
func (s *Service) RevokeInstallation(ctx context.Context, req *mlsv1.RevokeInstallationRequest) (*emptypb.Empty, error) {
191-
return nil, status.Errorf(codes.Unimplemented, "unimplemented")
195+
return nil, status.Error(codes.Unimplemented, "unimplemented")
192196
}
193197

194198
func (s *Service) GetIdentityUpdates(ctx context.Context, req *mlsv1.GetIdentityUpdatesRequest) (res *mlsv1.GetIdentityUpdatesResponse, err error) {
195-
return nil, status.Errorf(codes.Unimplemented, "unimplemented")
199+
return nil, status.Error(codes.Unimplemented, "unimplemented")
196200
}
197201

198202
func (s *Service) SendGroupMessages(ctx context.Context, req *mlsv1.SendGroupMessagesRequest) (res *emptypb.Empty, err error) {
@@ -521,11 +525,11 @@ func buildNatsSubjectForWelcomeMessages(installationId []byte) string {
521525

522526
func validateSendGroupMessagesRequest(req *mlsv1.SendGroupMessagesRequest) error {
523527
if req == nil || len(req.Messages) == 0 {
524-
return status.Errorf(codes.InvalidArgument, "no group messages to send")
528+
return status.Error(codes.InvalidArgument, "no group messages to send")
525529
}
526530
for _, input := range req.Messages {
527531
if input == nil || input.GetV1() == nil {
528-
return status.Errorf(codes.InvalidArgument, "invalid group message")
532+
return status.Error(codes.InvalidArgument, "invalid group message")
529533
}
530534
}
531535
return nil
@@ -537,37 +541,37 @@ func validateSendWelcomeMessagesRequest(req *mlsv1.SendWelcomeMessagesRequest) e
537541
}
538542
for _, input := range req.Messages {
539543
if input == nil || input.GetV1() == nil {
540-
return status.Errorf(codes.InvalidArgument, "invalid welcome message")
544+
return status.Error(codes.InvalidArgument, "invalid welcome message")
541545
}
542546

543547
v1 := input.GetV1()
544548
if len(v1.Data) == 0 || len(v1.InstallationKey) == 0 || len(v1.HpkePublicKey) == 0 {
545-
return status.Errorf(codes.InvalidArgument, "invalid welcome message")
549+
return status.Error(codes.InvalidArgument, "invalid welcome message")
546550
}
547551
}
548552
return nil
549553
}
550554

551555
func validateRegisterInstallationRequest(req *mlsv1.RegisterInstallationRequest) error {
552556
if req == nil || req.KeyPackage == nil {
553-
return status.Errorf(codes.InvalidArgument, "no key package")
557+
return status.Error(codes.InvalidArgument, "no key package")
554558
}
555559
return nil
556560
}
557561

558562
func validateUploadKeyPackageRequest(req *mlsv1.UploadKeyPackageRequest) error {
559563
if req == nil || req.KeyPackage == nil {
560-
return status.Errorf(codes.InvalidArgument, "no key package")
564+
return status.Error(codes.InvalidArgument, "no key package")
561565
}
562566
return nil
563567
}
564568

565569
func requireReadyToSend(groupId string, message []byte) error {
566570
if len(groupId) == 0 {
567-
return status.Errorf(codes.InvalidArgument, "group id is empty")
571+
return status.Error(codes.InvalidArgument, "group id is empty")
568572
}
569573
if len(message) == 0 {
570-
return status.Errorf(codes.InvalidArgument, "message is empty")
574+
return status.Error(codes.InvalidArgument, "message is empty")
571575
}
572576
return nil
573577
}

pkg/mls/api/v1/service_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ func TestRegisterInstallation(t *testing.T) {
8181
defer cleanup()
8282

8383
installationId := test.RandomBytes(32)
84-
inboxId := test.RandomInboxId()
84+
keyPackage := []byte("test")
8585

86-
mockValidateInboxIdKeyPackages(mlsValidationService, installationId, inboxId)
86+
mockValidateInboxIdKeyPackages(mlsValidationService, installationId, test.RandomInboxId())
8787

8888
res, err := svc.RegisterInstallation(ctx, &mlsv1.RegisterInstallationRequest{
8989
KeyPackage: &mlsv1.KeyPackageUpload{
90-
KeyPackageTlsSerialized: []byte("test"),
90+
KeyPackageTlsSerialized: keyPackage,
9191
},
9292
IsInboxIdCredential: false,
9393
})
@@ -98,7 +98,8 @@ func TestRegisterInstallation(t *testing.T) {
9898
installation, err := queries.New(mlsDb.DB).GetInstallation(ctx, installationId)
9999
require.NoError(t, err)
100100

101-
require.Equal(t, inboxId, installation.InboxID)
101+
require.Equal(t, installationId, installation.ID)
102+
require.Equal(t, []byte("test"), installation.KeyPackage)
102103
}
103104

104105
func TestRegisterInstallationError(t *testing.T) {

pkg/mls/store/queries.sql

+7-16
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,24 @@ WHERE (address, inbox_id, association_sequence_id) =(
8383
address,
8484
inbox_id);
8585

86-
-- name: CreateInstallation :exec
87-
INSERT INTO installations(id, created_at, updated_at, inbox_id, key_package, expiration)
88-
VALUES (@id, @created_at, @updated_at, decode(@inbox_id, 'hex'), @key_package, @expiration);
86+
-- name: CreateOrUpdateInstallation :exec
87+
INSERT INTO installations(id, created_at, updated_at, key_package)
88+
VALUES (@id, @created_at, @updated_at, @key_package)
89+
ON CONFLICT (id)
90+
DO UPDATE SET
91+
key_package = @key_package, updated_at = @updated_at;
8992

9093
-- name: GetInstallation :one
9194
SELECT
9295
id,
9396
created_at,
9497
updated_at,
95-
encode(inbox_id, 'hex') AS inbox_id,
96-
key_package,
97-
expiration
98+
key_package
9899
FROM
99100
installations
100101
WHERE
101102
id = $1;
102103

103-
-- name: UpdateKeyPackage :execrows
104-
UPDATE
105-
installations
106-
SET
107-
key_package = @key_package,
108-
updated_at = @updated_at,
109-
expiration = @expiration
110-
WHERE
111-
id = @id;
112-
113104
-- name: FetchKeyPackages :many
114105
SELECT
115106
id,

pkg/mls/store/queries/models.go

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)