Skip to content

Commit 752b4a4

Browse files
committed
feat: allow admins to rename and move items of other users and fix ownership security vulnerability
1 parent f9e57b7 commit 752b4a4

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

controllers/MoveItemsController.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ func MoveItems(c echo.Context) error {
1515
return c.String(status, err.Error())
1616
}
1717

18+
// Determine admin status
19+
isAdmin, _ := c.Get("Admin").(bool)
20+
1821
status, err := logic.MoveItems(
1922
c.Get("UserID").(uint),
2023
moveValidation.ParentFolderID,
2124
moveValidation.FolderIDs,
2225
moveValidation.LinkIDs,
26+
isAdmin,
2327
)
2428
if err != nil {
2529
return c.String(status, err.Error())

controllers/UpdateFileController.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,22 @@ func UpdateFile(c echo.Context) error {
2323
return c.String(http.StatusBadRequest, "File doesn't exist")
2424
}
2525

26+
// Verify ownership
27+
userID := c.Get("UserID").(uint)
28+
isAdmin, _ := c.Get("Admin").(bool)
29+
if !isAdmin && dbLink.UserID != userID {
30+
return c.String(http.StatusForbidden, "Unauthorized access to file")
31+
}
32+
2633
if linkValidation.ParentFolderID > 0 {
27-
if res := inits.DB.First(&models.Folder{}, linkValidation.ParentFolderID); res.Error != nil {
34+
var targetParent models.Folder
35+
if res := inits.DB.First(&targetParent, linkValidation.ParentFolderID); res.Error != nil {
2836
return c.String(http.StatusBadRequest, "Parent folder doesn't exist")
2937
}
38+
39+
if !isAdmin && targetParent.UserID != userID {
40+
return c.String(http.StatusForbidden, "Unauthorized access to target parent folder")
41+
}
3042
}
3143

3244
//update link data

controllers/UpdateFolderController.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,28 @@ func UpdateFolder(c echo.Context) error {
3030
return c.String(http.StatusBadRequest, "Folder doesn't exist")
3131
}
3232

33+
// Verify ownership
34+
userID := c.Get("UserID").(uint)
35+
isAdmin, _ := c.Get("Admin").(bool)
36+
if !isAdmin && dbFolder.UserID != userID {
37+
return c.String(http.StatusForbidden, "Unauthorized access to folder")
38+
}
39+
3340
/*
3441
check if ParentfolderID aint root folder (=0)
3542
check if requested parent folder id exists
3643
TODO: also check if the new parent folder is not a child of current folder or the folder itself
3744
*/
3845
if folderValidation.ParentFolderID > 0 {
39-
if res := inits.DB.First(&models.Folder{}, folderValidation.ParentFolderID); res.Error != nil {
46+
var targetParent models.Folder
47+
if res := inits.DB.First(&targetParent, folderValidation.ParentFolderID); res.Error != nil {
4048
return c.String(http.StatusBadRequest, "Parent folder doesn't exist")
4149
}
4250

51+
if !isAdmin && targetParent.UserID != userID {
52+
return c.String(http.StatusForbidden, "Unauthorized access to target parent folder")
53+
}
54+
4355
// if the new parent folder is inside the current folder we return an
4456
// error so the folders wont be in an infinite loop
4557
containsFolder, err := helpers.FolderContainsFolder(dbFolder.ID, dbFolder.ParentFolderID)

logic/MoveItems.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"net/http"
99
)
1010

11-
func MoveItems(userId uint, targetFolderId uint, folderIds []uint, linkIds []uint) (int, error) {
11+
func MoveItems(userId uint, targetFolderId uint, folderIds []uint, linkIds []uint, isAdmin bool) (int, error) {
1212
// check if at least one item is being moved
1313
if len(folderIds) == 0 && len(linkIds) == 0 {
1414
return http.StatusBadRequest, errors.New("no items selected to move")
@@ -20,7 +20,7 @@ func MoveItems(userId uint, targetFolderId uint, folderIds []uint, linkIds []uin
2020
if res := inits.DB.First(&targetFolder, targetFolderId); res.Error != nil {
2121
return http.StatusBadRequest, errors.New("target folder doesn't exist")
2222
}
23-
if targetFolder.UserID != userId {
23+
if !isAdmin && targetFolder.UserID != userId {
2424
return http.StatusForbidden, errors.New("unauthorized access to target folder")
2525
}
2626
}
@@ -36,8 +36,8 @@ func MoveItems(userId uint, targetFolderId uint, folderIds []uint, linkIds []uin
3636
if res := inits.DB.First(&folder, folderId); res.Error != nil {
3737
return http.StatusBadRequest, errors.New("folder to move not found")
3838
}
39-
40-
if folder.UserID != userId {
39+
40+
if !isAdmin && folder.UserID != userId {
4141
return http.StatusForbidden, errors.New("unauthorized access to folder")
4242
}
4343

@@ -64,8 +64,8 @@ func MoveItems(userId uint, targetFolderId uint, folderIds []uint, linkIds []uin
6464
if res := inits.DB.First(&link, linkId); res.Error != nil {
6565
return http.StatusBadRequest, errors.New("file to move not found")
6666
}
67-
68-
if link.UserID != userId {
67+
68+
if !isAdmin && link.UserID != userId {
6969
return http.StatusForbidden, errors.New("unauthorized access to file")
7070
}
7171

0 commit comments

Comments
 (0)