67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// Repeated -workspace-root flags keep the allowed trees explicit, without exposing /home/tt.
|
|
type workspaceRootFlags []string
|
|
|
|
func (roots *workspaceRootFlags) String() string { return strings.Join(*roots, ", ") }
|
|
func (roots *workspaceRootFlags) Set(value string) error {
|
|
path, e := filepath.Abs(value)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
*roots = append(*roots, filepath.Clean(path))
|
|
return nil
|
|
}
|
|
func (s *Server) allowedWorkspaceRoots() []string {
|
|
input := s.workspaceRoots
|
|
if len(input) == 0 {
|
|
root := s.workspaceRoot
|
|
if root == "" {
|
|
root = filepath.Join(s.root, "projects")
|
|
}
|
|
input = []string{root}
|
|
}
|
|
result := []string{}
|
|
seen := map[string]bool{}
|
|
for _, root := range input {
|
|
abs, e := filepath.Abs(root)
|
|
if e != nil {
|
|
continue
|
|
}
|
|
key := abs
|
|
if runtime.GOOS == "windows" {
|
|
key = strings.ToLower(key)
|
|
}
|
|
if !seen[key] {
|
|
seen[key] = true
|
|
result = append(result, abs)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
func (s *Server) containingWorkspaceRoot(path string, allowRoot bool) (string, bool) {
|
|
if !filepath.IsAbs(path) {
|
|
return "", false
|
|
}
|
|
best := ""
|
|
for _, root := range s.allowedWorkspaceRoots() {
|
|
rel, e := filepath.Rel(root, path)
|
|
if e != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
|
continue
|
|
}
|
|
if rel == "." && !allowRoot {
|
|
return "", false
|
|
}
|
|
if len(root) > len(best) {
|
|
best = root
|
|
}
|
|
}
|
|
return best, best != ""
|
|
}
|