Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Validate account key attributes and require recovery setup

Neeraj Gupta009bfce6b1b5
diff --git a/server/ente/user.go b/server/ente/user.go
index 0bb9fe901c..b7aae7bc2b 100644
--- a/server/ente/user.go
+++ b/server/ente/user.go
@@ -84,14 +84,32 @@ type SetUserAttributesRequest struct {
}
func (sk *SetUserAttributesRequest) Validate() error {
- strength := sk.KeyAttributes.MemLimit * sk.KeyAttributes.OpsLimit
- if strength != ExpectedKDFStrength {
- return NewBadRequestWithMessage("Unexpected KDF strength")
+ keys := sk.KeyAttributes
+ update := UpdateKeysRequest{
+ KEKSalt: keys.KEKSalt,
+ EncryptedKey: keys.EncryptedKey,
+ KeyDecryptionNonce: keys.KeyDecryptionNonce,
+ MemLimit: keys.MemLimit,
+ OpsLimit: keys.OpsLimit,
}
- if sk.KeyAttributes.MemLimit < 128*1024*1024 {
- return NewBadRequestWithMessage("memory limit must be at least 128MB")
+ if err := update.Validate(); err != nil {
+ return err
+ }
+ if err := ValidateBase64Length(keys.PublicKey, 32); err != nil {
+ return err
}
- return nil
+ if err := ValidateBase64Length(keys.EncryptedSecretKey, 48); err != nil {
+ return err
+ }
+ if err := ValidateBase64Length(keys.SecretKeyDecryptionNonce, 24); err != nil {
+ return err
+ }
+ return (SetRecoveryKeyRequest{
+ MasterKeyEncryptedWithRecoveryKey: keys.MasterKeyEncryptedWithRecoveryKey,
+ MasterKeyDecryptionNonce: keys.MasterKeyDecryptionNonce,
+ RecoveryKeyEncryptedWithMasterKey: keys.RecoveryKeyEncryptedWithMasterKey,
+ RecoveryKeyDecryptionNonce: keys.RecoveryKeyDecryptionNonce,
+ }).Validate()
}
type UpdateEmailMFA struct {
@@ -114,7 +132,13 @@ func (u *UpdateKeysRequest) Validate() error {
if u.MemLimit < 128*1024*1024 {
return NewBadRequestWithMessage("memory limit must be at least 128MB")
}
- return nil
+ if err := ValidateBase64Length(u.KEKSalt, 16); err != nil {
+ return err
+ }
+ if err := ValidateBase64Length(u.EncryptedKey, 48); err != nil {
+ return err
+ }
+ return ValidateBase64Length(u.KeyDecryptionNonce, 24)
}
type SetRecoveryKeyRequest struct {
@@ -124,6 +148,19 @@ type SetRecoveryKeyRequest struct {
RecoveryKeyDecryptionNonce string `json:"recoveryKeyDecryptionNonce"`
}
+func (r SetRecoveryKeyRequest) Validate() error {
+ if err := ValidateBase64Length(r.MasterKeyEncryptedWithRecoveryKey, 48); err != nil {
+ return err
+ }
+ if err := ValidateBase64Length(r.MasterKeyDecryptionNonce, 24); err != nil {
+ return err
+ }
+ if err := ValidateBase64Length(r.RecoveryKeyEncryptedWithMasterKey, 48); err != nil {
+ return err
+ }
+ return ValidateBase64Length(r.RecoveryKeyDecryptionNonce, 24)
+}
+
type EventReportRequest struct {
Event string `json:"event"`
}
diff --git a/server/ente/user_test.go b/server/ente/user_test.go
index b64f948cc8..1e90ae1ce6 100644
--- a/server/ente/user_test.go
+++ b/server/ente/user_test.go
@@ -1,6 +1,8 @@
package ente
import (
+ "encoding/base64"
+ "strings"
"testing"
"github.com/stretchr/testify/require"
@@ -17,13 +19,16 @@ func TestKeyRequestValidation(t *testing.T) {
validate func(memLimit, opsLimit int64) error
}{
{name: "set attributes", validate: func(memLimit, opsLimit int64) error {
- return (&SetUserAttributesRequest{KeyAttributes: KeyAttributes{
- MemLimit: memLimit,
- OpsLimit: opsLimit,
- }}).Validate()
+ request := testSetAttributesRequest(testRecoveryKeyRequest())
+ request.KeyAttributes.MemLimit = memLimit
+ request.KeyAttributes.OpsLimit = opsLimit
+ return request.Validate()
}},
{name: "update keys", validate: func(memLimit, opsLimit int64) error {
- return (&UpdateKeysRequest{MemLimit: memLimit, OpsLimit: opsLimit}).Validate()
+ request := testUpdateKeysRequest(testSetAttributesRequest(testRecoveryKeyRequest()).KeyAttributes)
+ request.MemLimit = memLimit
+ request.OpsLimit = opsLimit
+ return request.Validate()
}},
}
@@ -44,18 +49,133 @@ func TestKeyRequestValidation(t *testing.T) {
}
}
+func TestKeyAttributeValidation(t *testing.T) {
+ valid := testSetAttributesRequest(testRecoveryKeyRequest())
+ require.NoError(t, valid.Validate())
+ update := testUpdateKeysRequest(valid.KeyAttributes)
+ require.NoError(t, update.Validate())
+
+ for _, field := range []struct {
+ name string
+ size int
+ value func(*KeyAttributes) *string
+ update bool
+ }{
+ {"salt", 16, func(k *KeyAttributes) *string { return &k.KEKSalt }, true},
+ {"encrypted key", 48, func(k *KeyAttributes) *string { return &k.EncryptedKey }, true},
+ {"key nonce", 24, func(k *KeyAttributes) *string { return &k.KeyDecryptionNonce }, true},
+ {"public key", 32, func(k *KeyAttributes) *string { return &k.PublicKey }, false},
+ {"encrypted secret key", 48, func(k *KeyAttributes) *string { return &k.EncryptedSecretKey }, false},
+ {"secret key nonce", 24, func(k *KeyAttributes) *string { return &k.SecretKeyDecryptionNonce }, false},
+ } {
+ for _, invalid := range invalidBase64Values(field.size) {
+ t.Run(field.name+"/"+invalid.name, func(t *testing.T) {
+ request := valid
+ *field.value(&request.KeyAttributes) = invalid.value
+ require.EqualError(t, request.Validate(), invalid.message)
+ if field.update {
+ update := testUpdateKeysRequest(request.KeyAttributes)
+ require.EqualError(t, update.Validate(), invalid.message)
+ }
+ })
+ }
+ }
+}
+
+func TestRecoveryKeyValidation(t *testing.T) {
+ valid := testRecoveryKeyRequest()
+ require.NoError(t, valid.Validate())
+ setup := testSetAttributesRequest(valid)
+ require.NoError(t, setup.Validate())
+
+ for _, field := range []struct {
+ name string
+ size int
+ value func(*SetRecoveryKeyRequest) *string
+ }{
+ {"master key", 48, func(r *SetRecoveryKeyRequest) *string { return &r.MasterKeyEncryptedWithRecoveryKey }},
+ {"master key nonce", 24, func(r *SetRecoveryKeyRequest) *string { return &r.MasterKeyDecryptionNonce }},
+ {"recovery key", 48, func(r *SetRecoveryKeyRequest) *string { return &r.RecoveryKeyEncryptedWithMasterKey }},
+ {"recovery key nonce", 24, func(r *SetRecoveryKeyRequest) *string { return &r.RecoveryKeyDecryptionNonce }},
+ } {
+ for _, invalid := range invalidBase64Values(field.size) {
+ t.Run(field.name+"/"+invalid.name, func(t *testing.T) {
+ request := valid
+ *field.value(&request) = invalid.value
+ require.EqualError(t, request.Validate(), invalid.message)
+ setup := testSetAttributesRequest(request)
+ require.EqualError(t, setup.Validate(), invalid.message)
+ })
+ }
+ }
+}
+
+func invalidBase64Values(size int) []struct{ name, value, message string } {
+ values := []struct{ name, value, message string }{
+ {"empty", "", "invalid decoded length"},
+ {"invalid base64", strings.Repeat("!", base64.StdEncoding.EncodedLen(size)), "invalid base64"},
+ {"short", base64.StdEncoding.EncodeToString(make([]byte, size-1)), "invalid decoded length"},
+ {"long", base64.StdEncoding.EncodeToString(make([]byte, size+1)), "invalid decoded length"},
+ }
+ if size%3 != 0 {
+ values = append(values, struct{ name, value, message string }{"missing padding", base64.RawStdEncoding.EncodeToString(make([]byte, size)), "invalid base64"})
+ }
+ return values
+}
+
+func testRecoveryKeyRequest() SetRecoveryKeyRequest {
+ encryptedKey := base64.StdEncoding.EncodeToString(make([]byte, 48))
+ nonce := base64.StdEncoding.EncodeToString(make([]byte, 24))
+ return SetRecoveryKeyRequest{
+ MasterKeyEncryptedWithRecoveryKey: encryptedKey,
+ MasterKeyDecryptionNonce: nonce,
+ RecoveryKeyEncryptedWithMasterKey: encryptedKey,
+ RecoveryKeyDecryptionNonce: nonce,
+ }
+}
+
+func testSetAttributesRequest(recovery SetRecoveryKeyRequest) SetUserAttributesRequest {
+ encryptedKey := base64.StdEncoding.EncodeToString(make([]byte, 48))
+ nonce := base64.StdEncoding.EncodeToString(make([]byte, 24))
+ return SetUserAttributesRequest{KeyAttributes: KeyAttributes{
+ KEKSalt: base64.StdEncoding.EncodeToString(make([]byte, 16)),
+ EncryptedKey: encryptedKey,
+ KeyDecryptionNonce: nonce,
+ PublicKey: base64.StdEncoding.EncodeToString(make([]byte, 32)),
+ EncryptedSecretKey: encryptedKey,
+ SecretKeyDecryptionNonce: nonce,
+ MemLimit: validArgonMemLimit,
+ OpsLimit: validArgonOpsLimit,
+ MasterKeyEncryptedWithRecoveryKey: recovery.MasterKeyEncryptedWithRecoveryKey,
+ MasterKeyDecryptionNonce: recovery.MasterKeyDecryptionNonce,
+ RecoveryKeyEncryptedWithMasterKey: recovery.RecoveryKeyEncryptedWithMasterKey,
+ RecoveryKeyDecryptionNonce: recovery.RecoveryKeyDecryptionNonce,
+ }}
+}
+
+func testUpdateKeysRequest(keys KeyAttributes) UpdateKeysRequest {
+ return UpdateKeysRequest{
+ KEKSalt: keys.KEKSalt,
+ EncryptedKey: keys.EncryptedKey,
+ KeyDecryptionNonce: keys.KeyDecryptionNonce,
+ MemLimit: keys.MemLimit,
+ OpsLimit: keys.OpsLimit,
+ }
+}
+
func TestUpdateSRPAndKeysRequestValidation(t *testing.T) {
request := UpdateSRPAndKeysRequest{}
assertBadRequestMessage(t, request.Validate(), "updatedKeyAttr is required")
- request.UpdateAttributes = &UpdateKeysRequest{
- MemLimit: validArgonMemLimit,
- OpsLimit: validArgonOpsLimit - 1,
- }
+ update := testUpdateKeysRequest(testSetAttributesRequest(testRecoveryKeyRequest()).KeyAttributes)
+ request.UpdateAttributes = &update
+ request.UpdateAttributes.OpsLimit--
assertBadRequestMessage(t, request.Validate(), "Unexpected KDF strength")
request.UpdateAttributes.OpsLimit = validArgonOpsLimit
require.NoError(t, request.Validate())
+ request.UpdateAttributes.EncryptedKey = "invalid"
+ require.EqualError(t, request.Validate(), "invalid base64")
}
func assertBadRequestMessage(t *testing.T, err error, wantMessage string) {
diff --git a/server/ente/validation.go b/server/ente/validation.go
new file mode 100644
index 0000000000..0b7f30ef25
--- /dev/null
+++ b/server/ente/validation.go
@@ -0,0 +1,17 @@
+package ente
+
+import (
+ "encoding/base64"
+ "errors"
+)
+
+func ValidateBase64Length(value string, size int) error {
+ decoded, err := base64.StdEncoding.DecodeString(value)
+ if err != nil {
+ return errors.New("invalid base64")
+ }
+ if len(decoded) != size {
+ return errors.New("invalid decoded length")
+ }
+ return nil
+}
diff --git a/server/pkg/api/collection_test.go b/server/pkg/api/collection_test.go
index 4f2c5e12dd..c799e993cc 100644
--- a/server/pkg/api/collection_test.go
+++ b/server/pkg/api/collection_test.go
@@ -1,6 +1,7 @@
package api
import (
+ "encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -10,8 +11,46 @@ import (
"github.com/ente/museum/ente"
"github.com/ente/museum/pkg/controller/collections"
"github.com/gin-gonic/gin"
+ "github.com/stretchr/testify/require"
)
+func TestCollectionHandlersRejectInvalidKeys(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ router := gin.New()
+ handler := &CollectionHandler{Controller: &collections.CollectionController{}}
+ router.POST("/collections", handler.Create)
+ router.POST("/collections/share", handler.Share)
+
+ for _, test := range []struct {
+ name string
+ path string
+ body any
+ }{
+ {"create invalid base64", "/collections", ente.Collection{
+ EncryptedKey: strings.Repeat("!", base64.StdEncoding.EncodedLen(48)),
+ KeyDecryptionNonce: base64.StdEncoding.EncodeToString(make([]byte, 24)),
+ Type: "album",
+ Attributes: ente.CollectionAttributes{Version: 1},
+ }},
+ {"share incorrect length", "/collections/share", ente.AlterShareRequest{
+ CollectionID: 1,
+ Email: "[email protected]",
+ EncryptedKey: base64.StdEncoding.EncodeToString(make([]byte, 79)),
+ }},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ body, err := json.Marshal(test.body)
+ require.NoError(t, err)
+ request := httptest.NewRequest(http.MethodPost, test.path, strings.NewReader(string(body)))
+ request.Header.Set("Content-Type", "application/json")
+ request.Header.Set("X-Auth-User-ID", "1")
+ recorder := httptest.NewRecorder()
+ router.ServeHTTP(recorder, request)
+ require.Equal(t, http.StatusInternalServerError, recorder.Code, recorder.Body.String())
+ })
+ }
+}
+
func TestBatchShareHandlerValidatesEachShare(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
diff --git a/server/pkg/api/user.go b/server/pkg/api/user.go
index 19475bcc38..8afb868c9e 100644
--- a/server/pkg/api/user.go
+++ b/server/pkg/api/user.go
@@ -132,6 +132,10 @@ func (h *UserHandler) SetRecoveryKey(c *gin.Context) {
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
+ if err := request.Validate(); err != nil {
+ handler.Error(c, stacktrace.Propagate(err, ""))
+ return
+ }
err := h.UserController.SetRecoveryKey(userID, request)
if err != nil {
handler.Error(c, stacktrace.Propagate(err, ""))
diff --git a/server/pkg/api/user_test.go b/server/pkg/api/user_test.go
index 709640aed4..797d5b1c64 100644
--- a/server/pkg/api/user_test.go
+++ b/server/pkg/api/user_test.go
@@ -15,8 +15,21 @@ import (
usercontroller "github.com/ente/museum/pkg/controller/user"
"github.com/ente/museum/pkg/repo"
"github.com/gin-gonic/gin"
+ "github.com/stretchr/testify/require"
)
+func TestSetRecoveryKeyRejectsInvalidAttributes(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ recorder := httptest.NewRecorder()
+ ctx, _ := gin.CreateTestContext(recorder)
+ ctx.Request = httptest.NewRequest(http.MethodPut, "/users/recovery-key", strings.NewReader(`{}`))
+ ctx.Request.Header.Set("Content-Type", "application/json")
+
+ (&UserHandler{}).SetRecoveryKey(ctx)
+
+ require.Equal(t, http.StatusInternalServerError, recorder.Code)
+}
+
func TestGetPublicKeyHandlerUsesEmailLookup(t *testing.T) {
handler, db := setupUserHandlerTest(t)
requesterUserID := testutil.InsertUser(t, db, testutil.UserFixture{
diff --git a/server/pkg/controller/collections/key_validation.go b/server/pkg/controller/collections/key_validation.go
index bd31dd128b..a108068422 100644
--- a/server/pkg/controller/collections/key_validation.go
+++ b/server/pkg/controller/collections/key_validation.go
@@ -3,7 +3,6 @@ package collections
import (
"encoding/base64"
"errors"
- "strconv"
"github.com/ente/museum/ente"
)
@@ -18,14 +17,14 @@ const (
)
func validateOwnedCollectionKey(encryptedKey string, keyDecryptionNonce string) error {
- if err := validateBase64DecodedLength(encryptedKey, encryptedCollectionKeyLen, "encryptedKey"); err != nil {
+ if err := validateBase64DecodedLength(encryptedKey, encryptedCollectionKeyLen); err != nil {
return err
}
- return validateBase64DecodedLength(keyDecryptionNonce, secretboxNonceBytes, "keyDecryptionNonce")
+ return validateBase64DecodedLength(keyDecryptionNonce, secretboxNonceBytes)
}
func validateSealedCollectionKey(encryptedKey string) error {
- return validateBase64DecodedLength(encryptedKey, sealedCollectionKeyLen, "encryptedKey")
+ return validateBase64DecodedLength(encryptedKey, sealedCollectionKeyLen)
}
func validateCollectionFileItems(files []ente.CollectionFileItem) error {
@@ -40,16 +39,9 @@ func validateCollectionFileItems(files []ente.CollectionFileItem) error {
return nil
}
-func validateBase64DecodedLength(value string, expected int, field string) error {
+func validateBase64DecodedLength(value string, expected int) error {
if len(value) != base64.StdEncoding.EncodedLen(expected) {
- return errors.New(field + " must decode to " + strconv.Itoa(expected) + " bytes")
+ return errors.New("invalid encoded length")
}
- decoded, err := base64.StdEncoding.DecodeString(value)
- if err != nil {
- return errors.New(field + " must be valid base64")
- }
- if len(decoded) != expected {
- return errors.New(field + " must decode to " + strconv.Itoa(expected) + " bytes")
- }
- return nil
+ return ente.ValidateBase64Length(value, expected)
}
diff --git a/server/pkg/controller/collections/key_validation_test.go b/server/pkg/controller/collections/key_validation_test.go
index e74b92bd93..7d55b68ad5 100644
--- a/server/pkg/controller/collections/key_validation_test.go
+++ b/server/pkg/controller/collections/key_validation_test.go
@@ -109,6 +109,14 @@ func TestValidateCollectionKeyRejectsInvalidBase64(t *testing.T) {
}
}
+func TestValidateCollectionKeyRejectsLineBreaks(t *testing.T) {
+ key := b64OfLen(encryptedCollectionKeyLen)
+ err := validateOwnedCollectionKey(key[:4]+"\r\n"+key[4:], b64OfLen(secretboxNonceBytes))
+ if err == nil {
+ t.Fatal("validateOwnedCollectionKey() error = nil, want error")
+ }
+}
+
func TestCollectionControllerRejectsInvalidCollectionKeysBeforeRepoAccess(t *testing.T) {
controller := CollectionController{}
ctx := testGinContext()