fix(permission): enhance the strictness of permissions (#7705 close #7680)

* fix(permission): enhance the strictness of permissions

* fix: add initial permissions to admin
pull/7673/head
KirCute_ECT 2024-12-25 21:17:58 +08:00 committed by GitHub
parent 5ecf5e823c
commit 48916cdedf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 33 deletions

View File

@ -32,6 +32,7 @@ func initUser() {
Role: model.ADMIN, Role: model.ADMIN,
BasePath: "/", BasePath: "/",
Authn: "[]", Authn: "[]",
Permission: 0xFF, // 0(can see hidden) - 7(can remove)
} }
if err := op.CreateUser(admin); err != nil { if err := op.CreateUser(admin); err != nil {
panic(err) panic(err)

View File

@ -42,6 +42,8 @@ type User struct {
// 7: can remove // 7: can remove
// 8: webdav read // 8: webdav read
// 9: webdav write // 9: webdav write
// 10: ftp/sftp login and read
// 11: ftp/sftp write
Permission int32 `json:"permission"` Permission int32 `json:"permission"`
OtpSecret string `json:"-"` OtpSecret string `json:"-"`
SsoID string `json:"sso_id"` // unique by sso platform SsoID string `json:"sso_id"` // unique by sso platform
@ -78,43 +80,43 @@ func (u *User) SetPassword(pwd string) *User {
} }
func (u *User) CanSeeHides() bool { func (u *User) CanSeeHides() bool {
return u.IsAdmin() || u.Permission&1 == 1 return u.Permission&1 == 1
} }
func (u *User) CanAccessWithoutPassword() bool { func (u *User) CanAccessWithoutPassword() bool {
return u.IsAdmin() || (u.Permission>>1)&1 == 1 return (u.Permission>>1)&1 == 1
} }
func (u *User) CanAddOfflineDownloadTasks() bool { func (u *User) CanAddOfflineDownloadTasks() bool {
return u.IsAdmin() || (u.Permission>>2)&1 == 1 return (u.Permission>>2)&1 == 1
} }
func (u *User) CanWrite() bool { func (u *User) CanWrite() bool {
return u.IsAdmin() || (u.Permission>>3)&1 == 1 return (u.Permission>>3)&1 == 1
} }
func (u *User) CanRename() bool { func (u *User) CanRename() bool {
return u.IsAdmin() || (u.Permission>>4)&1 == 1 return (u.Permission>>4)&1 == 1
} }
func (u *User) CanMove() bool { func (u *User) CanMove() bool {
return u.IsAdmin() || (u.Permission>>5)&1 == 1 return (u.Permission>>5)&1 == 1
} }
func (u *User) CanCopy() bool { func (u *User) CanCopy() bool {
return u.IsAdmin() || (u.Permission>>6)&1 == 1 return (u.Permission>>6)&1 == 1
} }
func (u *User) CanRemove() bool { func (u *User) CanRemove() bool {
return u.IsAdmin() || (u.Permission>>7)&1 == 1 return (u.Permission>>7)&1 == 1
} }
func (u *User) CanWebdavRead() bool { func (u *User) CanWebdavRead() bool {
return u.IsAdmin() || (u.Permission>>8)&1 == 1 return (u.Permission>>8)&1 == 1
} }
func (u *User) CanWebdavManage() bool { func (u *User) CanWebdavManage() bool {
return u.IsAdmin() || (u.Permission>>9)&1 == 1 return (u.Permission>>9)&1 == 1
} }
func (u *User) CanFTPAccess() bool { func (u *User) CanFTPAccess() bool {

View File

@ -11,7 +11,6 @@ import (
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op" "github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/internal/setting" "github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/webdav" "github.com/alist-org/alist/v3/server/webdav"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -99,12 +98,27 @@ func WebDAVAuth(c *gin.Context) {
c.Abort() c.Abort()
return return
} }
if !user.CanWebdavManage() && utils.SliceContains([]string{"PUT", "DELETE", "PROPPATCH", "MKCOL", "COPY", "MOVE"}, c.Request.Method) { if (c.Request.Method == "PUT" || c.Request.Method == "MKCOL") && (!user.CanWebdavManage() || !user.CanWrite()) {
if c.Request.Method == "OPTIONS" { c.Status(http.StatusForbidden)
c.Set("user", guest) c.Abort()
c.Next()
return return
} }
if c.Request.Method == "MOVE" && (!user.CanWebdavManage() || (!user.CanMove() && !user.CanRename())) {
c.Status(http.StatusForbidden)
c.Abort()
return
}
if c.Request.Method == "COPY" && (!user.CanWebdavManage() || !user.CanCopy()) {
c.Status(http.StatusForbidden)
c.Abort()
return
}
if c.Request.Method == "DELETE" && (!user.CanWebdavManage() || !user.CanRemove()) {
c.Status(http.StatusForbidden)
c.Abort()
return
}
if c.Request.Method == "PROPPATCH" && !user.CanWebdavManage() {
c.Status(http.StatusForbidden) c.Status(http.StatusForbidden)
c.Abort() c.Abort()
return return

View File

@ -33,6 +33,13 @@ func moveFiles(ctx context.Context, src, dst string, overwrite bool) (status int
dstDir := path.Dir(dst) dstDir := path.Dir(dst)
srcName := path.Base(src) srcName := path.Base(src)
dstName := path.Base(dst) dstName := path.Base(dst)
user := ctx.Value("user").(*model.User)
if srcDir != dstDir && !user.CanMove() {
return http.StatusForbidden, nil
}
if srcName != dstName && !user.CanRename() {
return http.StatusForbidden, nil
}
if srcDir == dstDir { if srcDir == dstDir {
err = fs.Rename(ctx, src, dstName) err = fs.Rename(ctx, src, dstName)
} else { } else {