mirror of
https://github.com/drakkan/sftpgo.git
synced 2024-11-21 15:10:23 +00:00
don't allow admins to change their own permissions
Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
parent
30fb1d6240
commit
ef98ee7d11
7 changed files with 24 additions and 33 deletions
|
@ -149,8 +149,8 @@ func updateAdmin(w http.ResponseWriter, r *http.Request) {
|
||||||
http.StatusBadRequest)
|
http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if claims.isCriticalPermRemoved(updatedAdmin.Permissions) {
|
if !util.SlicesEqual(admin.Permissions, updatedAdmin.Permissions) {
|
||||||
sendAPIResponse(w, r, errors.New("you cannot remove these permissions to yourself"), "", http.StatusBadRequest)
|
sendAPIResponse(w, r, errors.New("you cannot change your permissions"), "", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if updatedAdmin.Status == 0 {
|
if updatedAdmin.Status == 0 {
|
||||||
|
|
|
@ -226,19 +226,6 @@ func (c *jwtTokenClaims) Decode(token map[string]any) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jwtTokenClaims) isCriticalPermRemoved(permissions []string) bool {
|
|
||||||
if slices.Contains(permissions, dataprovider.PermAdminAny) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if (slices.Contains(c.Permissions, dataprovider.PermAdminManageAdmins) ||
|
|
||||||
slices.Contains(c.Permissions, dataprovider.PermAdminAny)) &&
|
|
||||||
!slices.Contains(permissions, dataprovider.PermAdminManageAdmins) &&
|
|
||||||
!slices.Contains(permissions, dataprovider.PermAdminAny) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *jwtTokenClaims) hasPerm(perm string) bool {
|
func (c *jwtTokenClaims) hasPerm(perm string) bool {
|
||||||
if slices.Contains(c.Permissions, dataprovider.PermAdminAny) {
|
if slices.Contains(c.Permissions, dataprovider.PermAdminAny) {
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -11843,7 +11843,7 @@ func TestUpdateAdminMock(t *testing.T) {
|
||||||
setBearerForReq(req, token)
|
setBearerForReq(req, token)
|
||||||
rr = executeRequest(req)
|
rr = executeRequest(req)
|
||||||
checkResponseCode(t, http.StatusBadRequest, rr)
|
checkResponseCode(t, http.StatusBadRequest, rr)
|
||||||
assert.Contains(t, rr.Body.String(), "you cannot remove these permissions to yourself")
|
assert.Contains(t, rr.Body.String(), "you cannot change your permissions")
|
||||||
admin.Permissions = []string{dataprovider.PermAdminAny}
|
admin.Permissions = []string{dataprovider.PermAdminAny}
|
||||||
admin.Role = "missing role"
|
admin.Role = "missing role"
|
||||||
asJSON, err = json.Marshal(admin)
|
asJSON, err = json.Marshal(admin)
|
||||||
|
@ -11858,7 +11858,7 @@ func TestUpdateAdminMock(t *testing.T) {
|
||||||
altToken, err := getJWTAPITokenFromTestServer(altAdminUsername, defaultTokenAuthPass)
|
altToken, err := getJWTAPITokenFromTestServer(altAdminUsername, defaultTokenAuthPass)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
admin.Password = "" // it must remain unchanged
|
admin.Password = "" // it must remain unchanged
|
||||||
admin.Permissions = []string{dataprovider.PermAdminManageAdmins, dataprovider.PermAdminCloseConnections}
|
admin.Permissions = []string{dataprovider.PermAdminManageAdmins}
|
||||||
asJSON, err = json.Marshal(admin)
|
asJSON, err = json.Marshal(admin)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
req, _ = http.NewRequest(http.MethodPut, path.Join(adminPath, altAdminUsername), bytes.NewBuffer(asJSON))
|
req, _ = http.NewRequest(http.MethodPut, path.Join(adminPath, altAdminUsername), bytes.NewBuffer(asJSON))
|
||||||
|
|
|
@ -3156,9 +3156,9 @@ func (s *httpdServer) handleWebUpdateAdminPost(w http.ResponseWriter, r *http.Re
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if username == claims.Username {
|
if username == claims.Username {
|
||||||
if claims.isCriticalPermRemoved(updatedAdmin.Permissions) {
|
if !util.SlicesEqual(admin.Permissions, updatedAdmin.Permissions) {
|
||||||
s.renderAddUpdateAdminPage(w, r, &updatedAdmin,
|
s.renderAddUpdateAdminPage(w, r, &updatedAdmin,
|
||||||
util.NewI18nError(errors.New("you cannot remove these permissions to yourself"),
|
util.NewI18nError(errors.New("you cannot change your permissions"),
|
||||||
util.I18nErrorAdminSelfPerms,
|
util.I18nErrorAdminSelfPerms,
|
||||||
), false)
|
), false)
|
||||||
return
|
return
|
||||||
|
|
|
@ -40,6 +40,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -127,18 +128,6 @@ var bytesSizeTable = map[string]uint64{
|
||||||
"e": eByte,
|
"e": eByte,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove removes an element from a string slice and
|
|
||||||
// returns the modified slice
|
|
||||||
func Remove(elems []string, val string) []string {
|
|
||||||
for idx, v := range elems {
|
|
||||||
if v == val {
|
|
||||||
elems[idx] = elems[len(elems)-1]
|
|
||||||
return elems[:len(elems)-1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return elems
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsStringPrefixInSlice searches a string prefix in a slice and returns true
|
// IsStringPrefixInSlice searches a string prefix in a slice and returns true
|
||||||
// if a matching prefix is found
|
// if a matching prefix is found
|
||||||
func IsStringPrefixInSlice(obj string, list []string) bool {
|
func IsStringPrefixInSlice(obj string, list []string) bool {
|
||||||
|
@ -921,3 +910,18 @@ func ReadConfigFromFile(name, configDir string) (string, error) {
|
||||||
}
|
}
|
||||||
return strings.TrimSpace(BytesToString(val)), nil
|
return strings.TrimSpace(BytesToString(val)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SlicesEqual checks if the provided slices contain the same elements,
|
||||||
|
// also in different order.
|
||||||
|
func SlicesEqual(s1, s2 []string) bool {
|
||||||
|
if len(s1) != len(s2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, v := range s1 {
|
||||||
|
if !slices.Contains(s2, v) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
|
@ -762,7 +762,7 @@
|
||||||
"role_permissions": "A role admin cannot have the following permissions: {{val}}",
|
"role_permissions": "A role admin cannot have the following permissions: {{val}}",
|
||||||
"view_manage": "View and manage admins",
|
"view_manage": "View and manage admins",
|
||||||
"self_delete": "You cannot delete yourself",
|
"self_delete": "You cannot delete yourself",
|
||||||
"self_permissions": "You cannot remove these permissions to yourself",
|
"self_permissions": "You cannot change your permissions",
|
||||||
"self_disable": "You cannot disable yourself",
|
"self_disable": "You cannot disable yourself",
|
||||||
"self_role": "You cannot add/change your role",
|
"self_role": "You cannot add/change your role",
|
||||||
"password_help": "If blank the current password will not be changed",
|
"password_help": "If blank the current password will not be changed",
|
||||||
|
|
|
@ -762,7 +762,7 @@
|
||||||
"role_permissions": "Un amministratore di ruolo non può avere le seguenti autorizzazioni: {{val}}",
|
"role_permissions": "Un amministratore di ruolo non può avere le seguenti autorizzazioni: {{val}}",
|
||||||
"view_manage": "Visualizza e gestisci amministratori",
|
"view_manage": "Visualizza e gestisci amministratori",
|
||||||
"self_delete": "Non puoi eliminare te stesso",
|
"self_delete": "Non puoi eliminare te stesso",
|
||||||
"self_permissions": "Non puoi rimuovere questi permessi a te stesso",
|
"self_permissions": "Non puoi cambiare i tuoi permessi",
|
||||||
"self_disable": "Non puoi disabilitare te stesso",
|
"self_disable": "Non puoi disabilitare te stesso",
|
||||||
"self_role": "Non puoi aggiungere/modificare il tuo ruolo",
|
"self_role": "Non puoi aggiungere/modificare il tuo ruolo",
|
||||||
"password_help": "Se vuoto la password corrente non verrà modificata",
|
"password_help": "Se vuoto la password corrente non verrà modificata",
|
||||||
|
|
Loading…
Reference in a new issue