Переглянути джерело

Experimental fix for realpath to virtual path converter

TC pushbot 5 4 роки тому
батько
коміт
16f695ccf2
2 змінених файлів з 29 додано та 9 видалено
  1. 7 1
      mod/agi/agi.file.go
  2. 22 8
      mod/user/directoryHandler.go

+ 7 - 1
mod/agi/agi.file.go

@@ -326,6 +326,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		vrootPath := filepath.Dir(regex)
 		regexFilename := filepath.Base(regex)
 		//Translate the virtual path to realpath
+
 		rrootPath, err := virtualPathToRealPath(vrootPath, u)
 		if err != nil {
 			g.raiseError(err)
@@ -349,7 +350,12 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 
 		parsedFilelist := []fs.FileData{}
 		for _, file := range suitableFiles {
-			vpath, _ := realpathToVirtualpath(filepath.ToSlash(file), u)
+			vpath, err := realpathToVirtualpath(filepath.ToSlash(file), u)
+			if err != nil {
+				g.raiseError(err)
+				reply, _ := vm.ToValue(false)
+				return reply
+			}
 			modtime, _ := fs.GetModTime(file)
 			parsedFilelist = append(parsedFilelist, fs.FileData{
 				Filename: filepath.Base(file),

+ 22 - 8
mod/user/directoryHandler.go

@@ -2,9 +2,10 @@ package user
 
 import (
 	"errors"
+	"os"
 	"path/filepath"
 	"strings"
-	"os"
+
 	fs "imuslab.com/arozos/mod/filesystem"
 )
 
@@ -82,9 +83,9 @@ func (u *User) VirtualPathToRealPath(vpath string) (string, error) {
 				return "", errors.New("Request Filesystem Handler has been closed by another process")
 			}
 			if storage.Hierarchy == "user" {
-				return filepath.Clean(storage.Path) + "/users/" + u.Username + subpath, nil
+				return filepath.ToSlash(filepath.Clean(storage.Path) + "/users/" + u.Username + subpath), nil
 			} else {
-				return filepath.Clean(storage.Path) + subpath, nil
+				return filepath.ToSlash(filepath.Clean(storage.Path) + subpath), nil
 			}
 
 		}
@@ -102,7 +103,20 @@ func (u *User) RealPathToVirtualPath(rpath string) (string, error) {
 
 	//Check for path escape
 	if len(realPath) > 2 && realPath[:2] == ".." {
-		return "", errors.New("Request path out of storage root")
+		//Fix: 20 May 2021: Allow using ../folder as virtual root directory
+		//Check if there are vroots that actually use relative path as root directory.
+		allowSpecialCasePassThrough := false
+		for _, fsh := range userFsHandlers {
+			thisVrootPath := fsh.Path
+			if len(realPath) > len(thisVrootPath) && filepath.ToSlash(realPath[:len(thisVrootPath)]) == filepath.ToSlash(thisVrootPath) {
+				allowSpecialCasePassThrough = true
+			}
+		}
+
+		if !allowSpecialCasePassThrough {
+			return "", errors.New("Request path out of storage root")
+		}
+
 	}
 
 	//Look for a real path of a virtual device that the realpath is containing
@@ -117,7 +131,7 @@ func (u *User) RealPathToVirtualPath(rpath string) (string, error) {
 		thisStorageRootAbs = filepath.ToSlash(filepath.Clean(thisStorageRootAbs))
 		pathContained := false
 		subPath := ""
-		if len(realPath) > len(thisStorageRoot) && realPath[:len(thisStorageRoot)] == thisStorageRoot {
+		if len(realPath) > len(thisStorageRoot) && filepath.ToSlash(realPath[:len(thisStorageRoot)]) == filepath.ToSlash(thisStorageRoot) {
 			//This realpath is in contained inside this storage root
 			pathContained = true
 			subtractionPath := thisStorageRoot
@@ -128,7 +142,7 @@ func (u *User) RealPathToVirtualPath(rpath string) (string, error) {
 			if len(subtractionPath) < len(realPath) {
 				subPath = realPath[len(subtractionPath):]
 			}
-		} else if len(realPath) > len(thisStorageRootAbs) && realPath[:len(thisStorageRootAbs)] == thisStorageRootAbs {
+		} else if len(realPath) > len(thisStorageRootAbs) && filepath.ToSlash(realPath[:len(thisStorageRootAbs)]) == filepath.ToSlash(thisStorageRootAbs) {
 			//The realpath contains the absolute path of this storage root
 			pathContained = true
 			subtractionPath := thisStorageRootAbs
@@ -139,7 +153,7 @@ func (u *User) RealPathToVirtualPath(rpath string) (string, error) {
 			if len(subtractionPath) < len(realPath) {
 				subPath = realPath[len(subtractionPath):]
 			}
-		} else if realPath == thisStorageRoot {
+		} else if filepath.ToSlash(realPath) == filepath.ToSlash(thisStorageRoot) {
 			//Storage Root's root
 			pathContained = true
 			subPath = ""
@@ -210,7 +224,7 @@ func getIDFromVirtualPath(vpath string) (string, string, error) {
 		return "", "", errors.New("Path missing Virtual Device ID. Given: " + vpath)
 	}
 
-	//Clean up the virutal path 
+	//Clean up the virutal path
 	vpath = filepath.ToSlash(filepath.Clean(vpath))
 
 	tmp := strings.Split(vpath, ":")