From 959ef620fbb7b715d15439179a8986c825f4eafc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= <927625802@qq.com> Date: Sat, 13 Nov 2021 16:12:12 +0800 Subject: [PATCH] :sparkles: check parent folder password --- server/check.go | 16 ++++++++++++++++ server/path.go | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/server/check.go b/server/check.go index 7c7404d8..d2551d30 100644 --- a/server/check.go +++ b/server/check.go @@ -6,6 +6,7 @@ import ( "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" "gorm.io/gorm" + "path/filepath" ) func Auth(c *gin.Context) { @@ -37,3 +38,18 @@ func CheckAccount(c *gin.Context) { } c.Next() } + +func CheckParent(path string, password string) bool { + meta, err := model.GetMetaByPath(path) + if err == nil { + if meta.Password != "" && meta.Password != password { + return false + } + return true + } else { + if path == "/" { + return true + } + return CheckParent(filepath.Dir(path), password) + } +} diff --git a/server/path.go b/server/path.go index 9b99e76f..b9283da1 100644 --- a/server/path.go +++ b/server/path.go @@ -2,10 +2,12 @@ package server import ( "fmt" + "github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" + "path/filepath" "strings" ) @@ -30,6 +32,12 @@ func Path(c *gin.Context) { } // TODO hide or ignore? } + if conf.CheckParent { + if !CheckParent(filepath.Dir(req.Path), req.Password) { + ErrorResp(c, fmt.Errorf("wrong password"), 401) + return + } + } if model.AccountsCount() > 1 && req.Path == "/" { files, err := model.GetAccountFiles() if err != nil {