mirror of
https://github.com/Mezeporta/Erupe.git
synced 2025-12-13 15:34:38 +01:00
Added utils to verify paths
This commit is contained in:
37
server/api/utils.go
Normal file
37
server/api/utils.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func inTrustedRoot(path string, trustedRoot string) error {
|
||||
for path != "/" {
|
||||
path = filepath.Dir(path)
|
||||
if path == trustedRoot {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New("path is outside of trusted root")
|
||||
}
|
||||
|
||||
func verifyPath(path string, trustedRoot string) (string, error) {
|
||||
|
||||
c := filepath.Clean(path)
|
||||
fmt.Println("Cleaned path: " + c)
|
||||
|
||||
r, err := filepath.EvalSymlinks(c)
|
||||
if err != nil {
|
||||
fmt.Println("Error " + err.Error())
|
||||
return c, errors.New("Unsafe or invalid path specified")
|
||||
}
|
||||
|
||||
err = inTrustedRoot(r, trustedRoot)
|
||||
if err != nil {
|
||||
fmt.Println("Error " + err.Error())
|
||||
return r, errors.New("Unsafe or invalid path specified")
|
||||
} else {
|
||||
return r, nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user