45 lines
1.4 KiB
Go
45 lines
1.4 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// ValidateRunWritePaths validates optional exact run-relative paths against an
|
|
// already resolved server workspace. Saves and manual/scheduled admission share
|
|
// this policy; admission rechecks it in case filesystem links changed since save.
|
|
// An omitted or empty list retains the existing run-level default, not a grant
|
|
// beyond the pinned workflow's own tool permissions.
|
|
func ValidateRunWritePaths(workspace string, paths []string) error {
|
|
if len(paths) > 20 {
|
|
return fmt.Errorf("too many write paths")
|
|
}
|
|
for _, path := range paths {
|
|
if filepath.IsAbs(path) || filepath.Clean(path) != path || path == ".." || strings.HasPrefix(path, ".."+string(os.PathSeparator)) || strings.ContainsAny(path, "*?\\") {
|
|
return fmt.Errorf("optional write paths must be exact run-directory-relative paths")
|
|
}
|
|
parent := filepath.Join(workspace, path)
|
|
for {
|
|
_, err := os.Lstat(parent)
|
|
if err == nil {
|
|
break
|
|
}
|
|
if !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
next := filepath.Dir(parent)
|
|
if next == parent {
|
|
return fmt.Errorf("write path is unavailable")
|
|
}
|
|
parent = next
|
|
}
|
|
resolved, err := filepath.EvalSymlinks(parent)
|
|
if err != nil || (resolved != workspace && !strings.HasPrefix(resolved, workspace+string(os.PathSeparator))) {
|
|
return fmt.Errorf("write path must stay within the run directory")
|
|
}
|
|
}
|
|
return nil
|
|
}
|