Skip to content

armgit / ente

Public Imported from GitHub
0

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

Update file upload validation

Neeraj Gupta8a1807fb62c2
diff --git a/server/pkg/controller/file.go b/server/pkg/controller/file.go
index 53daca0741..1ecc407016 100644
--- a/server/pkg/controller/file.go
+++ b/server/pkg/controller/file.go
@@ -215,18 +215,7 @@ func (c *FileController) Create(ctx *gin.Context, userID int64, file ente.File,
if err != nil {
return file, stacktrace.Propagate(err, "")
}
- _, existingApp, err := c.FileRepo.GetOwnerAndApp(existing.ID)
- if err != nil {
- return file, stacktrace.Propagate(err, "")
- }
- if existingApp != "" && existingApp != app {
- return file, stacktrace.Propagate(ente.ErrPermissionDenied, "file app does not match request app")
- }
- file, err = c.onDuplicateObjectDetected(ctx, file, existing, hotDC)
- if err != nil {
- return file, stacktrace.Propagate(err, "")
- }
- return file, nil
+ return c.onDuplicateObjectDetected(ctx, file, existing, hotDC, app)
}
return file, stacktrace.Propagate(err, "")
}
@@ -968,7 +957,7 @@ func (c *FileController) sizeOf(ctx context.Context, objectKey string) (int64, e
return -1, stacktrace.Propagate(err, "")
}
-func (c *FileController) onDuplicateObjectDetected(ctx *gin.Context, file ente.File, existing ente.File, hotDC string) (ente.File, error) {
+func (c *FileController) onDuplicateObjectDetected(ctx *gin.Context, file ente.File, existing ente.File, hotDC string, app ente.App) (ente.File, error) {
newJSON, _ := json.Marshal(file)
existingJSON, _ := json.Marshal(existing)
log.Info("Comparing " + string(newJSON) + " against " + string(existingJSON))
@@ -981,12 +970,18 @@ func (c *FileController) onDuplicateObjectDetected(ctx *gin.Context, file ente.F
file.Metadata.EncryptedData == existing.Metadata.EncryptedData &&
file.Metadata.DecryptionHeader == existing.Metadata.DecryptionHeader &&
file.OwnerID == existing.OwnerID {
+ _, existingApp, err := c.FileRepo.GetOwnerAndApp(existing.ID)
+ if err != nil {
+ return file, stacktrace.Propagate(err, "")
+ }
+ if existingApp != "" && existingApp != app {
+ return file, stacktrace.Propagate(ente.ErrPermissionDenied, "file app does not match request app")
+ }
file.ID = existing.ID
return file, nil
- } else {
- go c.onExistingObjectsReplaced(ctx, file, hotDC)
- return ente.File{}, ente.ErrBadRequest
}
+ go c.onExistingObjectsReplaced(ctx, file, hotDC)
+ return ente.File{}, ente.ErrBadRequest
}
func (c *FileController) safeAlert(msg string) {
diff --git a/server/pkg/controller/file_upload_binding_test.go b/server/pkg/controller/file_upload_binding_test.go
index f9d27f2777..5521b0ebab 100644
--- a/server/pkg/controller/file_upload_binding_test.go
+++ b/server/pkg/controller/file_upload_binding_test.go
@@ -147,13 +147,15 @@ func TestFileUpdateUploadBinding(t *testing.T) {
func TestDuplicateFileRegistrationAppBinding(t *testing.T) {
for _, tc := range []struct {
- name string
- app ente.App
- legacy bool
+ name string
+ app ente.App
+ legacy bool
+ collision bool
}{
{name: "photos", app: ente.Photos},
{name: "wrong app", app: ente.Locker},
{name: "legacy cross-app", app: ente.Locker, legacy: true},
+ {name: "cross-app collision", app: ente.Locker, collision: true},
} {
t.Run(tc.name, func(t *testing.T) {
controller, file, db := setupFileUploadBindingTest(t, ente.Photos)
@@ -168,15 +170,32 @@ func TestDuplicateFileRegistrationAppBinding(t *testing.T) {
_, err := db.Exec(`UPDATE collections SET app = 'locker' WHERE collection_id = $1`, file.CollectionID)
require.NoError(t, err)
}
+ _, err := db.Exec(`UPDATE object_copies SET wasabi = 1`)
+ require.NoError(t, err)
+ if tc.collision {
+ file.File.DecryptionHeader = "replacement-header"
+ }
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
ctx.Request = httptest.NewRequest(http.MethodPost, "/files", nil)
result, err := controller.Create(ctx, file.OwnerID, file, "test", tc.app)
- if tc.legacy || tc.app == ente.Photos {
+ if tc.collision {
+ require.ErrorIs(t, err, ente.ErrBadRequest)
+ require.Eventually(t, func() bool {
+ var pending int
+ err := db.QueryRow(`SELECT COUNT(*) FROM object_copies WHERE wasabi IS NULL`).Scan(&pending)
+ return err == nil && pending == 2
+ }, 2*time.Second, 10*time.Millisecond)
+ } else if tc.legacy || tc.app == ente.Photos {
require.NoError(t, err)
require.Equal(t, fileID, result.ID)
} else {
require.ErrorIs(t, err, ente.ErrPermissionDenied)
}
+ if !tc.collision {
+ var replicated int
+ require.NoError(t, db.QueryRow(`SELECT COUNT(*) FROM object_copies WHERE wasabi = 1`).Scan(&replicated))
+ require.Equal(t, 2, replicated)
+ }
})
}
}
@@ -184,6 +203,10 @@ func TestDuplicateFileRegistrationAppBinding(t *testing.T) {
func setupFileUploadBindingTest(t *testing.T, app ente.App) (*FileController, ente.File, *sql.DB) {
t.Helper()
s3Server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.Method == http.MethodGet && r.URL.Query().Has("versions") {
+ _, _ = w.Write([]byte(`<ListVersionsResult/>`))
+ return
+ }
if r.Method != http.MethodHead {
t.Errorf("unexpected storage request: %s", r.Method)
w.WriteHeader(http.StatusBadRequest)