github.com/databricks/cli@v0.203.0/libs/filer/workspace_root_path.go (about)

     1  package filer
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  	"strings"
     7  )
     8  
     9  // WorkspaceRootPath can be joined with a relative path and ensures that
    10  // the returned path is always a strict child of the root path.
    11  type WorkspaceRootPath struct {
    12  	rootPath string
    13  }
    14  
    15  // NewWorkspaceRootPath constructs and returns [RootPath].
    16  // The named path is cleaned on construction.
    17  func NewWorkspaceRootPath(name string) WorkspaceRootPath {
    18  	return WorkspaceRootPath{
    19  		rootPath: path.Clean(name),
    20  	}
    21  }
    22  
    23  // Join returns the specified path name joined to the root.
    24  // It returns an error if the resulting path is not a strict child of the root path.
    25  func (p *WorkspaceRootPath) Join(name string) (string, error) {
    26  	absPath := path.Join(p.rootPath, name)
    27  
    28  	// Don't allow escaping the specified root using relative paths.
    29  	if !strings.HasPrefix(absPath, p.rootPath) {
    30  		return "", fmt.Errorf("relative path escapes root: %s", name)
    31  	}
    32  
    33  	return absPath, nil
    34  }