Skip to content

Commit 195d357

Browse files
committed
test for cutting off location of owner check
1 parent 9f45d5d commit 195d357

File tree

4 files changed

+72
-8
lines changed

4 files changed

+72
-8
lines changed

pkg/drives/base.go

+9
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ func IsThridPartyDrives(dstType string) bool {
109109
}
110110
}
111111

112+
func IsBaseDrives(dstType string) bool {
113+
switch dstType {
114+
case SrcTypeDrive, SrcTypeCache:
115+
return true
116+
default:
117+
return false
118+
}
119+
}
120+
112121
func IsCloudDrives(dstType string) bool {
113122
switch dstType {
114123
case SrcTypeCloud, SrcTypeAWSS3, SrcTypeTencent, SrcTypeDropbox:

pkg/http/data.go

+60-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package http
22

33
import (
4+
"files/pkg/drives"
5+
"files/pkg/rpc"
46
"k8s.io/klog/v2"
57
"net/http"
68
"strconv"
@@ -15,12 +17,14 @@ type handleFunc func(w http.ResponseWriter, r *http.Request, d *common.Data) (in
1517

1618
func handle(fn handleFunc, prefix string, server *settings.Server) http.Handler {
1719
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18-
if prefix == "/api/paste" || (prefix == "/api/resources" && r.Method == http.MethodPatch) {
19-
klog.Warningf("Is src and dst yours? We'll check it for %s %s", r.Method, r.URL.Path)
20-
} else if prefix == "/api/resources" || prefix == "/api/raw" || prefix == "/api/preview" {
21-
klog.Warningf("Is src yours? We'll check it for %s %s", r.Method, r.URL.Path)
20+
checked, err := CheckPathOwner(r, prefix)
21+
if err != nil {
22+
http.Error(w, err.Error(), http.StatusForbidden)
2223
}
23-
24+
if !checked {
25+
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
26+
}
27+
2428
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
2529

2630
status, err := fn(w, r, &common.Data{
@@ -41,3 +45,54 @@ func handle(fn handleFunc, prefix string, server *settings.Server) http.Handler
4145

4246
return stripPrefix(prefix, handler)
4347
}
48+
49+
func CheckPathOwner(r *http.Request, prefix string) (bool, error) {
50+
if prefix != "/api/resources" && prefix != "/api/raw" && prefix != "/api/preview" && prefix != "/api/paste" {
51+
return true, nil
52+
}
53+
54+
var err error = nil
55+
method := r.Method
56+
src := r.URL.Path
57+
58+
srcType := r.URL.Query().Get("src_type")
59+
if srcType == "" {
60+
srcType = r.URL.Query().Get("src")
61+
if srcType == "" {
62+
srcType = drives.SrcTypeDrive
63+
}
64+
}
65+
66+
dst := r.URL.Query().Get("destination")
67+
dstType := r.URL.Query().Get("dst_type")
68+
if dstType == "" {
69+
dstType = drives.SrcTypeDrive
70+
}
71+
72+
klog.Infof("Checking owner for method: %s, prefix: %s, srcType: %s, src: %s, dstType: %s, dst: %s", method, prefix, srcType, src, dstType, dst)
73+
74+
bflRequest := r.Header.Get("X-Bfl-User")
75+
bflParsed := ""
76+
if drives.IsBaseDrives(srcType) {
77+
bflParsed, err = rpc.PVCs.GetBfl(rpc.ExtractPvcFromURL(src))
78+
if err != nil {
79+
return false, err
80+
}
81+
if bflParsed != bflRequest {
82+
return false, nil
83+
}
84+
}
85+
86+
if prefix == "/api/paste" || (prefix == "/api/resources" && r.Method == http.MethodPatch) {
87+
if drives.IsBaseDrives(dstType) {
88+
bflParsed, err = rpc.PVCs.GetBfl(rpc.ExtractPvcFromURL(dst))
89+
if err != nil {
90+
return false, err
91+
}
92+
if bflParsed != bflRequest {
93+
return false, nil
94+
}
95+
}
96+
}
97+
return true, nil
98+
}

pkg/rpc/k8s.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (p *PVCCache) getBflForCachePVCOrCache(cachePvc string) (string, error) {
7777
return bflName, nil
7878
}
7979

80-
func (p *PVCCache) getBfl(pvc string) (string, error) {
80+
func (p *PVCCache) GetBfl(pvc string) (string, error) {
8181
bflName, err := p.getBflForUserPVCOrCache(pvc)
8282
if bflName == "" || err != nil {
8383
bflName, err = p.getBflForCachePVCOrCache(pvc)

pkg/rpc/watcher.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func WatchPath(addPaths []string, deletePaths []string, focusPaths []string) {
221221
search3 = false
222222
}
223223
if search3 && checkString(path) {
224-
bflName, err := PVCs.getBfl(ExtractPvcFromURL(path))
224+
bflName, err := PVCs.GetBfl(ExtractPvcFromURL(path))
225225
if err != nil {
226226
klog.Info(err)
227227
} else {
@@ -359,7 +359,7 @@ func handleEvent(e jfsnotify.Event) error {
359359
var bflName string
360360
var err error
361361
if checkString(e.Name) {
362-
bflName, err = PVCs.getBfl(ExtractPvcFromURL(e.Name))
362+
bflName, err = PVCs.GetBfl(ExtractPvcFromURL(e.Name))
363363
if err != nil {
364364
klog.Info(err)
365365
} else {

0 commit comments

Comments
 (0)