iop/packages/go/agentguard/containment.go

25 lines
589 B
Go

package agentguard
import (
"path/filepath"
"strings"
)
func containsPath(base, candidate string) bool {
relative, err := filepath.Rel(base, candidate)
if err != nil {
return false
}
return relative == "." ||
(relative != ".." && !strings.HasPrefix(relative, ".."+string(filepath.Separator)))
}
func containsParentReference(path string) bool {
withoutVolume := strings.TrimPrefix(filepath.ToSlash(path), filepath.ToSlash(filepath.VolumeName(path)))
for _, component := range strings.Split(withoutVolume, "/") {
if component == ".." {
return true
}
}
return false
}