π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Harden Cast request validation
Manav Rathi90d1256c3871
diff --git a/server/pkg/controller/cast/controller.go b/server/pkg/controller/cast/controller.goindex 5b87b5f95e..8e2707b71b 100644--- a/server/pkg/controller/cast/controller.go+++ b/server/pkg/controller/cast/controller.go@@ -24,6 +24,7 @@ type Controller struct {const (maxRegisterDeviceUserAgentBytes = 1000+ maxEncryptedPayloadBytes = 2 * 1024pqPublicKeyBytes = 1216)@@ -64,7 +65,7 @@ func validatePQPublicKey(publicKey *string) error {if len(*publicKey) != base64.StdEncoding.EncodedLen(pqPublicKeyBytes) {return stacktrace.Propagate(ente.ErrBadRequest, "invalid pqPublicKey length")}- decoded, err := base64.StdEncoding.DecodeString(*publicKey)+ decoded, err := base64.StdEncoding.Strict().DecodeString(*publicKey)if err != nil || len(decoded) != pqPublicKeyBytes {return stacktrace.Propagate(ente.ErrBadRequest, "invalid pqPublicKey")}@@ -99,6 +100,9 @@ func (c *Controller) GetEncCastData(ctx context.Context, deviceCode string) (*st}func (c *Controller) InsertCastData(ctx *gin.Context, request *cast.CastRequest) (uuid.UUID, error) {+ if len(request.EncPayload) > maxEncryptedPayloadBytes {+ return uuid.Nil, stacktrace.Propagate(ente.ErrBadRequest, "encrypted payload too large")+ }userID := auth.GetUserID(ctx.Request.Header)devices, err := c.CastRepo.GetAllDevices(ctx, userID)if err != nil {diff --git a/server/pkg/controller/cast/controller_test.go b/server/pkg/controller/cast/controller_test.goindex b640fd1dce..63a635ab5f 100644--- a/server/pkg/controller/cast/controller_test.go+++ b/server/pkg/controller/cast/controller_test.go@@ -17,13 +17,15 @@ import (func TestValidatePQPublicKey(t *testing.T) {valid := base64.StdEncoding.EncodeToString(make([]byte, pqPublicKeyBytes))invalidBase64 := strings.Repeat("!", len(valid))+ nonCanonicalBase64 := valid[:len(valid)-3] + "B=="wrongLength := base64.StdEncoding.EncodeToString(make([]byte, pqPublicKeyBytes-1))for name, key := range map[string]*string{- "omitted": nil,- "valid": &valid,- "invalid base64": &invalidBase64,- "wrong length": &wrongLength,+ "omitted": nil,+ "valid": &valid,+ "invalid base64": &invalidBase64,+ "non-canonical base64": &nonCanonicalBase64,+ "wrong length": &wrongLength,} {t.Run(name, func(t *testing.T) {err := validatePQPublicKey(key)@@ -37,6 +39,16 @@ func TestValidatePQPublicKey(t *testing.T) {}}+func TestInsertCastDataRejectsOversizedPayload(t *testing.T) {+ controller := &Controller{}++ _, err := controller.InsertCastData(&gin.Context{}, &entity.CastRequest{+ EncPayload: strings.Repeat("a", maxEncryptedPayloadBytes+1),+ })++ require.ErrorIs(t, err, ente.ErrBadRequest)+}+func TestRegisterDeviceRejectsOversizedUserAgent(t *testing.T) {gin.SetMode(gin.TestMode)