feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,151 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
pathpkg "path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ErrOutsideRoot = errors.New("path escapes project root")
|
||||
var ErrNotRegular = errors.New("path is not a regular file")
|
||||
|
||||
func Resolve(root, requested string) (string, error) {
|
||||
rootAbs, err := filepath.Abs(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if strings.ContainsRune(requested, '\x00') || strings.Contains(requested, "\\") || strings.HasPrefix(requested, "/") {
|
||||
return "", ErrOutsideRoot
|
||||
}
|
||||
cleanRequested := pathpkg.Clean(requested)
|
||||
if requested == "" {
|
||||
cleanRequested = "."
|
||||
} else if cleanRequested != requested {
|
||||
return "", ErrOutsideRoot
|
||||
}
|
||||
for _, part := range strings.Split(cleanRequested, "/") {
|
||||
if part == ".." {
|
||||
return "", ErrOutsideRoot
|
||||
}
|
||||
}
|
||||
candidate := filepath.Join(rootAbs, filepath.FromSlash(cleanRequested))
|
||||
candidateAbs, err := filepath.Abs(candidate)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
relative, err := filepath.Rel(rootAbs, candidateAbs)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if relative == ".." || strings.HasPrefix(relative, ".."+string(os.PathSeparator)) {
|
||||
return "", ErrOutsideRoot
|
||||
}
|
||||
return candidateAbs, nil
|
||||
}
|
||||
|
||||
// RejectSymlinks prevents an existing path component from redirecting an operation outside the tenant root.
|
||||
func RejectSymlinks(root, target string) error {
|
||||
rootAbs, err := filepath.Abs(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
targetAbs, err := filepath.Abs(target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel, err := filepath.Rel(rootAbs, targetAbs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
||||
return ErrOutsideRoot
|
||||
}
|
||||
current := rootAbs
|
||||
for _, part := range strings.Split(rel, string(os.PathSeparator)) {
|
||||
if part == "." || part == "" {
|
||||
continue
|
||||
}
|
||||
current = filepath.Join(current, part)
|
||||
info, statErr := os.Lstat(current)
|
||||
if os.IsNotExist(statErr) {
|
||||
return nil
|
||||
}
|
||||
if statErr != nil {
|
||||
return statErr
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
return ErrOutsideRoot
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InspectRegular opens a file through os.Root after rejecting every symlink in
|
||||
// its path. Deployment checks use this instead of os.Stat so an uploaded link
|
||||
// can never redirect a privileged panel operation outside the virtual host.
|
||||
func InspectRegular(root, requested string) (os.FileInfo, error) {
|
||||
target, err := Resolve(root, requested)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = RejectSymlinks(root, target); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
relative, err := filepath.Rel(root, target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
projectRoot, err := os.OpenRoot(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer projectRoot.Close()
|
||||
file, err := projectRoot.Open(relative)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
return nil, ErrNotRegular
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// EnsureDirectory creates one final directory below an already prepared
|
||||
// project path using os.Root. It is used for aaPanel compatibility shims, not
|
||||
// arbitrary tenant path creation, and refuses links or non-directory targets.
|
||||
func EnsureDirectory(root, requested string, mode os.FileMode) error {
|
||||
target, err := Resolve(root, requested)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = RejectSymlinks(root, target); err != nil {
|
||||
return err
|
||||
}
|
||||
relative, err := filepath.Rel(root, target)
|
||||
if err != nil || relative == "." {
|
||||
return ErrOutsideRoot
|
||||
}
|
||||
projectRoot, err := os.OpenRoot(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer projectRoot.Close()
|
||||
info, statErr := projectRoot.Lstat(relative)
|
||||
if statErr == nil {
|
||||
if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return errors.New("path is not a physical directory")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !errors.Is(statErr, os.ErrNotExist) {
|
||||
return statErr
|
||||
}
|
||||
return projectRoot.Mkdir(relative, mode)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestResolveBlocksTraversal(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "project")
|
||||
for _, requested := range []string{
|
||||
"../../etc/passwd",
|
||||
`..\..\secret`,
|
||||
"/../../outside",
|
||||
"public/../secret",
|
||||
"public//index.html",
|
||||
"public/./index.html",
|
||||
} {
|
||||
if _, err := Resolve(root, requested); !errors.Is(err, ErrOutsideRoot) {
|
||||
t.Errorf("Resolve(%q) error = %v, want ErrOutsideRoot", requested, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInspectRegularRejectsDirectoriesAndReadsFiles(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(root, "app"), 0750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "app", "server.js"), []byte("ok"), 0640); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
info, err := InspectRegular(root, "app/server.js")
|
||||
if err != nil || info.Size() != 2 {
|
||||
t.Fatalf("InspectRegular(file) = %v, %v", info, err)
|
||||
}
|
||||
if _, err = InspectRegular(root, "app"); !errors.Is(err, ErrNotRegular) {
|
||||
t.Fatalf("InspectRegular(directory) error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveNormalizesInsideRoot(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "project")
|
||||
got, err := Resolve(root, "public/assets/app.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := filepath.Join(root, "public", "assets", "app.js")
|
||||
if got != want {
|
||||
t.Fatalf("got %q want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureDirectoryStaysInsidePhysicalProjectPath(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(root, "app"), 0750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := EnsureDirectory(root, "app/node_modules", 0770); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info, err := os.Lstat(filepath.Join(root, "app", "node_modules")); err != nil || !info.IsDir() {
|
||||
t.Fatalf("compatibility directory was not created: info=%v err=%v", info, err)
|
||||
}
|
||||
outside := t.TempDir()
|
||||
if err := os.Symlink(outside, filepath.Join(root, "linked")); err == nil {
|
||||
if err = EnsureDirectory(root, "linked/node_modules", 0770); err == nil {
|
||||
t.Fatal("EnsureDirectory followed a symlink outside the project root")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user