security: enforce recursive delete authorization

This commit is contained in:
OpenClaw Agent
2026-09-21 21:37:32 +08:00
parent 3d865259fc
commit ea6999cddd
2 changed files with 27 additions and 1 deletions
+23 -1
View File
@@ -1,6 +1,28 @@
package api
import "testing"
import (
"os"
"path/filepath"
"testing"
"chfs-ng/internal/auth"
"chfs-ng/internal/storage"
)
func TestCheckDeleteTreeRejectsDeniedDescendant(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "public", "private"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "public", "private", "secret.txt"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
store := storage.Local{Root: root}
account := &auth.Account{Mask: auth.Delete, Rules: []auth.PathRule{{Dir: "public/private", Mask: auth.None}}}
if err := checkDeleteTree(store, account, "public"); err == nil {
t.Fatal("expected denied descendant to block recursive deletion")
}
}
func TestUnsafePath(t *testing.T) {
cases := map[string]bool{
+4
View File
@@ -336,6 +336,10 @@ func (s *Server) remove(w http.ResponseWriter, r *http.Request) {
writeJSONError(w, http.StatusForbidden, "has no privilege")
return
}
if e := checkDeleteTree(s.Store, a, p); e != nil {
writeJSONError(w, http.StatusForbidden, "has no privilege")
return
}
if e := s.removePath(p); e != nil {
http.Error(w, e.Error(), 500)
return