fix: align CHFS permission error responses

This commit is contained in:
OpenClaw Agent
2026-09-21 19:44:26 +08:00
parent a1f9633d4a
commit 5ceba1104d
+11 -11
View File
@@ -226,7 +226,7 @@ func (s *Server) newdir(w http.ResponseWriter, r *http.Request) {
} }
a := account(r, s) a := account(r, s)
if !a.MaskFor(p).HasWrite() { if !a.MaskFor(p).HasWrite() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
if e := s.Store.MkdirAll(p); e != nil { if e := s.Store.MkdirAll(p); e != nil {
@@ -241,7 +241,7 @@ func (s *Server) rename(w http.ResponseWriter, r *http.Request) {
old := strings.Trim(r.FormValue("old"), "/") old := strings.Trim(r.FormValue("old"), "/")
neu := strings.Trim(r.FormValue("new"), "/") neu := strings.Trim(r.FormValue("new"), "/")
if old == "" || neu == "" || !a.MaskFor(old).HasWrite() || !a.MaskFor(path.Dir(neu)).HasWrite() { if old == "" || neu == "" || !a.MaskFor(old).HasWrite() || !a.MaskFor(path.Dir(neu)).HasWrite() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
if e := s.Store.Rename(old, neu); e != nil { if e := s.Store.Rename(old, neu); e != nil {
@@ -262,7 +262,7 @@ func (s *Server) remove(w http.ResponseWriter, r *http.Request) {
for _, raw := range items { for _, raw := range items {
p := strings.Trim(strings.ReplaceAll(raw, "\\", "/"), "/") p := strings.Trim(strings.ReplaceAll(raw, "\\", "/"), "/")
if p == "" || !a.MaskFor(p).HasDelete() { if p == "" || !a.MaskFor(p).HasDelete() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
if e := s.removePath(p); e != nil { if e := s.removePath(p); e != nil {
@@ -363,7 +363,7 @@ func (s *Server) newtext(w http.ResponseWriter, r *http.Request) {
} }
p := path.Join(dir, filename) p := path.Join(dir, filename)
if !a.MaskFor(p).HasWrite() { if !a.MaskFor(p).HasWrite() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
f, e := s.Store.Create(p) f, e := s.Store.Create(p)
@@ -382,7 +382,7 @@ func (s *Server) updatetext(w http.ResponseWriter, r *http.Request) {
name := strings.Trim(r.FormValue("filename"), "/") name := strings.Trim(r.FormValue("filename"), "/")
p := path.Join(dir, name) p := path.Join(dir, name)
if name == "" || !a.MaskFor(p).HasWrite() { if name == "" || !a.MaskFor(p).HasWrite() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
f, e := s.Store.Create(p) f, e := s.Store.Create(p)
@@ -406,7 +406,7 @@ func (s *Server) upload(w http.ResponseWriter, r *http.Request) {
a := account(r, s) a := account(r, s)
dir := strings.Trim(r.FormValue("folder"), "/") dir := strings.Trim(r.FormValue("folder"), "/")
if !a.MaskFor(dir).HasWrite() { if !a.MaskFor(dir).HasWrite() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
var saved []string var saved []string
@@ -448,7 +448,7 @@ func (s *Server) upload2(w http.ResponseWriter, r *http.Request) {
a := account(r, s) a := account(r, s)
dir := strings.Trim(r.URL.Query().Get("folder"), "/") dir := strings.Trim(r.URL.Query().Get("folder"), "/")
if !a.MaskFor(dir).HasWrite() { if !a.MaskFor(dir).HasWrite() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
if err := r.ParseMultipartForm(128 << 20); err != nil { if err := r.ParseMultipartForm(128 << 20); err != nil {
@@ -490,7 +490,7 @@ func (s *Server) search(w http.ResponseWriter, r *http.Request) {
a := account(r, s) a := account(r, s)
rootMask := a.MaskFor("") rootMask := a.MaskFor("")
if !rootMask.HasRead() { if !rootMask.HasRead() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
needle := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("str"))) needle := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("str")))
@@ -581,7 +581,7 @@ func (s *Server) preview(w http.ResponseWriter, r *http.Request) {
return return
} }
if !account(r, s).MaskFor(file).HasRead() { if !account(r, s).MaskFor(file).HasRead() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
if !s.ImagePreview { if !s.ImagePreview {
@@ -631,7 +631,7 @@ func (s *Server) serveFile(w http.ResponseWriter, r *http.Request, p string, att
a := account(r, s) a := account(r, s)
p = strings.Trim(strings.ReplaceAll(p, "\\", "/"), "/") p = strings.Trim(strings.ReplaceAll(p, "\\", "/"), "/")
if !a.MaskFor(p).HasRead() { if !a.MaskFor(p).HasRead() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
fi, err := s.Store.Stat(p) fi, err := s.Store.Stat(p)
@@ -661,7 +661,7 @@ func (s *Server) downloadDir(w http.ResponseWriter, r *http.Request) {
p := strings.TrimPrefix(r.URL.Path, "/chfs/downloaddir/") p := strings.TrimPrefix(r.URL.Path, "/chfs/downloaddir/")
p = strings.Trim(p, "/") p = strings.Trim(p, "/")
if !a.MaskFor(p).HasRead() { if !a.MaskFor(p).HasRead() {
http.Error(w, "forbidden", 403) writeJSONError(w, http.StatusForbidden, "has no privilege")
return return
} }
fi, err := s.Store.Stat(p) fi, err := s.Store.Stat(p)